-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathEditNewLine.ts
More file actions
74 lines (67 loc) · 2.03 KB
/
EditNewLine.ts
File metadata and controls
74 lines (67 loc) · 2.03 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
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../typings/Types";
import { commands, Selection } from "vscode";
class EditNewLine implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }];
constructor(private graph: Graph, private isAbove: boolean) {
this.run = this.run.bind(this);
}
private correctForParagraph(targets: TypedSelection[]) {
targets.forEach((target) => {
let { start, end } = target.selection.selection;
if (target.selectionType === "paragraph") {
if (
this.isAbove &&
target.selectionContext.leadingDelimiterRange != null
) {
start = start.translate({ lineDelta: -1 });
} else if (
!this.isAbove &&
target.selectionContext.trailingDelimiterRange != null
) {
end = end.translate({ lineDelta: 1 });
}
target.selection.selection = new Selection(start, end);
}
});
}
async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
this.correctForParagraph(targets);
if (this.isAbove) {
await this.graph.actions.setSelectionBefore.run([targets]);
await commands.executeCommand(
targets[0].selectionContext.isNotebookCell
? "jupyter.insertCellAbove"
: "editor.action.insertLineBefore"
);
} else {
await this.graph.actions.setSelectionAfter.run([targets]);
await commands.executeCommand(
targets[0].selectionContext.isNotebookCell
? "jupyter.insertCellBelow"
: "editor.action.insertLineAfter"
);
}
return {
thatMark: targets.map((target) => ({
selection: target.selection.editor.selection,
editor: target.selection.editor,
})),
};
}
}
export class EditNewLineAbove extends EditNewLine {
constructor(graph: Graph) {
super(graph, true);
}
}
export class EditNewLineBelow extends EditNewLine {
constructor(graph: Graph) {
super(graph, false);
}
}