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

Nickel: formatting of single-line annotated let #768

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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) [#755](https://github.com/tweag/topiary/pull/755) [#759](https://github.com/tweag/topiary/pull/759) [#764](https://github.com/tweag/topiary/pull/764 Various OCaml improvements
- [#762](https://github.com/tweag/topiary/pull/762) Various Rust improvements
- [#744](https://github.com/tweag/topiary/pull/744) Nickel: fix the indentation of `in` for annotated multiline let-bindings
- [#744](https://github.com/tweag/topiary/pull/744) [#768](https://github.com/tweag/topiary/pull/768) Nickel: fix the formatting of annotated multiline let-bindings
- [#761](https://github.com/tweag/topiary/pull/761) No longer use error code 1 for unspecified errors

### Changed
Expand Down
33 changes: 31 additions & 2 deletions topiary-cli/tests/samples/expected/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@
3
),

# regression test for https://github.com/tweag/topiary/issues/743
# (partially fixed)
# regression tests for https://github.com/tweag/topiary/issues/743
let foo
| Number
= [
Expand All @@ -153,6 +152,36 @@
in
foo
@ [],
let x | Number = 1 + 1 in x,
let x | Number = 1 + 1
in x,
let x | Number | std.number.PosNat =
1
+ 1
in x,
let x | Number = 1 + 1
in
x,
let x | Array String = ["a", "b"]
in x,
let x | Array Number = [
1,
2,
3
]
in
x,
let x
| Array Number
= [
1 + 1,
2 + 2,
3 + 3,
]
in
x,

# let blocks
let x = 1, y = 2 in x + y,
let
x = 1,
Expand Down
42 changes: 40 additions & 2 deletions topiary-cli/tests/samples/input/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ bar == 'Goodbye
else
3),

# regression test for https://github.com/tweag/topiary/issues/743
# (partially fixed)
# regression tests for https://github.com/tweag/topiary/issues/743
let foo
| Number
=
Expand All @@ -143,6 +142,45 @@ in
foo
@ [],

let x | Number = 1 + 1 in x,
let x | Number = 1 + 1
in x,

let x | Number | std.number.PosNat = 1
+ 1
in x,


let x | Number
= 1 + 1
in
x,

let x | Array String
=
["a","b"]
in x,

let x | Array Number
= [
1,
2,
3
]
in
x,

let x
| Array Number
=
[1 + 1,
2+2,
3+3,]
in
x,

# let blocks

let x = 1, y = 2 in x + y,

let x = 1,
Expand Down
59 changes: 28 additions & 31 deletions topiary-queries/queries/nickel.scm
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@

;; Annotations

; 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 preceding the annotations. This scope is used
; exclusively to lay out annotations on multiple lines. The rule checks if the
; annotations were intended to be on newlines as in:
;
; id
; | a -> a
Expand All @@ -378,11 +379,24 @@
(annot_atom) @prepend_spaced_scoped_softline
)

; Add a new line before the last annotation and the following equal sign.
; Start a new scope for annotations, used to properly indent any content coming
; after the annotations. We use Topiary's measuring scope feature: the
; multi-liness of this scope is entirely decided by the annotations only, but
; what we want to affect is larger, namely the content of the binding coming
; after the `=`.
(
(#scope_id! "annotations_with_content")
(_) @append_begin_scope @append_begin_measuring_scope
.
(annot) @append_end_measuring_scope
"="
(term) @append_end_scope
)

; Add a new line before the last annotation and the following equal sign, when
; the annotations are multi-line.
;
; [^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:
; Indeed, we want to have the following formatting (multi-line):
;
; let foo
; | Array Number
Expand All @@ -393,34 +407,16 @@
; ]
; in ...
;
; But
; But (single-line):
;
; 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) @append_spaced_softline
(#scope_id! "annotations_with_content")
(annot) @append_spaced_scoped_softline
.
"="
)
Expand All @@ -430,11 +426,12 @@
(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].
; Indent the bound expression of a let-binding (or a field definition) in
; presence of multi-line annotations. That's where we use the measuring scope of
; "annotations_with_content": we want this rule to fire only when (annot) is
; multi-line, regardless of the multi-liness of the ("=" (term)) part.
(_
(#multi_line_scope_only! "annotations_with_content")
(annot) @append_indent_start
"="
(term) @append_indent_end
Expand Down
Loading