Skip to content
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

Add Swift programming language #2734

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Conversation

atacan
Copy link

@atacan atacan commented Jan 18, 2025

Checklist

- Introduced `swiftScopeSupport` to manage language-specific scope features.
- Updated `languageScopeSupport` to include Swift support.
- Created a new file for Swift scope support definitions.
- 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.
- 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.
…tter 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
@atacan atacan requested a review from a team as a code owner January 18, 2025 15:41
@atacan
Copy link
Author

atacan commented Jan 18, 2025

Currently I see the following error when I look at the console. I couldn't figure it out yet why Swift cannot be found in the language definitions.

{
  "type": "primitive",
  "mark": {
    "type": "cursor"
  },
  "modifiers": [
    {
      "type": "containingScope",
      "scopeType": {
        "type": "statement"
      }
    }
  ]
}
extensionHostProcess.js:163
Error: Expected language definition entry is missing for languageId swift
    at _LanguageDefinitionsImpl.get (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/languages/LanguageDefinitions.ts:157:13)
    at ScopeHandlerFactoryImpl.maybeCreate (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts:127:12)
    at ContainingScopeStage.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts:37:51)
    at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:264:49
    at Array.flatMap (<anonymous>)
    at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:264:23
    at Array.forEach (<anonymous>)
    at processModifierStages (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:263:18)
    at TargetPipeline.processPrimitiveTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:241:12)
    at TargetPipeline.processTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:99:21)
    at TargetPipeline.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:86:29)
    at TargetPipelineRunner.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:59:7)
    at CommandRunnerImpl.getTargets (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:236:32)
    at CommandRunnerImpl.runAction (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:224:32)
    at CommandRunnerImpl.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:80:20)
    at runCommand (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/runCommand.ts:74:20)
    at runCommandWrapper (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/src/registerCommands.ts:43:14)
    at cw.h (file:///Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:115:32825)
extensionHostProcess.js:163

@AndreasArvidsson
Copy link
Member

AndreasArvidsson commented Jan 18, 2025

Currently I see the following error when I look at the console. I couldn't figure it out yet why Swift cannot be found in the language definitions.

{
  "type": "primitive",
  "mark": {
    "type": "cursor"
  },
  "modifiers": [
    {
      "type": "containingScope",
      "scopeType": {
        "type": "statement"
      }
    }
  ]
}
extensionHostProcess.js:163
Error: Expected language definition entry is missing for languageId swift
    at _LanguageDefinitionsImpl.get (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/languages/LanguageDefinitions.ts:157:13)
    at ScopeHandlerFactoryImpl.maybeCreate (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/ScopeHandlerFactoryImpl.ts:127:12)
    at ContainingScopeStage.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts:37:51)
    at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:264:49
    at Array.flatMap (<anonymous>)
    at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:264:23
    at Array.forEach (<anonymous>)
    at processModifierStages (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:263:18)
    at TargetPipeline.processPrimitiveTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:241:12)
    at TargetPipeline.processTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:99:21)
    at TargetPipeline.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:86:29)
    at TargetPipelineRunner.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/processTargets/TargetPipelineRunner.ts:59:7)
    at CommandRunnerImpl.getTargets (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:236:32)
    at CommandRunnerImpl.runAction (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:224:32)
    at CommandRunnerImpl.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/core/commandRunner/CommandRunnerImpl.ts:80:20)
    at runCommand (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-engine/src/runCommand.ts:74:20)
    at runCommandWrapper (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/src/registerCommands.ts:43:14)
    at cw.h (file:///Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:115:32825)
extensionHostProcess.js:163

It looks like load language has never run.

public async loadLanguage(languageId: string): Promise<void> {

Or this code ran, but it was an error. Whenever the cursorless extension starts up have a look at the log.

@atacan
Copy link
Author

atacan commented Jan 18, 2025

below is what I see in the first start

Activating tree-sitter...
extensionHostProcess.js:163
Creating communication dir /var/folders/_z/hg3p0pxs2fn7z1985xlhlt5c0000gn/T/vscode-command-server-501
extensionHostProcess.js:163
taking snapshot

when I say "take state" I see the following error. the error code is the same but I think the files mentioned are different

{
  "version": 7,
  "spokenForm": "take state",
  "usePrePhraseSnapshot": true,
  "action": {
    "name": "setSelection",
    "target": {
      "type": "primitive",
      "modifiers": [
        {
          "type": "containingScope",
          "scopeType": {
            "type": "statement"
          }
        }
      ]
    }
  }
}
extensionHostProcess.js:163
taking snapshot
extensionHostProcess.js:163
Full target:
extensionHostProcess.js:163
{
  "type": "primitive",
  "mark": {
    "type": "cursor"
  },
  "modifiers": [
    {
      "type": "containingScope",
      "scopeType": {
        "type": "statement"
      }
    }
  ]
}
extensionHostProcess.js:163
Error: Expected language definition entry is missing for languageId swift
	at _LanguageDefinitionsImpl.get (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:33609:13)
	at ScopeHandlerFactoryImpl.maybeCreate (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:28502:41)
	at ContainingScopeStage.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:33880:51)
	at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34184:49
	at Array.flatMap (<anonymous>)
	at /Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34184:23
	at Array.forEach (<anonymous>)
	at processModifierStages (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34183:18)
	at TargetPipeline.processPrimitiveTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34176:12)
	at TargetPipeline.processTarget (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34074:21)
	at TargetPipeline.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34062:29)
	at TargetPipelineRunner.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:34037:7)
	at CommandRunnerImpl.getTargets (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:40666:32)
	at CommandRunnerImpl.runAction (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:40658:32)
	at CommandRunnerImpl.run (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:40544:20)
	at async runCommand (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:40960:20)
	at async runCommandWrapper (/Users/atacan/Developer/Repositories/cursorless-dev-cursorless/packages/cursorless-vscode/dist/extension.cjs:59429:14)
	at async cw.h (file:///Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:115:32825)
extensionHostProcess.js:163

@AndreasArvidsson
Copy link
Member

When I run this code I get the following:
image

@AndreasArvidsson
Copy link
Member

AndreasArvidsson commented Jan 18, 2025

Also you can't just make up your own scopes for the capture names. Cursorless has no idea what to do with @struct, @enum etc.

@atacan
Copy link
Author

atacan commented Jan 18, 2025

When I run this code I get the following: image

interesting I don't see the red text

@AndreasArvidsson
Copy link
Member

When I run this code I get the following: image

interesting I don't see the red text

Restart. Clean and recompile.

@atacan
Copy link
Author

atacan commented Jan 18, 2025

okay, thank you very much I followed the advice to start small, and made function declaration work

@AndreasArvidsson
Copy link
Member

Glad to hear it!

@pokey
Copy link
Member

pokey commented Jan 18, 2025

You'll want to re-run step 6 now that you have things working, as the tests are currently actually checking to ensure things are still broken, so they'll fail in CI now

@atacan
Copy link
Author

atacan commented Jan 18, 2025

You'll want to re-run step 6 now that you have things working, as the tests are currently actually checking to ensure things are still broken, so they'll fail in CI now

In that step, I say the command "debug edit subset" Then, I get the "testSubsetGrep.properties does not exist on this computer" error because, in the repository, I don't see such a file: cursorless/packages/test-harness/testSubsetGrep.properties'

@pokey
Copy link
Member

pokey commented Jan 18, 2025

oh you might need to say "debug generate subset" the first time sorry; worth a quick docs PR saying you need to do that the first time if that's the case

@atacan
Copy link
Author

atacan commented Jan 19, 2025

I generated that file. Below is the content of it. But strangely, now when I say "cursorless record scope" I don't see 'swift' in the opened dropdown. But when I run the extension the scopes for Swift works and I don't see any error message.

Also, I assumed "Update fixtures subset" meant to be "VSCode: Update test fixtures (subset)" in the launch.json

# This file contains the grep strings to pass to Mocha when running a subset of tests.
# These grep strings will be used with the "Run test subset" launch configuration.
# See https://mochajs.org/#-grep-regexp-g-regexp for supported syntax.
#
# One regular expression per line.
# Tests matching any of the regular expressions will be run.

languages/swift/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants