Skip to content

Commit 7c37e8a

Browse files
authored
support cython (#14128)
1 parent d4c91da commit 7c37e8a

File tree

8 files changed

+483
-0
lines changed

8 files changed

+483
-0
lines changed

book/src/generated/lang-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| csv || | | | | |
3838
| cue || | | | | `cuelsp` |
3939
| cylc |||| | | |
40+
| cython || ||| | |
4041
| d |||| | | `serve-d` |
4142
| dart |||| | | `dart` |
4243
| dbml || | | | | |

languages.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4710,3 +4710,15 @@ comment-token = "#"
47104710
file-types = [{ glob = "cliff.toml" }]
47114711
language-servers = ["taplo", "tombi"]
47124712
indent = { tab-width = 2, unit = " " }
4713+
4714+
[[language]]
4715+
name = "cython"
4716+
scope = "source.cython"
4717+
file-types = ["pxd", "pxi", "pyx"]
4718+
comment-token = "#"
4719+
roots = ["pyproject.toml", "setup.py", "poetry.lock"]
4720+
indent = { tab-width = 4, unit = " " }
4721+
4722+
[[grammar]]
4723+
name = "cython"
4724+
source = { git = "https://github.com/b0o/tree-sitter-cython", rev = "62f44f5e7e41dde03c5f0a05f035e293bcf2bcf8" }

runtime/queries/cython/folds.scm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
(function_definition)
3+
(class_definition)
4+
(while_statement)
5+
(for_statement)
6+
(if_statement)
7+
(with_statement)
8+
(try_statement)
9+
(match_statement)
10+
(import_from_statement)
11+
(parameters)
12+
(argument_list)
13+
(parenthesized_expression)
14+
(generator_expression)
15+
(list_comprehension)
16+
(set_comprehension)
17+
(dictionary_comprehension)
18+
(tuple)
19+
(list)
20+
(set)
21+
(dictionary)
22+
(string)
23+
] @fold
24+
25+
[
26+
(import_statement)
27+
(import_from_statement)
28+
]+ @fold
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
; Punctuation
2+
3+
["," "." ":" ";" (ellipsis)] @punctuation.delimiter
4+
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
5+
(interpolation
6+
"{" @punctuation.special
7+
"}" @punctuation.special)
8+
9+
; Identifier naming conventions
10+
11+
(identifier) @variable
12+
13+
((identifier) @constructor
14+
(#match? @constructor "^[A-Z]"))
15+
16+
((identifier) @constant
17+
(#match? @constant "^[A-Z][A-Z_]*$"))
18+
19+
; Function calls
20+
21+
(decorator) @function
22+
23+
(call
24+
function: (attribute attribute: (identifier) @function.method))
25+
(call
26+
function: (identifier) @function)
27+
28+
; Builtin functions
29+
30+
((call
31+
function: (identifier) @function.builtin)
32+
(#any-of?
33+
@function.builtin
34+
"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__"))
35+
36+
; Types
37+
38+
(maybe_typed_name
39+
type: ((_) @type))
40+
41+
(type
42+
(identifier) @type)
43+
44+
(c_type
45+
type: ((_) @type))
46+
(c_type
47+
((identifier) @type))
48+
(c_type
49+
((int_type) @type))
50+
51+
(maybe_typed_name
52+
name: ((identifier) @variable))
53+
54+
; Function definitions
55+
56+
(function_definition
57+
name: (identifier) @function)
58+
59+
(cdef_statement
60+
(cvar_def
61+
(maybe_typed_name
62+
name: ((identifier) @function))
63+
(c_function_definition)))
64+
65+
(cvar_decl
66+
(c_type
67+
([(identifier) (int_type)]))
68+
(c_name
69+
((identifier) @function))
70+
(c_function_definition))
71+
72+
(attribute attribute: (identifier) @variable.other.member)
73+
74+
; Literals
75+
76+
[
77+
(none)
78+
] @constant.builtin
79+
80+
[
81+
(true)
82+
(false)
83+
] @constant.builtin.boolean
84+
85+
(integer) @constant.numeric.integer
86+
(float) @constant.numeric.float
87+
88+
(comment) @comment
89+
(string) @string
90+
(escape_sequence) @constant.character.escape
91+
92+
(interpolation
93+
"{" @punctuation.special
94+
"}" @punctuation.special) @embedded
95+
96+
[
97+
"-"
98+
"-="
99+
"!="
100+
"*"
101+
"**"
102+
"**="
103+
"*="
104+
"/"
105+
"//"
106+
"//="
107+
"/="
108+
"&"
109+
"&="
110+
"%"
111+
"%="
112+
"^"
113+
"^="
114+
"+"
115+
"->"
116+
"+="
117+
"<"
118+
"<<"
119+
"<<="
120+
"<="
121+
"<>"
122+
"="
123+
":="
124+
"=="
125+
">"
126+
">="
127+
">>"
128+
">>="
129+
"|"
130+
"|="
131+
"~"
132+
"@="
133+
"and"
134+
"in"
135+
"is"
136+
"not"
137+
"or"
138+
"@"
139+
] @operator
140+
141+
[
142+
"as"
143+
"assert"
144+
"async"
145+
"await"
146+
"break"
147+
"class"
148+
"continue"
149+
"def"
150+
"del"
151+
"elif"
152+
"else"
153+
"except"
154+
"exec"
155+
"finally"
156+
"for"
157+
"from"
158+
"global"
159+
"if"
160+
"import"
161+
"lambda"
162+
"nonlocal"
163+
"pass"
164+
"print"
165+
"raise"
166+
"return"
167+
"try"
168+
"while"
169+
"with"
170+
"yield"
171+
"match"
172+
"case"
173+
174+
; cython-specific
175+
"cdef"
176+
"cpdef"
177+
"ctypedef"
178+
"cimport"
179+
"nogil"
180+
"gil"
181+
"extern"
182+
"inline"
183+
"public"
184+
"readonly"
185+
"struct"
186+
"union"
187+
"enum"
188+
"fused"
189+
"property"
190+
"namespace"
191+
"cppclass"
192+
"const"
193+
] @keyword.control
194+
195+
(dotted_name
196+
(identifier)* @namespace)
197+
198+
(aliased_import
199+
alias: (identifier) @namespace)

runtime/queries/cython/indents.scm

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[
2+
(list)
3+
(tuple)
4+
(dictionary)
5+
(set)
6+
(if_statement)
7+
(for_statement)
8+
(while_statement)
9+
(with_statement)
10+
(try_statement)
11+
(match_statement)
12+
(case_clause)
13+
(import_from_statement)
14+
15+
(parenthesized_expression)
16+
(generator_expression)
17+
(list_comprehension)
18+
(set_comprehension)
19+
(dictionary_comprehension)
20+
21+
(tuple_pattern)
22+
(list_pattern)
23+
(argument_list)
24+
(parameters)
25+
(binary_operator)
26+
27+
(function_definition)
28+
(cdef_statement)
29+
(class_definition)
30+
] @indent
31+
32+
; Workaround for the tree-sitter grammar creating large errors when a
33+
; try_statement is missing the except/finally clause
34+
35+
(ERROR
36+
"try"
37+
.
38+
":" @indent @extend)
39+
(ERROR
40+
.
41+
"def") @indent @extend
42+
(ERROR
43+
(block) @indent @extend
44+
(#set! "scope" "all"))
45+
46+
(ERROR
47+
"try"
48+
.
49+
":"
50+
(ERROR
51+
(block
52+
(expression_statement
53+
(identifier) @_except) @indent.branch))
54+
(#eq? @_except "except"))
55+
56+
[
57+
(if_statement)
58+
(for_statement)
59+
(while_statement)
60+
(with_statement)
61+
(try_statement)
62+
(match_statement)
63+
(case_clause)
64+
65+
(cdef_statement)
66+
(function_definition)
67+
(class_definition)
68+
] @extend
69+
70+
[
71+
(return_statement)
72+
(break_statement)
73+
(continue_statement)
74+
(raise_statement)
75+
(pass_statement)
76+
] @extend.prevent-once
77+
78+
[
79+
")"
80+
"]"
81+
"}"
82+
] @outdent
83+
(elif_clause
84+
"elif" @outdent)
85+
(else_clause
86+
"else" @outdent)
87+
88+
(parameters
89+
.
90+
(identifier) @anchor
91+
(#set! "scope" "tail")) @align
92+
93+
(argument_list
94+
.
95+
(_) @anchor
96+
(#set! "scope" "tail")) @align
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(call
2+
function: (attribute
3+
object: (identifier) @_re)
4+
arguments: (argument_list
5+
.
6+
(string
7+
(string_content) @injection.content))
8+
(#eq? @_re "re")
9+
(#set! injection.language "regex"))
10+
11+
((binary_operator
12+
left: (string
13+
(string_content) @injection.content)
14+
operator: "%")
15+
(#set! injection.language "printf"))
16+
17+
((comment) @injection.content
18+
(#set! injection.language "comment"))

0 commit comments

Comments
 (0)