Skip to content

Commit fb56a4b

Browse files
authored
Improve tree-sitter queries for vlang (#6279)
- Update hightlight - add indents - add textobject - add injections
1 parent 64c137b commit fb56a4b

File tree

6 files changed

+158
-51
lines changed

6 files changed

+158
-51
lines changed

book/src/generated/lang-support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
| typescript |||| `typescript-language-server` |
141141
| ungrammar || | | |
142142
| uxntal || | | |
143-
| v || | | `v` |
143+
| v || | | `v` |
144144
| vala || | | `vala-language-server` |
145145
| verilog ||| | `svlangserver` |
146146
| vhs || | | |

languages.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ source = { git = "https://github.com/6cdh/tree-sitter-scheme", rev = "c0741320bf
16871687
[[language]]
16881688
name = "v"
16891689
scope = "source.v"
1690-
file-types = ["v", "vv"]
1690+
file-types = ["v", "vv", "vsh"]
16911691
shebangs = ["v run"]
16921692
roots = ["v.mod"]
16931693
language-server = { command = "v", args = ["ls"] }
@@ -1697,7 +1697,7 @@ indent = { tab-width = 4, unit = "\t" }
16971697

16981698
[[grammar]]
16991699
name = "v"
1700-
source = { git = "https://github.com/vlang/vls", subpath = "tree_sitter_v", rev = "3e8124ea4ab80aa08ec77f03df53f577902a0cdd" }
1700+
source = { git = "https://github.com/vlang/vls", subpath = "tree_sitter_v", rev = "66cf9d3086fb5ecc827cb32c64c5d812ab17d2c6" }
17011701

17021702
[[language]]
17031703
name = "verilog"

runtime/queries/v/highlights.scm

Lines changed: 105 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,130 @@
1414

1515
(field_identifier) @variable.other.member
1616
(selector_expression
17+
operand: (identifier) @variable
1718
field: (identifier) @variable.other.member)
1819

1920
(int_literal) @constant.numeric.integer
20-
(interpreted_string_literal) @string
21-
(rune_literal) @string
21+
22+
(attribute_declaration) @attribute
23+
(comment) @comment
24+
[
25+
(c_string_literal)
26+
(raw_string_literal)
27+
(interpreted_string_literal)
28+
(string_interpolation)
29+
(rune_literal)
30+
] @string
31+
2232
(escape_sequence) @constant.character.escape
2333

2434
[
25-
(type_identifier)
26-
(builtin_type)
2735
(pointer_type)
2836
(array_type)
2937
] @type
3038

39+
(const_spec name: (identifier) @constant)
40+
(global_var_type_initializer name: (identifier) @constant)
41+
(global_var_spec name: (identifier) @constant)
42+
((identifier) @constant (#match? @constant "^[A-Z][A-Z\\d_]*$"))
43+
44+
45+
[
46+
(generic_type)
47+
(type_identifier)
48+
] @constructor
49+
50+
(builtin_type) @type.builtin
51+
52+
[
53+
(true)
54+
(false)
55+
] @constant.builtin.boolean
56+
57+
3158
[
32-
(identifier)
3359
(module_identifier)
3460
(import_path)
61+
] @namespace
62+
63+
[
64+
(pseudo_comptime_identifier)
65+
(label_name)
66+
] @label
67+
68+
[
69+
(identifier)
3570
] @variable
3671

72+
3773
[
38-
"as"
39-
"asm"
40-
"assert"
41-
;"atomic"
42-
;"break"
43-
"const"
44-
;"continue"
45-
"defer"
46-
"else"
47-
"enum"
48-
"fn"
49-
"for"
50-
"$for"
51-
"go"
52-
"goto"
53-
"if"
54-
"$if"
55-
"import"
56-
"in"
57-
"!in"
58-
"interface"
59-
"is"
60-
"!is"
61-
"lock"
62-
"match"
63-
"module"
64-
"mut"
65-
"or"
66-
"pub"
67-
"return"
68-
"rlock"
69-
"select"
70-
;"shared"
71-
;"static"
72-
"struct"
73-
"type"
74-
;"union"
75-
"unsafe"
74+
"pub"
75+
"assert"
76+
"go"
77+
"asm"
78+
"defer"
79+
"unsafe"
80+
"sql"
81+
(none)
7682
] @keyword
7783

7884
[
79-
(true)
80-
(false)
81-
] @boolean
85+
"interface"
86+
"enum"
87+
"type"
88+
"union"
89+
"struct"
90+
"module"
91+
] @keyword.storage.type
92+
93+
[
94+
"static"
95+
"const"
96+
"__global"
97+
] @keyword.storage.modifier
98+
99+
[
100+
"mut"
101+
] @keyword.storage.modifier.mut
102+
103+
[
104+
"shared"
105+
"lock"
106+
"rlock"
107+
"spawn"
108+
] @keyword.control
109+
110+
[
111+
"if"
112+
"select"
113+
"else"
114+
"match"
115+
] @keyword.control.conditional
116+
117+
[
118+
"for"
119+
] @keyword.control.repeat
120+
121+
[
122+
"goto"
123+
"return"
124+
] @keyword.control.return
125+
126+
[
127+
"fn"
128+
] @keyword.control.function
129+
130+
131+
[
132+
"import"
133+
] @keyword.control.import
134+
135+
[
136+
"as"
137+
"in"
138+
"is"
139+
"or"
140+
] @keyword.operator
82141

83142
[
84143
"."
@@ -146,5 +205,3 @@
146205
".."
147206
"..."
148207
] @operator
149-
150-
(comment) @comment

runtime/queries/v/indents.scm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
(struct_declaration)
3+
(function_declaration)
4+
(if_expression)
5+
(match_expression)
6+
(expression_case)
7+
(default_case)
8+
(for_statement)
9+
(unsafe_expression)
10+
(short_var_declaration)
11+
] @indent
12+
13+
[
14+
"]"
15+
")"
16+
"}"
17+
] @outdent

runtime/queries/v/injections.scm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
((comment) @injection.content
2+
(#set! injection.language "comment"))
3+
4+
((sql_expression) @injection.content
5+
(#set! injection.language "sql"))
6+

runtime/queries/v/textobjects.scm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(function_declaration
2+
body: (block)? @function.inside) @function.around
3+
4+
((function_declaration
5+
name: (identifier) @_name
6+
body: (block)? @test.inside) @test.around
7+
(#match? @_name "^test"))
8+
9+
(fn_literal
10+
body: (block)? @function.inside) @function.around
11+
12+
(parameter_list
13+
((_) @parameter.inside . ","? @parameter.around) @parameter.around)
14+
15+
(call_expression
16+
(argument_list
17+
((_) @parameter.inside) @parameter.around))
18+
19+
(struct_declaration
20+
(struct_field_declaration_list) @class.inside) @class.around
21+
22+
(struct_field_declaration_list
23+
((_) @parameter.inside) @parameter.around)
24+
25+
(comment) @comment.inside
26+
(comment)+ @comment.around
27+

0 commit comments

Comments
 (0)