Skip to content

Commit 5a91e51

Browse files
Text selection: Improved functionality of text selection by allowing multiple edit ranges to be highlighted. If you've used command+I multiple times to add multiple ranges to Edit, then they should all be shown highlighted in the editor, not just the most recent one.
1 parent 11fa260 commit 5a91e51

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

extensions/vscode/src/commands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ const getCommandsMap: (
569569
const range =
570570
args?.range ?? new vscode.Range(startFromCharZero, endAtCharLast);
571571

572-
editDecorationManager.setDecoration(editor, range);
572+
editDecorationManager.setDecoration(editor, [range]);
573573

574574
const rangeInFileWithContents = getRangeInFileWithContents(true, range);
575575

extensions/vscode/src/quickEdit/EditDecorationManager.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import vscode from "vscode";
22
class EditDecorationManager {
33
private _lastEditor: vscode.TextEditor | undefined;
44
private decorationType: vscode.TextEditorDecorationType;
5+
private activeRanges: vscode.Range[] = []; // Store active ranges to be highlighted
6+
private activeRangeSet: Set<string> = new Set(); // Store active ranges as strings to avoid duplicates
57

68
constructor(context: vscode.ExtensionContext) {
79
this.decorationType = vscode.window.createTextEditorDecorationType({
@@ -23,18 +25,40 @@ class EditDecorationManager {
2325
);
2426
}
2527

26-
setDecoration(editor: vscode.TextEditor, range: vscode.Range) {
27-
if (this._lastEditor) {
28-
this._lastEditor.setDecorations(this.decorationType, []);
28+
// converting each range to a unique string for storing in set
29+
private rangeToString(range: vscode.Range): string {
30+
return `${range.start.line}:${range.start.character}-${range.end.line}:${range.end.character}`;
31+
}
32+
33+
setDecoration(editor: vscode.TextEditor, ranges: vscode.Range[]): void {
34+
if (this._lastEditor?.document !== editor.document) {
35+
this.clear(); // Clear previous decorations if editor has changed
2936
}
30-
editor.setDecorations(this.decorationType, [range]);
3137
this._lastEditor = editor;
38+
39+
// Filter out already highlighted ranges
40+
const newRanges = ranges.filter(range => {
41+
const rangeKey = this.rangeToString(range);
42+
if (this.activeRangeSet.has(rangeKey)) {
43+
return false; // already highlighted
44+
}
45+
this.activeRangeSet.add(rangeKey);
46+
return true;
47+
});
48+
49+
if (newRanges.length === 0) return; // No new ranges to highlight
50+
51+
// Update active ranges and apply decorations
52+
this.activeRanges.push(...newRanges);
53+
editor.setDecorations(this.decorationType, this.activeRanges);
3254
this.updateInEditMode(true);
3355
}
3456

3557
clear() {
3658
if (this._lastEditor) {
3759
this._lastEditor.setDecorations(this.decorationType, []);
60+
this.activeRanges = []; // Clear active ranges
61+
this.activeRangeSet.clear(); // Clear active range set
3862
this._lastEditor = undefined;
3963
this.updateInEditMode(false);
4064
}

0 commit comments

Comments
 (0)