|
| 1 | +;; Nix Formatter based on RFC 166 |
| 2 | + |
| 3 | +; Leaf nodes (strings, paths, etc.) should be preserved as-is |
| 4 | +[ (string) |
| 5 | + (indented_string) |
| 6 | + (path) |
| 7 | +] @leaf |
| 8 | + |
| 9 | +; Comments |
| 10 | +(comment) @comment |
| 11 | + |
| 12 | +; Convert single-line /* */ comments to # |
| 13 | +(comment) @convert_to_hash_comment |
| 14 | + |
| 15 | +; Preserve multiline comments |
| 16 | +(comment) @preserve_multiline_comment |
| 17 | + |
| 18 | +; Append space after specific tokens |
| 19 | +":" @append_space |
| 20 | +"=" @append_space |
| 21 | +"," @append_spaced_softline |
| 22 | + |
| 23 | +; Handle indentation and line breaks |
| 24 | +; Define softline breaks and indentation starts/ends |
| 25 | +"{" @append_spaced_softline @append_indent_start |
| 26 | +"}" @prepend_spaced_softline @prepend_indent_end |
| 27 | +"[" @append_spaced_softline @append_indent_start |
| 28 | +"]" @prepend_spaced_softline @prepend_indent_end |
| 29 | + |
| 30 | +; Functions |
| 31 | + |
| 32 | +(function |
| 33 | + parameters: (formals |
| 34 | + "(" @append_spaced_softline @append_indent_start |
| 35 | + (formal_parameter) @formal_param |
| 36 | + ")" @prepend_spaced_softline @prepend_indent_end |
| 37 | + ) |
| 38 | + body: (_) @function_body |
| 39 | +) @function_declaration |
| 40 | + |
| 41 | +; Ensure each parameter is on a new line with proper indentation |
| 42 | +(formal_parameter) @prepend_spaced_softline |
| 43 | + |
| 44 | +; Attribute Sets |
| 45 | + |
| 46 | +(attrset |
| 47 | + "{" @append_spaced_softline @append_indent_start |
| 48 | + (binding) @attr_binding |
| 49 | + "}" @prepend_spaced_softline @prepend_indent_end |
| 50 | +) @attribute_set |
| 51 | + |
| 52 | +; Ensure each key-value pair is on its own line |
| 53 | +(binding |
| 54 | + attrpath: (_) @attr_key |
| 55 | + "=" @append_space |
| 56 | + value: (_) @attr_value |
| 57 | + ";" @append_spaced_softline |
| 58 | +) |
| 59 | + |
| 60 | +; Lists |
| 61 | + |
| 62 | +(list |
| 63 | + "[" @append_spaced_softline @append_indent_start |
| 64 | + (_) @list_item |
| 65 | + "]" @prepend_spaced_softline @prepend_indent_end |
| 66 | +) @list |
| 67 | + |
| 68 | +; Ensure each list element is on its own line |
| 69 | +(list |
| 70 | + "," @append_spaced_softline |
| 71 | +) |
| 72 | + |
| 73 | +; If-Then-Else Expressions |
| 74 | + |
| 75 | +(if_expression |
| 76 | + "if" @keyword_if @append_space @prepend_spaced_softline |
| 77 | + condition: (_) @if_condition |
| 78 | + "then" @keyword_then @append_space @prepend_spaced_softline @append_indent_start |
| 79 | + body: (_) @if_body @append_indent_end |
| 80 | + "else" @keyword_else @append_space @prepend_spaced_softline @append_indent_start |
| 81 | + alternative: (_) @else_body @append_indent_end |
| 82 | +) |
| 83 | + |
| 84 | +; Let-In Expressions |
| 85 | + |
| 86 | +(let_expression |
| 87 | + "let" @keyword_let @append_spaced_softline @append_indent_start |
| 88 | + (binding |
| 89 | + attrpath: (_) @let_binding_name |
| 90 | + "=" @append_space |
| 91 | + value: (_) @let_binding_value |
| 92 | + ";" @append_spaced_softline |
| 93 | + )+ |
| 94 | + "in" @keyword_in @prepend_spaced_softline @prepend_indent_end @append_spaced_softline |
| 95 | + body: (_) @let_body |
| 96 | +) |
| 97 | + |
| 98 | +; With Expressions |
| 99 | + |
| 100 | +(with_expression |
| 101 | + "with" @keyword_with @append_space |
| 102 | + (_) @with_expression |
| 103 | + ";" @append_spaced_softline |
| 104 | + body: (_) @with_body |
| 105 | +) |
| 106 | + |
| 107 | +; Assert Expressions |
| 108 | + |
| 109 | +(assert_expression |
| 110 | + "assert" @keyword_assert @append_space |
| 111 | + (_) @assert_condition |
| 112 | + ";" @append_spaced_softline |
| 113 | + body: (_) @assert_body |
| 114 | +) |
| 115 | + |
| 116 | +; Operator Formatting |
| 117 | + |
| 118 | +(binary_expression |
| 119 | + left: (_) @op_left |
| 120 | + operator: (_) @op_symbol |
| 121 | + right: (_) @op_right |
| 122 | +) |
| 123 | + |
| 124 | +; Ensure operators start on new lines if they don't fit on a single line |
| 125 | +(binary_expression |
| 126 | + operator: (_) @prepend_spaced_softline |
| 127 | +) |
| 128 | + |
| 129 | +; Inherit Statements |
| 130 | + |
| 131 | +(inherit |
| 132 | + "inherit" @keyword_inherit @append_space |
| 133 | + (inherited_attrs |
| 134 | + (attrpath) @inherit_item |
| 135 | + )+ |
| 136 | + ";" @inherit_semicolon |
| 137 | +) |
| 138 | + |
| 139 | +; Handle inherit with source |
| 140 | +(inherit |
| 141 | + "inherit" @keyword_inherit @append_space |
| 142 | + "(" @inherit_parentheses_start |
| 143 | + (_) @inherit_source |
| 144 | + ")" @inherit_parentheses_end |
| 145 | + (inherited_attrs |
| 146 | + (attrpath) @inherit_item |
| 147 | + )+ |
| 148 | + ";" @inherit_semicolon |
| 149 | +) |
| 150 | + |
| 151 | +; Empty Objects and Arrays |
| 152 | + |
| 153 | +(attrset |
| 154 | + "{" @object_open |
| 155 | + "}" @object_close |
| 156 | +) @empty_attribute_set |
| 157 | + |
| 158 | +(list |
| 159 | + "[" @array_open |
| 160 | + "]" @array_close |
| 161 | +) @empty_array |
| 162 | + |
| 163 | +; String Interpolation |
| 164 | + |
| 165 | +(interpolation |
| 166 | + "${" @interp_start |
| 167 | + (_) @interp_content |
| 168 | + "}" @interp_end |
| 169 | +) |
| 170 | + |
| 171 | +; Additional Rules |
| 172 | + |
| 173 | +; Handle let bindings indentation |
| 174 | +(let_expression |
| 175 | + (binding) @let_binding_indent |
| 176 | +) |
| 177 | + |
| 178 | +; Handle assertions |
| 179 | +(assert_expression |
| 180 | + "assert" @keyword_assert @append_space |
| 181 | + (_) @assert_condition |
| 182 | + ";" @assert_semicolon |
| 183 | +) |
| 184 | + |
| 185 | +; Handle comments within expressions |
| 186 | +(comment) @comment_within_expr |
| 187 | + |
| 188 | +; Leaf Nodes Preservation |
| 189 | +(_) @leaf |
| 190 | + |
| 191 | +; Handle semicolon placement in bindings |
| 192 | +(binding |
| 193 | + ";" @semicolon_end |
| 194 | +) |
| 195 | + |
| 196 | +; Handle indentation for nested attribute sets |
| 197 | +(attrset |
| 198 | + (binding |
| 199 | + value: (attrset) @nested_attribute_set |
| 200 | + ) |
| 201 | +) |
| 202 | + |
| 203 | +; Handle newline preservation rules |
| 204 | +(ERROR) @collapse_empty_lines |
| 205 | + |
| 206 | +; Ensure consistent indentation levels |
| 207 | +(_) @indent_block |
0 commit comments