Merge pull request #4725 from GauravVBhambhani/multiple-range-highlighting

Improved text selection by allowing multiple edit ranges to be highlighted
This commit is contained in:
Nate Sesti 2025-04-23 16:28:23 -07:00 • committed by GitHub
commit 499a8dc567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 5 deletions

View file

@ -576,7 +576,7 @@ const getCommandsMap: (
const range =
args?.range ?? new vscode.Range(startFromCharZero, endAtCharLast);
editDecorationManager.setDecoration(editor, range);
editDecorationManager.addDecorations(editor, [range]);
const rangeInFileWithContents = getRangeInFileWithContents(true, range);

View file

@ -2,6 +2,7 @@ import vscode from "vscode";
class EditDecorationManager {
private _lastEditor: vscode.TextEditor | undefined;
private decorationType: vscode.TextEditorDecorationType;
private activeRangesMap: Map<string, vscode.Range> = new Map(); // Store unique active ranges
constructor(context: vscode.ExtensionContext) {
this.decorationType = vscode.window.createTextEditorDecorationType({
@ -23,18 +24,55 @@ class EditDecorationManager {
);
}
setDecoration(editor: vscode.TextEditor, range: vscode.Range) {
if (this._lastEditor) {
this._lastEditor.setDecorations(this.decorationType, []);
// Converts each range to a unique string for storing in the map
private rangeToString(range: vscode.Range): string {
return `(${range.start.line},${range.start.character})-(${range.end.line},${range.end.character})`;
}
// Checks if two ranges are adjacent or overlapping
private rangesCoincide(range1: vscode.Range, range2: vscode.Range): boolean {
return range1.start.isEqual(range2.start)
|| range1.end.isEqual(range2.start)
|| range2.end.isEqual(range1.start)
|| !!range1.intersection(range2);
}
// Merges new range with existing ranges in the map
private mergeNewRange(newRange: vscode.Range): void {
let mergedRange = newRange;
const rangesToPrune: string[] = [];
for (const [key, existingRange] of this.activeRangesMap.entries()) {
if (!this.rangesCoincide(mergedRange, existingRange)) continue;
mergedRange = mergedRange.union(existingRange);
rangesToPrune.push(key);
}
for (const key of rangesToPrune) this.activeRangesMap.delete(key);
this.activeRangesMap.set(this.rangeToString(mergedRange), mergedRange);
}
// Adds a new decoration to the editor and merges it with existing ranges
addDecorations(editor: vscode.TextEditor, ranges: vscode.Range[]): void {
if (this._lastEditor?.document !== editor.document) {
this.clear(); // Clear previous decorations if editor has changed
}
editor.setDecorations(this.decorationType, [range]);
this._lastEditor = editor;
for (const range of ranges) this.mergeNewRange(range);
const activeRanges = Array.from(this.activeRangesMap.values());
if (activeRanges.length === 0) return; // No ranges to highlight
// Update active ranges and apply decorations
editor.setDecorations(this.decorationType, activeRanges);
this.updateInEditMode(true);
}
clear() {
if (this._lastEditor) {
this._lastEditor.setDecorations(this.decorationType, []);
this.activeRangesMap.clear();
this._lastEditor = undefined;
this.updateInEditMode(false);
}