-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathInsertEmptyLines.ts
More file actions
110 lines (95 loc) · 3.1 KB
/
InsertEmptyLines.ts
File metadata and controls
110 lines (95 loc) · 3.1 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
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../typings/Types";
import { Selection, Range } from "vscode";
import { displayPendingEditDecorationsForSelection } from "../util/editDisplayUtils";
import { runOnTargetsForEachEditor } from "../util/targetUtils";
import { performEditsAndUpdateSelections } from "../util/updateSelections";
import { flatten } from "lodash";
class InsertEmptyLines implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }];
constructor(
private graph: Graph,
private insertAbove: boolean,
private insertBelow: boolean
) {
this.run = this.run.bind(this);
}
private getRanges(targets: TypedSelection[]) {
let lines = targets.flatMap((target) => {
const lines = [];
if (this.insertAbove) {
lines.push(target.selection.selection.start.line);
}
if (this.insertBelow) {
lines.push(target.selection.selection.end.line + 1);
}
return lines;
});
// Remove duplicates
lines = [...new Set(lines)];
return lines.map((line) => new Range(line, 0, line, 0));
}
private getEdits(ranges: Range[]) {
return ranges.map((range) => ({
range,
text: "\n",
}));
}
async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
const results = flatten(
await runOnTargetsForEachEditor(targets, async (editor, targets) => {
const ranges = this.getRanges(targets);
const edits = this.getEdits(ranges);
const [updatedSelections, lineSelections, updatedOriginalSelections] =
await performEditsAndUpdateSelections(editor, edits, [
targets.map((target) => target.selection.selection),
ranges.map((range) => new Selection(range.start, range.end)),
editor.selections,
]);
editor.selections = updatedOriginalSelections;
return {
thatMark: updatedSelections.map((selection) => ({
editor,
selection,
})),
lineSelections: lineSelections.map((selection, index) => ({
editor,
selection:
ranges[index].start.line < editor.document.lineCount - 1
? new Selection(
selection.start.translate({ lineDelta: -1 }),
selection.end.translate({ lineDelta: -1 })
)
: selection,
})),
};
})
);
await displayPendingEditDecorationsForSelection(
results.flatMap((result) => result.lineSelections),
this.graph.editStyles.justAdded.line
);
const thatMark = results.flatMap((result) => result.thatMark);
return { thatMark };
}
}
export class InsertEmptyLinesAround extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, true, true);
}
}
export class InsertEmptyLineAbove extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, true, false);
}
}
export class InsertEmptyLineBelow extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, false, true);
}
}