Skip to content

Commit 7229c0f

Browse files
authored
Merge pull request #744 from tweag/nickel/fix-multiline-let
Fix Nickel inconsistent formatting of annotated let-bindings
2 parents 12c323f + 112350b commit 7229c0f

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This name should be decided amongst the team before the release.
5151
### Fixed
5252
- [#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)
5353
[#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
54+
- [#744](https://github.com/tweag/topiary/pull/744) Nickel: fix the indentation of `in` for annotated multiline let-bindings
5455

5556
### Changed
5657
- [#704](https://github.com/tweag/topiary/pull/704) Refactors our postprocessing code to be more versatile.

topiary-cli/tests/samples/expected/nickel.ncl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,20 @@
139139
1
140140
else
141141
3
142-
)
142+
),
143+
144+
# regression test for https://github.com/tweag/topiary/issues/743
145+
# (partially fixed)
146+
let foo
147+
| Number
148+
= [
149+
1,
150+
2,
151+
3
152+
]
153+
in
154+
foo
155+
@ [],
143156
],
144157

145158
# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df

topiary-cli/tests/samples/input/nickel.ncl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,21 @@ bar == 'Goodbye
127127
if x == 1 then
128128
1
129129
else
130-
3)
130+
3),
131+
132+
# regression test for https://github.com/tweag/topiary/issues/743
133+
# (partially fixed)
134+
let foo
135+
| Number
136+
=
137+
[
138+
1,
139+
2,
140+
3
141+
]
142+
in
143+
foo
144+
@ [],
131145
],
132146

133147
# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df

topiary-queries/queries/nickel.scm

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,15 @@
318318

319319
;; Annotations
320320

321-
; Start an indentation block from the start of the annotations to the
322-
; end of the enclosing node
323-
(_
324-
(annot) @prepend_indent_start
325-
) @append_indent_end
326-
327-
; Start a scope from the node previous to the annotations.
328-
; This properly checks if the annotations were intended to be
329-
; on newlines in such cases as:
321+
; Start a scope from the node previous to the annotations. This properly checks
322+
; if the annotations were intended to be on newlines in such cases as:
323+
;
330324
; id
331325
; | a -> a
326+
;
332327
; which, without the annotations scope, would consider the annotations to be a
333328
; single line node and format it as such:
329+
;
334330
; id | a -> a
335331
(
336332
(#scope_id! "annotations")
@@ -339,17 +335,72 @@
339335
(annot) @append_end_scope
340336
)
341337

342-
; Put each annotation -- and the equals sign, if it follows annotations
343-
; -- on a new line, in a multi-line context.
338+
; Put each annotation on a new line, in a multi-line context.
344339
(annot
345340
(#scope_id! "annotations")
346341
(annot_atom) @prepend_spaced_scoped_softline
347342
)
348343

344+
; Add a new line before the last annotation and the following equal sign.
345+
;
346+
; [^annotations-followed-by-eq]: Ideally, we would like to add this new
347+
; line for multi-line annotations only. That is, we would like to have the
348+
; following formatting:
349+
;
350+
; let foo
351+
; | Array Number
352+
; | doc "hello"
353+
; = [
354+
; 1,
355+
; 2,
356+
; ]
357+
; in ...
358+
;
359+
; But
360+
;
361+
; let foo | Array Number = [
362+
; 1,
363+
; 2,
364+
; ]
365+
; in ...
366+
;
367+
; While adding a scoped line isn't an issue, note that in the examples above,
368+
; the indentation of what comes after the `=` sign depends on the multi-liness
369+
; of the annotations (and thus of the multiliness of the "annotations" scope).
370+
; However, the RHS isn't part of this scope (and we don't want it to be).
371+
; Unfortunately, this can't be achieved in current Topiary.
372+
;
373+
; In the meantime, we always put the `=` sign a new line, whether in single-line
374+
; or multi-line mode, and always indent the RHS further in presence of
375+
; annotations. This give the following formatting for the second example:
376+
;
377+
; let foo | Array Number
378+
; = [
379+
; 1,
380+
; 2,
381+
; ]
382+
; in ...
383+
;
384+
; which isn't optimal but still acceptable.
349385
(
350-
(annot)
386+
(annot) @append_spaced_softline
351387
.
352-
"=" @prepend_spaced_softline
388+
"="
389+
)
390+
391+
; Indent the annotations with respect to the identifier they annotate.
392+
(
393+
(annot) @prepend_indent_start @append_indent_end
394+
)
395+
396+
; Indent the RHS of the let-binding in presence of annotations.
397+
;
398+
; Ideally, we would like to indent only when annotations are multi-line, but
399+
; this isn't current possible; see [^annotations-followed-by-eq].
400+
(_
401+
(annot) @append_indent_start
402+
"="
403+
(term) @append_indent_end
353404
)
354405

355406
; Break a multi-line polymorphic type annotation after the type

0 commit comments

Comments
 (0)