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

Use quoted strings for chunks of code (multiline strings) #13162

Merged
merged 1 commit into from
May 14, 2024

Conversation

MisterDA
Copy link
Contributor

Now that #12514 is merged, we can take advantage of OCaml 4.02's quoted strings, making both the code being emitted, and the formatting of the lines, somewhat less obscure.

@gasche
Copy link
Member

gasche commented May 13, 2024

This looks reasonable and the LaTeX code in particular benefits greatly. I try to find avenues for nitpicks that are strictly less important than the topic of the PR itself, and at first I thought that I would be stuck on a PR about lexical conventions. Fear not, we can talk about whitespace!

The use of quoted string literals introduces a dedentation of the content -- we cannot indent to the current indentation level inside the string. This is visually jarring but a reasonable cost to pay for the increased readability of the content. However, I would like to suggest a rule when this happens, that you do not always respect: when string-literal content is going to dedented at the very beginning of a line, I think that the input delimiter that introduces this content should also be dedented at the very beginning of a line.

So this:

  if ctx.has_refill then
    pr ctx {|
let rec __ocaml_lex_refill_buf lexbuf _buf _len _curr _last
                                           _last_action state k =
...
|}

should rather be written as

  if ctx.has_refill then
    pr ctx
{|
let rec __ocaml_lex_refill_buf lexbuf _buf _len _curr _last
                                           _last_action state k =
...
|}

so that it is clear what is going on. (There are several other instances in this PR.)

@MisterDA
Copy link
Contributor Author

Thanks for the review, I've applied your suggestion.

As a side note, I was tempted to introduce a new function handling the indentation of the generated code in lex/, in order to remove the leading %s and the trail of pref pref pref. My first idea was to scan the code for new lines and add the requested leading spaces, something like:

let pr' ~pref ctx =
  Printf.ksprintf (fun str ->
      let i = ref 0 in
      String.iteri (fun j -> function
          | '\n' when !i = j ->
             Out_channel.output_char ctx.oc '\n';
             incr i;
          | '\n' ->
             Out_channel.output_string ctx.oc pref;
             Out_channel.output_substring ctx.oc str !i (j - !i + 1);
             i := j + 1
          | _ -> ()) str;
      if !i <> String.length str then begin
          Out_channel.output_string ctx.oc pref;
          Out_channel.output_substring ctx.oc str !i (String.length str - !i)
        end)

…but maybe that's something more suited for the Format module?

Changes Outdated Show resolved Hide resolved
lex/outputbis.ml Show resolved Hide resolved
lex/outputbis.ml Show resolved Hide resolved
ocamldoc/odoc_latex_style.ml Outdated Show resolved Hide resolved
ocamldoc/odoc_latex_style.ml Show resolved Hide resolved
@gasche
Copy link
Member

gasche commented May 13, 2024

Your pr' function looks reasonable to me. (I don't have the energy to consider and discuss an addition to the Format module in the standard library.) Leaving the code as-is would also be fine, I think.

@MisterDA
Copy link
Contributor Author

Thanks, I've applied your suggestions to keep whitespace identical.

Your pr' function looks reasonable to me. (I don't have the energy to consider and discuss an addition to the Format module in the standard library.) Leaving the code as-is would also be fine, I think.

I'll keep it as-is for now and see later if I can come up with a satisfying design.

@nojb nojb merged commit 302c467 into ocaml:trunk May 14, 2024
18 checks passed
@MisterDA MisterDA deleted the quoted-strings branch May 14, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants