Skip to content

Commit 056a3f6

Browse files
committed
Add factories for stages and scope handlers
1 parent 0532bc6 commit 056a3f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+855
-600
lines changed

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Snippets } from "../core/Snippets";
22
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
3+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
34
import { Bring, Move, Swap } from "./BringMoveSwap";
45
import Call from "./Call";
56
import Clear from "./Clear";
@@ -58,16 +59,28 @@ import { ActionRecord } from "./actions.types";
5859
* Keeps a map from action names to objects that implement the given action
5960
*/
6061
export class Actions implements ActionRecord {
61-
constructor(private snippets: Snippets, private rangeUpdater: RangeUpdater) {}
62+
constructor(
63+
private snippets: Snippets,
64+
private rangeUpdater: RangeUpdater,
65+
private modifierStageFactory: ModifierStageFactory,
66+
) {}
6267

6368
callAsFunction = new Call(this);
6469
clearAndSetSelection = new Clear(this);
6570
copyToClipboard = new CopyToClipboard(this.rangeUpdater);
6671
cutToClipboard = new CutToClipboard(this);
6772
deselect = new Deselect();
68-
editNew = new EditNew(this.rangeUpdater, this);
69-
editNewLineAfter = new EditNewAfter(this.rangeUpdater, this);
70-
editNewLineBefore = new EditNewBefore(this.rangeUpdater, this);
73+
editNew = new EditNew(this.rangeUpdater, this, this.modifierStageFactory);
74+
editNewLineAfter = new EditNewAfter(
75+
this.rangeUpdater,
76+
this,
77+
this.modifierStageFactory,
78+
);
79+
editNewLineBefore = new EditNewBefore(
80+
this.rangeUpdater,
81+
this,
82+
this.modifierStageFactory,
83+
);
7184
executeCommand = new ExecuteCommand(this.rangeUpdater);
7285
extractVariable = new ExtractVariable(this.rangeUpdater);
7386
findInWorkspace = new FindInWorkspace(this);
@@ -77,12 +90,23 @@ export class Actions implements ActionRecord {
7790
getText = new GetText();
7891
highlight = new Highlight();
7992
indentLine = new IndentLine(this.rangeUpdater);
80-
insertCopyAfter = new InsertCopyAfter(this.rangeUpdater);
81-
insertCopyBefore = new InsertCopyBefore(this.rangeUpdater);
93+
insertCopyAfter = new InsertCopyAfter(
94+
this.rangeUpdater,
95+
this.modifierStageFactory,
96+
);
97+
insertCopyBefore = new InsertCopyBefore(
98+
this.rangeUpdater,
99+
this.modifierStageFactory,
100+
);
82101
insertEmptyLineAfter = new InsertEmptyLineAfter(this.rangeUpdater);
83102
insertEmptyLineBefore = new InsertEmptyLineBefore(this.rangeUpdater);
84103
insertEmptyLinesAround = new InsertEmptyLinesAround(this.rangeUpdater);
85-
insertSnippet = new InsertSnippet(this.rangeUpdater, this.snippets, this);
104+
insertSnippet = new InsertSnippet(
105+
this.rangeUpdater,
106+
this.snippets,
107+
this,
108+
this.modifierStageFactory,
109+
);
86110
moveToTarget = new Move(this.rangeUpdater);
87111
outdentLine = new OutdentLine(this.rangeUpdater);
88112
pasteFromClipboard = new PasteFromClipboard(this.rangeUpdater, this);
@@ -94,7 +118,10 @@ export class Actions implements ActionRecord {
94118
revealDefinition = new RevealDefinition(this.rangeUpdater);
95119
revealTypeDefinition = new RevealTypeDefinition(this.rangeUpdater);
96120
reverseTargets = new Reverse(this);
97-
rewrapWithPairedDelimiter = new Rewrap(this.rangeUpdater);
121+
rewrapWithPairedDelimiter = new Rewrap(
122+
this.rangeUpdater,
123+
this.modifierStageFactory,
124+
);
98125
scrollToBottom = new ScrollToBottom();
99126
scrollToCenter = new ScrollToCenter();
100127
scrollToTop = new ScrollToTop();
@@ -107,9 +134,13 @@ export class Actions implements ActionRecord {
107134
showReferences = new ShowReferences(this.rangeUpdater);
108135
sortTargets = new Sort(this);
109136
swapTargets = new Swap(this.rangeUpdater);
110-
toggleLineBreakpoint = new ToggleBreakpoint();
137+
toggleLineBreakpoint = new ToggleBreakpoint(this.modifierStageFactory);
111138
toggleLineComment = new ToggleLineComment(this.rangeUpdater);
112139
unfoldRegion = new Unfold(this.rangeUpdater);
113140
wrapWithPairedDelimiter = new Wrap(this.rangeUpdater);
114-
wrapWithSnippet = new WrapWithSnippet(this.rangeUpdater, this.snippets);
141+
wrapWithSnippet = new WrapWithSnippet(
142+
this.rangeUpdater,
143+
this.snippets,
144+
this.modifierStageFactory,
145+
);
115146
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { RangeUpdater } from "../../core/updateSelections/RangeUpdater";
2-
import { containingLineIfUntypedStage } from "../../processTargets/modifiers/commonContainingScopeIfUntypedStages";
2+
import { containingLineIfUntypedModifier } from "../../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
33
import PositionStage from "../../processTargets/modifiers/PositionStage";
4+
import { ModifierStageFactory } from "../../processTargets/ModifierStageFactory";
45
import { ModifierStage } from "../../processTargets/PipelineStages.types";
56
import { ide } from "../../singletons/ide.singleton";
67
import { Target } from "../../typings/target.types";
@@ -15,10 +16,14 @@ import { runEditNewNotebookCellTargets } from "./runNotebookCellTargets";
1516

1617
export class EditNew implements Action {
1718
getFinalStages(): ModifierStage[] {
18-
return [containingLineIfUntypedStage];
19+
return [this.modifierStageFactory.create(containingLineIfUntypedModifier)];
1920
}
2021

21-
constructor(private rangeUpdater: RangeUpdater, private actions: Actions) {
22+
constructor(
23+
private rangeUpdater: RangeUpdater,
24+
private actions: Actions,
25+
private modifierStageFactory: ModifierStageFactory,
26+
) {
2227
this.run = this.run.bind(this);
2328
}
2429

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ import {
88
import { flatten, zip } from "lodash";
99
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
1010
import { performEditsAndUpdateSelectionsWithBehavior } from "../core/updateSelections/updateSelections";
11-
import { containingLineIfUntypedStage } from "../processTargets/modifiers/commonContainingScopeIfUntypedStages";
11+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
12+
import { containingLineIfUntypedModifier } from "../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
1213
import { ide } from "../singletons/ide.singleton";
1314
import { Target } from "../typings/target.types";
1415
import { setSelectionsWithoutFocusingEditor } from "../util/setSelectionsAndFocusEditor";
1516
import { createThatMark, runOnTargetsForEachEditor } from "../util/targetUtils";
1617
import { Action, ActionReturnValue } from "./actions.types";
1718

1819
class InsertCopy implements Action {
19-
getFinalStages = () => [containingLineIfUntypedStage];
20+
getFinalStages = () => [
21+
this.modifierStageFactory.create(containingLineIfUntypedModifier),
22+
];
2023

21-
constructor(private rangeUpdater: RangeUpdater, private isBefore: boolean) {
24+
constructor(
25+
private rangeUpdater: RangeUpdater,
26+
private modifierStageFactory: ModifierStageFactory,
27+
private isBefore: boolean,
28+
) {
2229
this.run = this.run.bind(this);
2330
this.runForEditor = this.runForEditor.bind(this);
2431
}
@@ -90,13 +97,19 @@ class InsertCopy implements Action {
9097
}
9198

9299
export class CopyContentBefore extends InsertCopy {
93-
constructor(rangeUpdater: RangeUpdater) {
94-
super(rangeUpdater, true);
100+
constructor(
101+
rangeUpdater: RangeUpdater,
102+
modifierStageFactory: ModifierStageFactory,
103+
) {
104+
super(rangeUpdater, modifierStageFactory, true);
95105
}
96106
}
97107

98108
export class CopyContentAfter extends InsertCopy {
99-
constructor(rangeUpdater: RangeUpdater) {
100-
super(rangeUpdater, false);
109+
constructor(
110+
rangeUpdater: RangeUpdater,
111+
modifierStageFactory: ModifierStageFactory,
112+
) {
113+
super(rangeUpdater, modifierStageFactory, false);
101114
}
102115
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
callFunctionAndUpdateSelectionInfos,
1212
getSelectionInfo,
1313
} from "../core/updateSelections/updateSelections";
14+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
1415
import { ModifyIfUntypedExplicitStage } from "../processTargets/modifiers/ConditionalModifierStages";
1516
import { ide } from "../singletons/ide.singleton";
1617
import {
@@ -43,6 +44,7 @@ export default class InsertSnippet implements Action {
4344
private rangeUpdater: RangeUpdater,
4445
private snippets: Snippets,
4546
private actions: Actions,
47+
private modifierStageFactory: ModifierStageFactory,
4648
) {
4749
this.run = this.run.bind(this);
4850
}
@@ -53,7 +55,7 @@ export default class InsertSnippet implements Action {
5355
return defaultScopeTypes.length === 0
5456
? []
5557
: [
56-
new ModifyIfUntypedExplicitStage({
58+
new ModifyIfUntypedExplicitStage(this.modifierStageFactory, {
5759
type: "cascading",
5860
modifiers: defaultScopeTypes.map((scopeType) => ({
5961
type: "containingScope",

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { FlashStyle } from "@cursorless/common";
22
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
33
import { performEditsAndUpdateRanges } from "../core/updateSelections/updateSelections";
4-
import { containingSurroundingPairIfUntypedStage } from "../processTargets/modifiers/commonContainingScopeIfUntypedStages";
4+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
5+
import { containingSurroundingPairIfUntypedModifier } from "../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
56
import { ide } from "../singletons/ide.singleton";
67
import { Target } from "../typings/target.types";
78
import {
@@ -12,9 +13,16 @@ import {
1213
import { Action, ActionReturnValue } from "./actions.types";
1314

1415
export default class Rewrap implements Action {
15-
getFinalStages = () => [containingSurroundingPairIfUntypedStage];
16+
getFinalStages = () => [
17+
this.modifierStageFactory.create(
18+
containingSurroundingPairIfUntypedModifier,
19+
),
20+
];
1621

17-
constructor(private rangeUpdater: RangeUpdater) {
22+
constructor(
23+
private rangeUpdater: RangeUpdater,
24+
private modifierStageFactory: ModifierStageFactory,
25+
) {
1826
this.run = this.run.bind(this);
1927
}
2028

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import { BreakpointDescriptor, FlashStyle } from "@cursorless/common";
2+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
3+
import { containingLineIfUntypedModifier } from "../processTargets/modifiers/commonContainingScopeIfUntypedModifiers";
14
import { ide } from "../singletons/ide.singleton";
2-
import { containingLineIfUntypedStage } from "../processTargets/modifiers/commonContainingScopeIfUntypedStages";
35
import { Target } from "../typings/target.types";
46
import { flashTargets, runOnTargetsForEachEditor } from "../util/targetUtils";
57
import { Action, ActionReturnValue } from "./actions.types";
6-
import { BreakpointDescriptor, FlashStyle } from "@cursorless/common";
78

89
export default class ToggleBreakpoint implements Action {
9-
getFinalStages = () => [containingLineIfUntypedStage];
10+
getFinalStages = () => [
11+
this.modifierStageFactory.create(containingLineIfUntypedModifier),
12+
];
1013

11-
constructor() {
14+
constructor(private modifierStageFactory: ModifierStageFactory) {
1215
this.run = this.run.bind(this);
1316
}
1417

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FlashStyle, ScopeType } from "@cursorless/common";
22
import { Snippets } from "../core/Snippets";
33
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
44
import { callFunctionAndUpdateSelections } from "../core/updateSelections/updateSelections";
5+
import { ModifierStageFactory } from "../processTargets/ModifierStageFactory";
56
import { ModifyIfUntypedStage } from "../processTargets/modifiers/ConditionalModifierStages";
67
import { ide } from "../singletons/ide.singleton";
78
import {
@@ -29,7 +30,11 @@ type WrapWithSnippetArg = NamedSnippetArg | CustomSnippetArg;
2930
export default class WrapWithSnippet implements Action {
3031
private snippetParser = new SnippetParser();
3132

32-
constructor(private rangeUpdater: RangeUpdater, private snippets: Snippets) {
33+
constructor(
34+
private rangeUpdater: RangeUpdater,
35+
private snippets: Snippets,
36+
private modifierStageFactory: ModifierStageFactory,
37+
) {
3338
this.run = this.run.bind(this);
3439
}
3540

@@ -41,7 +46,7 @@ export default class WrapWithSnippet implements Action {
4146
}
4247

4348
return [
44-
new ModifyIfUntypedStage({
49+
new ModifyIfUntypedStage(this.modifierStageFactory, {
4550
type: "modifyIfUntyped",
4651
modifier: {
4752
type: "containingScope",

packages/cursorless-engine/src/compositionRoot/createCursorlessEngine.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { Debug } from "../core/Debug";
55
import { HatTokenMapImpl } from "../core/HatTokenMapImpl";
66
import { Snippets } from "../core/Snippets";
77
import { RangeUpdater } from "../core/updateSelections/RangeUpdater";
8+
import { MarkStageFactoryImpl } from "../processTargets/getMarkStage";
9+
import { ModifierStageFactoryImpl } from "../processTargets/ModifierStageFactoryImpl";
10+
import { ScopeHandlerFactoryImpl } from "../processTargets/modifiers/scopeHandlers";
811
import { injectIde } from "../singletons/ide.singleton";
912

1013
export function createCursorlessEngine(
@@ -33,7 +36,12 @@ export function createCursorlessEngine(
3336

3437
const testCaseRecorder = new TestCaseRecorder(hatTokenMap);
3538

36-
const actions = new Actions(snippets, rangeUpdater);
39+
const scopeHandlerFactory = new ScopeHandlerFactoryImpl();
40+
const markStageFactory = new MarkStageFactoryImpl();
41+
const modifierStageFactory = new ModifierStageFactoryImpl(
42+
scopeHandlerFactory,
43+
);
44+
const actions = new Actions(snippets, rangeUpdater, modifierStageFactory);
3745

3846
const thatMark = new ThatMark();
3947
const sourceMark = new ThatMark();
@@ -46,6 +54,8 @@ export function createCursorlessEngine(
4654
actions,
4755
thatMark,
4856
sourceMark,
57+
modifierStageFactory,
58+
markStageFactory,
4959
);
5060

5161
return {

packages/cursorless-engine/src/core/commandRunner/CommandRunner.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ import { ActionRecord } from "../../actions/actions.types";
88
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-imports
99
import { Actions } from "../../actions/Actions";
1010
import { TestCaseRecorder } from "../../index";
11-
import processTargets from "../../processTargets";
11+
import { TargetPipeline } from "../../processTargets";
12+
import { MarkStageFactory } from "../../processTargets/MarkStageFactory";
13+
import { ModifierStageFactory } from "../../processTargets/ModifierStageFactory";
1214
import { ide } from "../../singletons/ide.singleton";
13-
import { Target } from "../../typings/target.types";
1415
import { TreeSitter } from "../../typings/TreeSitter";
1516
import {
1617
ProcessedTargetsContext,
1718
SelectionWithEditor,
1819
} from "../../typings/Types";
20+
import { Target } from "../../typings/target.types";
1921
import { isString } from "../../util/type";
22+
import { Debug } from "../Debug";
23+
import { ThatMark } from "../ThatMark";
2024
import {
2125
canonicalizeAndValidateCommand,
2226
checkForOldInference,
2327
} from "../commandVersionUpgrades/canonicalizeAndValidateCommand";
24-
import { Debug } from "../Debug";
2528
import inferFullTargets from "../inferFullTargets";
26-
import { ThatMark } from "../ThatMark";
2729
import { selectionToThatTarget } from "./selectionToThatTarget";
2830

29-
// TODO: Do this using the graph once we migrate its dependencies onto the graph
3031
export class CommandRunner {
3132
constructor(
3233
private treeSitter: TreeSitter,
@@ -36,6 +37,8 @@ export class CommandRunner {
3637
private actions: ActionRecord,
3738
private thatMark: ThatMark,
3839
private sourceMark: ThatMark,
40+
private modifierStageFactory: ModifierStageFactory,
41+
private markStageFactory: MarkStageFactory,
3942
) {
4043
this.runCommandBackwardCompatible =
4144
this.runCommandBackwardCompatible.bind(this);
@@ -136,11 +139,17 @@ export class CommandRunner {
136139
// warning.
137140
checkForOldInference(partialTargetDescriptors);
138141

139-
const targets = processTargets(
142+
// FIXME: Construct this on a per-request basis in the composition root.
143+
// Then we don't need `CommandRunner` to depend on these factories and be
144+
// tightly coupled to `TargetPipeline`.
145+
const pipeline = new TargetPipeline(
146+
this.modifierStageFactory,
147+
this.markStageFactory,
140148
processedTargetsContext,
141-
targetDescriptors,
142149
);
143150

151+
const targets = pipeline.processTargets(targetDescriptors);
152+
144153
const {
145154
returnValue,
146155
thatSelections: newThatSelections,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Mark } from "@cursorless/common";
2+
import { MarkStage } from "./PipelineStages.types";
3+
4+
export interface MarkStageFactory {
5+
create(mark: Mark): MarkStage;
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
ContainingScopeModifier,
3+
EveryScopeModifier,
4+
Modifier,
5+
} from "@cursorless/common";
6+
import { ModifierStage } from "./PipelineStages.types";
7+
8+
export interface ModifierStageFactory {
9+
create(modifier: Modifier): ModifierStage;
10+
getLegacyScopeStage(
11+
modifier: ContainingScopeModifier | EveryScopeModifier,
12+
): ModifierStage;
13+
}

0 commit comments

Comments
 (0)