Skip to content

Commit aa1630a

Browse files
author
Arturs Krumins
authored
Update Swift Grammar and Queries (#10802)
1 parent 44504b7 commit aa1630a

File tree

6 files changed

+78
-13
lines changed

6 files changed

+78
-13
lines changed

book/src/generated/lang-support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
| supercollider || | | |
191191
| svelte || || `svelteserver` |
192192
| sway |||| `forc` |
193-
| swift || | | `sourcekit-lsp` |
193+
| swift || | | `sourcekit-lsp` |
194194
| t32 || | | |
195195
| tablegen |||| |
196196
| tact |||| |

languages.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,16 +1908,17 @@ language-servers = [ "r" ]
19081908
name = "swift"
19091909
scope = "source.swift"
19101910
injection-regex = "swift"
1911-
file-types = ["swift"]
1911+
file-types = ["swift", "swiftinterface"]
19121912
roots = [ "Package.swift" ]
19131913
comment-token = "//"
19141914
block-comment-tokens = { start = "/*", end = "*/" }
1915+
formatter = { command = "swift-format", args = [ "--configuration", ".swift-format"] }
19151916
auto-format = true
19161917
language-servers = [ "sourcekit-lsp" ]
19171918

19181919
[[grammar]]
19191920
name = "swift"
1920-
source = { git = "https://github.com/alex-pinkus/tree-sitter-swift", rev = "b1b66955d420d5cf5ff268ae552f0d6e43ff66e1" }
1921+
source = { git = "https://github.com/alex-pinkus/tree-sitter-swift", rev = "57c1c6d6ffa1c44b330182d41717e6fe37430704" }
19211922

19221923
[[language]]
19231924
name = "erb"

runtime/queries/swift/highlights.scm

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; Upstream: https://github.com/alex-pinkus/tree-sitter-swift/blob/1c586339fb00014b23d6933f2cc32b588a226f3b/queries/highlights.scm
1+
; Upstream: https://github.com/alex-pinkus/tree-sitter-swift/blob/57c1c6d6ffa1c44b330182d41717e6fe37430704/queries/highlights.scm
22

33
(line_string_literal
44
["\\(" ")"] @punctuation.special)
@@ -10,6 +10,7 @@
1010
(attribute) @variable
1111
(type_identifier) @type
1212
(self_expression) @variable.builtin
13+
(user_type (type_identifier) @variable.builtin (#eq? @variable.builtin "Self"))
1314

1415
; Declarations
1516
"func" @keyword.function
@@ -23,7 +24,9 @@
2324
] @keyword
2425

2526
(function_declaration (simple_identifier) @function.method)
26-
(function_declaration "init" @constructor)
27+
(init_declaration ["init" @constructor])
28+
(deinit_declaration ["deinit" @constructor])
29+
2730
(throws) @keyword
2831
"async" @keyword
2932
"await" @keyword
@@ -48,10 +51,23 @@
4851
"override"
4952
"convenience"
5053
"required"
51-
"some"
54+
"mutating"
55+
"associatedtype"
56+
"package"
5257
"any"
5358
] @keyword
5459

60+
(opaque_type ["some" @keyword])
61+
(existential_type ["any" @keyword])
62+
63+
(precedence_group_declaration
64+
["precedencegroup" @keyword]
65+
(simple_identifier) @type)
66+
(precedence_group_attribute
67+
(simple_identifier) @keyword
68+
[(simple_identifier) @type
69+
(boolean_literal) @constant.builtin.boolean])
70+
5571
[
5672
(getter_specifier)
5773
(setter_specifier)
@@ -73,6 +89,10 @@
7389
((navigation_expression
7490
(simple_identifier) @type) ; SomeType.method(): highlight SomeType as a type
7591
(#match? @type "^[A-Z]"))
92+
(call_expression (simple_identifier) @keyword (#eq? @keyword "defer")) ; defer { ... }
93+
94+
(try_operator) @operator
95+
(try_operator ["try" @keyword])
7696

7797
(directive) @function.macro
7898
(diagnostic) @function.macro
@@ -136,10 +156,8 @@
136156

137157
; Operators
138158
[
139-
"try"
140-
"try?"
141-
"try!"
142159
"!"
160+
"?"
143161
"+"
144162
"-"
145163
"*"
@@ -171,3 +189,8 @@
171189
"..."
172190
(custom_operator)
173191
] @operator
192+
193+
(value_parameter_pack ["each" @keyword])
194+
(value_pack_expansion ["repeat" @keyword])
195+
(type_parameter_pack ["each" @keyword])
196+
(type_pack_expansion ["repeat" @keyword])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; Upstream: https://github.com/alex-pinkus/tree-sitter-swift/blob/57c1c6d6ffa1c44b330182d41717e6fe37430704/queries/injections.scm
2+
3+
; Parse regex syntax within regex literals
4+
5+
((regex_literal) @injection.content
6+
(#set! injection.language "regex"))

runtime/queries/swift/locals.scm

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
; Upstream: https://github.com/alex-pinkus/tree-sitter-swift/blob/57c1c6d6ffa1c44b330182d41717e6fe37430704/queries/locals.scm
2+
(import_declaration (identifier) @definition.import)
3+
(function_declaration name: (simple_identifier) @definition.function)
4+
5+
; Scopes
16
[
7+
(for_statement)
8+
(while_statement)
9+
(repeat_while_statement)
10+
(do_statement)
11+
(if_statement)
12+
(guard_statement)
13+
(switch_statement)
14+
(property_declaration)
215
(function_declaration)
16+
(class_declaration)
17+
(protocol_declaration)
18+
(lambda_literal)
319
] @local.scope
4-
5-
(parameter name: (simple_identifier) @local.definition)
6-
7-
(simple_identifier) @local.reference
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(class_declaration
2+
body: (_) @class.inside) @class.around
3+
4+
(protocol_declaration
5+
body: (_) @class.inside) @class.around
6+
7+
(function_declaration
8+
body: (_) @function.inside) @function.around
9+
10+
(parameter
11+
(_) @parameter.inside) @parameter.around
12+
13+
(lambda_parameter
14+
(_) @parameter.inside) @parameter.around
15+
16+
[
17+
(comment)
18+
(multiline_comment)
19+
] @comment.inside
20+
21+
(comment)+ @comment.around
22+
23+
(multiline_comment) @comment.around

0 commit comments

Comments
 (0)