Skip to content

Commit

Permalink
Add swift language (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dispersia authored Apr 8, 2022
1 parent 8e12fd5 commit 9caf7c0
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/lang-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
| scala || || `metals` |
| solidity || | | `solc` |
| svelte || || `svelteserver` |
| swift || | | `sourcekit-lsp` |
| tablegen |||| |
| toml || | | |
| tsq || | | |
Expand Down
14 changes: 14 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1071,3 +1071,17 @@ roots = []
indent = { tab-width = 2, unit = " " }
grammar = "markdown"
language-server = { command = "R", args = ["--slave", "-e", "languageserver::run()"] }

[[language]]
name = "swift"
scope = "source.swift"
injection-regex = "swift"
file-types = ["swift"]
roots = [ "Package.swift" ]
comment-token = "//"
auto-format = true
language-server = { command = "sourcekit-lsp" }

[[grammar]]
name = "swift"
source = { git = "https://github.com/Dispersia/tree-sitter-swift", rev = "e75240f89bb3bfd3396155859ae364e5c58d7377" }
158 changes: 158 additions & 0 deletions runtime/queries/swift/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
[ "." ";" ":" "," ] @punctuation.delimiter
[ "\\(" "(" ")" "[" "]" "{" "}"] @punctuation.bracket ; TODO: "\\(" ")" in interpolations should be @punctuation.special

; Identifiers
(attribute) @variable
(type_identifier) @type
(self_expression) @variable.builtin

; Declarations
"func" @keyword.function
[
(visibility_modifier)
(member_modifier)
(function_modifier)
(property_modifier)
(parameter_modifier)
(inheritance_modifier)
] @keyword

(function_declaration (simple_identifier) @function.method)
(function_declaration ["init" @constructor])
(throws) @keyword
"async" @keyword
(where_keyword) @keyword
(parameter external_name: (simple_identifier) @variable.parameter)
(parameter name: (simple_identifier) @variable.parameter)
(type_parameter (type_identifier) @variable.parameter)
(inheritance_constraint (identifier (simple_identifier) @variable.parameter))
(equality_constraint (identifier (simple_identifier) @variable.parameter))
(non_binding_pattern bound_identifier: (simple_identifier)) @variable

[
"typealias"
"struct"
"class"
"enum"
"protocol"
"extension"
"indirect"
"some"
] @keyword

[
(getter_specifier)
(setter_specifier)
(modify_specifier)
] @keyword

(class_body (property_declaration (value_binding_pattern (non_binding_pattern (simple_identifier) @variable.other.member))))
(protocol_property_declaration (value_binding_pattern (non_binding_pattern (simple_identifier) @variable.other.member)))

(import_declaration ["import" @keyword.control.import])

(enum_entry ["case" @keyword])

; Function calls
(call_expression (simple_identifier) @function) ; foo()
(call_expression ; foo.bar.baz(): highlight the baz()
(navigation_expression
(navigation_suffix (simple_identifier) @function)))
((navigation_expression
(simple_identifier) @type) ; SomeType.method(): highlight SomeType as a type
(#match? @type "^[A-Z]"))

(directive) @function.macro
(diagnostic) @function.macro

; Statements
(for_statement ["for" @keyword.control.repeat])
(for_statement ["in" @keyword.control.repeat])
(for_statement item: (simple_identifier) @variable)
(else) @keyword
(as_operator) @keyword

["while" "repeat" "continue" "break"] @keyword.control.repeat

["let" "var"] @keyword
(non_binding_pattern (simple_identifier) @variable)

(guard_statement ["guard" @keyword.control.conditional])
(if_statement ["if" @keyword.control.conditional])
(switch_statement ["switch" @keyword.control.conditional])
(switch_entry ["case" @keyword])
(switch_entry ["fallthrough" @keyword])
(switch_entry (default_keyword) @keyword)
"return" @keyword.control.return
(ternary_expression
["?" ":"] @keyword.control.conditional)

["do" (throw_keyword) (catch_keyword)] @keyword

(statement_label) @label

; Comments
(comment) @comment.line
(multiline_comment) @comment.block

; String literals
(line_str_text) @string
(str_escaped_char) @string
(multi_line_str_text) @string
(raw_str_part) @string
(raw_str_end_part) @string
(raw_str_interpolation_start) @string.special
["\"" "\"\"\""] @string

; Lambda literals
(lambda_literal ["in" @keyword.operator])

; Basic literals
(integer_literal) @constant.numeric.integer
[
(hex_literal)
(oct_literal)
(bin_literal)
] @constant.numeric
(real_literal) @constant.numeric.float
(boolean_literal) @constant.builtin.boolean
"nil" @variable.builtin

; Operators
(custom_operator) @operator
[
"try"
"try?"
"try!"
"!"
"+"
"-"
"*"
"/"
"%"
"="
"+="
"-="
"*="
"/="
"<"
">"
"<="
">="
"++"
"--"
"&"
"~"
"%="
"!="
"!=="
"=="
"==="
"??"

"->"

"..<"
"..."
] @operator

18 changes: 18 additions & 0 deletions runtime/queries/swift/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(import_declaration (identifier) @definition.import)
(function_declaration name: (simple_identifier) @definition.function)

; Scopes
[
(statements)
(for_statement)
(while_statement)
(repeat_while_statement)
(do_statement)
(if_statement)
(guard_statement)
(switch_statement)
(property_declaration)
(function_declaration)
(class_declaration)
(protocol_declaration)
] @scope

0 comments on commit 9caf7c0

Please sign in to comment.