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 support for multi-lines commands #2

Merged
merged 1 commit into from
Jul 10, 2018
Merged

Fix support for multi-lines commands #2

merged 1 commit into from
Jul 10, 2018

Conversation

samoht
Copy link
Collaborator

@samoht samoht commented Jul 10, 2018

No description provided.

@samoht samoht merged commit fae1b0c into master Jul 10, 2018
@samoht samoht deleted the multilines branch July 10, 2018 18:55
sorawee added a commit to sorawee/mdx that referenced this pull request Dec 2, 2023
Prior this PR, the following code fails:

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

because MDX incorrectly infers that the code block is a toplevel
interaction (from the fact that the block starts with `#`),
resulting in an error:

    incomplete toplevel entry: unexpected character '#'.
    Did you forget a space after the '#' at the start of the line

It is possible to workaround this issue as suggested in realworldocaml#421 by adding a
comment as a first line.

```
{@ocaml ocaml[
  (* This works! *)
  #require "astring";;
  let x = Astring.strf;;
]}
```

but ideally the workaround should not be needed.

One may wonder why the inference is needed, since the above code block
is already specified to be in the `ocaml` mode.
The answer appears to be that we are expected to use the inference heuristics
for a light sanity check, as the existing tests
("invalid ocaml" and "invalid toplevel" in `test_block.ml`) require:

    "let x = 2;;" in the toplevel mode should error with
    invalid toplevel syntax in toplevel blocks

    "# let x = 2;;" in the ocaml mode should error with
    toplevel syntax is not allowed in OCaml blocks

As a result, this PR keeps the light sanity check intact, but
adjusts the inference heuristics to be more conservative.
A block is now considered a toplevel interaction when
it starts with `#` followed by a space.
This fixes the issue, making it possible to use directives.
As a bonus, directives will now also work even when
the mode is not specified at all. But one disadvantage is that
this kind of code will no longer be considered invalid.

    realworldocaml#1+1;;
    ...
    realworldocaml#2+2;;
    ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant