-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathBringMoveSwap.ts
More file actions
252 lines (220 loc) · 6.85 KB
/
BringMoveSwap.ts
File metadata and controls
252 lines (220 loc) · 6.85 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
Edit,
} from "../typings/Types";
import { runForEachEditor } from "../util/targetUtils";
import update from "immutability-helper";
import displayPendingEditDecorations from "../util/editDisplayUtils";
import { performOutsideAdjustment } from "../util/performInsideOutsideAdjustment";
import { flatten, zip } from "lodash";
import { Selection, TextEditor, Range } from "vscode";
import { performEditsAndUpdateSelections } from "../util/updateSelections";
import { getTextWithPossibleDelimiter } from "../util/getTextWithPossibleDelimiter";
type ActionType = "bring" | "move" | "swap";
interface ExtendedEdit extends Edit {
editor: TextEditor;
isSource: boolean;
originalSelection: TypedSelection;
}
interface MarkEntry {
editor: TextEditor;
selection: Selection;
isSource: boolean;
typedSelection: TypedSelection;
}
class BringMoveSwap implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [
{ insideOutsideType: null },
{ insideOutsideType: null },
];
constructor(private graph: Graph, private type: ActionType) {
this.run = this.run.bind(this);
}
private broadcastSource(
sources: TypedSelection[],
destinations: TypedSelection[]
) {
if (sources.length === 1 && this.type !== "swap") {
// If there is only one source target, expand it to same length as
// destination target
return Array(destinations.length).fill(sources[0]);
}
return sources;
}
private getDecorationStyles() {
let sourceStyle;
if (this.type === "bring") {
sourceStyle = this.graph.editStyles.referenced;
} else if (this.type === "move") {
sourceStyle = this.graph.editStyles.pendingDelete;
}
// NB this.type === "swap"
else {
sourceStyle = this.graph.editStyles.pendingModification1;
}
return {
sourceStyle,
destinationStyle: this.graph.editStyles.pendingModification0,
};
}
private async decorateTargets(
sources: TypedSelection[],
destinations: TypedSelection[]
) {
const decorationTypes = this.getDecorationStyles();
await Promise.all([
displayPendingEditDecorations(sources, decorationTypes.sourceStyle),
displayPendingEditDecorations(
destinations,
decorationTypes.destinationStyle
),
]);
}
private getEdits(
sources: TypedSelection[],
destinations: TypedSelection[]
): ExtendedEdit[] {
const usedSources: TypedSelection[] = [];
return zip(sources, destinations).flatMap(([source, destination]) => {
if (source == null || destination == null) {
throw new Error("Targets must have same number of args");
}
// Get text adjusting for destination position
const text = getTextWithPossibleDelimiter(source, destination);
// Add destination edit
const result = [
{
range: destination.selection.selection as Range,
text,
editor: destination.selection.editor,
originalSelection: destination,
isSource: false,
extendOnEqualEmptyRange: true,
},
];
// Add source edit
// Prevent multiple instances of the same expanded source.
if (!usedSources.includes(source)) {
usedSources.push(source);
let text: string;
let range: Range;
if (this.type !== "move") {
text = destination.selection.editor.document.getText(
destination.selection.selection
);
range = source.selection.selection;
}
// NB: this.type === "move"
else {
text = "";
source = performOutsideAdjustment(source);
range = source.selection.selection;
}
result.push({
range,
text,
editor: source.selection.editor,
originalSelection: source,
isSource: true,
extendOnEqualEmptyRange: true,
});
}
return result;
});
}
private async performEditsAndComputeThatMark(
edits: ExtendedEdit[]
): Promise<MarkEntry[]> {
return flatten(
await runForEachEditor(
edits,
(edit) => edit.editor,
async (editor, edits) => {
// For bring we don't want to update the sources
const filteredEdits =
this.type !== "bring"
? edits
: edits.filter(({ isSource }) => !isSource);
const [updatedSelections]: Selection[][] =
await performEditsAndUpdateSelections(editor, filteredEdits, [
edits.map((edit) => edit.originalSelection.selection.selection),
]);
return edits.map((edit, index) => {
const selection = updatedSelections[index];
return {
editor,
selection,
isSource: edit!.isSource,
typedSelection: update(edit!.originalSelection, {
selection: {
selection: { $set: selection! },
},
}),
};
});
}
)
);
}
private async decorateThatMark(thatMark: MarkEntry[]) {
const decorationTypes = this.getDecorationStyles();
return Promise.all([
displayPendingEditDecorations(
thatMark
.filter(({ isSource }) => isSource)
.map(({ typedSelection }) => typedSelection),
decorationTypes.sourceStyle
),
displayPendingEditDecorations(
thatMark
.filter(({ isSource }) => !isSource)
.map(({ typedSelection }) => typedSelection),
decorationTypes.destinationStyle
),
]);
}
private calculateMarks(markEntries: MarkEntry[]) {
// Only swap has sources as a "that" mark
const thatMark =
this.type === "swap"
? markEntries
: markEntries.filter(({ isSource }) => !isSource);
// Only swap doesn't have a source mark
const sourceMark =
this.type === "swap"
? []
: markEntries.filter(({ isSource }) => isSource);
return { thatMark, sourceMark };
}
async run([sources, destinations]: [
TypedSelection[],
TypedSelection[]
]): Promise<ActionReturnValue> {
sources = this.broadcastSource(sources, destinations);
await this.decorateTargets(sources, destinations);
const edits = this.getEdits(sources, destinations);
const markEntries = await this.performEditsAndComputeThatMark(edits);
const { thatMark, sourceMark } = this.calculateMarks(markEntries);
await this.decorateThatMark(thatMark);
return { thatMark, sourceMark };
}
}
export class Bring extends BringMoveSwap {
constructor(graph: Graph) {
super(graph, "bring");
}
}
export class Move extends BringMoveSwap {
constructor(graph: Graph) {
super(graph, "move");
}
}
export class Swap extends BringMoveSwap {
constructor(graph: Graph) {
super(graph, "swap");
}
}