generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b1c316
commit 4bc7ec2
Showing
7 changed files
with
271 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ ocaml_interface = [] | |
ocamllex = [] | ||
rust = [] | ||
toml = [] | ||
nix = [] | ||
tree_sitter_query = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
;; Nix Formatter based on RFC 166 | ||
|
||
; Leaf nodes (strings, paths, etc.) should be preserved as-is | ||
[ (string) | ||
(indented_string) | ||
(path) | ||
] @leaf | ||
|
||
; Comments | ||
(comment) @comment | ||
|
||
; Convert single-line /* */ comments to # | ||
(comment) @convert_to_hash_comment | ||
|
||
; Preserve multiline comments | ||
(comment) @preserve_multiline_comment | ||
|
||
; Append space after specific tokens | ||
":" @append_space | ||
"=" @append_space | ||
"," @append_spaced_softline | ||
|
||
; Handle indentation and line breaks | ||
; Define softline breaks and indentation starts/ends | ||
"{" @append_spaced_softline @append_indent_start | ||
"}" @prepend_spaced_softline @prepend_indent_end | ||
"[" @append_spaced_softline @append_indent_start | ||
"]" @prepend_spaced_softline @prepend_indent_end | ||
|
||
; Functions | ||
|
||
(function | ||
parameters: (formals | ||
"(" @append_spaced_softline @append_indent_start | ||
(formal_parameter) @formal_param | ||
")" @prepend_spaced_softline @prepend_indent_end | ||
) | ||
body: (_) @function_body | ||
) @function_declaration | ||
|
||
; Ensure each parameter is on a new line with proper indentation | ||
(formal_parameter) @prepend_spaced_softline | ||
|
||
; Attribute Sets | ||
|
||
(attrset | ||
"{" @append_spaced_softline @append_indent_start | ||
(binding) @attr_binding | ||
"}" @prepend_spaced_softline @prepend_indent_end | ||
) @attribute_set | ||
|
||
; Ensure each key-value pair is on its own line | ||
(binding | ||
attrpath: (_) @attr_key | ||
"=" @append_space | ||
value: (_) @attr_value | ||
";" @append_spaced_softline | ||
) | ||
|
||
; Lists | ||
|
||
(list | ||
"[" @append_spaced_softline @append_indent_start | ||
(_) @list_item | ||
"]" @prepend_spaced_softline @prepend_indent_end | ||
) @list | ||
|
||
; Ensure each list element is on its own line | ||
(list | ||
"," @append_spaced_softline | ||
) | ||
|
||
; If-Then-Else Expressions | ||
|
||
(if_expression | ||
"if" @keyword_if @append_space @prepend_spaced_softline | ||
condition: (_) @if_condition | ||
"then" @keyword_then @append_space @prepend_spaced_softline @append_indent_start | ||
body: (_) @if_body @append_indent_end | ||
"else" @keyword_else @append_space @prepend_spaced_softline @append_indent_start | ||
alternative: (_) @else_body @append_indent_end | ||
) | ||
|
||
; Let-In Expressions | ||
|
||
(let_expression | ||
"let" @keyword_let @append_spaced_softline @append_indent_start | ||
(binding | ||
attrpath: (_) @let_binding_name | ||
"=" @append_space | ||
value: (_) @let_binding_value | ||
";" @append_spaced_softline | ||
)+ | ||
"in" @keyword_in @prepend_spaced_softline @prepend_indent_end @append_spaced_softline | ||
body: (_) @let_body | ||
) | ||
|
||
; With Expressions | ||
|
||
(with_expression | ||
"with" @keyword_with @append_space | ||
(_) @with_expression | ||
";" @append_spaced_softline | ||
body: (_) @with_body | ||
) | ||
|
||
; Assert Expressions | ||
|
||
(assert_expression | ||
"assert" @keyword_assert @append_space | ||
(_) @assert_condition | ||
";" @append_spaced_softline | ||
body: (_) @assert_body | ||
) | ||
|
||
; Operator Formatting | ||
|
||
(binary_expression | ||
left: (_) @op_left | ||
operator: (_) @op_symbol | ||
right: (_) @op_right | ||
) | ||
|
||
; Ensure operators start on new lines if they don't fit on a single line | ||
(binary_expression | ||
operator: (_) @prepend_spaced_softline | ||
) | ||
|
||
; Inherit Statements | ||
|
||
(inherit | ||
"inherit" @keyword_inherit @append_space | ||
(inherited_attrs | ||
(attrpath) @inherit_item | ||
)+ | ||
";" @inherit_semicolon | ||
) | ||
|
||
; Handle inherit with source | ||
(inherit | ||
"inherit" @keyword_inherit @append_space | ||
"(" @inherit_parentheses_start | ||
(_) @inherit_source | ||
")" @inherit_parentheses_end | ||
(inherited_attrs | ||
(attrpath) @inherit_item | ||
)+ | ||
";" @inherit_semicolon | ||
) | ||
|
||
; Empty Objects and Arrays | ||
|
||
(attrset | ||
"{" @object_open | ||
"}" @object_close | ||
) @empty_attribute_set | ||
|
||
(list | ||
"[" @array_open | ||
"]" @array_close | ||
) @empty_array | ||
|
||
; String Interpolation | ||
|
||
(interpolation | ||
"${" @interp_start | ||
(_) @interp_content | ||
"}" @interp_end | ||
) | ||
|
||
; Additional Rules | ||
|
||
; Handle let bindings indentation | ||
(let_expression | ||
(binding) @let_binding_indent | ||
) | ||
|
||
; Handle assertions | ||
(assert_expression | ||
"assert" @keyword_assert @append_space | ||
(_) @assert_condition | ||
";" @assert_semicolon | ||
) | ||
|
||
; Handle comments within expressions | ||
(comment) @comment_within_expr | ||
|
||
; Leaf Nodes Preservation | ||
(_) @leaf | ||
|
||
; Handle semicolon placement in bindings | ||
(binding | ||
";" @semicolon_end | ||
) | ||
|
||
; Handle indentation for nested attribute sets | ||
(attrset | ||
(binding | ||
value: (attrset) @nested_attribute_set | ||
) | ||
) | ||
|
||
; Handle newline preservation rules | ||
(ERROR) @collapse_empty_lines | ||
|
||
; Ensure consistent indentation levels | ||
(_) @indent_block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters