Skip to content

Commit

Permalink
Syntax improvements (#1)
Browse files Browse the repository at this point in the history
Moved from zed-industries/zed#22293.

- Fixes various issues with current syntax highlighting: modules and
patterns are not highlighted, number literals are treated as constants
etc. New highlights are mostly copied from
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/elm/highlights.scm)
with some modifications and removals of things that are not supported by
Zed. In particular, `@modules` are highlighted as `@title`, similarly to
how it's done in Zed's Haskell extension.
- Added `brackets.scm`.
- Changed the default tab_size to 4 as that's how most of Elm projects
are formatted and what elm-format uses.
  • Loading branch information
edkv authored Jan 10, 2025
1 parent 4b66971 commit 889d630
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 55 deletions.
3 changes: 3 additions & 0 deletions languages/elm/brackets.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
("(" @open ")" @close)
("[" @open "]" @close)
("{" @open "}" @close)
2 changes: 1 addition & 1 deletion languages/elm/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ brackets = [
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
]
tab_size = 2
tab_size = 4
246 changes: 192 additions & 54 deletions languages/elm/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,72 +1,210 @@
[
"if"
"then"
"else"
"let"
"in"
(case)
(of)
(backslash)
(as)
(port)
(exposing)
(alias)
(import)
(module)
(type)
(arrow)
] @keyword
(line_comment)
(block_comment)
] @comment

; Keywords
;---------
[
(eq)
(operator_identifier)
(colon)
] @operator

(type_annotation(lower_case_identifier) @function)
(port_annotation(lower_case_identifier) @function)
(function_declaration_left(lower_case_identifier) @function.definition)
(module)
(import)
(exposing)
(port)
(type)
(alias)
(infix)
(as)
(case)
(of)
"if"
"then"
"else"
"let"
"in"
] @keyword

(function_call_expr
target: (value_expr
name: (value_qid (lower_case_identifier) @function)))

(exposed_value(lower_case_identifier) @function)
(exposed_type(upper_case_identifier) @type)
; Punctuation
;------------
(double_dot) @punctuation.special

(field_access_expr(value_expr(value_qid)) @identifier)
(lower_pattern) @variable
(record_base_identifier) @identifier
[
","
"|"
(dot)
] @punctuation.delimiter

[
"("
")"
"("
")"
"{"
"}"
"["
"]"
] @punctuation.bracket

[
"|"
","
] @punctuation.delimiter
; Variables
;----------
(value_qid
(lower_case_identifier) @variable)

(value_declaration
(function_declaration_left
(lower_case_identifier) @variable))

(type_annotation
(lower_case_identifier) @variable)

(port_annotation
(lower_case_identifier) @variable)

(number_constant_expr) @constant
(record_base_identifier
(lower_case_identifier) @variable)

(type_declaration(upper_case_identifier) @type)
(type_ref) @type
(type_alias_declaration name: (upper_case_identifier) @type)
(lower_pattern
(lower_case_identifier) @variable)

(value_expr(upper_case_qid(upper_case_identifier)) @type)
(exposed_value
(lower_case_identifier) @variable)

(value_qid
((dot)
(lower_case_identifier) @variable))

(field_access_expr
((dot)
(lower_case_identifier) @variable))

(function_declaration_left
(lower_pattern
(lower_case_identifier) @variable))

; Functions
;----------
(value_declaration
functionDeclarationLeft: (function_declaration_left
(lower_case_identifier) @function
(pattern)))

(value_declaration
functionDeclarationLeft: (function_declaration_left
(lower_case_identifier) @function
pattern: (_)))

(value_declaration
functionDeclarationLeft: (function_declaration_left
(lower_case_identifier) @function)
body: (anonymous_function_expr))

(type_annotation
name: (lower_case_identifier) @function
typeExpression: (type_expression
(arrow)))

(port_annotation
name: (lower_case_identifier) @function
typeExpression: (type_expression
(arrow)))

(function_call_expr
target: (value_expr
(value_qid
(lower_case_identifier) @function)))

; Operators
;----------
[
(line_comment)
(block_comment)
] @comment
(operator_identifier)
(eq)
(colon)
(arrow)
(backslash)
"::"
] @operator

; Modules
;--------
(module_declaration
(upper_case_qid
(upper_case_identifier) @title))

(import_clause
(upper_case_qid
(upper_case_identifier) @title))

(as_clause
(upper_case_identifier) @title)

(value_expr
(value_qid
(upper_case_identifier) @title))

; Types
;------
(type_declaration
(upper_case_identifier) @type)

(type_ref
(upper_case_qid
(upper_case_identifier) @type))

(type_variable
(lower_case_identifier) @type)

(lower_type_name
(lower_case_identifier) @type)

(exposed_type
(upper_case_identifier) @type)

(type_alias_declaration
(upper_case_identifier) @type)

(string_escape) @string.escape
(field_type
name: (lower_case_identifier) @property)

(field
name: (lower_case_identifier) @property)

(type_declaration
(union_variant
(upper_case_identifier) @constructor))

(nullary_constructor_argument_pattern
(upper_case_qid
(upper_case_identifier) @constructor))

(union_pattern
(upper_case_qid
(upper_case_identifier) @constructor))

(value_expr
(upper_case_qid
(upper_case_identifier)) @constructor)

; Literals
;---------
(number_constant_expr
(number_literal) @number)

(upper_case_qid
((upper_case_identifier) @boolean
(#any-of? @boolean "True" "False")))

[
(open_quote)
(close_quote)
(regular_string_part)
(open_char)
(close_char)
(open_quote)
(close_quote)
(open_char)
(close_char)
] @string

(string_constant_expr
(string_escape) @string)

(string_constant_expr
(regular_string_part) @string)

(char_constant_expr
(string_escape) @string)

(char_constant_expr
(regular_string_part) @string)

0 comments on commit 889d630

Please sign in to comment.