Skip to content

Commit

Permalink
Refactor queries for ecma based languages (helix-editor#7207)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuphgammut authored and mtoohey31 committed Jun 2, 2024
1 parent 267f088 commit 1039a22
Show file tree
Hide file tree
Showing 38 changed files with 492 additions and 305 deletions.
3 changes: 0 additions & 3 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ file-types = ["js", "mjs", "cjs"]
shebangs = ["node"]
roots = []
comment-token = "//"
# TODO: highlights-params
language-servers = [ "typescript-language-server" ]
indent = { tab-width = 2, unit = " " }

Expand Down Expand Up @@ -564,7 +563,6 @@ file-types = ["ts", "mts", "cts"]
language-id = "typescript"
shebangs = []
roots = []
# TODO: highlights-params
language-servers = [ "typescript-language-server" ]
indent = { tab-width = 2, unit = " " }

Expand All @@ -579,7 +577,6 @@ injection-regex = "(tsx)" # |typescript
language-id = "typescriptreact"
file-types = ["tsx"]
roots = []
# TODO: highlights-params
language-servers = [ "typescript-language-server" ]
indent = { tab-width = 2, unit = " " }

Expand Down
36 changes: 36 additions & 0 deletions runtime/queries/_javascript/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; Function and method parameters
;-------------------------------

; Javascript and Typescript Treesitter grammars deviate when defining the
; tree structure for parameters, so we need to address them in each specific
; language instead of ecma.

; (p)
(formal_parameters
(identifier) @variable.parameter)

; (...p)
(formal_parameters
(rest_pattern
(identifier) @variable.parameter))

; ({ p })
(formal_parameters
(object_pattern
(shorthand_property_identifier_pattern) @variable.parameter))

; ({ a: p })
(formal_parameters
(object_pattern
(pair_pattern
value: (identifier) @variable.parameter)))

; ([ p ])
(formal_parameters
(array_pattern
(identifier) @variable.parameter))

; (p = 1)
(formal_parameters
(assignment_pattern
left: (identifier) @variable.parameter))
14 changes: 14 additions & 0 deletions runtime/queries/_javascript/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; Definitions
;------------
; Javascript and Typescript Treesitter grammars deviate when defining the
; tree structure for parameters, so we need to address them in each specific
; language instead of ecma.

; (i)
(formal_parameters
(identifier) @local.definition)

; (i = 1)
(formal_parameters
(assignment_pattern
left: (identifier) @local.definition))
88 changes: 88 additions & 0 deletions runtime/queries/_javascript/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
(
(comment)* @doc
.
(method_definition
name: (property_identifier) @name) @definition.method
(#not-eq? @name "constructor")
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.method)
)

(
(comment)* @doc
.
[
(class
name: (_) @name)
(class_declaration
name: (_) @name)
] @definition.class
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.class)
)

