Skip to content

Commit cfa46fc

Browse files
committed
Attempt at complex version
1 parent 180f374 commit cfa46fc

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,28 @@
400400
"verticalOffset": 0
401401
}
402402
}
403+
},
404+
"cursorless.wrapperSnippets": {
405+
"description": "Snippets to use with the wrap with snippet action",
406+
"type": "object",
407+
"scope": "language-overridable",
408+
"additionalProperties": {
409+
"type": "object",
410+
"properties": {
411+
"snippet": {
412+
"type": "string"
413+
},
414+
"name": {
415+
"type": "string"
416+
},
417+
"langId": {
418+
"type": "string"
419+
},
420+
"defaultScopeType": {
421+
"type": "string"
422+
}
423+
}
424+
}
403425
}
404426
}
405427
}

src/actions/WrapWithSnippet.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SnippetString } from "vscode";
1+
import { commands, SnippetString, workspace } from "vscode";
22
import { snippets } from "../languages";
33
import {
44
Action,
@@ -11,6 +11,15 @@ import {
1111
import displayPendingEditDecorations from "../util/editDisplayUtils";
1212
import { ensureSingleEditor } from "../util/targetUtils";
1313

14+
interface UserLanguageSnippet {
15+
snippet?: string;
16+
name?: string;
17+
langId?: string;
18+
defaultScopeType?: string;
19+
}
20+
21+
type UserLanguageSnippetMap = Record<string, UserLanguageSnippet>;
22+
1423
export default class WrapWithSnippet implements Action {
1524
targetPreferences: ActionPreferences[] = [
1625
{
@@ -33,29 +42,40 @@ export default class WrapWithSnippet implements Action {
3342
): Promise<ActionReturnValue> {
3443
const editor = ensureSingleEditor(targets);
3544

36-
const languageId = editor.document.languageId;
37-
const languageSnippets = snippets[languageId];
38-
if (languageSnippets == null) {
39-
throw new Error(`Snippets not supported for language ${languageId}`);
40-
}
45+
await this.graph.actions.setSelection.run([targets]);
4146

42-
const snippetString = languageSnippets[snippetName];
43-
if (snippetString == null) {
44-
throw new Error(
45-
`Snippet ${snippetName} not supported for language ${languageId}`
46-
);
47-
}
47+
const languageId = editor.document.languageId;
48+
const snippet = this.getSnippet(languageId, snippetName);
4849

4950
await displayPendingEditDecorations(
5051
targets,
5152
this.graph.editStyles.pendingModification0
5253
);
5354

54-
await editor.insertSnippet(
55-
snippetString,
56-
targets.map((target) => target.selection.selection)
57-
);
55+
if (snippet.snippet != null) {
56+
await editor.insertSnippet(snippet.snippet);
57+
} else {
58+
await commands.executeCommand("editor.action.codeAction", {
59+
kind: "refactor.extract.constant",
60+
preferred: true,
61+
});
62+
}
5863

5964
return {};
6065
}
66+
67+
private getSnippet(languageId: string, snippetName: string) {
68+
const languageSnippets = snippets[languageId];
69+
70+
const userLanguageSnippets = workspace
71+
.getConfiguration("cursorless")
72+
.get<UserLanguageSnippetMap>(`wrapperSnippets`);
73+
const snippetString = languageSnippets[snippetName];
74+
if (snippetString == null) {
75+
throw new Error(
76+
`Snippet ${snippetName} not supported for language ${languageId}`
77+
);
78+
}
79+
return snippetString;
80+
}
6181
}

0 commit comments

Comments
 (0)