-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Snippet wrap #296
Snippet wrap #296
Conversation
@AndreasArvidsson looks like this is failing because the snippet inserts |
package.json
Outdated
@@ -400,9 +400,100 @@ | |||
"verticalOffset": 0 | |||
} | |||
} | |||
}, | |||
"cursorless.wrapperSnippetPreferences": { | |||
"description": "Allows snippets to signal what their default scope types etcetera should be", |
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.
Down the road we may allow signalling things like selectionType
, etc, so I figured let's have this be a map
package.json
Outdated
"properties": { | ||
"scopeType": { | ||
"type": "string", | ||
"enum": [ |
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.
This enum will get stale, but I don't think it will cause any serious problems, and the autocomplete will be nice
snippets/cpp.json
Outdated
@@ -0,0 +1,26 @@ | |||
{ |
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.
Decided to duplicate these snippets between languages as I'd imagine they'll start to diverge. but easy to just use one file as well
@@ -31,7 +31,7 @@ interface MarkEntry { | |||
} | |||
|
|||
class BringMoveSwap implements Action { | |||
targetPreferences: ActionPreferences[] = [ | |||
getTargetPreferences: () => ActionPreferences[] = () => [ |
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.
We now have a function to give action a chance to see args
src/actions/WrapWithSnippet.ts
Outdated
|
||
export default class WrapWithSnippet implements Action { | ||
getTargetPreferences( | ||
partialTargets: PartialTarget[], |
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.
We have this arg here so that below FIXME can be implemented in the future. Mixed feelings about letting action see this thing but not sure how else to let it do something like figure out which editor a target is referring to
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.
- pass in context object with callbacks instead
@@ -0,0 +1,47 @@ | |||
spokenForm: try catch wrap this |
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.
Test for multiple cursors
@@ -59,6 +60,12 @@ async function runTest(file: string) { | |||
}); | |||
const editor = await vscode.window.showTextDocument(document); | |||
|
|||
if (!fixture.initialState.documentContents.includes("\n")) { |
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.
This seemed reasonable to me and doesn't break your line ending tests
); | ||
if (process.env.CURSORLESS_TEST_UPDATE_FIXTURES == "true") { | ||
const outputFixture = { ...fixture, finalState: resultState, returnValue }; | ||
await fsp.writeFile(file, serialize(outputFixture)); |
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.
Easy way to update snapshots
@@ -36,6 +36,25 @@ | |||
"${workspaceFolder}/out/test/**/*.js" | |||
], | |||
"preLaunchTask": "${defaultBuildTask}" | |||
}, | |||
{ | |||
"name": "Update fixtures", |
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.
Easy way to update snapshots
a0d5e21
to
b492207
Compare
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.
- remove prefixes so just
ifStatement
- double-check that can say "if wrap token air"
package.json
Outdated
@@ -400,9 +400,100 @@ | |||
"verticalOffset": 0 | |||
} | |||
} | |||
}, | |||
"cursorless.wrapperSnippetPreferences": { |
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.
- have a
cursorless.snippets
setting with just adefaultScopeType
property.
@@ -7,7 +7,7 @@ import { | |||
} from "../typings/Types"; | |||
|
|||
export class Sort implements Action { | |||
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }]; | |||
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }]; |
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.
- see if i can get rid of type here
src/actions/WrapWithSnippet.ts
Outdated
|
||
export default class WrapWithSnippet implements Action { | ||
getTargetPreferences( | ||
partialTargets: PartialTarget[], |
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.
- pass in context object with callbacks instead
package.json
Outdated
"ifElseStatement": { | ||
"definitions": [ | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"typescript", | ||
"typescriptreact", | ||
"javascript", | ||
"javascriptreact", | ||
"cpp", | ||
"c", | ||
"java", | ||
"csharp" | ||
] | ||
}, | ||
"body": [ | ||
"if ($condition) {\n\t$consequence\n} else {\n\t$alternative\n}" | ||
] | ||
}, | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"python" | ||
] | ||
}, | ||
"body": [ | ||
"if $condition:\n\t$consequence\nelse:\n\t$alternative" | ||
] | ||
} | ||
], | ||
"defaultScopeTypes": { | ||
"consequence": "statement", | ||
"alternative": "statement" | ||
}, | ||
"description": "If else statement" | ||
} |
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.
@AndreasArvidsson wdyt? See how we have a single snippet for if / else?
package.json
Outdated
"type": "string" | ||
} | ||
}, | ||
"scopeType": { |
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.
This scopeType
isn't yet supported, but will allow us to differentiate "snip funk" in a class vs top-level in the future, as discussed
package.json
Outdated
@@ -400,6 +400,216 @@ | |||
"verticalOffset": 0 | |||
} | |||
} | |||
}, | |||
"cursorless.experimental.snippets": { |
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.
Tagged the setting as experimental
package.json
Outdated
"type": "object", | ||
"default": { | ||
"ifStatement": { | ||
"definitions": [], |
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.
We put the definitions elsewhere so that the user can easily put their own, which take precedence over built-in ones
@@ -115,15 +115,18 @@ function inferPrimitiveTarget( | |||
"contents"; | |||
|
|||
const selectionType = | |||
maybeSelectionType ?? actionPreferences.selectionType ?? "token"; | |||
maybeSelectionType ?? | |||
(target.modifier == null ? actionPreferences.selectionType : null) ?? |
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.
Basically a hack until we merge scopeType and selectionType using pipeline support
@@ -0,0 +1,436 @@ | |||
/*--------------------------------------------------------------------------------------------- |
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.
We vendor in vscode snippet parser. I don't feel great about it. But I think it should be ok as snippet syntax shouldn't change too fast
], | ||
"jsonValidation": [ | ||
{ | ||
"fileMatch": "*.cursorless-snippets", |
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.
This extension is a bit verbose; open to suggestions. Need some way to indicate that the file should be checked by the schema on the next line so that user gets autocomplete
@@ -432,12 +450,12 @@ | |||
"js-yaml": "^4.1.0", | |||
"mocha": "^8.1.3", | |||
"sinon": "^11.1.1", | |||
"typescript": "^4.1.2", | |||
"typescript": "^4.4.4", |
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.
Note the typescript version bump
@@ -280,6 +285,9 @@ export async function activate(context: vscode.ExtensionContext) { | |||
thatMark, | |||
sourceMark, | |||
addDecorations, | |||
experimental: { | |||
registerThirdPartySnippets: graph.snippets.registerThirdPartySnippets, |
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.
Seemed super easy to support third-party snippets so I just decided implement it
this.registerThirdPartySnippets = | ||
this.registerThirdPartySnippets.bind(this); | ||
|
||
const timer = setInterval( |
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.
Note that we just poll the user snippet dir, but we check modification times and don't do anything if nothing has changed
@@ -0,0 +1,15 @@ | |||
import Actions from "../actions"; |
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.
We use factories now so that we can add the extension context, which is not a class
Closes #21
cursorless.wrappers.scopeTypes
for settingsthat
mark