Skip to content

Commit be953a5

Browse files
Support legacy snippets (#2837)
Still support Cursorless (legacy) snippets extension side
1 parent f309411 commit be953a5

File tree

16 files changed

+1083
-11
lines changed

16 files changed

+1083
-11
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
languageId: typescript
2+
command:
3+
version: 6
4+
spokenForm: snippet funk before class
5+
action:
6+
name: insertSnippet
7+
snippetDescription: {type: named, name: functionDeclaration}
8+
destination:
9+
type: primitive
10+
insertionMode: before
11+
target:
12+
type: primitive
13+
modifiers:
14+
- type: containingScope
15+
scopeType: {type: class}
16+
usePrePhraseSnapshot: true
17+
spokenFormError: named insertion snippet
18+
initialState:
19+
documentContents: |-
20+
class Aaa {
21+
22+
}
23+
selections:
24+
- anchor: {line: 1, character: 4}
25+
active: {line: 1, character: 4}
26+
marks: {}
27+
finalState:
28+
documentContents: |-
29+
function () {
30+
31+
}
32+
33+
class Aaa {
34+
35+
}
36+
selections:
37+
- anchor: {line: 0, character: 9}
38+
active: {line: 0, character: 9}
39+
thatMark:
40+
- type: UntypedTarget
41+
contentRange:
42+
start: {line: 0, character: 0}
43+
end: {line: 2, character: 1}
44+
isReversed: false
45+
hasExplicitRange: true

data/fixtures/recorded/actions/snippets/tryWrapThis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ finalState:
3030
selections:
3131
- anchor: {line: 3, character: 4}
3232
active: {line: 3, character: 4}
33+
thatMark:
34+
- type: UntypedTarget
35+
contentRange:
36+
start: {line: 0, character: 0}
37+
end: {line: 4, character: 1}
38+
isReversed: false
39+
hasExplicitRange: true
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
languageId: typescript
2+
command:
3+
version: 6
4+
spokenForm: try wrap this
5+
action:
6+
name: wrapWithSnippet
7+
snippetDescription: {type: named, name: tryCatchStatement, variableName: body}
8+
target:
9+
type: primitive
10+
mark: {type: cursor}
11+
usePrePhraseSnapshot: true
12+
spokenFormError: named wrap with snippet
13+
initialState:
14+
documentContents: const foo = "bar";
15+
selections:
16+
- anchor: {line: 0, character: 0}
17+
active: {line: 0, character: 0}
18+
marks: {}
19+
finalState:
20+
documentContents: |-
21+
try {
22+
const foo = "bar";
23+
} catch (err) {
24+
25+
}
26+
selections:
27+
- anchor: {line: 3, character: 4}
28+
active: {line: 3, character: 4}
29+
thatMark:
30+
- type: UntypedTarget
31+
contentRange:
32+
start: {line: 0, character: 0}
33+
end: {line: 4, character: 1}
34+
isReversed: false
35+
hasExplicitRange: true

packages/common/src/types/command/ActionDescriptor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export interface GenerateSnippetActionDescriptor {
145145
target: PartialTargetDescriptor;
146146
}
147147

148-
interface NamedInsertSnippetArg {
148+
export interface NamedInsertSnippetArg {
149149
type: "named";
150150
name: string;
151151
substitutions?: Record<string, string>;
@@ -176,7 +176,7 @@ export interface InsertSnippetActionDescriptor {
176176
destination: DestinationDescriptor;
177177
}
178178

179-
interface NamedWrapWithSnippetArg {
179+
export interface NamedWrapWithSnippetArg {
180180
type: "named";
181181
name: string;
182182
variableName: string;

packages/cursorless-engine/src/actions/InsertSnippet.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getPreferredSnippet } from "../core/getPreferredSnippet";
55
import type { RangeUpdater } from "../core/updateSelections/RangeUpdater";
66
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
77
import type { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
8+
import type { ModifierStage } from "../processTargets/PipelineStages.types";
89
import { ModifyIfUntypedExplicitStage } from "../processTargets/modifiers/ConditionalModifierStages";
910
import { ide } from "../singletons/ide.singleton";
1011
import { transformSnippetVariables } from "../snippets/transformSnippetVariables";
@@ -13,6 +14,7 @@ import type { Destination } from "../typings/target.types";
1314
import { ensureSingleEditor } from "../util/targetUtils";
1415
import type { Actions } from "./Actions";
1516
import type { ActionReturnValue } from "./actions.types";
17+
import InsertSnippetLegacy from "./snippetsLegacy/InsertSnippetLegacy";
1618

1719
export default class InsertSnippet {
1820
private snippetParser = new SnippetParser();
@@ -29,7 +31,11 @@ export default class InsertSnippet {
2931
getFinalStages(
3032
destinations: Destination[],
3133
snippetDescription: InsertSnippetArg,
32-
) {
34+
): ModifierStage[] {
35+
if (snippetDescription.type === "named") {
36+
return this.legacy().getFinalStages(snippetDescription);
37+
}
38+
3339
const editor = ensureSingleEditor(destinations);
3440
const snippet = getPreferredSnippet(
3541
snippetDescription,
@@ -53,6 +59,10 @@ export default class InsertSnippet {
5359
destinations: Destination[],
5460
snippetDescription: InsertSnippetArg,
5561
): Promise<ActionReturnValue> {
62+
if (snippetDescription.type === "named") {
63+
return this.legacy().run(destinations, snippetDescription);
64+
}
65+
5666
const editor = ide().getEditableTextEditor(
5767
ensureSingleEditor(destinations),
5868
);
@@ -93,4 +103,14 @@ export default class InsertSnippet {
93103
})),
94104
};
95105
}
106+
107+
// DEPRECATED @ 2025-02-01
108+
private legacy() {
109+
return new InsertSnippetLegacy(
110+
this.rangeUpdater,
111+
this.snippets,
112+
this.actions,
113+
this.modifierStageFactory,
114+
);
115+
}
96116
}

packages/cursorless-engine/src/actions/WrapWithSnippet.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import { getPreferredSnippet } from "../core/getPreferredSnippet";
55
import type { RangeUpdater } from "../core/updateSelections/RangeUpdater";
66
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
77
import type { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
8+
import type { ModifierStage } from "../processTargets/PipelineStages.types";
89
import { ModifyIfUntypedStage } from "../processTargets/modifiers/ConditionalModifierStages";
910
import { ide } from "../singletons/ide.singleton";
1011
import { transformSnippetVariables } from "../snippets/transformSnippetVariables";
1112
import { SnippetParser } from "../snippets/vendor/vscodeSnippet/snippetParser";
1213
import type { Target } from "../typings/target.types";
1314
import { ensureSingleEditor, flashTargets } from "../util/targetUtils";
1415
import type { ActionReturnValue } from "./actions.types";
16+
import WrapWithSnippetLegacy from "./snippetsLegacy/WrapWithSnippetLegacy";
1517

1618
export default class WrapWithSnippet {
1719
private snippetParser = new SnippetParser();
@@ -24,7 +26,14 @@ export default class WrapWithSnippet {
2426
this.run = this.run.bind(this);
2527
}
2628

27-
getFinalStages(targets: Target[], snippetDescription: WrapWithSnippetArg) {
29+
getFinalStages(
30+
targets: Target[],
31+
snippetDescription: WrapWithSnippetArg,
32+
): ModifierStage[] {
33+
if (snippetDescription.type === "named") {
34+
return this.legacy().getFinalStages(snippetDescription);
35+
}
36+
2837
const editor = ensureSingleEditor(targets);
2938
const snippet = getPreferredSnippet(
3039
snippetDescription,
@@ -50,6 +59,10 @@ export default class WrapWithSnippet {
5059
targets: Target[],
5160
snippetDescription: WrapWithSnippetArg,
5261
): Promise<ActionReturnValue> {
62+
if (snippetDescription.type === "named") {
63+
return this.legacy().run(targets, snippetDescription);
64+
}
65+
5366
const editor = ide().getEditableTextEditor(ensureSingleEditor(targets));
5467
const snippet = getPreferredSnippet(
5568
snippetDescription,
@@ -84,4 +97,13 @@ export default class WrapWithSnippet {
8497
})),
8598
};
8699
}
100+
101+
// DEPRECATED @ 2025-02-01
102+
private legacy() {
103+
return new WrapWithSnippetLegacy(
104+
this.rangeUpdater,
105+
this.snippets,
106+
this.modifierStageFactory,
107+
);
108+
}
87109
}

0 commit comments

Comments
 (0)