realworldocaml/mdx

Question: Dynamic "open CurrentModule" preludes for .mli files?

Opened this issue · 1 comments

I'm trying to set up a minimal-boilerplate example of a project with mdx-tested .mli files. I'm still learning dune and mdx, but I've gotten pretty close to my goal using one prelude file.

Currently, my utils.mli looks like this, and mdx is happy with it:

(** The Utils module define helper functions commonly used throughout the
    project. *)

val greet : string -> string
(** Returns a greeting message.

    {[
      # Utils.greet "Tom"
      - : string = "Hello Tom!"
    ]}
*)

While this looks just as clean as the Ruby documentation I'm used to (where examples always include the module name), I'd like to be able to just have this (without the module name), which is more consistent with the OCaml documentation I've seen:

# greet "Tom"
- : string = "Hello Tom!"

My current ./lib directory looks like this:

lib/dune
lib/prelude.txt
lib/config.ml
lib/config.mli
lib/utils.ml
lib/utils.mli
; ./lib/dune

(library
 (name romanum)
 (public_name romanum)
 (inline_tests)
 (preprocess
  (pps ppx_inline_test)))

(mdx
 (files *.mli)
 (preludes prelude.txt))

(include_subdirs unqualified)
(* ./lib/prelude.txt *)

#require "romanum";;

open Romanum

I know I can just add open Utils to my existing prelude file, but it wouldn't be practical to open every module in one prelude file, and I'd prefer not to create a separate subdirectory, dune file, and prelude file for every .mli just to achieve conventional-looking documentation.

Is this something I could achieve just using dune stanzas?

I would be nice if there was a way to set the test command flags used by the mdx stanza using variable substitution, but I realize that would require changes to dune itself.

I can't find any way to tell dune, "for each file in *.mli, run the following commands". I know I can write a script which does the looping for me, and invoke it using a run stanza or similar, but from the documentation it sounds like I would lose out on the promote and --auto-promote features if I'm not explicitly using a diff or diff? stanza. 🤔

One hacky way to do this is to add something like this at the top of your mli:

(**/**)
(** {[
         open MyModule
      ]}
*)
(**/**)

The stop comments mean it won't show up in documentation, but will still be run by mdx.