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

This commit is contained in:
JJ 2024-02-28 17:49:10 -08:00 committed by GitHub
parent 1143f47954
commit e51a1e4e2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 329 additions and 286 deletions

View File

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

View File

@ -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_literal) @constant.builtin
[(nil_lit)] @constant.builtin ((identifier) @constant.builtin.boolean
[(bool_lit)] @constant.builtin.boolean (#any-of? @constant.builtin.boolean "true" "false" "on" "off"))
[(char_lit)] @constant.character
[(char_esc_seq) (str_esc_seq)] @constant.character.escape (char_literal) @constant.character
[(custom_numeric_lit)] @constant.numeric (escape_sequence) @constant.character.escape
[(int_lit) (int_suffix)] @constant.numeric.integer (custom_numeric_literal) @constant.numeric
[(float_lit) (float_suffix)] @constant.numeric.float (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 [(operator) "="] @operator
; all operators (must come after @keyword.operator) (infix_expression operator: _ @operator)
(prefix_expression operator: _ @operator)
(pragma) @attribute
; pragmas
(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)))))))?)
(fromStmt
(keyw) @keyword.control.import
(expr (primary (symbol) @namespace))?
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
(includeStmt
(keyw) @keyword.control.import
(expr (primary (symbol) @namespace))?
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
(importExceptStmt
(keyw) @keyword.control.import
(expr (primary (symbol) @namespace))?
(expr (primary (arrayConstr (exprColonExprList (exprColonExpr (expr (primary (symbol) @namespace)))))))?)
; import statements
; yeah, this is a bit gross.
(import_statement
[
(identifier) @namespace
(expression_list (identifier) @namespace)
(except_clause
"except" @keyword.control.import
(expression_list (identifier) @namespace))])
(import_from_statement
(identifier) @namespace
(expression_list (identifier) @namespace))
(include_statement (expression_list (identifier) @namespace))
(export_statement (expression_list (identifier) @namespace))
;; 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) "for"
(discardStmt (keyw) @keyword.control.repeat) "while"
(breakStmt (keyw) @keyword.control.repeat) "continue"
(continueStmt (keyw) @keyword.control.repeat) "break"
; control flow statements ] @keyword.control.repeat
(for "in" @keyword.control.repeat)
(raiseStmt (keyw) @keyword.control.exception) [
(tryStmt (keyw) @keyword.control.exception) "return"
(tryExceptStmt (keyw) @keyword.control.exception) "yield"
(tryFinallyStmt (keyw) @keyword.control.exception) ] @keyword.control.return
(inlineTryStmt (keyw) @keyword.control.exception) ; return statements
; (inlineTryExceptStmt (keyw) @keyword.control.exception)
; (inlineTryFinallyStmt (keyw) @keyword.control.exception) [
"try"
"except"
"finally"
"raise"
] @keyword.control.exception
; exception handling statements ; exception handling statements
(staticStmt (keyw) @keyword) [
(deferStmt (keyw) @keyword) "asm"
(asmStmt (keyw) @keyword) "bind"
(bindStmt (keyw) @keyword) "mixin"
(mixinStmt (keyw) @keyword) "defer"
; miscellaneous blocks "static"
] @keyword
(blockStmt ; miscellaneous keywords
(keyw) @keyword.control
(symbol) @label)
; block statements
;; Types and Type Declarations ;; Types and Type Declarations
(typeDef [
(keyw) @keyword.storage.type "let"
(symbol) @type) "var"
; names of new types type declarations "const"
"type"
"object"
"tuple"
"enum"
"concept"
] @keyword.storage.type
(exprColonEqExpr (var_type "var" @keyword.storage.modifier)
. (expr (primary (symbol) @variable)) (out_type "out" @keyword.storage.modifier)
. (expr (primary (symbol) @type))) (distinct_type "distinct" @keyword.storage.modifier)
; variables in inline tuple declarations (ref_type "ref" @keyword.storage.modifier)
(pointer_type "ptr" @keyword.storage.modifier)
(primarySuffix (var_parameter "var" @keyword.storage.modifier)
(indexSuffix (type_parameter "type" @keyword.storage.modifier)
(exprColonEqExprList (static_parameter "static" @keyword.storage.modifier)
(exprColonEqExpr (ref_parameter "ref" @keyword.storage.modifier)
(expr (pointer_parameter "ptr" @keyword.storage.modifier)
(primary ; (var_parameter (identifier) @variable.parameter)
(symbol) @type)))))) ; (type_parameter (identifier) @variable.parameter)
; nested types in brackets, i.e. seq[string] ; (static_parameter (identifier) @variable.parameter)
; (ref_parameter (identifier) @variable.parameter)
; (pointer_parameter (identifier) @variable.parameter)
; todo: when are these used??
(primaryTypeDef (symbol) @type) (type_section
; primary types of type declarations (NOT nested types) (type_declaration
(type_symbol_declaration
name: (_) @type)))
; types in type declarations
(primaryTypeDef (primaryPrefix (keyw) @type)) (enum_field_declaration
; for consistency (symbol_declaration
name: (_) @type.enum.variant))
; types as enum variants
(primaryTypeDesc (symbol) @type) (variant_declaration
; type annotations, on declarations or in objects alternative: (of_branch
values: (expression_list (_) @type.enum.variant)))
; types as object variants
(primaryTypeDesc (primaryPrefix (keyw) @type)) (case
; var types etc (of_branch
values: (expression_list (_) @constant)))
; case values are guaranteed to be constant
(genericParamList (genericParam (symbol) @type)) (type_expression
; types in generic blocks [
(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
(enumDecl (keyw) @keyword.storage.type) (call
(enumElement (symbol) @type.enum.variant) function: (bracket_expression
; enum declarations and elements right: (argument_list (identifier) @type)))
; types as generic parameters
(tupleDecl (keyw) @keyword.storage.type) ; (dot_generic_call
; tuple declarations ; generic_arguments: (_) @type)
; ???
(objectDecl (keyw) @keyword.storage.type) (infix_expression
(objectPart (symbol) @variable.other.member) operator:
; object declarations and fields [
"is"
(objectCase "isnot"
(keyw) @keyword.control.conditional ]
(symbol) @variable.other.member) right: (_) @type)
(objectBranch (keyw) @keyword.control.conditional) ; types in "is" comparisions
(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?
(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"
"iterator"
"template"
"macro"
] @keyword.function
(routineExpr (keyw) @keyword.function) (exported_symbol "*" @attribute)
; discarded function (_ "=" @punctuation.delimiter [body: (_) value: (_)])
(routineExprTypeDesc (keyw) @keyword.function) (proc_declaration name: (_) @function)
; function declarations as types (func_declaration name: (_) @function)
(iterator_declaration name: (_) @function)
(primary (converter_declaration name: (_) @function)
. (symbol) @function.call (method_declaration name: (_) @function.method)
. (primarySuffix (functionCall))) (template_declaration name: (_) @function.macro)
; regular function calls (macro_declaration name: (_) @function.macro)
(symbol_declaration name: (_) @variable)
(primary
. (symbol) @function.call
. (primarySuffix (cmdCall)))
; function calls without parenthesis
(primary
(primarySuffix (qualifiedSuffix (symbol) @function.call))
. (primarySuffix (functionCall)))
; uniform function call syntax calls
(primary
(primarySuffix (qualifiedSuffix (symbol) @function.call))
. (primarySuffix (cmdCall)))
; just in case
(primary
(symbol) @constructor
(primarySuffix (objectConstr)))
; object constructor
; does not appear to be a way to distinguish these without verbatium matching
; [] @function.builtin
; [] @function.method
; [] @function.macro
; [] @function.special
(call
function: [
(identifier) @function.call
(dot_expression
right: (identifier) @function.call)
(bracket_expression
left: [
(identifier) @function.call
(dot_expression
right: (identifier) @function.call)])])
(generalized_string
function: [
(identifier) @function.call
(dot_expression
right: (identifier) @function.call)
(bracket_expression
left: [
(identifier) @function.call
(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
name: (_) @variable.parameter)))
(argument_list
(equal_expression
left: (_) @variable.parameter))
(concept_declaration
parameters: (parameter_list (identifier) @variable.parameter))
(identColon (ident) @variable.other.member) (field_declaration
; named parts of tuples (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))))
(symbolColonExpr (symbol) @variable) ;; Miscellaneous Matches
; object constructor parameters
(symbolEqExpr (symbol) @variable) [
; named parameters "cast"
"discard"
"do"
] @keyword
; also: addr end interface using
(variable (blank_identifier) @variable.builtin
(keyw) @keyword.storage.type ((identifier) @variable.builtin
(declColonEquals (symbol) @variable)) (#eq? @variable.builtin "result"))
; let, var, const expressions
((primary (symbol) @variable.builtin) (dot_expression
(#match? @variable.builtin "result")) left: (identifier) @variable
; `result` is an implicit builtin variable inside function scopes right: (identifier) @variable.other.member)
((primary (symbol) @type) (identifier) @variable
(#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)
; overzealous, matches variables
(primary (primarySuffix (qualifiedSuffix (symbol) @variable.other.member)))
; overzealous, matches member variables: i.e. x in foo.x
(keyw) @keyword
; more specific matches are done above whenever possible

View File

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

View File

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