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