Skip to content

Commit

Permalink
Indent behavior repro
Browse files Browse the repository at this point in the history
Initial state. Topiary succeeds but the content keep being indented to
the right without being properly un-indented then (try on
nickel-indent-repro.ncl)
  • Loading branch information
yannham committed Sep 19, 2024
1 parent 67413d3 commit 1a163f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
13 changes: 13 additions & 0 deletions nickel-indent-repro.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
foo | Number,

foo
| Number,

foo | Number = 1,

foo
| Array Number
= [1,2,
3],
}]
74 changes: 51 additions & 23 deletions topiary-queries/queries/nickel.scm
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@
; Note that this query is disjoint from the other one defining the "annotations"
; scope above, which that only applies when the annotation is the last named
; node of its parent. Thus, only one of the two ever matches.
;
; WARNING: currently, splitting the rule in two is useless because we actually
; add the newline before `=` inconditionally (see
; [^annotations-followed-by-eq]). However, when this limitation is lifted, the
; current rule architecture will allow for the desired formatting with minimal
; change.
(
(#scope_id! "annotations")
(_) @append_begin_scope
Expand All @@ -371,8 +377,47 @@
(annot_atom) @prepend_spaced_scoped_softline
)

; Add a new line before the last annotation and the following equal sign, if any,
; in a multi-line context.
; Add a new line before the last annotation and the following equal sign.
;
; [^annotations-followed-by-eq]: Ideally, we would like to only 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.
(
(#scope_id! "annotations")
(annot) @append_spaced_scoped_softline
Expand All @@ -387,29 +432,12 @@
(annot_atom) @prepend_indent_start @append_indent_end
)

; In multi-line mode, in presence of annotations, we want to indent the
; potential definitions that comes after.
;
; In single-line mode (in the "annotations" scope), we want a packed
; representation:
;
; let foo | Number = [
; 1,
; 2,
; ]
; in foo
; Indent the RHS of the let-binding in presence of annotations.
;
; But we want to format a multi-line version as:
;
; let foo
; | Number
; = [
; 1,
; 2,
; ]
; in foo
; Ideally, we would like to indent only when annotations are multi-line, but
; this isn't current possible; see [^annotations-followed-by-eq].
(_
(annot (#multi_line_scope_only! "annotations")) @prepend_indent_start
(annot) @append_indent_start
.
"="
.
Expand Down

0 comments on commit 1a163f9

Please sign in to comment.