|
1 |
| -import { commands, Range, TextEditor } from "vscode"; |
| 1 | +import { |
| 2 | + commands as vscommands, |
| 3 | + Position, |
| 4 | + Range, |
| 5 | + Selection, |
| 6 | + TextEditor, |
| 7 | +} from "vscode"; |
2 | 8 | import { Target } from "../typings/target.types";
|
3 | 9 | import { Graph } from "../typings/Types";
|
4 |
| -import { getNotebookFromCellDocument } from "../util/notebook"; |
| 10 | +import { createThatMark, ensureSingleEditor } from "../util/targetUtils"; |
5 | 11 | import { Action, ActionReturnValue } from "./actions.types";
|
6 | 12 |
|
7 | 13 | class EditNewLine implements Action {
|
8 |
| - constructor(private graph: Graph, private isAbove: boolean) { |
| 14 | + constructor(private graph: Graph, private isBefore: boolean) { |
9 | 15 | this.run = this.run.bind(this);
|
10 | 16 | }
|
11 | 17 |
|
12 |
| - private correctForParagraph(targets: Target[]) { |
13 |
| - targets.forEach((target) => { |
14 |
| - let { start, end } = target.contentRange; |
15 |
| - if (target.scopeType === "paragraph") { |
16 |
| - if (this.isAbove && target.leadingDelimiter != null) { |
17 |
| - start = start.translate({ lineDelta: -1 }); |
18 |
| - } else if (!this.isAbove && target.trailingDelimiter != null) { |
19 |
| - end = end.translate({ lineDelta: 1 }); |
20 |
| - } |
21 |
| - target.contentRange = new Range(start, end); |
22 |
| - } |
23 |
| - }); |
24 |
| - } |
| 18 | + async run([targets]: [Target[]]): Promise<ActionReturnValue> { |
| 19 | + const editor = ensureSingleEditor(targets); |
25 | 20 |
|
26 |
| - private isNotebookEditor(editor: TextEditor) { |
27 |
| - return getNotebookFromCellDocument(editor.document) != null; |
28 |
| - } |
| 21 | + const targetsWithContext = targets.map((target) => ({ |
| 22 | + target, |
| 23 | + context: target.getEditNewLineContext(this.isBefore), |
| 24 | + })); |
| 25 | + const commandTargets = targetsWithContext.filter( |
| 26 | + ({ context }) => !!(<any>context).command |
| 27 | + ); |
| 28 | + const delimiterTargets = targetsWithContext.filter( |
| 29 | + ({ context }) => !!(<any>context).delimiter |
| 30 | + ); |
29 | 31 |
|
30 |
| - private getCommand(target: Target) { |
31 |
| - if (target.scopeType === "notebookCell") { |
32 |
| - if (this.isNotebookEditor(target.editor)) { |
33 |
| - return this.isAbove |
34 |
| - ? "notebook.cell.insertCodeCellAbove" |
35 |
| - : "notebook.cell.insertCodeCellBelow"; |
36 |
| - } |
37 |
| - return this.isAbove |
38 |
| - ? "jupyter.insertCellAbove" |
39 |
| - : "jupyter.insertCellBelow"; |
| 32 | + if (commandTargets.length > 0 && delimiterTargets.length > 0) { |
| 33 | + throw new Error("Can't insert edit using command and delimiter at once"); |
| 34 | + } |
| 35 | + |
| 36 | + if (commandTargets.length > 0) { |
| 37 | + const commands = commandTargets.map( |
| 38 | + ({ context }) => (<any>context).command |
| 39 | + ); |
| 40 | + return { |
| 41 | + thatMark: await this.runCommand(targets, commands), |
| 42 | + }; |
40 | 43 | }
|
41 |
| - return this.isAbove |
42 |
| - ? "editor.action.insertLineBefore" |
43 |
| - : "editor.action.insertLineAfter"; |
| 44 | + |
| 45 | + return { |
| 46 | + thatMark: await this.runDelimiter(targets, editor), |
| 47 | + }; |
44 | 48 | }
|
45 | 49 |
|
46 |
| - async run([targets]: [Target[]]): Promise<ActionReturnValue> { |
47 |
| - this.correctForParagraph(targets); |
| 50 | + async runDelimiter(targets: Target[], editor: TextEditor) { |
| 51 | + const edits = targets.map((target) => { |
| 52 | + const { contentRange } = target; |
| 53 | + const context = target.getEditNewLineContext(this.isBefore); |
| 54 | + const delimiter = (<any>context).delimiter as string; |
48 | 55 |
|
49 |
| - if (this.isAbove) { |
| 56 | + // Delimiter is one or more new lines. Handle as lines. |
| 57 | + if (delimiter.includes("\n")) { |
| 58 | + const lineNumber = this.isBefore |
| 59 | + ? contentRange.start.line |
| 60 | + : contentRange.end.line; |
| 61 | + const line = editor.document.lineAt(lineNumber); |
| 62 | + const characterIndex = line.isEmptyOrWhitespace |
| 63 | + ? contentRange.start.character |
| 64 | + : line.firstNonWhitespaceCharacterIndex; |
| 65 | + const padding = line.text.slice(0, characterIndex); |
| 66 | + const positionSelection = new Position( |
| 67 | + this.isBefore ? lineNumber : lineNumber + delimiter.length, |
| 68 | + characterIndex |
| 69 | + ); |
| 70 | + return { |
| 71 | + contentRange, |
| 72 | + text: this.isBefore ? padding + delimiter : delimiter + padding, |
| 73 | + insertPosition: this.isBefore ? line.range.start : line.range.end, |
| 74 | + selection: new Selection(positionSelection, positionSelection), |
| 75 | + thatMarkRange: this.isBefore |
| 76 | + ? new Range( |
| 77 | + contentRange.start.translate({ |
| 78 | + lineDelta: delimiter.length, |
| 79 | + }), |
| 80 | + contentRange.end.translate({ |
| 81 | + lineDelta: delimiter.length, |
| 82 | + }) |
| 83 | + ) |
| 84 | + : contentRange, |
| 85 | + }; |
| 86 | + } |
| 87 | + // Delimiter is something else. Handle as inline. |
| 88 | + else { |
| 89 | + const positionSelection = this.isBefore |
| 90 | + ? contentRange.start |
| 91 | + : contentRange.end.translate({ |
| 92 | + characterDelta: delimiter.length, |
| 93 | + }); |
| 94 | + return { |
| 95 | + contentRange, |
| 96 | + text: delimiter, |
| 97 | + insertPosition: this.isBefore ? contentRange.start : contentRange.end, |
| 98 | + selection: new Selection(positionSelection, positionSelection), |
| 99 | + thatMarkRange: this.isBefore |
| 100 | + ? new Range( |
| 101 | + contentRange.start.translate({ |
| 102 | + characterDelta: delimiter.length, |
| 103 | + }), |
| 104 | + contentRange.end.translate({ |
| 105 | + characterDelta: delimiter.length, |
| 106 | + }) |
| 107 | + ) |
| 108 | + : contentRange, |
| 109 | + }; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + await editor.edit((editBuilder) => { |
| 114 | + edits.forEach((edit) => { |
| 115 | + editBuilder.replace(edit.insertPosition, edit.text); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + editor.selections = edits.map((edit) => edit.selection); |
| 120 | + |
| 121 | + const thatMarkRanges = edits.map((edit) => edit.thatMarkRange); |
| 122 | + |
| 123 | + return createThatMark(targets, thatMarkRanges); |
| 124 | + } |
| 125 | + |
| 126 | + async runCommand(targets: Target[], commands: string[]) { |
| 127 | + if (new Set(commands).size > 1) { |
| 128 | + throw new Error("Can't run multiple different commands at once"); |
| 129 | + } |
| 130 | + if (this.isBefore) { |
50 | 131 | await this.graph.actions.setSelectionBefore.run([targets]);
|
51 | 132 | } else {
|
52 | 133 | await this.graph.actions.setSelectionAfter.run([targets]);
|
53 | 134 | }
|
54 |
| - |
55 |
| - const command = this.getCommand(targets[0]); |
56 |
| - await commands.executeCommand(command); |
57 |
| - |
58 |
| - return { |
59 |
| - thatMark: targets.map((target) => ({ |
60 |
| - selection: target.editor.selection, |
61 |
| - editor: target.editor, |
62 |
| - })), |
63 |
| - }; |
| 135 | + await vscommands.executeCommand(commands[0]); |
| 136 | + return createThatMark(targets); |
64 | 137 | }
|
65 | 138 | }
|
66 | 139 |
|
|
0 commit comments