| 1 | import * as vscode from "vscode"; |
| 2 | |
| 3 | const removedLineDecorationType = (line: string) => |
| 4 | vscode.window.createTextEditorDecorationType({ |
| 5 | isWholeLine: true, |
| 6 | backgroundColor: { id: "diffEditor.removedLineBackground" }, |
| 7 | outlineWidth: "1px", |
| 8 | outlineStyle: "solid", |
| 9 | outlineColor: { id: "diffEditor.removedTextBorder" }, |
| 10 | rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, |
| 11 | after: { |
| 12 | contentText: line, |
| 13 | color: "#808080", |
| 14 | textDecoration: "none; white-space: pre", |
| 15 | }, |
| 16 | // NOTE this has the effect of hiding text the user enters into a red line, which may cause linting errors |
| 17 | // But probably worth saving the ugly effect of having the ghost text after entered text |
| 18 | // And resolved upon accept/reject when line deleted anyways |
| 19 | textDecoration: "none; display: none", |
| 20 | }); |
| 21 | |
| 22 | const addedLineDecorationType = vscode.window.createTextEditorDecorationType({ |
| 23 | isWholeLine: true, |
| 24 | backgroundColor: { id: "diffEditor.insertedLineBackground" }, |
| 25 | outlineWidth: "1px", |
| 26 | outlineStyle: "solid", |
| 27 | outlineColor: { id: "diffEditor.insertedTextBorder" }, |
| 28 | rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, |
| 29 | }); |
| 30 | |
| 31 | export const indexDecorationType = vscode.window.createTextEditorDecorationType( |
| 32 | { |
| 33 | isWholeLine: true, |
| 34 | backgroundColor: "rgba(255, 255, 255, 0.2)", |
| 35 | rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, |
| 36 | }, |
| 37 | ); |
| 38 | export const belowIndexDecorationType = |
| 39 | vscode.window.createTextEditorDecorationType({ |
| 40 | isWholeLine: true, |
| 41 | backgroundColor: "rgba(255, 255, 255, 0.1)", |
| 42 | rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, |
| 43 | }); |
| 44 | |
| 45 | function translateRange(range: vscode.Range, lineOffset: number): vscode.Range { |
| 46 | return new vscode.Range( |
| 47 | range.start.translate(lineOffset), |
| 48 | range.end.translate(lineOffset), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | // Class for managing highlight decorations for added lines (e.g. GREEN) |
| 53 | export class AddedLineDecorationManager { |
| 54 | constructor(private editor: vscode.TextEditor) {} |
| 55 | |
| 56 | ranges: vscode.Range[] = []; |
| 57 | decorationType = addedLineDecorationType; |
| 58 | |
| 59 | applyToNewEditor(newEditor: vscode.TextEditor) { |
| 60 | this.editor = newEditor; |
| 61 | this.editor.setDecorations(this.decorationType, this.ranges); |
| 62 | } |
| 63 | |
| 64 | addLines(startIndex: number, numLines: number) { |
| 65 | const lastRange = this.ranges[this.ranges.length - 1]; |
| 66 | if (lastRange && lastRange.end.line === startIndex - 1) { |
| 67 | this.ranges[this.ranges.length - 1] = lastRange.with( |
| 68 | undefined, |
| 69 | lastRange.end.translate(numLines), |
| 70 | ); |
| 71 | } else { |
| 72 | this.ranges.push( |
| 73 | new vscode.Range( |
| 74 | startIndex, |
| 75 | 0, |
| 76 | startIndex + numLines - 1, |
| 77 | Number.MAX_SAFE_INTEGER, |
| 78 | ), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | this.editor.setDecorations(this.decorationType, this.ranges); |
| 83 | } |
| 84 | |
| 85 | addLine(index: number) { |
| 86 | this.addLines(index, 1); |
| 87 | } |
| 88 | |
| 89 | clear() { |
| 90 | this.ranges = []; |
| 91 | this.editor.setDecorations(this.decorationType, this.ranges); |
| 92 | } |
| 93 | |
| 94 | shiftDownAfterLine(afterLine: number, offset: number) { |
| 95 | for (let i = 0; i < this.ranges.length; i++) { |
| 96 | if (this.ranges[i].start.line >= afterLine) { |
| 97 | this.ranges[i] = translateRange(this.ranges[i], offset); |
| 98 | } |
| 99 | } |
| 100 | this.editor.setDecorations(this.decorationType, this.ranges); |
| 101 | } |
| 102 | |
| 103 | deleteRangeStartingAt(line: number) { |
| 104 | for (let i = 0; i < this.ranges.length; i++) { |
| 105 | if (this.ranges[i].start.line === line) { |
| 106 | return this.ranges.splice(i, 1)[0]; |
| 107 | } |
| 108 | } |
| 109 | this.editor.setDecorations(this.decorationType, this.ranges); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Class for managing ghost-text decorations for removed lines (e.g. RED) |
| 114 | // Behavior is slightly different all around |
| 115 | // because each line will have a unique decoration type |
| 116 | export class RemovedLineDecorationManager { |
| 117 | constructor(private editor: vscode.TextEditor) {} |
| 118 | |
| 119 | ranges: { |
| 120 | line: string; |
| 121 | range: vscode.Range; |
| 122 | decoration: vscode.TextEditorDecorationType; |
| 123 | }[] = []; |
| 124 | |
| 125 | applyToNewEditor(newEditor: vscode.TextEditor) { |
| 126 | this.editor = newEditor; |
| 127 | this.applyDecorations(); |
| 128 | } |
| 129 | |
| 130 | addLines(startIndex: number, lines: string[]) { |
| 131 | let i = 0; |
| 132 | for (const line of lines) { |
| 133 | this.ranges.push({ |
| 134 | line, |
| 135 | range: new vscode.Range( |
| 136 | startIndex + i, |
| 137 | 0, |
| 138 | startIndex + i, |
| 139 | Number.MAX_SAFE_INTEGER, |
| 140 | ), |
| 141 | decoration: removedLineDecorationType(line), |
| 142 | }); |
| 143 | i++; |
| 144 | } |
| 145 | this.applyDecorations(); |
| 146 | } |
| 147 | |
| 148 | addLine(index: number, line: string) { |
| 149 | this.addLines(index, [line]); |
| 150 | } |
| 151 | |
| 152 | applyDecorations() { |
| 153 | this.ranges.forEach((r) => { |
| 154 | this.editor.setDecorations(r.decoration, [r.range]); |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | // Removed decorations are always unique, so we'll always dispose |
| 159 | clear() { |
| 160 | this.ranges.forEach((r) => { |
| 161 | r.decoration.dispose(); |
| 162 | }); |
| 163 | this.ranges = []; |
| 164 | } |
| 165 | |
| 166 | shiftDownAfterLine(afterLine: number, offset: number) { |
| 167 | for (let i = 0; i < this.ranges.length; i++) { |
| 168 | if (this.ranges[i].range.start.line >= afterLine) { |
| 169 | this.ranges[i].range = translateRange(this.ranges[i].range, offset); |
| 170 | } |
| 171 | } |
| 172 | this.applyDecorations(); |
| 173 | } |
| 174 | |
| 175 | // Red ranges are always single-line, so to delete group, delete sequential ranges |
| 176 | deleteRangesStartingAt(line: number) { |
| 177 | for (let i = 0; i < this.ranges.length; i++) { |
| 178 | if (this.ranges[i].range.start.line === line) { |
| 179 | let sequential = 0; |
| 180 | while ( |
| 181 | i + sequential < this.ranges.length && |
| 182 | this.ranges[i + sequential].range.start.line === line + sequential |
| 183 | ) { |
| 184 | this.ranges[i + sequential].decoration.dispose(); |
| 185 | sequential++; |
| 186 | } |
| 187 | return this.ranges.splice(i, sequential); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |