diff --git a/CHANGELOG.md b/CHANGELOG.md index 794443ad..035fc2d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ This name should be decided amongst the team before the release. - [#711](https://github.com/tweag/topiary/pull/711) Feature gate all grammars, with supported and contributed languages built by default. - [#716](https://github.com/tweag/topiary/pull/716) Dynamicly fetch, compile, and load language grammars. Topiary now no longer ships with statically linked grammars. - [#732](https://github.com/tweag/topiary/pull/732) Change how function application and parenthesized expressions are treated in Nickel to reduce the overall noise and indentation +- [#736](https://github.com/tweag/topiary/pull/668) Updates our Nickel grammar, and adds support for let blocks. ## v0.4.0 - Exquisite Elm - 2024-05-15 diff --git a/topiary-cli/tests/samples/expected/nickel.ncl b/topiary-cli/tests/samples/expected/nickel.ncl index 5407fb33..206ae561 100644 --- a/topiary-cli/tests/samples/expected/nickel.ncl +++ b/topiary-cli/tests/samples/expected/nickel.ncl @@ -153,6 +153,16 @@ in foo @ [], + let x = 1, y = 2 in x + y, + let + x = 1, + y = 2, + in x + y, + let rec + x = 1, + y = 2, + in + x + y, ], # Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df diff --git a/topiary-cli/tests/samples/input/nickel.ncl b/topiary-cli/tests/samples/input/nickel.ncl index 05a86a50..0b439c95 100644 --- a/topiary-cli/tests/samples/input/nickel.ncl +++ b/topiary-cli/tests/samples/input/nickel.ncl @@ -142,6 +142,18 @@ else in foo @ [], + + let x = 1, y = 2 in x + y, + + let x = 1, + y = 2, + in x + y, + + let + rec x = 1, + y = 2, + in + x + y, ], # Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df diff --git a/topiary-config/languages.ncl b/topiary-config/languages.ncl index e866ad9c..bc3603b5 100644 --- a/topiary-config/languages.ncl +++ b/topiary-config/languages.ncl @@ -43,7 +43,7 @@ extensions = ["ncl"], grammar = { git = "https://github.com/nickel-lang/tree-sitter-nickel", - rev = "43433d8477b24cd13acaac20a66deda49b7e2547", + rev = "88d836a24b3b11c8720874a1a9286b8ae838d30a", }, }, diff --git a/topiary-queries/queries/nickel.scm b/topiary-queries/queries/nickel.scm index 012a28b7..be4b0579 100644 --- a/topiary-queries/queries/nickel.scm +++ b/topiary-queries/queries/nickel.scm @@ -295,6 +295,14 @@ ; ; let [rec] IDENT = EXPR in EXPR ; +; or +; +; let [rec] +; IDENT = EXPR, +; IDENT = EXPR, +; in +; EXPR +; ; The formatting for the bound expression is handled by the above rules, ; which also apply to record field values. The "in" should appear on a ; new line, if the entire let expression is multi-line. The result @@ -303,14 +311,43 @@ ; expression, to avoid long diagonals in a series of let expressions ; (which is idiomatic). +; A let block containing multiple bindings. If this is multiline, the first +; binding should appear on a new line, and the bindings should be indented. (let_expr (#scope_id! "let_result") (let_in_block + "let" + . + ; Prepend before the first binding instead of appending after the "let", + ; so that in the case of a "let rec" the line break goes after the "rec". + (let_binding) @prepend_spaced_softline @prepend_indent_start + (let_binding) + "in" @prepend_indent_end @prepend_begin_scope @prepend_spaced_softline + ) + (term) @append_end_scope +) + +; A let with a single binding. The binding should be on the same line as the "let". +(let_expr + (#scope_id! "let_result") + (let_in_block + "let" + . + (let_binding) + . "in" @prepend_begin_scope @prepend_spaced_softline ) (term) @append_end_scope ) +; When binding multiple values in a let block, allow new lines between the bindings. +(let_expr + (#scope_id! "let_result") + (let_in_block + ("," @append_spaced_softline) + ) +) + (let_expr (#scope_id! "let_result") (term) @prepend_spaced_scoped_softline @@ -603,12 +640,33 @@ . ) +; Allow newlines after the comma (or semicolon) following a container +; element. ( (#scope_id! "container") [ - "," - ";" - ] @append_spaced_scoped_softline + (record_field) + (record_last_field) + (field_pattern) + (last_field_pat) + (match_branch) + (term) + (pattern) + (last_elem_pat) + (enum) + ] + . + ["," ";"] @append_spaced_scoped_softline + . + (comment)? @do_nothing +) + +; Enums and records can have a `;` at the very beginning; allow spaces after +; these ones also. +(_ + (#scope_id! "container") + . + ";" @append_spaced_scoped_softline . (comment)? @do_nothing ) diff --git a/web-playground/public/scripts/tree-sitter-nickel.wasm b/web-playground/public/scripts/tree-sitter-nickel.wasm index a2bfbc40..6ffd7e8e 100755 Binary files a/web-playground/public/scripts/tree-sitter-nickel.wasm and b/web-playground/public/scripts/tree-sitter-nickel.wasm differ