From 730d01c8c39a10f308e9cc8ee6afb403fb84ace4 Mon Sep 17 00:00:00 2001 From: Nicolas BACQUEY Date: Mon, 2 Sep 2024 18:16:50 +0200 Subject: [PATCH 1/2] fix: unbalanced parenthesized expressions This commit simplifies the old logic of "Do not prepend spaces if the node follows an open parenthesis" by either never prepend a space, or always prepend it, depending on the node. Resolves: https://github.com/tweag/topiary/issues/721 --- topiary-cli/tests/samples/expected/ocaml.ml | 3 + topiary-cli/tests/samples/input/ocaml.ml | 3 + topiary-queries/queries/ocaml.scm | 172 ++------------------ 3 files changed, 17 insertions(+), 161 deletions(-) diff --git a/topiary-cli/tests/samples/expected/ocaml.ml b/topiary-cli/tests/samples/expected/ocaml.ml index 66a5ffb9..fc29b762 100644 --- a/topiary-cli/tests/samples/expected/ocaml.ml +++ b/topiary-cli/tests/samples/expected/ocaml.ml @@ -1166,6 +1166,9 @@ let _ = match foo with | Bar { baz } -> qux +(* #721: unbalanced spacing around parenthesized expressions *) +let _ = (begin end) + (* #659 handling of the `;;` separator *) let bonjour () = "Bonjour" diff --git a/topiary-cli/tests/samples/input/ocaml.ml b/topiary-cli/tests/samples/input/ocaml.ml index 811e58c2..2fa26120 100644 --- a/topiary-cli/tests/samples/input/ocaml.ml +++ b/topiary-cli/tests/samples/input/ocaml.ml @@ -1097,6 +1097,9 @@ let _ = match foo with | Bar { baz } -> qux +(* #721: unbalanced spacing around parenthesized expressions *) +let _ = (begin end) + (* #659 handling of the `;;` separator *) let bonjour () = "Bonjour" diff --git a/topiary-queries/queries/ocaml.scm b/topiary-queries/queries/ocaml.scm index 9295a92f..d7436f57 100644 --- a/topiary-queries/queries/ocaml.scm +++ b/topiary-queries/queries/ocaml.scm @@ -154,7 +154,7 @@ ) ; Surround spaces -; A space is put after, and before (except just after an open parenthesis). +; A space is put after, except when followed by a PPX "%". ( [ "and" @@ -228,16 +228,22 @@ "%"? @do_nothing ) -; Those keywords are not expected to come right after an open parenthesis. +; Prepend a space. Note that these nodes are not expected to come after an open parenthesis, +; or if they do, we still want a space before. [ "as" + (attribute) "constraint" "do" "done" "downto" "else" + (floating_attribute) "in" - ; Infix operators can come after an open parenthesis, but we want a space before anyway + (item_attribute) + (module_parameter) + "nonrec" + "of" (pow_operator) (mult_operator) (add_operator) @@ -246,14 +252,14 @@ (and_operator) (or_operator) (assign_operator) - "nonrec" - "of" + (parameter) "rec" "then" "to" "virtual" "when" "with" + "=" "|" "->" "<-" @@ -277,162 +283,6 @@ ")"* @do_nothing ) -; For those queries, we should not have multiple queries, -; however, due to a known bug in tree-sitter queries -; https://github.com/tree-sitter/tree-sitter/issues/1811 -; using an alternative after the starred parenthesis does not work as intented. -; -( - "("* @do_nothing - . - "assert" @prepend_space -) -( - "("* @do_nothing - . - (attribute) @prepend_space -) -( - "("* @do_nothing - . - "begin" @prepend_space -) -( - "("* @do_nothing - . - "class" @prepend_space -) -( - "("* @do_nothing - . - "exception" @prepend_space -) -( - "("* @do_nothing - . - "external" @prepend_space -) -( - "("* @do_nothing - . - (floating_attribute) @prepend_space -) -( - "("* @do_nothing - . - "for" @prepend_space -) -( - "("* @do_nothing - . - "include" @prepend_space -) -( - "("* @do_nothing - . - "inherit" @prepend_space -) -( - "("* @do_nothing - . - "initializer" @prepend_space -) -( - "("* @do_nothing - . - (item_attribute) @prepend_space -) -( - "("* @do_nothing - . - "let" @prepend_space -) -( - "("* @do_nothing - . - "method" @prepend_space -) -( - "("* @do_nothing - . - "module" @prepend_space -) -( - "("* @do_nothing - . - (module_parameter) @prepend_space -) -( - "("* @do_nothing - . - "mutable" @prepend_space -) -( - "("* @do_nothing - . - "new" @prepend_space -) -( - "("* @do_nothing - . - "object" @prepend_space -) -( - "("* @do_nothing - . - "open" @prepend_space -) -( - "("* @do_nothing - . - (parameter) @prepend_space -) -( - "("* @do_nothing - . - "private" @prepend_space -) -( - "("* @do_nothing - . - "sig" @prepend_space -) -( - "("* @do_nothing - . - "try" @prepend_space -) -( - "("* @do_nothing - . - "type" @prepend_space -) -( - "("* @do_nothing - . - "val" @prepend_space -) -( - "("* @do_nothing - . - "while" @prepend_space -) -( - "("* @do_nothing - . - "*" @prepend_space -) -( - "("* @do_nothing - . - "=" @prepend_space -) -( - "("* @do_nothing - . - "}" @prepend_space -) - ; Put a space after commas, except the last one. ( "," @append_space From 7a9f2e012355f5129f27167d3674a044758e1ea8 Mon Sep 17 00:00:00 2001 From: Nicolas BACQUEY Date: Mon, 2 Sep 2024 18:28:19 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f038302..31c429dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,9 @@ This name should be decided amongst the team before the release. ### Added - [#705](https://github.com/tweag/topiary/pull/705) Added support for Nickel 1.7 extended pattern formatting +### Fixed +- [#720](https://github.com/tweag/topiary/pull/720) [#722](https://github.com/tweag/topiary/pull/722) Various OCaml improvements + ### Changed - [#704](https://github.com/tweag/topiary/pull/704) Refactors our postprocessing code to be more versatile. - [#711](https://github.com/tweag/topiary/pull/711) Feature gate all grammars, with supported and contributed languages built by default.