Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
languageId: typescript
command:
version: 6
spokenForm: snippet funk before class
action:
name: insertSnippet
snippetDescription: {type: named, name: functionDeclaration}
destination:
type: primitive
insertionMode: before
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: class}
usePrePhraseSnapshot: true
spokenFormError: named insertion snippet
initialState:
documentContents: |-
class Aaa {

}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: |-
function () {

}

class Aaa {

}
selections:
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 0, character: 0}
end: {line: 2, character: 1}
isReversed: false
hasExplicitRange: true
7 changes: 7 additions & 0 deletions data/fixtures/recorded/actions/snippets/tryWrapThis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ finalState:
selections:
- anchor: {line: 3, character: 4}
active: {line: 3, character: 4}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 0, character: 0}
end: {line: 4, character: 1}
isReversed: false
hasExplicitRange: true
35 changes: 35 additions & 0 deletions data/fixtures/recorded/actions/snippets/tryWrapThisLegacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
languageId: typescript
command:
version: 6
spokenForm: try wrap this
action:
name: wrapWithSnippet
snippetDescription: {type: named, name: tryCatchStatement, variableName: body}
target:
type: primitive
mark: {type: cursor}
usePrePhraseSnapshot: true
spokenFormError: named wrap with snippet
initialState:
documentContents: const foo = "bar";
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks: {}
finalState:
documentContents: |-
try {
const foo = "bar";
} catch (err) {

}
selections:
- anchor: {line: 3, character: 4}
active: {line: 3, character: 4}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 0, character: 0}
end: {line: 4, character: 1}
isReversed: false
hasExplicitRange: true
4 changes: 2 additions & 2 deletions packages/common/src/types/command/ActionDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export interface GenerateSnippetActionDescriptor {
target: PartialTargetDescriptor;
}

interface NamedInsertSnippetArg {
export interface NamedInsertSnippetArg {
type: "named";
name: string;
substitutions?: Record<string, string>;
Expand Down Expand Up @@ -176,7 +176,7 @@ export interface InsertSnippetActionDescriptor {
destination: DestinationDescriptor;
}

interface NamedWrapWithSnippetArg {
export interface NamedWrapWithSnippetArg {
type: "named";
name: string;
variableName: string;
Expand Down
22 changes: 21 additions & 1 deletion packages/cursorless-engine/src/actions/InsertSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getPreferredSnippet } from "../core/getPreferredSnippet";
import type { RangeUpdater } from "../core/updateSelections/RangeUpdater";
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
import type { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
import type { ModifierStage } from "../processTargets/PipelineStages.types";
import { ModifyIfUntypedExplicitStage } from "../processTargets/modifiers/ConditionalModifierStages";
import { ide } from "../singletons/ide.singleton";
import { transformSnippetVariables } from "../snippets/transformSnippetVariables";
Expand All @@ -13,6 +14,7 @@ import type { Destination } from "../typings/target.types";
import { ensureSingleEditor } from "../util/targetUtils";
import type { Actions } from "./Actions";
import type { ActionReturnValue } from "./actions.types";
import InsertSnippetLegacy from "./snippetsLegacy/InsertSnippetLegacy";

export default class InsertSnippet {
private snippetParser = new SnippetParser();
Expand All @@ -29,7 +31,11 @@ export default class InsertSnippet {
getFinalStages(
destinations: Destination[],
snippetDescription: InsertSnippetArg,
) {
): ModifierStage[] {
if (snippetDescription.type === "named") {
return this.legacy().getFinalStages(snippetDescription);
}

const editor = ensureSingleEditor(destinations);
const snippet = getPreferredSnippet(
snippetDescription,
Expand All @@ -53,6 +59,10 @@ export default class InsertSnippet {
destinations: Destination[],
snippetDescription: InsertSnippetArg,
): Promise<ActionReturnValue> {
if (snippetDescription.type === "named") {
return this.legacy().run(destinations, snippetDescription);
}

const editor = ide().getEditableTextEditor(
ensureSingleEditor(destinations),
);
Expand Down Expand Up @@ -93,4 +103,14 @@ export default class InsertSnippet {
})),
};
}

// DEPRECATED @ 2025-02-01
private legacy() {
return new InsertSnippetLegacy(
this.rangeUpdater,
this.snippets,
this.actions,
this.modifierStageFactory,
);
}
}
24 changes: 23 additions & 1 deletion packages/cursorless-engine/src/actions/WrapWithSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { getPreferredSnippet } from "../core/getPreferredSnippet";
import type { RangeUpdater } from "../core/updateSelections/RangeUpdater";
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
import type { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
import type { ModifierStage } from "../processTargets/PipelineStages.types";
import { ModifyIfUntypedStage } from "../processTargets/modifiers/ConditionalModifierStages";
import { ide } from "../singletons/ide.singleton";
import { transformSnippetVariables } from "../snippets/transformSnippetVariables";
import { SnippetParser } from "../snippets/vendor/vscodeSnippet/snippetParser";
import type { Target } from "../typings/target.types";
import { ensureSingleEditor, flashTargets } from "../util/targetUtils";
import type { ActionReturnValue } from "./actions.types";
import WrapWithSnippetLegacy from "./snippetsLegacy/WrapWithSnippetLegacy";

export default class WrapWithSnippet {
private snippetParser = new SnippetParser();
Expand All @@ -24,7 +26,14 @@ export default class WrapWithSnippet {
this.run = this.run.bind(this);
}

getFinalStages(targets: Target[], snippetDescription: WrapWithSnippetArg) {
getFinalStages(
targets: Target[],
snippetDescription: WrapWithSnippetArg,
): ModifierStage[] {
if (snippetDescription.type === "named") {
return this.legacy().getFinalStages(snippetDescription);
}

const editor = ensureSingleEditor(targets);
const snippet = getPreferredSnippet(
snippetDescription,
Expand All @@ -50,6 +59,10 @@ export default class WrapWithSnippet {
targets: Target[],
snippetDescription: WrapWithSnippetArg,
): Promise<ActionReturnValue> {
if (snippetDescription.type === "named") {
return this.legacy().run(targets, snippetDescription);
}

const editor = ide().getEditableTextEditor(ensureSingleEditor(targets));
const snippet = getPreferredSnippet(
snippetDescription,
Expand Down Expand Up @@ -84,4 +97,13 @@ export default class WrapWithSnippet {
})),
};
}

// DEPRECATED @ 2025-02-01
private legacy() {
return new WrapWithSnippetLegacy(
this.rangeUpdater,
this.snippets,
this.modifierStageFactory,
);
}
}
Loading
Loading