(
(comment)* @doc
.
[
(function
name: (identifier) @name)
(function_declaration
name: (identifier) @name)
(generator_function
name: (identifier) @name)
(generator_function_declaration
name: (identifier) @name)
] @definition.function
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(variable_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(assignment_expression
left: [
(identifier) @name
(member_expression
property: (property_identifier) @name)
]
right: [(arrow_function) (function)]
) @definition.function

(pair
key: (property_identifier) @name
value: [(arrow_function) (function)]) @definition.function

(
(call_expression
function: (identifier) @name) @reference.call
(#not-match? @name "^(require)$")
)

(call_expression
function: (member_expression
property: (property_identifier) @name)
arguments: (_) @reference.call)

(new_expression
constructor: (_) @name) @reference.class
55 changes: 55 additions & 0 deletions runtime/queries/_jsx/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
; Opening elements
; ----------------

(jsx_opening_element ((identifier) @constructor
(#match? @constructor "^[A-Z]")))

; Handle the dot operator effectively - <My.Component>
(jsx_opening_element ((nested_identifier (identifier) @tag (identifier) @constructor)))

(jsx_opening_element (identifier) @tag)

; Closing elements
; ----------------

(jsx_closing_element ((identifier) @constructor
(#match? @constructor "^[A-Z]")))

; Handle the dot operator effectively - </My.Component>
(jsx_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor)))

(jsx_closing_element (identifier) @tag)

; Self-closing elements
; ---------------------

(jsx_self_closing_element ((identifier) @constructor
(#match? @constructor "^[A-Z]")))

; Handle the dot operator effectively - <My.Component />
(jsx_self_closing_element ((nested_identifier (identifier) @tag (identifier) @constructor)))

(jsx_self_closing_element (identifier) @tag)

; Attributes
; ----------

(jsx_attribute (property_identifier) @variable.other.member)

; Punctuation
; -----------

; Handle attribute delimiter
(jsx_attribute "=" @punctuation.delimiter)

; <Component>
(jsx_opening_element ["<" ">"] @punctuation.bracket)

; </Component>
(jsx_closing_element ["<" "/" ">"] @punctuation.bracket)

; <Component />
(jsx_self_closing_element ["<" "/" ">"] @punctuation.bracket)

; <> ... </>
(jsx_fragment ["<" "/" ">"] @punctuation.bracket)
7 changes: 7 additions & 0 deletions runtime/queries/_jsx/indents.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
(jsx_fragment)
(jsx_element)
(jsx_self_closing_element)
] @indent

(parenthesized_expression) @indent
139 changes: 139 additions & 0 deletions runtime/queries/_typescript/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
; Namespaces
; ----------

(internal_module
[((identifier) @namespace) ((nested_identifier (identifier) @namespace))])

(ambient_declaration "global" @namespace)

; Parameters
; ----------
; Javascript and Typescript Treesitter grammars deviate when defining the
; tree structure for parameters, so we need to address them in each specific
; language instead of ecma.

; (p: t)
; (p: t = 1)
(required_parameter
(identifier) @variable.parameter)

; (...p: t)
(required_parameter
(rest_pattern
(identifier) @variable.parameter))

; ({ p }: { p: t })
(required_parameter
(object_pattern
(shorthand_property_identifier_pattern) @variable.parameter))

; ({ a: p }: { a: t })
(required_parameter
(object_pattern
(pair_pattern
value: (identifier) @variable.parameter)))

; ([ p ]: t[])
(required_parameter
(array_pattern
(identifier) @variable.parameter))

; (p?: t)
; (p?: t = 1) // Invalid but still posible to hihglight.
(optional_parameter
(identifier) @variable.parameter)

; (...p?: t) // Invalid but still posible to hihglight.
(optional_parameter
(rest_pattern
(identifier) @variable.parameter))

; ({ p }: { p?: t})
(optional_parameter
(object_pattern
(shorthand_property_identifier_pattern) @variable.parameter))

; ({ a: p }: { a?: t })
(optional_parameter
(object_pattern
(pair_pattern
value: (identifier) @variable.parameter)))

; ([ p ]?: t[]) // Invalid but still posible to hihglight.
(optional_parameter
(array_pattern
(identifier) @variable.parameter))

; Punctuation
; -----------

[
":"
] @punctuation.delimiter

(optional_parameter "?" @punctuation.special)
(property_signature "?" @punctuation.special)

(conditional_type ["?" ":"] @operator)

; Keywords
; --------

[
"abstract"
"declare"
"export"
"infer"
"implements"
"keyof"
"namespace"
"override"
] @keyword

[
"type"
"interface"
"enum"
] @keyword.storage.type

[
"public"
"private"
"protected"
"readonly"
] @keyword.storage.modifier

; Types
; -----

(type_identifier) @type
(predefined_type) @type.builtin

; Type arguments and parameters
; -----------------------------

(type_arguments
[
"<"
">"
] @punctuation.bracket)

(type_parameters
[
"<"
">"
] @punctuation.bracket)

; Literals
; --------

[
(template_literal_type)
] @string

; Tokens
; ------

(template_type
"${" @punctuation.special
"}" @punctuation.special) @embedded
5 changes: 5 additions & 0 deletions runtime/queries/_typescript/indents.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
(enum_declaration)
(interface_declaration)
(object_type)
] @indent
16 changes: 16 additions & 0 deletions runtime/queries/_typescript/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; Definitions
;------------

; Javascript and Typescript Treesitter grammars deviate when defining the
; tree structure for parameters, so we need to address them in each specific
; language instead of ecma.

; (i: t)
; (i: t = 1)
(required_parameter
(identifier) @local.definition)

; (i?: t)
; (i?: t = 1) // Invalid but still posible to hihglight.
(optional_parameter
(identifier) @local.definition)
Loading

0 comments on commit 1039a22

Please sign in to comment.