Switch Nim tree-sitter queries to alaviss/tree-sitter-nim (#9722)

pull/9767/head
JJ 9 months ago committed by GitHub
parent 1143f47954
commit e51a1e4e2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -50,6 +50,7 @@ metals = { command = "metals", config = { "isHttpEnabled" = true } }
mint = { command = "mint", args = ["ls"] } mint = { command = "mint", args = ["ls"] }
nil = { command = "nil" } nil = { command = "nil" }
nimlangserver = { command = "nimlangserver" } nimlangserver = { command = "nimlangserver" }
nimlsp = { command = "nimlsp" }
nls = { command = "nls" } nls = { command = "nls" }
nu-lsp = { command = "nu", args = [ "--lsp" ] } nu-lsp = { command = "nu", args = [ "--lsp" ] }
ocamllsp = { command = "ocamllsp" } ocamllsp = { command = "ocamllsp" }
@ -2858,10 +2859,9 @@ language-servers = [ "nimlangserver" ]
"'" = "'" "'" = "'"
'{' = '}' '{' = '}'
# Nim's tree-sitter grammar is in heavy development.
[[grammar]] [[grammar]]
name = "nim" name = "nim"
source = { git = "https://github.com/aMOPel/tree-sitter-nim", rev = "240239b232550e431d67de250d1b5856209e7f06" } source = { git = "https://github.com/alaviss/tree-sitter-nim", rev = "c5f0ce3b65222f5dbb1a12f9fe894524881ad590" }
[[language]] [[language]]
name = "cabal" name = "cabal"

@ -1,33 +1,32 @@
;; Constants, Comments, and Literals ;; Constants, Comments, and Literals
(comment) @comment.line (comment) @comment.line
(multilineComment) @comment.block (block_comment) @comment.block
(docComment) @comment.block.documentation [
(multilineDocComment) @comment.block.documentation (documentation_comment)
; comments (block_documentation_comment)
] @comment.block.documentation
[(literal) (generalizedLit)] @constant
[(nil_lit)] @constant.builtin (nil_literal) @constant.builtin
[(bool_lit)] @constant.builtin.boolean ((identifier) @constant.builtin.boolean
[(char_lit)] @constant.character (#any-of? @constant.builtin.boolean "true" "false" "on" "off"))
[(char_esc_seq) (str_esc_seq)] @constant.character.escape
[(custom_numeric_lit)] @constant.numeric (char_literal) @constant.character
[(int_lit) (int_suffix)] @constant.numeric.integer (escape_sequence) @constant.character.escape
[(float_lit) (float_suffix)] @constant.numeric.float (custom_numeric_literal) @constant.numeric
(integer_literal) @constant.numeric.integer
(float_literal) @constant.numeric.float
; literals ; literals
; note: somewhat irritatingly for testing, lits have the same syntax highlighting as types ; todo: literal?
[ [
(str_lit) (long_string_literal)
(triplestr_lit) (raw_string_literal)
(rstr_lit) (generalized_string)
(generalized_str_lit) (interpreted_string_literal)
(generalized_triplestr_lit)
(interpolated_str_lit)
(interpolated_triplestr_lit)
] @string ] @string
; (generalized_string (string_content) @none) ; todo: attempt to un-match string_content
; [] @string.regexp ; [] @string.regexp
; string literals
[ [
"." "."
@ -44,272 +43,291 @@
"}" "}"
"{." "{."
".}" ".}"
"#["
"]#"
] @punctuation.bracket ] @punctuation.bracket
(interpolated_str_lit "&" @punctuation.special) ; todo: interpolated_str_lit?? & { }?
(interpolated_str_lit "{" @punctuation.special)
(interpolated_str_lit "}" @punctuation.special)
; punctuation
[ [
"and" "and"
"or" "or"
"xor" "xor"
"not" "not"
"in"
"notin"
"is"
"isnot"
"div" "div"
"mod" "mod"
"shl" "shl"
"shr" "shr"
"from"
"as"
"of"
"in"
"notin"
"is"
"isnot"
] @keyword.operator ] @keyword.operator
; operators: we list them explicitly to deliminate them from symbolic operators
[(operator) (opr) "="] @operator
; all operators (must come after @keyword.operator)
(pragma) @attribute [(operator) "="] @operator
; pragmas (infix_expression operator: _ @operator)
(prefix_expression operator: _ @operator)
(pragma_list
(identifier)? @attribute
(colon_expression
(identifier) @attribute)?)
;; Imports and Exports ;; Imports and Exports
(importStmt [
(keyw) @keyword.control.import "import"
(expr (primary (symbol) @namespace))? "export"
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?) "include"
(exportStmt "from"
(keyw) @keyword.control.import ] @keyword.control.import
(expr (primary (symbol) @namespace))?
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?) (import_statement
(fromStmt [
(keyw) @keyword.control.import (identifier) @namespace
(expr (primary (symbol) @namespace))? (expression_list (identifier) @namespace)
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?) (except_clause
(includeStmt "except" @keyword.control.import
(keyw) @keyword.control.import (expression_list (identifier) @namespace))])
(expr (primary (symbol) @namespace))? (import_from_statement
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?) (identifier) @namespace
(importExceptStmt (expression_list (identifier) @namespace))
(keyw) @keyword.control.import (include_statement (expression_list (identifier) @namespace))
(expr (primary (symbol) @namespace))? (export_statement (expression_list (identifier) @namespace))
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
; import statements
; yeah, this is a bit gross.
;; Control Flow ;; Control Flow
(ifStmt (keyw) @keyword.control.conditional) [
(whenStmt (keyw) @keyword.control.conditional) "if"
(elifStmt (keyw) @keyword.control.conditional) "when"
(elseStmt (keyw) @keyword.control.conditional) "case"
(caseStmt (keyw) @keyword.control.conditional) "elif"
(ofBranch (keyw) @keyword.control.conditional) "else"
(inlineIfStmt (keyw) @keyword.control.conditional) ] @keyword.control.conditional
(inlineWhenStmt (keyw) @keyword.control.conditional) (of_branch "of" @keyword.control.conditional)
; conditional statements ; conditional statements
; todo: do block ; todo: do block
(forStmt "block" @keyword.control
. (keyw) @keyword.control.repeat (block label: (_) @label)
. (symbol) @variable
. (keyw) @keyword.control.repeat)
(whileStmt (keyw) @keyword.control.repeat)
; loop statements
(returnStmt (keyw) @keyword.control.repeat)
(yieldStmt (keyw) @keyword.control.repeat)
(discardStmt (keyw) @keyword.control.repeat)
(breakStmt (keyw) @keyword.control.repeat)
(continueStmt (keyw) @keyword.control.repeat)
; control flow statements
(raiseStmt (keyw) @keyword.control.exception)
(tryStmt (keyw) @keyword.control.exception)
(tryExceptStmt (keyw) @keyword.control.exception)
(tryFinallyStmt (keyw) @keyword.control.exception)
(inlineTryStmt (keyw) @keyword.control.exception)
; (inlineTryExceptStmt (keyw) @keyword.control.exception)
; (inlineTryFinallyStmt (keyw) @keyword.control.exception)
; exception handling statements
(staticStmt (keyw) @keyword) [
(deferStmt (keyw) @keyword) "for"
(asmStmt (keyw) @keyword) "while"
(bindStmt (keyw) @keyword) "continue"
(mixinStmt (keyw) @keyword) "break"
; miscellaneous blocks ] @keyword.control.repeat
(for "in" @keyword.control.repeat)
(blockStmt [
(keyw) @keyword.control "return"
(symbol) @label) "yield"
; block statements ] @keyword.control.return
; return statements
[
"try"
"except"
"finally"
"raise"
] @keyword.control.exception
; exception handling statements
;; Types and Type Declarations [
"asm"
"bind"
"mixin"
"defer"
"static"
] @keyword
; miscellaneous keywords
(typeDef ;; Types and Type Declarations
(keyw) @keyword.storage.type
(symbol) @type)
; names of new types type declarations
(exprColonEqExpr
. (expr (primary (symbol) @variable))
. (expr (primary (symbol) @type)))
; variables in inline tuple declarations
(primarySuffix
(indexSuffix
(exprColonEqExprList
(exprColonEqExpr
(expr
(primary
(symbol) @type))))))
; nested types in brackets, i.e. seq[string]
(primaryTypeDef (symbol) @type)
; primary types of type declarations (NOT nested types)
(primaryTypeDef (primaryPrefix (keyw) @type))
; for consistency
(primaryTypeDesc (symbol) @type)
; type annotations, on declarations or in objects
(primaryTypeDesc (primaryPrefix (keyw) @type))
; var types etc
(genericParamList (genericParam (symbol) @type))
; types in generic blocks
(enumDecl (keyw) @keyword.storage.type)
(enumElement (symbol) @type.enum.variant)
; enum declarations and elements
(tupleDecl (keyw) @keyword.storage.type)
; tuple declarations
(objectDecl (keyw) @keyword.storage.type)
(objectPart (symbol) @variable.other.member)
; object declarations and fields
(objectCase
(keyw) @keyword.control.conditional
(symbol) @variable.other.member)
(objectBranch (keyw) @keyword.control.conditional)
(objectElif (keyw) @keyword.control.conditional)
(objectElse (keyw) @keyword.control.conditional)
(objectWhen (keyw) @keyword.control.conditional)
; variant objects
(conceptDecl (keyw) @keyword.storage.type)
(conceptParam (keyw) @type)
(conceptParam (symbol) @variable)
; concept declarations, parameters, and qualifiers on those parameters
((expr
(primary (symbol))
(operator) @operator
(primary (symbol) @type))
(#match? @operator "is"))
((exprStmt
(primary (symbol))
(operator) @operator
(primary (symbol) @type))
(#match? @operator "is"))
; symbols likely to be types: "x is t" means t is either a type or a type variable
; distinct?
[
"let"
"var"
"const"
"type"
"object"
"tuple"
"enum"
"concept"
] @keyword.storage.type
(var_type "var" @keyword.storage.modifier)
(out_type "out" @keyword.storage.modifier)
(distinct_type "distinct" @keyword.storage.modifier)
(ref_type "ref" @keyword.storage.modifier)
(pointer_type "ptr" @keyword.storage.modifier)
(var_parameter "var" @keyword.storage.modifier)
(type_parameter "type" @keyword.storage.modifier)
(static_parameter "static" @keyword.storage.modifier)
(ref_parameter "ref" @keyword.storage.modifier)
(pointer_parameter "ptr" @keyword.storage.modifier)
; (var_parameter (identifier) @variable.parameter)
; (type_parameter (identifier) @variable.parameter)
; (static_parameter (identifier) @variable.parameter)
; (ref_parameter (identifier) @variable.parameter)
; (pointer_parameter (identifier) @variable.parameter)
; todo: when are these used??
(type_section
(type_declaration
(type_symbol_declaration
name: (_) @type)))
; types in type declarations
(enum_field_declaration
(symbol_declaration
name: (_) @type.enum.variant))
; types as enum variants
(variant_declaration
alternative: (of_branch
values: (expression_list (_) @type.enum.variant)))
; types as object variants
(case
(of_branch
values: (expression_list (_) @constant)))
; case values are guaranteed to be constant
(type_expression
[
(identifier) @type
(bracket_expression
[
(identifier) @type
(argument_list (identifier) @type)])
(tuple_construction
[
(identifier) @type
(bracket_expression
[
(identifier) @type
(argument_list (identifier) @type)])])])
; types in type expressions
(call
function: (bracket_expression
right: (argument_list (identifier) @type)))
; types as generic parameters
; (dot_generic_call
; generic_arguments: (_) @type)
; ???
(infix_expression
operator:
[
"is"
"isnot"
]
right: (_) @type)
; types in "is" comparisions
(except_branch
values: (expression_list
[
(identifier) @type
(infix_expression
left: (identifier) @type
operator: "as"
right: (_) @variable)]))
; types in exception branches
;; Functions ;; Functions
(routine [
. (keyw) @keyword.function "proc"
. (symbol) @function) "func"
; function declarations "method"
"converter"
(routineExpr (keyw) @keyword.function) "iterator"
; discarded function "template"
"macro"
(routineExprTypeDesc (keyw) @keyword.function) ] @keyword.function
; function declarations as types
(exported_symbol "*" @attribute)
(primary (_ "=" @punctuation.delimiter [body: (_) value: (_)])
. (symbol) @function.call
. (primarySuffix (functionCall))) (proc_declaration name: (_) @function)
; regular function calls (func_declaration name: (_) @function)
(iterator_declaration name: (_) @function)
(primary (converter_declaration name: (_) @function)
. (symbol) @function.call (method_declaration name: (_) @function.method)
. (primarySuffix (cmdCall))) (template_declaration name: (_) @function.macro)
; function calls without parenthesis (macro_declaration name: (_) @function.macro)
(symbol_declaration name: (_) @variable)
(primary
(primarySuffix (qualifiedSuffix (symbol) @function.call)) (call
. (primarySuffix (functionCall))) function: [
; uniform function call syntax calls (identifier) @function.call
(dot_expression
(primary right: (identifier) @function.call)
(primarySuffix (qualifiedSuffix (symbol) @function.call)) (bracket_expression
. (primarySuffix (cmdCall))) left: [
; just in case (identifier) @function.call
(dot_expression
(primary right: (identifier) @function.call)])])
(symbol) @constructor (generalized_string
(primarySuffix (objectConstr))) function: [
; object constructor (identifier) @function.call
(dot_expression
; does not appear to be a way to distinguish these without verbatium matching right: (identifier) @function.call)
; [] @function.builtin (bracket_expression
; [] @function.method left: [
; [] @function.macro (identifier) @function.call
; [] @function.special (dot_expression
right: (identifier) @function.call)])])
(dot_generic_call function: (_) @function.call)
;; Variables ;; Variables
(paramList (paramColonEquals (symbol) @variable.parameter)) (parameter_declaration
; parameter identifiers (symbol_declaration_list
(symbol_declaration
(identColon (ident) @variable.other.member) name: (_) @variable.parameter)))
; named parts of tuples (argument_list
(equal_expression
(symbolColonExpr (symbol) @variable) left: (_) @variable.parameter))
; object constructor parameters (concept_declaration
parameters: (parameter_list (identifier) @variable.parameter))
(symbolEqExpr (symbol) @variable)
; named parameters (field_declaration
(symbol_declaration_list
(symbol_declaration
name: (_) @variable.other.member)))
(call
(argument_list
(colon_expression
left: (_) @variable.other.member)))
(tuple_construction
(colon_expression
left: (_) @variable.other.member))
(variant_declaration
(variant_discriminator_declaration
(symbol_declaration_list
(symbol_declaration
name: (_) @variable.other.member))))
;; Miscellaneous Matches
(variable [
(keyw) @keyword.storage.type "cast"
(declColonEquals (symbol) @variable)) "discard"
; let, var, const expressions "do"
] @keyword
((primary (symbol) @variable.builtin) ; also: addr end interface using
(#match? @variable.builtin "result"))
; `result` is an implicit builtin variable inside function scopes
((primary (symbol) @type)
(#match? @type "^[A-Z]"))
; assume PascalCase identifiers to be types
((primary
(primarySuffix
(qualifiedSuffix
(symbol) @type)))
(#match? @type "^[A-Z]"))
; assume PascalCase member variables to be enum entries
(primary (symbol) @variable) (blank_identifier) @variable.builtin
; overzealous, matches variables ((identifier) @variable.builtin
(#eq? @variable.builtin "result"))
(primary (primarySuffix (qualifiedSuffix (symbol) @variable.other.member))) (dot_expression
; overzealous, matches member variables: i.e. x in foo.x left: (identifier) @variable
right: (identifier) @variable.other.member)
(keyw) @keyword (identifier) @variable
; more specific matches are done above whenever possible

@ -1,48 +1,59 @@
[ [
(typeDef) (if)
(ifStmt) (when)
(whenStmt) (elif_branch)
(elifStmt) (else_branch)
(elseStmt) (of_branch) ; note: not case_statement
(ofBranch) ; note: not caseStmt (block)
(whileStmt) (while)
(tryStmt) (for)
(tryExceptStmt) (try)
(tryFinallyStmt) (except_branch)
(forStmt) (finally_branch)
(blockStmt) (defer)
(staticStmt) (static_statement)
(deferStmt) (proc_declaration)
(asmStmt) (func_declaration)
; exprStmt? (iterator_declaration)
(converter_declaration)
(method_declaration)
(template_declaration)
(macro_declaration)
(symbol_declaration)
] @indent ] @indent
;; increase the indentation level ;; increase the indentation level
[ [
(ifStmt) (if)
(whenStmt) (when)
(elifStmt) (elif_branch)
(elseStmt) (else_branch)
(ofBranch) ; note: not caseStmt (of_branch) ; note: not case_statement
(whileStmt) (block)
(tryStmt) (while)
(tryExceptStmt) (for)
(tryFinallyStmt) (try)
(forStmt) (except_branch)
(blockStmt) (finally_branch)
(staticStmt) (defer)
(deferStmt) (static_statement)
(asmStmt) (proc_declaration)
; exprStmt? (func_declaration)
(iterator_declaration)
(converter_declaration)
(method_declaration)
(template_declaration)
(macro_declaration)
(symbol_declaration)
] @extend ] @extend
;; ??? ;; ???
[ [
(returnStmt) (return_statement)
(raiseStmt) (raise_statement)
(yieldStmt) (yield_statement)
(breakStmt) (break_statement)
(continueStmt) (continue_statement)
] @extend.prevent-once ] @extend.prevent-once
;; end a level of indentation while staying indented ;; end a level of indentation while staying indented

@ -1,19 +1,33 @@
(routine (proc_declaration
(block) @function.inside) @function.around body: (_) @function.inside) @function.around
(func_declaration
body: (_) @function.inside) @function.around
(iterator_declaration
body: (_) @function.inside) @function.around
(converter_declaration
body: (_) @function.inside) @function.around
(method_declaration
body: (_) @function.inside) @function.around
(template_declaration
body: (_) @function.inside) @function.around
(macro_declaration
body: (_) @function.inside) @function.around
; @class.inside (types?) (type_declaration (_) @class.inside) @class.around
; @class.around
; paramListSuffix is strange and i do not understand it (parameter_declaration
(paramList (symbol_declaration_list) @parameter.inside) @parameter.around
(paramColonEquals) @parameter.inside) @parameter.around
(comment) @comment.inside [
(multilineComment) @comment.inside (comment)
(docComment) @comment.inside (block_comment)
(multilineDocComment) @comment.inside (documentation_comment)
(block_documentation_comment)
] @comment.inside
(comment)+ @comment.around [
(multilineComment) @comment.around (comment)+
(docComment)+ @comment.around (block_comment)
(multilineDocComment) @comment.around (documentation_comment)+
(block_documentation_comment)+
] @comment.around

Loading…
Cancel
Save