Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make directives usable in the ocaml mode #443

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Added

- Handle the error-blocks syntax (#439, @jonludlam, @gpetiot)
- Fix directives not allowed in the `ocaml` mode.

### 2.3.1

Expand Down
7 changes: 5 additions & 2 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ let guess_ocaml_kind contents =
| h :: t ->
let h = String.trim h in
if h = "" then aux t
else if String.length h > 1 && h.[0] = '#' then `Toplevel
else if String.length h > 2 && h.[0] = '#' && h.[1] = ' ' then `Toplevel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If needed, the check could be more strict as top-level directives necessarily start with a lowercase ASCII letter (or the sequence \# which is never going to be used).

else `Code
in
aux contents
Expand Down Expand Up @@ -383,7 +383,10 @@ let mk_ocaml ~loc ~config ~header ~contents ~errors =
let kind = "OCaml" in
match config with
| { file_inc = None; part = None; env; non_det; _ } -> (
(* TODO: why does this call guess_ocaml_kind when infer_block already did? *)
(* mk_ocaml can be invoked because an explicit "$MDX ocaml" is given.
In such case, we still want to make it an error if
a toplevel interaction is given (see the test "invalid ocaml"),
so we use guess_ocaml_kind here to do that. *)
match guess_ocaml_kind contents with
| `Code -> Ok (OCaml { env = Ocaml_env.mk env; non_det; errors; header })
| `Toplevel ->
Expand Down
12 changes: 12 additions & 0 deletions test/bin/mdx-test/expect/simple-mld/test-case.mld
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ Line 1, characters 15-18:
Error: This expression has type string but an expression was expected of type
int
]err}]}


{@ocaml ocaml[
#require "astring";;
let x = Astring.strf;;
]}


{[
# x;;
- : ('a, Format.formatter, unit, string) format4 -> 'a = <fun>
]}
Loading