From c70643686a11d754a31d8fb2f5fe5274f8f19d42 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 15:33:29 +0100 Subject: [PATCH 01/21] initial edits for Swift language support - Introduced `swiftScopeSupport` to manage language-specific scope features. - Updated `languageScopeSupport` to include Swift support. - Created a new file for Swift scope support definitions. --- .../languageScopeSupport.ts | 2 + .../common/src/scopeSupportFacets/swift.ts | 105 ++++++++++++++++++ queries/swift.scm | 0 3 files changed, 107 insertions(+) create mode 100644 packages/common/src/scopeSupportFacets/swift.ts create mode 100644 queries/swift.scm diff --git a/packages/common/src/scopeSupportFacets/languageScopeSupport.ts b/packages/common/src/scopeSupportFacets/languageScopeSupport.ts index 2c05c76c6b..f1ef64f4b7 100644 --- a/packages/common/src/scopeSupportFacets/languageScopeSupport.ts +++ b/packages/common/src/scopeSupportFacets/languageScopeSupport.ts @@ -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"; @@ -55,6 +56,7 @@ export const languageScopeSupport: StringRecord = scala: scalaScopeSupport, scm: scmScopeSupport, scss: scssScopeSupport, + swift: swiftScopeSupport, talon: talonScopeSupport, typescript: typescriptScopeSupport, typescriptreact: typescriptreactScopeSupport, diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts new file mode 100644 index 0000000000..0d86e9f802 --- /dev/null +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -0,0 +1,105 @@ +import type { LanguageScopeSupportFacetMap } from "./scopeSupportFacets.types"; +import { ScopeSupportFacetLevel } from "./scopeSupportFacets.types"; + +const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel; + +export const swiftScopeSupport: LanguageScopeSupportFacetMap = { + list: supported, + map: supported, + ifStatement: supported, + regularExpression: supported, + switchStatementSubject: supported, + fieldAccess: supported, + disqualifyDelimiter: supported, + + "textFragment.string.singleLine": supported, + "textFragment.string.multiLine": supported, + "textFragment.comment.line": supported, + "textFragment.comment.block": supported, + + statement: supported, + "statement.iteration.document": supported, + "statement.iteration.block": supported, + + class: supported, + className: supported, + + anonymousFunction: supported, + namedFunction: supported, + "namedFunction.iteration.document": supported, + "namedFunction.method": supported, + "namedFunction.method.iteration.class": supported, + "namedFunction.constructor": supported, + + functionName: supported, + "functionName.iteration.document": supported, + "functionName.method": supported, + "functionName.method.iteration.class": supported, + "functionName.constructor": supported, + + functionCall: supported, + "functionCall.constructor": supported, + functionCallee: supported, + "functionCallee.constructor": supported, + + "argument.actual": supported, + "argument.actual.iteration": supported, + "argument.actual.method": supported, + "argument.actual.method.iteration": supported, + "argument.actual.constructor": supported, + "argument.actual.constructor.iteration": supported, + "argument.formal": supported, + "argument.formal.iteration": supported, + "argument.formal.method": supported, + "argument.formal.method.iteration": supported, + "argument.formal.constructor": supported, + "argument.formal.constructor.iteration": supported, + + "comment.line": supported, + "comment.block": supported, + + "string.singleLine": supported, + "string.multiLine": supported, + + "branch.if": supported, + "branch.if.iteration": supported, + "branch.try": supported, + "branch.switchCase": supported, + "branch.switchCase.iteration": supported, + "branch.ternary": supported, + + "condition.if": supported, + "condition.while": supported, + "condition.doWhile": supported, + "condition.for": supported, + "condition.ternary": supported, + "condition.switchCase": supported, + + "name.argument.formal": supported, + "name.argument.formal.iteration": supported, + "name.argument.formal.method": supported, + "name.argument.formal.method.iteration": supported, + "name.argument.formal.constructor": supported, + "name.argument.formal.constructor.iteration": supported, + "name.foreach": supported, + "name.assignment": supported, + "name.assignment.pattern": supported, + "name.variable": supported, + "name.variable.pattern": supported, + "name.function": supported, + "name.method": supported, + "name.constructor": supported, + "name.class": supported, + "name.field": supported, + + "type.argument.formal": supported, + "type.argument.formal.iteration": supported, + "type.argument.formal.method": supported, + "type.argument.formal.method.iteration": supported, + "type.argument.formal.constructor": supported, + "type.argument.formal.constructor.iteration": supported, + "type.return": supported, + "type.field": supported, + "type.interface": supported, + "type.variable": supported, +}; \ No newline at end of file diff --git a/queries/swift.scm b/queries/swift.scm new file mode 100644 index 0000000000..e69de29bb2 From c3a0c3c83c18674ab45a571ab4189fed1443d5ba Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 15:54:31 +0100 Subject: [PATCH 02/21] additional scope features - Expanded `swiftScopeSupport` to include new language-specific features such as `collectionKey`, `className.iteration`, and `type.protocol`. - Updated handling of ternary operators and introduced Swift-specific conditions like `condition.guard`. - Added a new query file `swift.scm` to define grammar rules for Swift, covering statements, declarations, and control flow constructs. --- .../common/src/scopeSupportFacets/swift.ts | 40 +++- queries/swift.scm | 207 ++++++++++++++++++ 2 files changed, 241 insertions(+), 6 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 0d86e9f802..a723d33187 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -4,44 +4,57 @@ import { ScopeSupportFacetLevel } from "./scopeSupportFacets.types"; const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel; export const swiftScopeSupport: LanguageScopeSupportFacetMap = { + // Collections list: supported, map: supported, + collectionKey: supported, + "collectionKey.iteration": supported, + + // Control flow ifStatement: supported, - regularExpression: supported, switchStatementSubject: supported, - fieldAccess: supported, - disqualifyDelimiter: supported, - + + // Text fragments "textFragment.string.singleLine": supported, "textFragment.string.multiLine": supported, "textFragment.comment.line": supported, "textFragment.comment.block": supported, + // Statements and blocks statement: supported, "statement.iteration.document": supported, "statement.iteration.block": supported, + // Classes and types class: supported, className: supported, + "className.iteration": supported, + "className.iteration.document": supported, + // Functions anonymousFunction: supported, namedFunction: supported, + "namedFunction.iteration": supported, "namedFunction.iteration.document": supported, "namedFunction.method": supported, "namedFunction.method.iteration.class": supported, "namedFunction.constructor": supported, + // Function names functionName: supported, + "functionName.iteration": supported, "functionName.iteration.document": supported, "functionName.method": supported, "functionName.method.iteration.class": supported, "functionName.constructor": supported, + // Function calls functionCall: supported, "functionCall.constructor": supported, functionCallee: supported, "functionCallee.constructor": supported, + // Arguments and parameters "argument.actual": supported, "argument.actual.iteration": supported, "argument.actual.method": supported, @@ -55,26 +68,32 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "argument.formal.constructor": supported, "argument.formal.constructor.iteration": supported, + // Comments "comment.line": supported, "comment.block": supported, + // Strings "string.singleLine": supported, "string.multiLine": supported, + // Branches "branch.if": supported, "branch.if.iteration": supported, "branch.try": supported, "branch.switchCase": supported, "branch.switchCase.iteration": supported, - "branch.ternary": supported, + "branch.ternary": notApplicable, // Swift doesn't have ternary operators in the same way + // Conditions "condition.if": supported, "condition.while": supported, "condition.doWhile": supported, "condition.for": supported, - "condition.ternary": supported, + "condition.guard": supported, // Swift-specific + "condition.ternary": notApplicable, "condition.switchCase": supported, + // Names "name.argument.formal": supported, "name.argument.formal.iteration": supported, "name.argument.formal.method": supported, @@ -92,6 +111,7 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "name.class": supported, "name.field": supported, + // Types "type.argument.formal": supported, "type.argument.formal.iteration": supported, "type.argument.formal.method": supported, @@ -102,4 +122,12 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "type.field": supported, "type.interface": supported, "type.variable": supported, + "type.protocol": supported, // Swift-specific + "type.struct": supported, // Swift-specific + "type.enum": supported, // Swift-specific + + // Swift-specific features + disqualifyDelimiter: supported, + fieldAccess: supported, + regularExpression: notApplicable, // Swift doesn't have regex literals }; \ No newline at end of file diff --git a/queries/swift.scm b/queries/swift.scm index e69de29bb2..e9b28155a1 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -0,0 +1,207 @@ +;; 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 @textFragment + +;; Comments +[ + (line_comment) + (multiline_comment) +] @comment @textFragment + +;; Class declarations +(class_declaration + name: (_) @className +) @class @_.domain + +;; Function declarations +(function_declaration + name: (_) @functionName +) @namedFunction @functionName.domain + +;; Method declarations in classes/protocols +(function_declaration + name: (_) @functionName +) @namedFunction.method @functionName.method.domain + +;; Initializer declarations +(initializer_declaration) @namedFunction.constructor @functionName.constructor.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: (_) @collectionKey @value.leading.endOf + value: (_) @value @collectionKey.trailing.startOf +) @_.domain + +;; Control flow +(if_statement) @ifStatement + +;; Switch statement and cases +(switch_statement + condition: (_) @private.switchStatementSubject +) @_.domain + +(switch_case + value: (_) @condition +) @branch @condition.domain + +(default_case) @branch + +(switch_statement) @branch.iteration @condition.iteration + +;; If statement branches +(if_statement + condition: (_) @condition + body: (_) @branch.end.endOf @branch.removal.end.endOf + alternative: (_ + (if_statement) @branch.removal.end.startOf + )? +) @branch.start.startOf @branch.removal.start.startOf @condition.domain + +;; Else if branches +(else_clause + (if_statement + condition: (_) @condition + body: (_) @branch.end.endOf @condition.domain.end.endOf + ) +) @branch.start.startOf @condition.domain.start.startOf + +;; Else branches +(else_clause + (code_block) +) @branch + +;; If statement iteration +(if_statement) @branch.iteration + +;; Try-catch blocks +(do_statement + "try" @branch.start + body: (_) @branch.end +) + +(catch_clause) @branch + +(do_statement) @branch.iteration + +;; Conditions +(if_statement + condition: (_) @condition +) @_.domain + +(while_statement + condition: (_) @condition +) @_.domain + +(guard_statement + condition: (_) @condition +) @_.domain + +;; 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 + +;; Operators that should be disqualified as delimiters +operator: [ + "<" + "<=" + ">" + ">=" +] @disqualifyDelimiter + +;; Function arguments +(call_expression + arguments: (tuple_expression + (_)? @_.leading.endOf + . + (_) @argumentOrParameter + . + (_)? @_.trailing.startOf + ) +) @_.domain + +;; Parameter list iteration scope +(parameter_clause + "(" @argumentOrParameter.iteration.start.endOf + ")" @argumentOrParameter.iteration.end.startOf +) @argumentOrParameter.iteration.domain + +;; Argument list iteration scope +(call_expression + arguments: (tuple_expression + "(" @argumentOrParameter.iteration.start.endOf + ")" @argumentOrParameter.iteration.end.startOf + ) +) @argumentOrParameter.iteration.domain + +;; Block iteration scopes +(code_block + "{" @statement.iteration.start.endOf @name.iteration.start.endOf @value.iteration.start.endOf @type.iteration.start.endOf + "}" @statement.iteration.end.startOf @name.iteration.end.startOf @value.iteration.end.startOf @type.iteration.end.startOf +) + +;; Class/struct body iteration scope +[ + (class_declaration) + (struct_declaration) +] @namedFunction.method.iteration.class @functionName.method.iteration.class + +;; File-level iteration scope +(source_file) @statement.iteration @name.iteration @value.iteration @type.iteration @namedFunction.iteration @functionName.iteration From 37c03d1179e483bed33c5d306608bd6ff81d7054 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 15:59:49 +0100 Subject: [PATCH 03/21] replace `collectionKey` with `key.mapPair` - Updated `swiftScopeSupport` to use `key.mapPair` and `key.mapPair.iteration` instead of `collectionKey` and its iteration counterpart. - Modified the corresponding grammar rules in `swift.scm` to reflect these changes, ensuring proper handling of dictionary key-value pairs. --- packages/common/src/scopeSupportFacets/swift.ts | 6 ++---- queries/swift.scm | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index a723d33187..2b881a3980 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -7,8 +7,8 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { // Collections list: supported, map: supported, - collectionKey: supported, - "collectionKey.iteration": supported, + "key.mapPair": supported, + "key.mapPair.iteration": supported, // Control flow ifStatement: supported, @@ -28,8 +28,6 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { // Classes and types class: supported, className: supported, - "className.iteration": supported, - "className.iteration.document": supported, // Functions anonymousFunction: supported, diff --git a/queries/swift.scm b/queries/swift.scm index e9b28155a1..417af3574b 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -67,8 +67,8 @@ ;; Dictionary key-value pairs (dictionary_element - key: (_) @collectionKey @value.leading.endOf - value: (_) @value @collectionKey.trailing.startOf + key: (_) @key.mapPair @value.leading.endOf + value: (_) @value @key.mapPair.trailing.startOf ) @_.domain ;; Control flow From e01350eb9cbbb8c7b4eeaac220350a7d904f7972 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 16:01:01 +0100 Subject: [PATCH 04/21] remove the Swift-specific type scopes since they're not in the standard scope types --- packages/common/src/scopeSupportFacets/swift.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 2b881a3980..4fa85d1048 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -87,7 +87,6 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "condition.while": supported, "condition.doWhile": supported, "condition.for": supported, - "condition.guard": supported, // Swift-specific "condition.ternary": notApplicable, "condition.switchCase": supported, @@ -120,9 +119,6 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "type.field": supported, "type.interface": supported, "type.variable": supported, - "type.protocol": supported, // Swift-specific - "type.struct": supported, // Swift-specific - "type.enum": supported, // Swift-specific // Swift-specific features disqualifyDelimiter: supported, From 1110787c309fb5c1e921050d2117d7b71b7b0912 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 16:21:25 +0100 Subject: [PATCH 05/21] ensure proper alignment between the scope support map and the tree-sitter query file. Here are the key changes made: 1. In swift.ts: Removed all iteration-specific scopes Simplified to use base scope types only Removed unsupported Swift-specific scopes Kept only standard scope types that are documented and used across languages In swift.scm: Removed @textFragment annotations Simplified function declarations to use standard @namedFunction Updated dictionary key-value pairs to use standard @key and @value Removed iteration-specific scopes Updated argument handling to use @argument.actual and @argument.formal Removed operator-specific annotations Simplified branch and condition handling Removed all iteration scopes for blocks, classes, and file-level --- .../common/src/scopeSupportFacets/swift.ts | 104 ++---------------- queries/swift.scm | 103 ++++------------- 2 files changed, 31 insertions(+), 176 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 4fa85d1048..0239b4bbf3 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -7,121 +7,39 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { // Collections list: supported, map: supported, - "key.mapPair": supported, - "key.mapPair.iteration": supported, + key: supported, // Control flow ifStatement: supported, - switchStatementSubject: supported, - + branch: supported, + condition: supported, + // Text fragments - "textFragment.string.singleLine": supported, - "textFragment.string.multiLine": supported, - "textFragment.comment.line": supported, - "textFragment.comment.block": supported, + string: supported, + comment: supported, // Statements and blocks statement: supported, - "statement.iteration.document": supported, - "statement.iteration.block": supported, // Classes and types class: supported, className: supported, + type: supported, // Functions anonymousFunction: supported, namedFunction: supported, - "namedFunction.iteration": supported, - "namedFunction.iteration.document": supported, - "namedFunction.method": supported, - "namedFunction.method.iteration.class": supported, - "namedFunction.constructor": supported, - - // Function names - functionName: supported, - "functionName.iteration": supported, - "functionName.iteration.document": supported, - "functionName.method": supported, - "functionName.method.iteration.class": supported, - "functionName.constructor": supported, - - // Function calls functionCall: supported, - "functionCall.constructor": supported, functionCallee: supported, - "functionCallee.constructor": supported, // Arguments and parameters - "argument.actual": supported, - "argument.actual.iteration": supported, - "argument.actual.method": supported, - "argument.actual.method.iteration": supported, - "argument.actual.constructor": supported, - "argument.actual.constructor.iteration": supported, "argument.formal": supported, - "argument.formal.iteration": supported, - "argument.formal.method": supported, - "argument.formal.method.iteration": supported, - "argument.formal.constructor": supported, - "argument.formal.constructor.iteration": supported, - - // Comments - "comment.line": supported, - "comment.block": supported, - - // Strings - "string.singleLine": supported, - "string.multiLine": supported, - - // Branches - "branch.if": supported, - "branch.if.iteration": supported, - "branch.try": supported, - "branch.switchCase": supported, - "branch.switchCase.iteration": supported, - "branch.ternary": notApplicable, // Swift doesn't have ternary operators in the same way - - // Conditions - "condition.if": supported, - "condition.while": supported, - "condition.doWhile": supported, - "condition.for": supported, - "condition.ternary": notApplicable, - "condition.switchCase": supported, - - // Names - "name.argument.formal": supported, - "name.argument.formal.iteration": supported, - "name.argument.formal.method": supported, - "name.argument.formal.method.iteration": supported, - "name.argument.formal.constructor": supported, - "name.argument.formal.constructor.iteration": supported, - "name.foreach": supported, - "name.assignment": supported, - "name.assignment.pattern": supported, - "name.variable": supported, - "name.variable.pattern": supported, - "name.function": supported, - "name.method": supported, - "name.constructor": supported, - "name.class": supported, - "name.field": supported, + "argument.actual": supported, - // Types - "type.argument.formal": supported, - "type.argument.formal.iteration": supported, - "type.argument.formal.method": supported, - "type.argument.formal.method.iteration": supported, - "type.argument.formal.constructor": supported, - "type.argument.formal.constructor.iteration": supported, - "type.return": supported, - "type.field": supported, - "type.interface": supported, - "type.variable": supported, + // Names and values + name: supported, + value: supported, // Swift-specific features - disqualifyDelimiter: supported, - fieldAccess: supported, regularExpression: notApplicable, // Swift doesn't have regex literals }; \ No newline at end of file diff --git a/queries/swift.scm b/queries/swift.scm index 417af3574b..2ac2393800 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -24,13 +24,13 @@ ] @statement ;; String literals -(line_string_literal) @string @textFragment +(line_string_literal) @string ;; Comments [ (line_comment) (multiline_comment) -] @comment @textFragment +] @comment ;; Class declarations (class_declaration @@ -40,15 +40,15 @@ ;; Function declarations (function_declaration name: (_) @functionName -) @namedFunction @functionName.domain +) @namedFunction @_.domain ;; Method declarations in classes/protocols (function_declaration name: (_) @functionName -) @namedFunction.method @functionName.method.domain +) @namedFunction @_.domain ;; Initializer declarations -(initializer_declaration) @namedFunction.constructor @functionName.constructor.domain +(initializer_declaration) @namedFunction @_.domain ;; Anonymous functions (closures) (closure_expression) @anonymousFunction @@ -67,8 +67,8 @@ ;; Dictionary key-value pairs (dictionary_element - key: (_) @key.mapPair @value.leading.endOf - value: (_) @value @key.mapPair.trailing.startOf + key: (_) @key @value.leading.endOf + value: (_) @value @key.trailing.startOf ) @_.domain ;; Control flow @@ -76,41 +76,21 @@ ;; Switch statement and cases (switch_statement - condition: (_) @private.switchStatementSubject + condition: (_) @condition ) @_.domain (switch_case value: (_) @condition -) @branch @condition.domain +) @branch @_.domain (default_case) @branch -(switch_statement) @branch.iteration @condition.iteration - ;; If statement branches (if_statement condition: (_) @condition - body: (_) @branch.end.endOf @branch.removal.end.endOf - alternative: (_ - (if_statement) @branch.removal.end.startOf - )? -) @branch.start.startOf @branch.removal.start.startOf @condition.domain - -;; Else if branches -(else_clause - (if_statement - condition: (_) @condition - body: (_) @branch.end.endOf @condition.domain.end.endOf - ) -) @branch.start.startOf @condition.domain.start.startOf - -;; Else branches -(else_clause - (code_block) -) @branch - -;; If statement iteration -(if_statement) @branch.iteration + body: (_) @branch + alternative: (_)? @branch +) @_.domain ;; Try-catch blocks (do_statement @@ -120,21 +100,6 @@ (catch_clause) @branch -(do_statement) @branch.iteration - -;; Conditions -(if_statement - condition: (_) @condition -) @_.domain - -(while_statement - condition: (_) @condition -) @_.domain - -(guard_statement - condition: (_) @condition -) @_.domain - ;; Parameters (parameter name: (_) @name @@ -158,50 +123,22 @@ return_type: (_) @type ) @_.domain -;; Operators that should be disqualified as delimiters -operator: [ - "<" - "<=" - ">" - ">=" -] @disqualifyDelimiter - ;; Function arguments (call_expression arguments: (tuple_expression (_)? @_.leading.endOf . - (_) @argumentOrParameter + (_) @argument.actual . (_)? @_.trailing.startOf ) ) @_.domain -;; Parameter list iteration scope +;; Function parameters (parameter_clause - "(" @argumentOrParameter.iteration.start.endOf - ")" @argumentOrParameter.iteration.end.startOf -) @argumentOrParameter.iteration.domain - -;; Argument list iteration scope -(call_expression - arguments: (tuple_expression - "(" @argumentOrParameter.iteration.start.endOf - ")" @argumentOrParameter.iteration.end.startOf - ) -) @argumentOrParameter.iteration.domain - -;; Block iteration scopes -(code_block - "{" @statement.iteration.start.endOf @name.iteration.start.endOf @value.iteration.start.endOf @type.iteration.start.endOf - "}" @statement.iteration.end.startOf @name.iteration.end.startOf @value.iteration.end.startOf @type.iteration.end.startOf -) - -;; Class/struct body iteration scope -[ - (class_declaration) - (struct_declaration) -] @namedFunction.method.iteration.class @functionName.method.iteration.class - -;; File-level iteration scope -(source_file) @statement.iteration @name.iteration @value.iteration @type.iteration @namedFunction.iteration @functionName.iteration + (_)? @_.leading.endOf + . + (_) @argument.formal + . + (_)? @_.trailing.startOf +) @_.domain From 3a5432880c328be7ca10d39f9ac2538ffd6e4aac Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 16:40:29 +0100 Subject: [PATCH 06/21] wip --- data/fixtures/scopes/swift/index.json | 3 +++ .../common/src/scopeSupportFacets/swift.ts | 26 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 data/fixtures/scopes/swift/index.json diff --git a/data/fixtures/scopes/swift/index.json b/data/fixtures/scopes/swift/index.json new file mode 100644 index 0000000000..54e245a881 --- /dev/null +++ b/data/fixtures/scopes/swift/index.json @@ -0,0 +1,3 @@ +{ + "imports": ["swift"] +} \ No newline at end of file diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 0239b4bbf3..abe32c4a75 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -7,16 +7,18 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { // Collections list: supported, map: supported, - key: supported, // Control flow ifStatement: supported, - branch: supported, - condition: supported, + "condition.if": supported, + "condition.switchCase": supported, + "condition.switchCase.iteration": supported, // Text fragments - string: supported, - comment: supported, + "string.singleLine": supported, + "string.multiLine": supported, + "comment.line": supported, + "comment.block": supported, // Statements and blocks statement: supported, @@ -24,7 +26,10 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { // Classes and types class: supported, className: supported, - type: supported, + "type.class": supported, + "type.variable": supported, + "type.return": supported, + "type.field": supported, // Functions anonymousFunction: supported, @@ -37,9 +42,12 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "argument.actual": supported, // Names and values - name: supported, - value: supported, + "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 -}; \ No newline at end of file +}; \ No newline at end of file From e9323fed6431a0050fea0d030a5a6a751ddc48c3 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 16:48:57 +0100 Subject: [PATCH 07/21] swift MDX --- .../cursorless-org-docs/src/docs/user/languages/swift.mdx | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/cursorless-org-docs/src/docs/user/languages/swift.mdx diff --git a/packages/cursorless-org-docs/src/docs/user/languages/swift.mdx b/packages/cursorless-org-docs/src/docs/user/languages/swift.mdx new file mode 100644 index 0000000000..75330bbd25 --- /dev/null +++ b/packages/cursorless-org-docs/src/docs/user/languages/swift.mdx @@ -0,0 +1,5 @@ +import Language from "./Language"; + +# Swift + + From 3f4506c45aef99e31266cd509b016de7d4caf366 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 16:57:01 +0100 Subject: [PATCH 08/21] deleted --- data/fixtures/scopes/swift/index.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 data/fixtures/scopes/swift/index.json diff --git a/data/fixtures/scopes/swift/index.json b/data/fixtures/scopes/swift/index.json deleted file mode 100644 index 54e245a881..0000000000 --- a/data/fixtures/scopes/swift/index.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "imports": ["swift"] -} \ No newline at end of file From 1655be73a68adbac25ccf2b8a4b1435a2e813c6a Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 17:05:15 +0100 Subject: [PATCH 09/21] start from scratch --- queries/swift.scm | 144 ---------------------------------------------- 1 file changed, 144 deletions(-) diff --git a/queries/swift.scm b/queries/swift.scm index 2ac2393800..e69de29bb2 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -1,144 +0,0 @@ -;; 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 From e6b91c81dcee27acdb1abc239eb3f54a5d21bab7 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 17:08:58 +0100 Subject: [PATCH 10/21] Add Swift grammar rules and declarations to swift.scm --- queries/swift.scm | 172 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/queries/swift.scm b/queries/swift.scm index e69de29bb2..f2a5938dd3 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -0,0 +1,172 @@ +[ + (class_declaration) + (struct_declaration) + (enum_declaration) + (protocol_declaration) + (extension_declaration) + (import_declaration) + (if_statement) + (guard_statement) + (for_statement) + (while_statement) + (repeat_while_statement) + (switch_statement) + (do_statement) + (defer_statement) + (return_statement) + (break_statement) + (continue_statement) + (fallthrough_statement) + (throw_statement) + (do_catch_statement) + (expression_statement) + (variable_declaration) + (constant_declaration) + (function_declaration) + (initializer_declaration) + (subscript_declaration) + (operator_declaration) + (deinitializer_declaration) + (typealias_declaration) + (associatedtype_declaration) + (import_declaration) + (access_level_modifier) + (member_access) + (identifier) + (identifier_pattern) + (identifier_expression) + (identifier_expression_pattern) + (identifier_reference) +] @statement + +(class_declaration + name: (_) @name @className +) @class @_.domain + +(struct_declaration + name: (_) @name @structName +) @struct @_.domain + +(enum_declaration + name: (_) @name @enumName +) @enum @_.domain + +(protocol_declaration + name: (_) @name @protocolName +) @protocol @_.domain + +(extension_declaration + name: (_) @name @extensionName +) @extension @_.domain + +(function_declaration + name: (_) @name @functionName +) @namedFunction @_.domain + +(subscript_declaration + name: (_) @name @subscriptName +) @subscript @_.domain + +(initializer_declaration + name: (_) @name @initializerName +) @initializer @_.domain + +(deinitializer_declaration + name: (_) @name @deinitializerName +) @deinitializer @_.domain + +(variable_declaration + name: (_) @name @variableName +) @variable @_.domain + +(constant_declaration + name: (_) @name @constantName +) @constant @_.domain + +(return_statement + value: (_) @value +) @return @_.domain + +(if_statement + condition: (_) @condition + consequence: (block) @consequence + alternative: (else_clause)? @alternative +) @ifStatement @_.domain + +(for_statement + variable: (_) @variable + iterable: (_) @iterable + body: (block) @body +) @forLoop @_.domain + +(while_statement + condition: (_) @condition + body: (block) @body +) @whileLoop @_.domain + +(repeat_while_statement + body: (block) @body + condition: (_) @condition +) @repeatWhileLoop @_.domain + +(switch_statement + condition: (_) @condition + cases: (switch_case)+ @cases +) @switchStatement @_.domain + +(do_statement + body: (block) @body +) @doStatement @_.domain + +(defer_statement + body: (block) @deferBody +) @deferStatement @_.domain + +(throw_statement + expression: (_) @throwExpression +) @throwStatement @_.domain + +(do_catch_statement + do_block: (block) @doBlock + catch_clauses: (catch_clause)+ @catchClauses +) @doCatchStatement @_.domain + +(variable_declaration + pattern: (_) @pattern + initializer: (_) @initializer +) @variableDeclaration @_.domain + +# Additional patterns and configurations can be added here as needed. + +operator: [ + "+" + "-" + "*" + "/" + "%" + "==" + "!=" + "<" + "<=" + ">" + ">=" + "&&" + "||" + "!" + "=" + "+=" + "-=" + "*=" + "/=" + "%=" + "++" + "--" + "->" + "?." + "!" + "??" +] @disqualifyDelimiter + +(lambda_expression + "in" @disqualifyDelimiter +) From 231064a5b558a973f5121c1a47f6644128c590a7 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 17:49:08 +0100 Subject: [PATCH 11/21] start small --- .../common/src/scopeSupportFacets/swift.ts | 50 +---- queries/swift.scm | 172 ------------------ 2 files changed, 5 insertions(+), 217 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index abe32c4a75..9078ae6646 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -4,50 +4,10 @@ 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 + "namedFunction.method": supported, + "namedFunction.method.iteration.class": supported, + "namedFunction.constructor": supported, + "namedFunction.iteration": supported, + "namedFunction.iteration.document": supported, }; \ No newline at end of file diff --git a/queries/swift.scm b/queries/swift.scm index f2a5938dd3..e69de29bb2 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -1,172 +0,0 @@ -[ - (class_declaration) - (struct_declaration) - (enum_declaration) - (protocol_declaration) - (extension_declaration) - (import_declaration) - (if_statement) - (guard_statement) - (for_statement) - (while_statement) - (repeat_while_statement) - (switch_statement) - (do_statement) - (defer_statement) - (return_statement) - (break_statement) - (continue_statement) - (fallthrough_statement) - (throw_statement) - (do_catch_statement) - (expression_statement) - (variable_declaration) - (constant_declaration) - (function_declaration) - (initializer_declaration) - (subscript_declaration) - (operator_declaration) - (deinitializer_declaration) - (typealias_declaration) - (associatedtype_declaration) - (import_declaration) - (access_level_modifier) - (member_access) - (identifier) - (identifier_pattern) - (identifier_expression) - (identifier_expression_pattern) - (identifier_reference) -] @statement - -(class_declaration - name: (_) @name @className -) @class @_.domain - -(struct_declaration - name: (_) @name @structName -) @struct @_.domain - -(enum_declaration - name: (_) @name @enumName -) @enum @_.domain - -(protocol_declaration - name: (_) @name @protocolName -) @protocol @_.domain - -(extension_declaration - name: (_) @name @extensionName -) @extension @_.domain - -(function_declaration - name: (_) @name @functionName -) @namedFunction @_.domain - -(subscript_declaration - name: (_) @name @subscriptName -) @subscript @_.domain - -(initializer_declaration - name: (_) @name @initializerName -) @initializer @_.domain - -(deinitializer_declaration - name: (_) @name @deinitializerName -) @deinitializer @_.domain - -(variable_declaration - name: (_) @name @variableName -) @variable @_.domain - -(constant_declaration - name: (_) @name @constantName -) @constant @_.domain - -(return_statement - value: (_) @value -) @return @_.domain - -(if_statement - condition: (_) @condition - consequence: (block) @consequence - alternative: (else_clause)? @alternative -) @ifStatement @_.domain - -(for_statement - variable: (_) @variable - iterable: (_) @iterable - body: (block) @body -) @forLoop @_.domain - -(while_statement - condition: (_) @condition - body: (block) @body -) @whileLoop @_.domain - -(repeat_while_statement - body: (block) @body - condition: (_) @condition -) @repeatWhileLoop @_.domain - -(switch_statement - condition: (_) @condition - cases: (switch_case)+ @cases -) @switchStatement @_.domain - -(do_statement - body: (block) @body -) @doStatement @_.domain - -(defer_statement - body: (block) @deferBody -) @deferStatement @_.domain - -(throw_statement - expression: (_) @throwExpression -) @throwStatement @_.domain - -(do_catch_statement - do_block: (block) @doBlock - catch_clauses: (catch_clause)+ @catchClauses -) @doCatchStatement @_.domain - -(variable_declaration - pattern: (_) @pattern - initializer: (_) @initializer -) @variableDeclaration @_.domain - -# Additional patterns and configurations can be added here as needed. - -operator: [ - "+" - "-" - "*" - "/" - "%" - "==" - "!=" - "<" - "<=" - ">" - ">=" - "&&" - "||" - "!" - "=" - "+=" - "-=" - "*=" - "/=" - "%=" - "++" - "--" - "->" - "?." - "!" - "??" -] @disqualifyDelimiter - -(lambda_expression - "in" @disqualifyDelimiter -) From 8a0ddd2d0b3027e35557c3f237f0bacc542edb53 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 18:10:41 +0100 Subject: [PATCH 12/21] make the function work --- .../scopes/swift/namedFunction.constructor.scope | 11 +++++++++++ .../scopes/swift/namedFunction.method.scope | 13 +++++++++++++ data/fixtures/scopes/swift/namedFunction.scope | 6 ++++++ queries/swift.scm | 3 +++ 4 files changed, 33 insertions(+) create mode 100644 data/fixtures/scopes/swift/namedFunction.constructor.scope create mode 100644 data/fixtures/scopes/swift/namedFunction.method.scope create mode 100644 data/fixtures/scopes/swift/namedFunction.scope diff --git a/data/fixtures/scopes/swift/namedFunction.constructor.scope b/data/fixtures/scopes/swift/namedFunction.constructor.scope new file mode 100644 index 0000000000..ba5eb8bd00 --- /dev/null +++ b/data/fixtures/scopes/swift/namedFunction.constructor.scope @@ -0,0 +1,11 @@ +class SurveyQuestion { + var text: String + var response: String? + init(text: String) { + self.text = text + } + func ask() { + print(text) + } +} +--- diff --git a/data/fixtures/scopes/swift/namedFunction.method.scope b/data/fixtures/scopes/swift/namedFunction.method.scope new file mode 100644 index 0000000000..d7e0360112 --- /dev/null +++ b/data/fixtures/scopes/swift/namedFunction.method.scope @@ -0,0 +1,13 @@ +class Counter { + var count = 0 + func increment() { + count += 1 + } + func increment(by amount: Int) { + count += amount + } + func reset() { + count = 0 + } +} +--- diff --git a/data/fixtures/scopes/swift/namedFunction.scope b/data/fixtures/scopes/swift/namedFunction.scope new file mode 100644 index 0000000000..797d574a4b --- /dev/null +++ b/data/fixtures/scopes/swift/namedFunction.scope @@ -0,0 +1,6 @@ +func greet(person: String) -> String { + let greeting = "Hello, " + person + "!" + return greeting +} + +--- diff --git a/queries/swift.scm b/queries/swift.scm index e69de29bb2..77c85890f6 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -0,0 +1,3 @@ +(function_declaration + name: (_) @functionName +) @namedFunction @functionName.domain \ No newline at end of file From f2d8d64e7ba2b6881f9c69e20147682fde758bc5 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 18:36:17 +0100 Subject: [PATCH 13/21] swift.scm by adding class and protocol declarations. --- queries/swift.scm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/queries/swift.scm b/queries/swift.scm index 77c85890f6..161aa7f927 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -1,3 +1,12 @@ (function_declaration name: (_) @functionName -) @namedFunction @functionName.domain \ No newline at end of file +) @namedFunction @functionName.domain + +(class_declaration + name: (_) @className +) @class @className.domain + +[ + (class_declaration) + (protocol_declaration) +] @namedFunction.iteration @functionName.iteration \ No newline at end of file From 31d4d2af0f9885d6e6010cc64e9ecf1ece344f0f Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 19:06:39 +0100 Subject: [PATCH 14/21] full support not-support list --- .../common/src/scopeSupportFacets/swift.ts | 177 +++++++++++++++++- 1 file changed, 176 insertions(+), 1 deletion(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 9078ae6646..46a1411fb1 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -4,10 +4,185 @@ import { ScopeSupportFacetLevel } from "./scopeSupportFacets.types"; const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel; export const swiftScopeSupport: LanguageScopeSupportFacetMap = { + // Command related + command: notApplicable, + + // XML/HTML related + element: notApplicable, + startTag: notApplicable, + endTag: notApplicable, + tags: notApplicable, + attribute: supported, // Swift has attributes like @available, @objc + environment: notApplicable, + + // Document structure + section: notApplicable, + + // Data structures and control flow + list: supported, + map: supported, + ifStatement: supported, + regularExpression: supported, + switchStatementSubject: supported, + fieldAccess: supported, + + // Statements and classes + statement: supported, + "statement.class": supported, + "statement.iteration.document": supported, + "statement.iteration.block": supported, + + class: supported, + "class.iteration.document": supported, + "class.iteration.block": supported, + className: supported, + "className.iteration.document": supported, + "className.iteration.block": supported, + + // Functions namedFunction: supported, "namedFunction.method": supported, "namedFunction.method.iteration.class": supported, - "namedFunction.constructor": supported, + "namedFunction.constructor": supported, // Swift has initializers "namedFunction.iteration": supported, "namedFunction.iteration.document": supported, + anonymousFunction: supported, // Swift has closures + functionName: supported, + "functionName.method": supported, + "functionName.method.iteration.class": supported, + "functionName.constructor": supported, + "functionName.iteration": supported, + "functionName.iteration.document": supported, + + // Function calls + functionCall: supported, + "functionCall.constructor": supported, + functionCallee: supported, + "functionCallee.constructor": supported, + + // Arguments + "argument.actual": supported, + "argument.actual.iteration": supported, + "argument.actual.method": supported, + "argument.actual.method.iteration": supported, + "argument.actual.constructor": supported, + "argument.actual.constructor.iteration": supported, + "argument.formal": supported, + "argument.formal.iteration": supported, + "argument.formal.method": supported, + "argument.formal.method.iteration": supported, + "argument.formal.constructor": supported, + "argument.formal.constructor.iteration": supported, + + // Comments + "comment.line": supported, + "comment.block": supported, + + // Strings + "string.singleLine": supported, + "string.multiLine": supported, + + // Text fragments + "textFragment.comment.line": supported, + "textFragment.comment.block": supported, + "textFragment.string.singleLine": supported, + "textFragment.string.multiLine": supported, + + // Delimiters + disqualifyDelimiter: supported, + pairDelimiter: supported, + + // Branches + "branch.if": supported, + "branch.loop": supported, + "branch.if.iteration": supported, + "branch.try": supported, + "branch.try.iteration": supported, + "branch.switchCase": supported, + "branch.switchCase.iteration": supported, + "branch.ternary": supported, + + // Collection items + "collectionItem.unenclosed": supported, + "collectionItem.unenclosed.iteration": supported, + + // Conditions + "condition.if": supported, + "condition.while": supported, + "condition.doWhile": unsupported, // Swift doesn't have do-while loops + "condition.for": supported, + "condition.ternary": supported, + "condition.switchCase": supported, + "condition.switchCase.iteration": supported, + + // Names + "name.assignment": supported, + "name.assignment.pattern": supported, + "name.variable": supported, + "name.variable.pattern": supported, + "name.foreach": supported, + "name.function": supported, + "name.method": supported, + "name.constructor": supported, + "name.class": supported, + "name.field": supported, + "name.resource": unsupported, // Swift doesn't have explicit resource management blocks + "name.resource.iteration": unsupported, + "name.argument.formal": supported, + "name.argument.formal.iteration": supported, + "name.argument.formal.method": supported, + "name.argument.formal.method.iteration": supported, + "name.argument.formal.constructor": supported, + "name.argument.formal.constructor.iteration": supported, + "name.iteration.block": supported, + "name.iteration.document": supported, + + // Keys + "key.attribute": supported, + "key.mapPair": supported, + "key.mapPair.iteration": supported, + + // Values + "value.assignment": supported, + "value.variable": supported, + "value.variable.pattern": supported, + "value.mapPair": supported, + "value.mapPair.iteration": supported, + "value.foreach": supported, + "value.attribute": supported, + "value.return": supported, + "value.return.lambda": supported, + "value.field": supported, + "value.yield": unsupported, // Swift doesn't have yield statements + "value.resource": unsupported, + "value.resource.iteration": unsupported, + "value.argument.formal": supported, + "value.argument.formal.iteration": supported, + "value.argument.formal.method": supported, + "value.argument.formal.method.iteration": supported, + "value.argument.formal.constructor": supported, + "value.argument.formal.constructor.iteration": supported, + "value.typeAlias": supported, + + // Types + "type.variable": supported, + "type.argument.formal": supported, + "type.argument.formal.iteration": supported, + "type.argument.formal.method": supported, + "type.argument.formal.method.iteration": supported, + "type.argument.formal.constructor": supported, + "type.argument.formal.constructor.iteration": supported, + "type.return": supported, + "type.field": supported, + "type.field.iteration": supported, + "type.foreach": supported, + "type.interface": supported, // Swift has protocols which are similar to interfaces + "type.class": supported, + "type.alias": supported, + "type.cast": supported, + "type.typeArgument": supported, + "type.typeArgument.iteration": supported, + + // Notebook + notebookCell: notApplicable }; \ No newline at end of file From dbda618ccf02989a342af5dc8e7a36e5986948c6 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 19:06:49 +0100 Subject: [PATCH 15/21] Enhance swift.scm by adding support for import, property, typealias, array, dictionary, and regex declarations --- queries/swift.scm | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/queries/swift.scm b/queries/swift.scm index 161aa7f927..36eda5e242 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -1,3 +1,9 @@ +[ + (import_declaration) + (property_declaration) + (typealias_declaration) +] @statement + (function_declaration name: (_) @functionName ) @namedFunction @functionName.domain @@ -9,4 +15,16 @@ [ (class_declaration) (protocol_declaration) -] @namedFunction.iteration @functionName.iteration \ No newline at end of file +] @namedFunction.iteration @functionName.iteration + +(array_literal) @list + +(dictionary_literal) @map + +(regex_literal) @regularExpression + +[ + (class_declaration) + (protocol_declaration) + (function_declaration) +] @statement.iteration From a568661237feb15ef39ee5f5ef7a2e0ab6ddc703 Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 19:38:21 +0100 Subject: [PATCH 16/21] Add support for ternary expressions and enhance comment handling in swift.scm --- queries/swift.scm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/queries/swift.scm b/queries/swift.scm index 36eda5e242..f140c0fc48 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -28,3 +28,22 @@ (protocol_declaration) (function_declaration) ] @statement.iteration + +(value_argument) @argumentOrParameter + +[ + (comment) + (multiline_comment) +] @comment @textFragment + +;;!! true ? 0 : 1; +;;! ^^^^ +;;! ^ ^ +;;! -------------- +(ternary_expression + condition: (_) @condition + if_true: (_) @branch +) @condition.domain +(ternary_expression + if_false: (_) @branch +) \ No newline at end of file From df4ac3daeed2682d0f24c6c98e1c34ff4db20781 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:41:13 +0000 Subject: [PATCH 17/21] [pre-commit.ci lite] apply automatic fixes --- packages/common/src/scopeSupportFacets/swift.ts | 4 ++-- queries/swift.scm | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/common/src/scopeSupportFacets/swift.ts b/packages/common/src/scopeSupportFacets/swift.ts index 46a1411fb1..68f0fab5ae 100644 --- a/packages/common/src/scopeSupportFacets/swift.ts +++ b/packages/common/src/scopeSupportFacets/swift.ts @@ -184,5 +184,5 @@ export const swiftScopeSupport: LanguageScopeSupportFacetMap = { "type.typeArgument.iteration": supported, // Notebook - notebookCell: notApplicable -}; \ No newline at end of file + notebookCell: notApplicable, +}; diff --git a/queries/swift.scm b/queries/swift.scm index f140c0fc48..eb1bc21088 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -46,4 +46,4 @@ ) @condition.domain (ternary_expression if_false: (_) @branch -) \ No newline at end of file +) From 53ec6d119f6cb09a8049904bd95f3b79c3595fab Mon Sep 17 00:00:00 2001 From: atacan Date: Sat, 18 Jan 2025 20:15:50 +0100 Subject: [PATCH 18/21] Add support for switch statements and enhance if/guard statement handling in swift.scm --- queries/swift.scm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/queries/swift.scm b/queries/swift.scm index eb1bc21088..1fa50d9f61 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -2,6 +2,7 @@ (import_declaration) (property_declaration) (typealias_declaration) + (switch_entry) ] @statement (function_declaration @@ -36,6 +37,18 @@ (multiline_comment) ] @comment @textFragment +[ + (if_statement) + (guard_statement) +] @ifStatement + +(switch_statement + expr: (_) @private.switchStatementSubject + (#child-range! @private.switchStatementSubject 0 -1 true true) +) @_.domain + +(switch_pattern) @condition + ;;!! true ? 0 : 1; ;;! ^^^^ ;;! ^ ^ From 1cc4244f7a7d3b203ea7c45505e2c29715fe1e42 Mon Sep 17 00:00:00 2001 From: atacan Date: Sun, 19 Jan 2025 11:47:20 +0100 Subject: [PATCH 19/21] wrong folder? --- .../scopes/swift/namedFunction.constructor.scope | 11 ----------- .../scopes/swift/namedFunction.method.scope | 13 ------------- data/fixtures/scopes/swift/namedFunction.scope | 6 ------ 3 files changed, 30 deletions(-) delete mode 100644 data/fixtures/scopes/swift/namedFunction.constructor.scope delete mode 100644 data/fixtures/scopes/swift/namedFunction.method.scope delete mode 100644 data/fixtures/scopes/swift/namedFunction.scope diff --git a/data/fixtures/scopes/swift/namedFunction.constructor.scope b/data/fixtures/scopes/swift/namedFunction.constructor.scope deleted file mode 100644 index ba5eb8bd00..0000000000 --- a/data/fixtures/scopes/swift/namedFunction.constructor.scope +++ /dev/null @@ -1,11 +0,0 @@ -class SurveyQuestion { - var text: String - var response: String? - init(text: String) { - self.text = text - } - func ask() { - print(text) - } -} ---- diff --git a/data/fixtures/scopes/swift/namedFunction.method.scope b/data/fixtures/scopes/swift/namedFunction.method.scope deleted file mode 100644 index d7e0360112..0000000000 --- a/data/fixtures/scopes/swift/namedFunction.method.scope +++ /dev/null @@ -1,13 +0,0 @@ -class Counter { - var count = 0 - func increment() { - count += 1 - } - func increment(by amount: Int) { - count += amount - } - func reset() { - count = 0 - } -} ---- diff --git a/data/fixtures/scopes/swift/namedFunction.scope b/data/fixtures/scopes/swift/namedFunction.scope deleted file mode 100644 index 797d574a4b..0000000000 --- a/data/fixtures/scopes/swift/namedFunction.scope +++ /dev/null @@ -1,6 +0,0 @@ -func greet(person: String) -> String { - let greeting = "Hello, " + person + "!" - return greeting -} - ---- From 93437b6f929b064ef9aab9330a24720614e0f7c7 Mon Sep 17 00:00:00 2001 From: atacan Date: Sun, 19 Jan 2025 13:08:24 +0100 Subject: [PATCH 20/21] swift playground --- data/playground/swift.swift | 175 ++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 data/playground/swift.swift diff --git a/data/playground/swift.swift b/data/playground/swift.swift new file mode 100644 index 0000000000..d794974dcc --- /dev/null +++ b/data/playground/swift.swift @@ -0,0 +1,175 @@ +// Source: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/ + +print("Hello, world!") +// Prints "Hello, world!" + +var myVariable = 42 +myVariable = 50 +let myConstant = 42 + +let apples = 3 +let oranges = 5 + +let quotation = """ + Even though there's whitespace to the left, + the actual lines aren't indented. + Except for this line. + Double quotes (") can appear without being escaped. + + I still have \(apples + oranges) pieces of fruit. + """ + +var fruits = ["strawberries", "limes", "tangerines"] +fruits[1] = "grapes" + +var occupations = [ + "Malcolm": "Captain", + "Kaylee": "Mechanic", +] +occupations["Jayne"] = "Public Relations" + +let individualScores = [75, 43, 103, 87, 12] +var teamScore = 0 +for score in individualScores { + if score > 50 { + teamScore += 3 + } + else { + teamScore += 1 + } +} + +let vegetable = "red pepper" +switch vegetable { +case "celery": + print("Add some raisins and make ants on a log.") +case "cucumber", "watercress": + print("That would make a good tea sandwich.") +case let x where x.hasSuffix("pepper"): + print("Is it a spicy \(x)?") +default: + print("Everything tastes good in soup.") +} +// Prints "Is it a spicy red pepper?" + +@discardableResult +func greet(person: String, day: String) -> String { + "Hello \(person), today is \(day)." +} +greet(person: "Bob", day: "Tuesday") + +func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { + var min = scores[0] + var max = scores[0] + var sum = 0 + + for score in scores { + if score > max { + max = score + } + else if score < min { + min = score + } + sum += score + } + + return (min, max, sum) +} +let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9]) +print(statistics.sum) + +class NamedShape { + var numberOfSides: Int = 0 + var name: String + + init(name: String) { + self.name = name + } + + func simpleDescription() -> String { + "A shape with \(numberOfSides) sides." + } +} + +class Square: NamedShape { + var sideLength: Double + + init(sideLength: Double, name: String) { + self.sideLength = sideLength + super.init(name: name) + numberOfSides = 4 + } + + func area() -> Double { + sideLength * sideLength + } + + override func simpleDescription() -> String { + "A square with sides of length \(sideLength)." + } +} + +let test = Square(sideLength: 5.2, name: "my test square") +_ = test.area() +_ = test.simpleDescription() + +enum Rank: Int { + case ace = 1 + case two, three, four, five, six, seven, eight, nine, ten + case jack, queen, king + + func simpleDescription() -> String { + switch self { + case .ace: + return "ace" + case .jack: + return "jack" + case .queen: + return "queen" + case .king: + return "king" + default: + return String(self.rawValue) + } + } +} +let ace = Rank.ace + +func fetchUserID(from server: String) async -> Int { + if server == "primary" { + return 97 + } + return 501 +} + +Task { + await fetchUserID(from: "primary") +} + +protocol ExampleProtocol { + var simpleDescription: String { get } + mutating func adjust() +} + +var fridgeIsOpen = false +let fridgeContent = ["milk", "eggs", "leftovers"] + +func fridgeContains(_ food: String) -> Bool { + fridgeIsOpen = true + defer { + fridgeIsOpen = false + } + + let result = fridgeContent.contains(food) + return result +} + +if fridgeContains("banana") { + print("Found a banana") +} +print(fridgeIsOpen) +// Prints "false" + +let contentHeight = 40 +let hasHeader = true +let rowHeight = contentHeight + (hasHeader ? 50 : 20) From 5a7de2e5b9945b8e9015eec48aad1bcdab38bfdc Mon Sep 17 00:00:00 2001 From: atacan Date: Sun, 19 Jan 2025 13:08:50 +0100 Subject: [PATCH 21/21] Add support for protocol declarations and enhance argument handling in swift.scm --- queries/swift.scm | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/queries/swift.scm b/queries/swift.scm index 1fa50d9f61..69a201dde7 100644 --- a/queries/swift.scm +++ b/queries/swift.scm @@ -13,6 +13,10 @@ name: (_) @className ) @class @className.domain +(protocol_declaration + name: (_) @className +) @class @className.domain + [ (class_declaration) (protocol_declaration) @@ -30,13 +34,57 @@ (function_declaration) ] @statement.iteration -(value_argument) @argumentOrParameter + +( + (value_arguments + (_)? @_.leading.endOf + . + (_) @argumentOrParameter + . + (_)? @_.trailing.startOf + ) @_dummy + (#not-type? @argumentOrParameter "comment") + (#single-or-multi-line-delimiter! @argumentOrParameter @_dummy ", " ",\n") +) + +( + (type_arguments + (_)? @_.leading.endOf + . + (_) @argumentOrParameter + . + (_)? @_.trailing.startOf + ) @_dummy + (#not-type? @argumentOrParameter "comment") + (#single-or-multi-line-delimiter! @argumentOrParameter @_dummy ", " ",\n") +) + +(value_arguments + "(" @argumentOrParameter.iteration.start.endOf + ")" @argumentOrParameter.iteration.end.startOf +) @argumentOrParameter.iteration.domain + +(type_arguments + "<" @argumentOrParameter.iteration.start.endOf + ">" @argumentOrParameter.iteration.end.startOf +) @argumentOrParameter.iteration.domain [ (comment) (multiline_comment) ] @comment @textFragment +[ + (line_string_literal) + (multi_line_string_literal) + (raw_string_literal) +] @string @textFragment + +;; (line_string_literal +;; "\"" @textFragment.start.endOf +;; "\"" @textFragment.end.startOf +;; ) @string + [ (if_statement) (guard_statement)