-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCommandAction.ts
More file actions
92 lines (78 loc) · 2.65 KB
/
CommandAction.ts
File metadata and controls
92 lines (78 loc) · 2.65 KB
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
import { commands, window } from "vscode";
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
SelectionWithEditor,
} from "../typings/Types";
import displayPendingEditDecorations from "../util/editDisplayUtils";
import { runOnTargetsForEachEditor } from "../util/targetUtils";
import {
focusEditor,
setSelectionsAndFocusEditor,
} from "../util/setSelectionsAndFocusEditor";
import { flatten } from "lodash";
import { callFunctionAndUpdateSelections } from "../util/updateSelections";
import { ensureSingleEditor } from "../util/targetUtils";
export default class CommandAction implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }];
private ensureSingleEditor: boolean;
constructor(
private graph: Graph,
private command: string,
{ ensureSingleEditor = false } = {}
) {
this.run = this.run.bind(this);
this.ensureSingleEditor = ensureSingleEditor;
}
private async runCommandAndUpdateSelections(targets: TypedSelection[]) {
return flatten(
await runOnTargetsForEachEditor<SelectionWithEditor[]>(
targets,
async (editor, targets) => {
const originalSelections = editor.selections;
const targetSelections = targets.map(
(target) => target.selection.selection
);
// For command to the work we have to have the correct editor focused
await setSelectionsAndFocusEditor(editor, targetSelections, false);
const [updatedOriginalSelections, updatedTargetSelections] =
await callFunctionAndUpdateSelections(
() => commands.executeCommand(this.command),
editor,
[originalSelections, targetSelections]
);
// Reset original selections
editor.selections = updatedOriginalSelections;
return updatedTargetSelections.map((selection) => ({
editor,
selection,
}));
}
)
);
}
async run(
[targets]: [TypedSelection[]],
{ showDecorations = true } = {}
): Promise<ActionReturnValue> {
if (showDecorations) {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced
);
}
if (this.ensureSingleEditor) {
ensureSingleEditor(targets);
}
const originalEditor = window.activeTextEditor;
const thatMark = await this.runCommandAndUpdateSelections(targets);
// If necessary focus back original editor
if (originalEditor != null && originalEditor !== window.activeTextEditor) {
await focusEditor(originalEditor);
}
return { thatMark };
}
}