-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
atacan
wants to merge
22
commits into
cursorless-dev:main
Choose a base branch
from
atacan:add-swift
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c706436
initial edits for Swift language support
atacan c3a0c3c
additional scope features
atacan 37c03d1
replace `collectionKey` with `key.mapPair`
atacan e01350e
remove the Swift-specific type scopes since they're not in the standa…
atacan 1110787
ensure proper alignment between the scope support map and the tree-si…
atacan 3a54328
wip
atacan e9323fe
swift MDX
atacan 3f4506c
deleted
atacan 1655be7
start from scratch
atacan e6b91c8
Add Swift grammar rules and declarations to swift.scm
atacan 231064a
start small
atacan 8a0ddd2
make the function work
atacan f2d8d64
swift.scm by adding class and protocol declarations.
atacan 31d4d2a
full support not-support list
atacan dbda618
Enhance swift.scm by adding support for import, property, typealias, …
atacan a568661
Add support for ternary expressions and enhance comment handling in s…
atacan df4ac3d
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 53ec6d1
Add support for switch statements and enhance if/guard statement hand…
atacan 1cc4244
wrong folder?
atacan 93437b6
swift playground
atacan 5a7de2e
Add support for protocol declarations and enhance argument handling i…
atacan 4e7087c
Merge branch 'main' into add-swift
AndreasArvidsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import type { LanguageScopeSupportFacetMap } from "./scopeSupportFacets.types"; | ||
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, // 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, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/cursorless-org-docs/src/docs/user/languages/swift.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Language from "./Language"; | ||
|
||
# Swift | ||
|
||
<Language languageId="swift"></Language> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to group the facets after supported/unsupported/notApplicable and in that order.
See javascript for example
cursorless/packages/common/src/scopeSupportFacets/javascript.ts
Line 7 in e5e9929