Skip to content

Add Swift programming language #2734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c706436
initial edits for Swift language support
atacan Jan 18, 2025
c3a0c3c
additional scope features
atacan Jan 18, 2025
37c03d1
replace `collectionKey` with `key.mapPair`
atacan Jan 18, 2025
e01350e
remove the Swift-specific type scopes since they're not in the standa…
atacan Jan 18, 2025
1110787
ensure proper alignment between the scope support map and the tree-si…
atacan Jan 18, 2025
3a54328
wip
atacan Jan 18, 2025
e9323fe
swift MDX
atacan Jan 18, 2025
3f4506c
deleted
atacan Jan 18, 2025
1655be7
start from scratch
atacan Jan 18, 2025
e6b91c8
Add Swift grammar rules and declarations to swift.scm
atacan Jan 18, 2025
231064a
start small
atacan Jan 18, 2025
8a0ddd2
make the function work
atacan Jan 18, 2025
f2d8d64
swift.scm by adding class and protocol declarations.
atacan Jan 18, 2025
31d4d2a
full support not-support list
atacan Jan 18, 2025
dbda618
Enhance swift.scm by adding support for import, property, typealias, …
atacan Jan 18, 2025
a568661
Add support for ternary expressions and enhance comment handling in s…
atacan Jan 18, 2025
df4ac3d
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jan 18, 2025
53ec6d1
Add support for switch statements and enhance if/guard statement hand…
atacan Jan 18, 2025
1cc4244
wrong folder?
atacan Jan 19, 2025
93437b6
swift playground
atacan Jan 19, 2025
5a7de2e
Add support for protocol declarations and enhance argument handling i…
atacan Jan 19, 2025
4e7087c
Merge branch 'main' into add-swift
AndreasArvidsson Feb 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/fixtures/scopes/swift/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"imports": ["swift"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { scalaScopeSupport } from "./scala";
import { scmScopeSupport } from "./scm";
import type { LanguageScopeSupportFacetMap } from "./scopeSupportFacets.types";
import { scssScopeSupport } from "./scss";
import { swiftScopeSupport } from "./swift";
import { talonScopeSupport } from "./talon";
import { typescriptScopeSupport } from "./typescript";
import { typescriptreactScopeSupport } from "./typescriptreact";
Expand Down Expand Up @@ -55,6 +56,7 @@ export const languageScopeSupport: StringRecord<LanguageScopeSupportFacetMap> =
scala: scalaScopeSupport,
scm: scmScopeSupport,
scss: scssScopeSupport,
swift: swiftScopeSupport,
talon: talonScopeSupport,
typescript: typescriptScopeSupport,
typescriptreact: typescriptreactScopeSupport,
Expand Down
53 changes: 53 additions & 0 deletions packages/common/src/scopeSupportFacets/swift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { LanguageScopeSupportFacetMap } from "./scopeSupportFacets.types";
import { ScopeSupportFacetLevel } from "./scopeSupportFacets.types";

const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel;

export const swiftScopeSupport: LanguageScopeSupportFacetMap = {
// Collections
list: supported,
map: supported,

// Control flow
ifStatement: supported,
"condition.if": supported,
"condition.switchCase": supported,
"condition.switchCase.iteration": supported,

// Text fragments
"string.singleLine": supported,
"string.multiLine": supported,
"comment.line": supported,
"comment.block": supported,

// Statements and blocks
statement: supported,

// Classes and types
class: supported,
className: supported,
"type.class": supported,
"type.variable": supported,
"type.return": supported,
"type.field": supported,

// Functions
anonymousFunction: supported,
namedFunction: supported,
functionCall: supported,
functionCallee: supported,

// Arguments and parameters
"argument.formal": supported,
"argument.actual": supported,

// Names and values
"name.function": supported,
"name.variable": supported,
"name.field": supported,
"value.variable": supported,
"value.field": supported,

// Swift-specific features
regularExpression: notApplicable, // Swift doesn't have regex literals
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Language from "./Language";

# Swift

<Language languageId="swift"></Language>
144 changes: 144 additions & 0 deletions queries/swift.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
;; https://github.com/tree-sitter/tree-sitter-swift/blob/master/src/grammar.json

;; Basic statements and declarations
[
(function_declaration)
(class_declaration)
(struct_declaration)
(protocol_declaration)
(enum_declaration)
(extension_declaration)
(import_declaration)
(variable_declaration)
(if_statement)
(guard_statement)
(for_statement)
(while_statement)
(repeat_while_statement)
(do_statement)
(return_statement)
(break_statement)
(continue_statement)
(throw_statement)
(switch_statement)
] @statement

;; String literals
(line_string_literal) @string

;; Comments
[
(line_comment)
(multiline_comment)
] @comment

;; Class declarations
(class_declaration
name: (_) @className
) @class @_.domain

;; Function declarations
(function_declaration
name: (_) @functionName
) @namedFunction @_.domain

;; Method declarations in classes/protocols
(function_declaration
name: (_) @functionName
) @namedFunction @_.domain

;; Initializer declarations
(initializer_declaration) @namedFunction @_.domain

;; Anonymous functions (closures)
(closure_expression) @anonymousFunction

;; Function calls
(call_expression) @functionCall

;; Function callee
(call_expression
function: (_) @functionCallee
) @_.domain

;; Collections
(array_expression) @list
(dictionary_expression) @map

;; Dictionary key-value pairs
(dictionary_element
key: (_) @key @value.leading.endOf
value: (_) @value @key.trailing.startOf
) @_.domain

;; Control flow
(if_statement) @ifStatement

;; Switch statement and cases
(switch_statement
condition: (_) @condition
) @_.domain

(switch_case
value: (_) @condition
) @branch @_.domain

(default_case) @branch

;; If statement branches
(if_statement
condition: (_) @condition
body: (_) @branch
alternative: (_)? @branch
) @_.domain

;; Try-catch blocks
(do_statement
"try" @branch.start
body: (_) @branch.end
)

(catch_clause) @branch

;; Parameters
(parameter
name: (_) @name
type: (_) @type
) @_.domain

;; Variable declarations
(variable_declaration
pattern: (_) @name
type: (_)? @type
value: (_)? @value
) @_.domain

;; Type annotations
(type_annotation
type: (_) @type
) @_.domain

;; Return type annotations
(function_declaration
return_type: (_) @type
) @_.domain

;; Function arguments
(call_expression
arguments: (tuple_expression
(_)? @_.leading.endOf
.
(_) @argument.actual
.
(_)? @_.trailing.startOf
)
) @_.domain

;; Function parameters
(parameter_clause
(_)? @_.leading.endOf
.
(_) @argument.formal
.
(_)? @_.trailing.startOf
) @_.domain