Unnamed repository; edit this file 'description' to name the repository.
support cython (#14128)
Arthur 6 months ago
parent d4c91da · commit 7c37e8a
-rw-r--r--book/src/generated/lang-support.md1
-rw-r--r--languages.toml12
-rw-r--r--runtime/queries/cython/folds.scm28
-rw-r--r--runtime/queries/cython/highlights.scm199
-rw-r--r--runtime/queries/cython/indents.scm96
-rw-r--r--runtime/queries/cython/injections.scm18
-rw-r--r--runtime/queries/cython/locals.scm115
-rw-r--r--runtime/queries/cython/tags.scm14
8 files changed, 483 insertions, 0 deletions
diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md
index f2e2fdb8..d8284307 100644
--- a/book/src/generated/lang-support.md
+++ b/book/src/generated/lang-support.md
@@ -37,6 +37,7 @@
| csv | ✓ | | | | | |
| cue | ✓ | | | | | `cuelsp` |
| cylc | ✓ | ✓ | ✓ | | | |
+| cython | ✓ | | ✓ | ✓ | | |
| d | ✓ | ✓ | ✓ | | | `serve-d` |
| dart | ✓ | ✓ | ✓ | | | `dart` |
| dbml | ✓ | | | | | |
diff --git a/languages.toml b/languages.toml
index 44d6246e..6cf2799a 100644
--- a/languages.toml
+++ b/languages.toml
@@ -4710,3 +4710,15 @@ comment-token = "#"
file-types = [{ glob = "cliff.toml" }]
language-servers = ["taplo", "tombi"]
indent = { tab-width = 2, unit = " " }
+
+[[language]]
+name = "cython"
+scope = "source.cython"
+file-types = ["pxd", "pxi", "pyx"]
+comment-token = "#"
+roots = ["pyproject.toml", "setup.py", "poetry.lock"]
+indent = { tab-width = 4, unit = " " }
+
+[[grammar]]
+name = "cython"
+source = { git = "https://github.com/b0o/tree-sitter-cython", rev = "62f44f5e7e41dde03c5f0a05f035e293bcf2bcf8" }
diff --git a/runtime/queries/cython/folds.scm b/runtime/queries/cython/folds.scm
new file mode 100644
index 00000000..ecb9352d
--- /dev/null
+++ b/runtime/queries/cython/folds.scm
@@ -0,0 +1,28 @@
+[
+ (function_definition)
+ (class_definition)
+ (while_statement)
+ (for_statement)
+ (if_statement)
+ (with_statement)
+ (try_statement)
+ (match_statement)
+ (import_from_statement)
+ (parameters)
+ (argument_list)
+ (parenthesized_expression)
+ (generator_expression)
+ (list_comprehension)
+ (set_comprehension)
+ (dictionary_comprehension)
+ (tuple)
+ (list)
+ (set)
+ (dictionary)
+ (string)
+] @fold
+
+[
+ (import_statement)
+ (import_from_statement)
+]+ @fold
diff --git a/runtime/queries/cython/highlights.scm b/runtime/queries/cython/highlights.scm
new file mode 100644
index 00000000..3fdd7f1c
--- /dev/null
+++ b/runtime/queries/cython/highlights.scm
@@ -0,0 +1,199 @@
+; Punctuation
+
+["," "." ":" ";" (ellipsis)] @punctuation.delimiter
+["(" ")" "[" "]" "{" "}"] @punctuation.bracket
+(interpolation
+ "{" @punctuation.special
+ "}" @punctuation.special)
+
+; Identifier naming conventions
+
+(identifier) @variable
+
+((identifier) @constructor
+ (#match? @constructor "^[A-Z]"))
+
+((identifier) @constant
+ (#match? @constant "^[A-Z][A-Z_]*$"))
+
+; Function calls
+
+(decorator) @function
+
+(call
+ function: (attribute attribute: (identifier) @function.method))
+(call
+ function: (identifier) @function)
+
+; Builtin functions
+
+((call
+ function: (identifier) @function.builtin)
+ (#any-of?
+ @function.builtin
+ "abs" "all" "any" "ascii" "bin" "bool" "breakpoint" "bytearray" "bytes" "callable" "chr" "classmethod" "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate" "eval" "exec" "filter" "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance" "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview" "min" "next" "object" "oct" "open" "ord" "pow" "print" "property" "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip" "__import__"))
+
+; Types
+
+(maybe_typed_name
+ type: ((_) @type))
+
+(type
+ (identifier) @type)
+
+(c_type
+ type: ((_) @type))
+(c_type
+ ((identifier) @type))
+(c_type
+ ((int_type) @type))
+
+(maybe_typed_name
+ name: ((identifier) @variable))
+
+; Function definitions
+
+(function_definition
+ name: (identifier) @function)
+
+(cdef_statement
+ (cvar_def
+ (maybe_typed_name
+ name: ((identifier) @function))
+ (c_function_definition)))
+
+(cvar_decl
+ (c_type
+ ([(identifier) (int_type)]))
+ (c_name
+ ((identifier) @function))
+ (c_function_definition))
+
+(attribute attribute: (identifier) @variable.other.member)
+
+; Literals
+
+[
+ (none)
+] @constant.builtin
+
+[
+ (true)
+ (false)
+] @constant.builtin.boolean
+
+(integer) @constant.numeric.integer
+(float) @constant.numeric.float
+
+(comment) @comment
+(string) @string
+(escape_sequence) @constant.character.escape
+
+(interpolation
+ "{" @punctuation.special
+ "}" @punctuation.special) @embedded
+
+[
+ "-"
+ "-="
+ "!="
+ "*"
+ "**"
+ "**="
+ "*="
+ "/"
+ "//"
+ "//="
+ "/="
+ "&"
+ "&="
+ "%"
+ "%="
+ "^"
+ "^="
+ "+"
+ "->"
+ "+="
+ "<"
+ "<<"
+ "<<="
+ "<="
+ "<>"
+ "="
+ ":="
+ "=="
+ ">"
+ ">="
+ ">>"
+ ">>="
+ "|"
+ "|="
+ "~"
+ "@="
+ "and"
+ "in"
+ "is"
+ "not"
+ "or"
+ "@"
+] @operator
+
+[
+ "as"
+ "assert"
+ "async"
+ "await"
+ "break"
+ "class"
+ "continue"
+ "def"
+ "del"
+ "elif"
+ "else"
+ "except"
+ "exec"
+ "finally"
+ "for"
+ "from"
+ "global"
+ "if"
+ "import"
+ "lambda"
+ "nonlocal"
+ "pass"
+ "print"
+ "raise"
+ "return"
+ "try"
+ "while"
+ "with"
+ "yield"
+ "match"
+ "case"
+
+ ; cython-specific
+ "cdef"
+ "cpdef"
+ "ctypedef"
+ "cimport"
+ "nogil"
+ "gil"
+ "extern"
+ "inline"
+ "public"
+ "readonly"
+ "struct"
+ "union"
+ "enum"
+ "fused"
+ "property"
+ "namespace"
+ "cppclass"
+ "const"
+] @keyword.control
+
+(dotted_name
+ (identifier)* @namespace)
+
+(aliased_import
+ alias: (identifier) @namespace)
diff --git a/runtime/queries/cython/indents.scm b/runtime/queries/cython/indents.scm
new file mode 100644
index 00000000..8f3a569a
--- /dev/null
+++ b/runtime/queries/cython/indents.scm
@@ -0,0 +1,96 @@
+[
+ (list)
+ (tuple)
+ (dictionary)
+ (set)
+ (if_statement)
+ (for_statement)
+ (while_statement)
+ (with_statement)
+ (try_statement)
+ (match_statement)
+ (case_clause)
+ (import_from_statement)
+
+ (parenthesized_expression)
+ (generator_expression)
+ (list_comprehension)
+ (set_comprehension)
+ (dictionary_comprehension)
+
+ (tuple_pattern)
+ (list_pattern)
+ (argument_list)
+ (parameters)
+ (binary_operator)
+
+ (function_definition)
+ (cdef_statement)
+ (class_definition)
+] @indent
+
+; Workaround for the tree-sitter grammar creating large errors when a
+; try_statement is missing the except/finally clause
+
+(ERROR
+ "try"
+ .
+ ":" @indent @extend)
+(ERROR
+ .
+ "def") @indent @extend
+(ERROR
+ (block) @indent @extend
+ (#set! "scope" "all"))
+
+(ERROR
+ "try"
+ .
+ ":"
+ (ERROR
+ (block
+ (expression_statement
+ (identifier) @_except) @indent.branch))
+ (#eq? @_except "except"))
+
+[
+ (if_statement)
+ (for_statement)
+ (while_statement)
+ (with_statement)
+ (try_statement)
+ (match_statement)
+ (case_clause)
+
+ (cdef_statement)
+ (function_definition)
+ (class_definition)
+] @extend
+
+[
+ (return_statement)
+ (break_statement)
+ (continue_statement)
+ (raise_statement)
+ (pass_statement)
+] @extend.prevent-once
+
+[
+ ")"
+ "]"
+ "}"
+] @outdent
+(elif_clause
+ "elif" @outdent)
+(else_clause
+ "else" @outdent)
+
+(parameters
+ .
+ (identifier) @anchor
+ (#set! "scope" "tail")) @align
+
+(argument_list
+ .
+ (_) @anchor
+ (#set! "scope" "tail")) @align
diff --git a/runtime/queries/cython/injections.scm b/runtime/queries/cython/injections.scm
new file mode 100644
index 00000000..bbc924be
--- /dev/null
+++ b/runtime/queries/cython/injections.scm
@@ -0,0 +1,18 @@
+(call
+ function: (attribute
+ object: (identifier) @_re)
+ arguments: (argument_list
+ .
+ (string
+ (string_content) @injection.content))
+ (#eq? @_re "re")
+ (#set! injection.language "regex"))
+
+((binary_operator
+ left: (string
+ (string_content) @injection.content)
+ operator: "%")
+ (#set! injection.language "printf"))
+
+((comment) @injection.content
+ (#set! injection.language "comment"))
diff --git a/runtime/queries/cython/locals.scm b/runtime/queries/cython/locals.scm
new file mode 100644
index 00000000..9abed8f7
--- /dev/null
+++ b/runtime/queries/cython/locals.scm
@@ -0,0 +1,115 @@
+; Program structure
+(module) @local.scope
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (identifier) @local.definition)))) @local.scope
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (_
+ (identifier) @local.definition))))) @local.scope
+
+; Imports
+(aliased_import
+ alias: (identifier) @local.definition.namespace)
+
+(import_statement
+ name: (dotted_name
+ (identifier) @local.definition.namespace))
+
+(import_from_statement
+ name: (dotted_name
+ (identifier) @local.definition.namespace))
+
+; Function with parameters, defines parameters
+(parameters
+ (identifier) @local.definition.variable.parameter)
+
+(default_parameter
+ (identifier) @local.definition.variable.parameter)
+
+(typed_parameter
+ (identifier) @local.definition.variable.parameter)
+
+(typed_default_parameter
+ (identifier) @local.definition.variable.parameter)
+
+; *args parameter
+(parameters
+ (list_splat_pattern
+ (identifier) @local.definition))
+
+; **kwargs parameter
+(parameters
+ (dictionary_splat_pattern
+ (identifier) @local.definition.variable.parameter))
+
+(class_definition
+ body: (block
+ (function_definition
+ name: (identifier) @local.definition.function)))
+
+; Loops
+; not a scope!
+(for_in_loop
+ left: (pattern_list
+ (identifier) @local.definition.variable))
+
+(for_in_loop
+ left: (tuple_pattern
+ (identifier) @local.definition.variable))
+
+(for_in_loop
+ left: (identifier) @local.definition.variable)
+
+; not a scope!
+;(while_statement) @local.scope
+; for in list comprehension
+(for_in_clause
+ left: (identifier) @local.definition.variable)
+
+(for_in_clause
+ left: (tuple_pattern
+ (identifier) @local.definition.variable))
+
+(for_in_clause
+ left: (pattern_list
+ (identifier) @local.definition.variable))
+
+(dictionary_comprehension) @local.scope
+
+(list_comprehension) @local.scope
+
+(set_comprehension) @local.scope
+
+; Assignments
+(assignment
+ left: (identifier) @local.definition.variable)
+
+(assignment
+ left: (pattern_list
+ (identifier) @local.definition.variable))
+
+(assignment
+ left: (tuple_pattern
+ (identifier) @local.definition.variable))
+
+(assignment
+ left: (attribute
+ (identifier)
+ (identifier) @local.definition.variable))
+
+; Walrus operator x := 1
+(named_expression
+ (identifier) @local.definition.variable)
+
+(as_pattern
+ alias: (as_pattern_target) @local.definition.variable)
+
+; REFERENCES
+(identifier) @local.reference
diff --git a/runtime/queries/cython/tags.scm b/runtime/queries/cython/tags.scm
new file mode 100644
index 00000000..4fe36552
--- /dev/null
+++ b/runtime/queries/cython/tags.scm
@@ -0,0 +1,14 @@
+(module (expression_statement (assignment left: (identifier) @name) @definition.constant))
+
+(class_definition
+ name: (identifier) @name) @definition.class
+
+(function_definition
+ name: (identifier) @name) @definition.function
+
+(call
+ function: [
+ (identifier) @name
+ (attribute
+ attribute: (identifier) @name)
+ ]) @reference.call