@@ -10,7 +10,6 @@ export default class DecorationManager {
10
10
> ;
11
11
private timeout : NodeJS . Timer | undefined = undefined ;
12
12
private fileName : string = "" ;
13
- private fileLineCount : number = 0 ;
14
13
15
14
constructor ( $activeEditor : vscode . TextEditor | undefined , $fullToken : any ) {
16
15
this . activeEditor = $activeEditor ;
@@ -21,16 +20,19 @@ export default class DecorationManager {
21
20
setActiveEditor ( editor : vscode . TextEditor ) {
22
21
this . activeEditor = editor ;
23
22
this . fileName = editor . document . fileName ;
24
- this . fileLineCount = editor . document . lineCount ;
25
23
}
26
24
27
25
triggerUpdateDecorations (
28
26
throttle : boolean = false ,
29
27
isEdit : boolean = false ,
28
+ diffLine : number = 0 ,
30
29
startLine ?: number ,
31
30
endLine ?: number
32
31
) {
33
32
console . log ( "fileName" , this . fileName ) ;
33
+ /**
34
+ * Active editor changed event
35
+ */
34
36
if ( ! isEdit ) {
35
37
if ( this . fileDecorationMap . has ( this . fileName ) ) {
36
38
this . clearCurrentFileDecoration ( ) ;
@@ -45,41 +47,43 @@ export default class DecorationManager {
45
47
46
48
if ( throttle ) {
47
49
this . timeout = setTimeout ( ( ) => {
48
- this . setupDecorations ( startLine , endLine ) ;
50
+ this . setupDecorations ( diffLine , startLine , endLine ) ;
49
51
} , 500 ) ;
50
52
} else {
51
- this . setupDecorations ( startLine , endLine ) ;
53
+ this . setupDecorations ( diffLine , startLine , endLine ) ;
52
54
}
53
55
}
54
56
55
- setupDecorations ( startLine ?: number , endLine ?: number ) {
57
+ setupDecorations ( diffLine : number , startLine ?: number , endLine ?: number ) {
56
58
if ( startLine && endLine ) {
57
- console . log ( "editing" , startLine , endLine ) ;
58
- const currentLineCount = this . activeEditor ! . document . lineCount ;
59
- const diffLine = currentLineCount - this . fileLineCount ;
60
59
console . log ( "diffLine" , diffLine ) ;
61
60
61
+ const lines = this . getLines ( startLine , endLine ) ;
62
62
if ( diffLine < 0 ) {
63
- const lines = this . getLines ( startLine , endLine ) ;
64
- return this . clearCurrentFileDecoration ( lines ) ;
63
+ this . clearCurrentFileDecoration ( lines ) ;
64
+ this . updateFileDecorationMap ( diffLine , startLine ) ;
65
65
}
66
66
67
67
if ( diffLine === 0 ) {
68
68
this . clearCurrentFileDecoration ( [ startLine ] ) ;
69
+ this . setDecorations ( [ startLine ] ) ;
69
70
}
70
- }
71
71
72
- this . setDecorations ( ) ;
72
+ if ( diffLine > 0 ) {
73
+ this . updateFileDecorationMap ( diffLine , startLine ) ;
74
+ this . setDecorations ( lines ) ;
75
+ }
76
+ } else {
77
+ this . setDecorations ( ) ;
78
+ }
73
79
}
74
80
75
- setDecorations ( ) {
81
+ setDecorations ( sepecificLines ?: number [ ] ) {
76
82
console . log ( "!!!!!!updating!!!!!" ) ;
77
83
const text = this . activeEditor ! . document . getText ( ) ;
78
84
const fullTokenKeys = Object . keys ( this . fullToken ) ;
79
- const lineDecorationMap = new Map <
80
- number ,
81
- vscode . TextEditorDecorationType [ ]
82
- > ( ) ;
85
+ const lineDecorationMap : Map < number , vscode . TextEditorDecorationType [ ] > =
86
+ this . fileDecorationMap . get ( this . fileName ) || new Map ( ) ;
83
87
84
88
if ( ! this . fileDecorationMap . has ( this . fileName ) ) {
85
89
this . fileDecorationMap . set ( this . fileName , new Map ( ) ) ;
@@ -94,7 +98,12 @@ export default class DecorationManager {
94
98
match . index
95
99
) . line ;
96
100
97
- this . setDecoration ( match , key , lineDecorationMap ) ;
101
+ if (
102
+ ! sepecificLines ||
103
+ ( sepecificLines && sepecificLines . includes ( currentLine ) )
104
+ ) {
105
+ this . setDecoration ( match , key , lineDecorationMap ) ;
106
+ }
98
107
}
99
108
} ) ;
100
109
@@ -104,12 +113,15 @@ export default class DecorationManager {
104
113
105
114
clearCurrentFileDecoration ( lines ?: number [ ] ) {
106
115
const lineDecorationMapItem = this . fileDecorationMap . get ( this . fileName ) ;
107
-
108
- if ( lineDecorationMapItem ) {
116
+ console . log ( "lineDecorationMapItem" , lineDecorationMapItem ) ;
117
+ if ( lineDecorationMapItem ?. size ) {
109
118
if ( lines ) {
110
119
lines . forEach ( ( line ) => {
111
120
console . log ( "dispose line" , line ) ;
112
- lineDecorationMapItem . get ( line ) ?. forEach ( this . dispose ) ;
121
+ if ( lineDecorationMapItem . has ( line ) ) {
122
+ lineDecorationMapItem . get ( line ) ?. forEach ( this . dispose ) ;
123
+ lineDecorationMapItem . delete ( line ) ;
124
+ }
113
125
} ) ;
114
126
} else {
115
127
lineDecorationMapItem . forEach ( ( value ) => {
@@ -182,4 +194,27 @@ export default class DecorationManager {
182
194
// console.log("setDecorations", currentLine);
183
195
this . activeEditor ! . setDecorations ( decorationType , valueDecorations ) ;
184
196
}
197
+
198
+ updateFileDecorationMap ( diffLine : number , startLine : number ) {
199
+ const lineDecorationMapItem = this . fileDecorationMap . get ( this . fileName ) ;
200
+ const newMap : Map < number , vscode . TextEditorDecorationType [ ] > = new Map ( ) ;
201
+ console . log ( "before: " , lineDecorationMapItem ) ;
202
+ console . log ( "startLine" , startLine ) ;
203
+
204
+ if ( lineDecorationMapItem ?. size ) {
205
+ lineDecorationMapItem . forEach ( ( value , key ) => {
206
+ if ( key >= startLine ) {
207
+ newMap . set (
208
+ diffLine > 0 ? key + diffLine : key - Math . abs ( diffLine ) ,
209
+ lineDecorationMapItem . get ( key ) !
210
+ ) ;
211
+ } else {
212
+ newMap . set ( key , lineDecorationMapItem . get ( key ) ! ) ;
213
+ }
214
+ } ) ;
215
+ }
216
+
217
+ console . log ( "after: " , newMap ) ;
218
+ this . fileDecorationMap . set ( this . fileName , newMap ) ;
219
+ }
185
220
}
0 commit comments