@@ -2,6 +2,8 @@ import vscode from "vscode";
2
2
class EditDecorationManager {
3
3
private _lastEditor : vscode . TextEditor | undefined ;
4
4
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
5
7
6
8
constructor ( context : vscode . ExtensionContext ) {
7
9
this . decorationType = vscode . window . createTextEditorDecorationType ( {
@@ -23,18 +25,40 @@ class EditDecorationManager {
23
25
) ;
24
26
}
25
27
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
29
36
}
30
- editor . setDecorations ( this . decorationType , [ range ] ) ;
31
37
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 ) ;
32
54
this . updateInEditMode ( true ) ;
33
55
}
34
56
35
57
clear ( ) {
36
58
if ( this . _lastEditor ) {
37
59
this . _lastEditor . setDecorations ( this . decorationType , [ ] ) ;
60
+ this . activeRanges = [ ] ; // Clear active ranges
61
+ this . activeRangeSet . clear ( ) ; // Clear active range set
38
62
this . _lastEditor = undefined ;
39
63
this . updateInEditMode ( false ) ;
40
64
}
0 commit comments