Elffuss BraidHub

kikocis/continue public

main · 3.0 KiBRawPermalink
1import vscode from "vscode";
2class EditDecorationManager {
3 private _lastEditor: vscode.TextEditor | undefined;
4 private decorationType: vscode.TextEditorDecorationType;
5 private activeRangesMap: Map<string, vscode.Range> = new Map(); // Store unique active ranges
6
7 constructor(context: vscode.ExtensionContext) {
8 this.decorationType = vscode.window.createTextEditorDecorationType({
9 backgroundColor: new vscode.ThemeColor(
10 // "editor.selectionHighlightBackground" requires partial transparency.
11 // This ensures the highlight does not completely obscure the selection,
12 // making it useful for repurposing here.
13 "editor.selectionHighlightBackground",
14 ),
15 isWholeLine: true,
16 });
17 }
18
19 private updateInEditMode(inEditMode: boolean) {
20 vscode.commands.executeCommand(
21 "setContext",
22 "continue.inEditMode",
23 inEditMode,
24 );
25 }
26
27 // Converts each range to a unique string for storing in the map
28 private rangeToString(range: vscode.Range): string {
29 return `(${range.start.line},${range.start.character})-(${range.end.line},${range.end.character})`;
30 }
31
32 // Checks if two ranges are adjacent or overlapping
33 private rangesCoincide(range1: vscode.Range, range2: vscode.Range): boolean {
34 return (
35 range1.start.isEqual(range2.start) ||
36 range1.end.isEqual(range2.start) ||
37 range2.end.isEqual(range1.start) ||
38 !!range1.intersection(range2)
39 );
40 }
41
42 // Merges new range with existing ranges in the map
43 private mergeNewRange(newRange: vscode.Range): void {
44 let mergedRange = newRange;
45 const rangesToPrune: string[] = [];
46
47 for (const [key, existingRange] of this.activeRangesMap.entries()) {
48 if (!this.rangesCoincide(mergedRange, existingRange)) {
49 continue;
50 }
51 mergedRange = mergedRange.union(existingRange);
52 rangesToPrune.push(key);
53 }
54
55 for (const key of rangesToPrune) {
56 this.activeRangesMap.delete(key);
57 }
58 this.activeRangesMap.set(this.rangeToString(mergedRange), mergedRange);
59 }
60
61 // Adds a new decoration to the editor and merges it with existing ranges
62 addDecorations(editor: vscode.TextEditor, ranges: vscode.Range[]): void {
63 if (this._lastEditor?.document !== editor.document) {
64 this.clear(); // Clear previous decorations if editor has changed
65 }
66 this._lastEditor = editor;
67
68 for (const range of ranges) {
69 this.mergeNewRange(range);
70 }
71
72 const activeRanges = Array.from(this.activeRangesMap.values());
73 if (activeRanges.length === 0) {
74 return;
75 } // No ranges to highlight
76
77 // Update active ranges and apply decorations
78 editor.setDecorations(this.decorationType, activeRanges);
79 this.updateInEditMode(true);
80 }
81
82 clear() {
83 if (this._lastEditor) {
84 this._lastEditor.setDecorations(this.decorationType, []);
85 this.activeRangesMap.clear();
86 this._lastEditor = undefined;
87 this.updateInEditMode(false);
88 }
89 }
90}
91export default EditDecorationManager;