Refactor queries for ecma based languages (#7207)

pull/16/head
Gammut 1 year ago committed by GitHub
parent 28452e1f2a
commit 607b426e26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 = " " }
@ -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 = " " }
@ -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 = " " }

@ -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))

@ -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))

@ -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

@ -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)

@ -0,0 +1,7 @@
[
(jsx_fragment)
(jsx_element)
(jsx_self_closing_element)
] @indent
(parenthesized_expression) @indent

@ -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

@ -0,0 +1,5 @@
[
(enum_declaration)
(interface_declaration)
(object_type)
] @indent

@ -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)

@ -0,0 +1,23 @@
(function_signature
name: (identifier) @name) @definition.function
(method_signature
name: (property_identifier) @name) @definition.method
(abstract_method_signature
name: (property_identifier) @name) @definition.method
(abstract_class_declaration
name: (type_identifier) @name) @definition.class
(module
name: (identifier) @name) @definition.module
(interface_declaration
name: (type_identifier) @name) @definition.interface
(type_annotation
(type_identifier) @name) @reference.type
(new_expression
constructor: (identifier) @name) @reference.class

@ -0,0 +1,6 @@
[
(interface_declaration
body:(_) @class.inside)
(type_alias_declaration
value: (_) @class.inside)
] @class.around

@ -0,0 +1,14 @@
# Inheritance model for ecma-based languages
Ecma-based languages share many traits. Because of this we want to share as many queries as possible while avoiding nested inheritance that can make query behaviour unpredictable due to unexpected precedence.
To achieve that, there are "public" and "private" versions for javascript, jsx, and typescript query files, that share the same name, but the "private" version name starts with an underscore (with the exception of ecma, that already exists as a sort of base "private" language). This allows the "private" versions to host the specific queries of the language excluding any `; inherits` statement, in order to make them safe to be inherited by the "public" version of the same language and other languages as well. The tsx language doesn't have a "private" version given that currently it doesn't need to be inherited by other languages.
| Language | Inherits from |
| ---------- | ----------------------- |
| javascript | _javascript, ecma |
| jsx | _jsx, _javascript, ecma |
| typescript | _typescript, ecma |
| tsx | _jsx, _typescript, ecma |
If you intend to add queries to any of the ecma-based languages above, make sure you add them to the correct private language they belong to, so that other languages down the line can benefit from them.

@ -46,8 +46,15 @@
(assignment_expression
left: (identifier) @function
right: [(function) (arrow_function)])
; Function and method parameters
;-------------------------------
; Arrow function parameters in the form `p => ...` are supported by both
; javascript and typescript grammars without conflicts.
(arrow_function
parameter: (identifier) @variable.parameter)
; Function and method calls
;--------------------------

@ -12,14 +12,28 @@
; Definitions
;------------
(pattern/identifier) @local.definition
; ...i
(rest_pattern
(identifier) @local.definition)
; { i }
(object_pattern
(shorthand_property_identifier_pattern) @local.definition)
(pattern/rest_pattern
; { a: i }
(object_pattern
(pair_pattern
value: (identifier) @local.definition))
; [ i ]
(array_pattern
(identifier) @local.definition)
; i => ...
(arrow_function
parameter: (identifier) @local.definition)
; const/let/var i = ...
(variable_declarator
name: (identifier) @local.definition)

@ -1,38 +1,3 @@
; Function and method parameters
;-------------------------------
; See runtime/queries/ecma/README.md for more info.
; (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))
; p => ...
(arrow_function
parameter: (identifier) @variable.parameter)
; inherits: ecma
; inherits: _javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _javascript,ecma

@ -1,88 +1,3 @@
(
(comment)* @doc
.
(method_definition
name: (property_identifier) @name) @definition.method
(#not-eq? @name "constructor")
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.method)
)
; See runtime/queries/ecma/README.md for more info.
(
(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
; inherits: _javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _javascript,ecma

@ -1,37 +1,3 @@
; Highlight component names differently
(jsx_opening_element ((identifier) @constructor
(#match? @constructor "^[A-Z]")))
; See runtime/queries/ecma/README.md for more info.
; Handle the dot operator effectively - <My.Component>
(jsx_opening_element ((nested_identifier (identifier) @tag (identifier) @constructor)))
; Highlight brackets differently
(jsx_opening_element ["<" ">"] @punctuation.bracket)
(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)))
; Highlight brackets differently
(jsx_closing_element ["<" "/" ">"] @punctuation.bracket)
(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)))
; Highlight brackets differently
(jsx_self_closing_element ["<" "/" ">"] @punctuation.bracket)
; Handle attribute delimiter
(jsx_attribute "=" @punctuation.delimiter)
(jsx_opening_element (identifier) @tag)
(jsx_closing_element (identifier) @tag)
(jsx_self_closing_element (identifier) @tag)
(jsx_attribute (property_identifier) @variable.other.member)
; inherits: ecma
; inherits: _jsx,_javascript,ecma

@ -1,9 +1,3 @@
[
(jsx_fragment)
(jsx_element)
(jsx_self_closing_element)
] @indent
; See runtime/queries/ecma/README.md for more info.
(parenthesized_expression) @indent
; inherits: ecma
; inherits: _jsx,_javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_javascript,ecma

@ -0,0 +1,3 @@
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_javascript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_javascript,ecma

@ -1 +1,3 @@
; inherits: jsx,typescript
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -1 +1,3 @@
; inherits: typescript,jsx
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -1 +1,3 @@
; inherits: typescript
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -0,0 +1,3 @@
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -0,0 +1,3 @@
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -1 +1,3 @@
; inherits: typescript,jsx
; See runtime/queries/ecma/README.md for more info.
; inherits: _jsx,_typescript,ecma

@ -1,82 +1,3 @@
; Namespaces
; See runtime/queries/ecma/README.md for more info.
(internal_module
[((identifier) @namespace) ((nested_identifier (identifier) @namespace))])
(ambient_declaration "global" @namespace)
; Variables
(required_parameter (identifier) @variable.parameter)
(optional_parameter (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
[
"<"
">"
] @punctuation.bracket)
(type_parameters
[
"<"
">"
] @punctuation.bracket)
((identifier) @type
(#match? @type "^[A-Z]"))
; Literals
[
(template_literal_type)
] @string
; Tokens
(template_type
"${" @punctuation.special
"}" @punctuation.special) @embedded
; inherits: ecma
; inherits: _typescript,ecma

@ -1,7 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
[
(enum_declaration)
(interface_declaration)
(object_type)
] @indent
; inherits: _typescript,ecma

@ -1 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
; inherits: _typescript,ecma

@ -1,2 +1,3 @@
(required_parameter (identifier) @local.definition)
(optional_parameter (identifier) @local.definition)
; See runtime/queries/ecma/README.md for more info.
; inherits: _typescript,ecma

@ -1,23 +1,3 @@
(function_signature
name: (identifier) @name) @definition.function
; See runtime/queries/ecma/README.md for more info.
(method_signature
name: (property_identifier) @name) @definition.method
(abstract_method_signature
name: (property_identifier) @name) @definition.method
(abstract_class_declaration
name: (type_identifier) @name) @definition.class
(module
name: (identifier) @name) @definition.module
(interface_declaration
name: (type_identifier) @name) @definition.interface
(type_annotation
(type_identifier) @name) @reference.type
(new_expression
constructor: (identifier) @name) @reference.class
; inherits: _typescript,ecma

@ -1,8 +1,3 @@
; inherits: ecma
; See runtime/queries/ecma/README.md for more info.
[
(interface_declaration
body:(_) @class.inside)
(type_alias_declaration
value: (_) @class.inside)
] @class.around
; inherits: _typescript,ecma

Loading…
Cancel
Save