-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathWrapWithSnippet.ts
129 lines (106 loc) · 3.86 KB
/
WrapWithSnippet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { FlashStyle, ScopeType, WrapWithSnippetArg } from "@cursorless/common";
import { Snippets } from "../core/Snippets";
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
import { callFunctionAndUpdateSelections } from "../core/updateSelections/updateSelections";
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
import { ModifyIfUntypedStage } from "../processTargets/modifiers/ConditionalModifierStages";
import { ide } from "../singletons/ide.singleton";
import {
findMatchingSnippetDefinitionStrict,
transformSnippetVariables,
} from "../snippets/snippet";
import { SnippetParser } from "../snippets/vendor/vscodeSnippet/snippetParser";
import { Target } from "../typings/target.types";
import { ensureSingleEditor, flashTargets } from "../util/targetUtils";
import { ActionReturnValue } from "./actions.types";
export default class WrapWithSnippet {
private snippetParser = new SnippetParser();
constructor(
private rangeUpdater: RangeUpdater,
private snippets: Snippets | undefined,
private modifierStageFactory: ModifierStageFactory,
) {
this.run = this.run.bind(this);
}
getFinalStages(snippet: WrapWithSnippetArg) {
const defaultScopeType = this.getScopeType(snippet);
if (defaultScopeType == null) {
return [];
}
return [
new ModifyIfUntypedStage(this.modifierStageFactory, {
type: "modifyIfUntyped",
modifier: {
type: "containingScope",
scopeType: defaultScopeType,
},
}),
];
}
private getScopeType(
snippetDescription: WrapWithSnippetArg,
): ScopeType | undefined {
if (snippetDescription.type === "named") {
if (this.snippets == null) {
return undefined;
}
const { name, variableName } = snippetDescription;
const snippet = this.snippets.getSnippetStrict(name);
const variables = snippet.variables ?? {};
const scopeTypeType = variables[variableName]?.wrapperScopeType;
return scopeTypeType == null
? undefined
: {
type: scopeTypeType,
};
} else {
return snippetDescription.scopeType;
}
}
private getBody(
snippetDescription: WrapWithSnippetArg,
targets: Target[],
): string {
if (snippetDescription.type === "named") {
if (this.snippets == null) {
throw Error("Named snippets are not enabled");
}
const { name } = snippetDescription;
const snippet = this.snippets.getSnippetStrict(name);
const definition = findMatchingSnippetDefinitionStrict(
this.modifierStageFactory,
targets,
snippet.definitions,
);
return definition.body.join("\n");
} else {
return snippetDescription.body;
}
}
async run(
targets: Target[],
snippetDescription: WrapWithSnippetArg,
): Promise<ActionReturnValue> {
const editor = ide().getEditableTextEditor(ensureSingleEditor(targets));
const body = this.getBody(snippetDescription, targets);
const parsedSnippet = this.snippetParser.parse(body);
transformSnippetVariables(parsedSnippet, snippetDescription.variableName);
const snippetString = parsedSnippet.toTextmateString();
await flashTargets(ide(), targets, FlashStyle.pendingModification0);
const targetSelections = targets.map((target) => target.contentSelection);
// NB: We used the command "editor.action.insertSnippet" instead of calling editor.insertSnippet
// because the latter doesn't support special variables like CLIPBOARD
const [updatedTargetSelections] = await callFunctionAndUpdateSelections(
this.rangeUpdater,
() => editor.insertSnippet(snippetString, targetSelections),
editor.document,
[targetSelections],
);
return {
thatSelections: updatedTargetSelections.map((selection) => ({
editor,
selection,
})),
};
}
}