Skip to content

Commit

Permalink
Merge pull request #744 from tweag/nickel/fix-multiline-let
Browse files Browse the repository at this point in the history
Fix Nickel inconsistent formatting of annotated let-bindings
  • Loading branch information
yannham authored Sep 20, 2024
2 parents 12c323f + 112350b commit 7229c0f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This name should be decided amongst the team before the release.
### Fixed
- [#720](https://github.com/tweag/topiary/pull/720) [#722](https://github.com/tweag/topiary/pull/722) [#723](https://github.com/tweag/topiary/pull/723) [#724](https://github.com/tweag/topiary/pull/724) [#735](https://github.com/tweag/topiary/pull/735)
[#738](https://github.com/tweag/topiary/pull/738) [#739](https://github.com/tweag/topiary/pull/739) [#745](https://github.com/tweag/topiary/pull/745) Various OCaml improvements
- [#744](https://github.com/tweag/topiary/pull/744) Nickel: fix the indentation of `in` for annotated multiline let-bindings

### Changed
- [#704](https://github.com/tweag/topiary/pull/704) Refactors our postprocessing code to be more versatile.
Expand Down
15 changes: 14 additions & 1 deletion topiary-cli/tests/samples/expected/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,20 @@
1
else
3
)
),

# regression test for https://github.com/tweag/topiary/issues/743
# (partially fixed)
let foo
| Number
= [
1,
2,
3
]
in
foo
@ [],
],

# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df
Expand Down
16 changes: 15 additions & 1 deletion topiary-cli/tests/samples/input/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,21 @@ bar == 'Goodbye
if x == 1 then
1
else
3)
3),

# regression test for https://github.com/tweag/topiary/issues/743
# (partially fixed)
let foo
| Number
=
[
1,
2,
3
]
in
foo
@ [],
],

# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df
Expand Down
77 changes: 64 additions & 13 deletions topiary-queries/queries/nickel.scm
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,15 @@

;; Annotations

; Start an indentation block from the start of the annotations to the
; end of the enclosing node
(_
(annot) @prepend_indent_start
) @append_indent_end

; Start a scope from the node previous to the annotations.
; This properly checks if the annotations were intended to be
; on newlines in such cases as:
; Start a scope from the node previous to the annotations. This properly checks
; if the annotations were intended to be on newlines in such cases as:
;
; id
; | a -> a
;
; which, without the annotations scope, would consider the annotations to be a
; single line node and format it as such:
;
; id | a -> a
(
(#scope_id! "annotations")
Expand All @@ -339,17 +335,72 @@
(annot) @append_end_scope
)

; Put each annotation -- and the equals sign, if it follows annotations
; -- on a new line, in a multi-line context.
; Put each annotation on a new line, in a multi-line context.
(annot
(#scope_id! "annotations")
(annot_atom) @prepend_spaced_scoped_softline
)

; Add a new line before the last annotation and the following equal sign.
;
; [^annotations-followed-by-eq]: Ideally, we would like to add this new
; line for multi-line annotations only. That is, we would like to have the
; following formatting:
;
; let foo
; | Array Number
; | doc "hello"
; = [
; 1,
; 2,
; ]
; in ...
;
; But
;
; let foo | Array Number = [
; 1,
; 2,
; ]
; in ...
;
; While adding a scoped line isn't an issue, note that in the examples above,
; the indentation of what comes after the `=` sign depends on the multi-liness
; of the annotations (and thus of the multiliness of the "annotations" scope).
; However, the RHS isn't part of this scope (and we don't want it to be).
; Unfortunately, this can't be achieved in current Topiary.
;
; In the meantime, we always put the `=` sign a new line, whether in single-line
; or multi-line mode, and always indent the RHS further in presence of
; annotations. This give the following formatting for the second example:
;
; let foo | Array Number
; = [
; 1,
; 2,
; ]
; in ...
;
; which isn't optimal but still acceptable.
(
(annot)
(annot) @append_spaced_softline
.
"=" @prepend_spaced_softline
"="
)

; Indent the annotations with respect to the identifier they annotate.
(
(annot) @prepend_indent_start @append_indent_end
)

; Indent the RHS of the let-binding in presence of annotations.
;
; Ideally, we would like to indent only when annotations are multi-line, but
; this isn't current possible; see [^annotations-followed-by-eq].
(_
(annot) @append_indent_start
"="
(term) @append_indent_end
)

; Break a multi-line polymorphic type annotation after the type
Expand Down

0 comments on commit 7229c0f

Please sign in to comment.