1
1
import * as vscode from "vscode" ;
2
2
import { genMarkdownString , getColorTokenValue } from "./utils" ;
3
3
4
- interface DecorationItem {
5
- line : number ;
6
- disposable : vscode . TextEditorDecorationType ;
7
- }
8
-
9
4
export default function setupChangeEvent (
10
5
context : vscode . ExtensionContext ,
11
6
fullToken : any
12
7
) {
13
8
let timeout : NodeJS . Timer | undefined = undefined ;
14
9
let activeEditor = vscode . window . activeTextEditor ;
15
10
const fullTokenKeys = Object . keys ( fullToken ) ;
16
- const fileDecorationMap = new Map < string , DecorationItem [ ] > ( ) ;
11
+ const fileDecorationMap = new Map <
12
+ string ,
13
+ vscode . TextEditorDecorationType [ ]
14
+ > ( ) ;
17
15
const openedFileNameSet = new Set < string > ( ) ;
18
- let lineCount = 0 ;
19
16
20
17
if ( activeEditor ) {
21
- lineCount = activeEditor . document . lineCount ;
22
18
const isOpened = checkOpenedFile ( activeEditor . document . fileName ) ;
23
19
24
20
if ( ! isOpened ) {
@@ -32,19 +28,11 @@ export default function setupChangeEvent(
32
28
return ;
33
29
}
34
30
35
- const startLine = event . contentChanges [ 0 ] . range . start . line ;
36
- let endLine = event . contentChanges [ 0 ] . range . end . line ;
37
-
38
31
if ( activeEditor && event . document === activeEditor . document ) {
39
32
/**
40
33
* As undo (reason === 1) or redo (reason === 2) are very fast, do it very fast too.
41
- * Same as `delete` some code
42
34
*/
43
- const throttle =
44
- event . reason !== undefined
45
- ? false
46
- : event . document . lineCount - lineCount >= 0 ;
47
- triggerUpdateDecorations ( throttle , true , startLine , endLine ) ;
35
+ triggerUpdateDecorations ( true ) ;
48
36
}
49
37
} ,
50
38
null ,
@@ -55,7 +43,6 @@ export default function setupChangeEvent(
55
43
( editor ) => {
56
44
activeEditor = editor ;
57
45
if ( editor ) {
58
- lineCount = editor . document . lineCount ;
59
46
const isOpened = checkOpenedFile ( editor . document . fileName ) ;
60
47
61
48
if ( ! isOpened ) {
@@ -67,69 +54,36 @@ export default function setupChangeEvent(
67
54
context . subscriptions
68
55
) ;
69
56
70
- function triggerUpdateDecorations (
71
- throttle = false ,
72
- isEdit = false ,
73
- startLine ?: number ,
74
- endLine ?: number
75
- ) {
57
+ function triggerUpdateDecorations ( throttle = false ) {
76
58
if ( timeout ) {
77
59
clearTimeout ( timeout ) ;
78
60
timeout = undefined ;
79
61
}
80
62
if ( throttle ) {
81
63
timeout = setTimeout ( ( ) => {
82
- updateDecorations ( isEdit , startLine , endLine ) ;
64
+ updateDecorations ( ) ;
83
65
} , 500 ) ;
84
66
} else {
85
- updateDecorations ( isEdit , startLine , endLine ) ;
67
+ updateDecorations ( ) ;
86
68
}
87
69
}
88
70
89
- function updateDecorations (
90
- isEdit : boolean ,
91
- startLine ?: number ,
92
- endLine ?: number
93
- ) {
71
+ function updateDecorations ( ) {
94
72
if ( activeEditor ) {
95
- console . log ( "!!!!!!update!!!!" , startLine , endLine ) ;
96
73
const text = activeEditor . document . getText ( ) ;
97
74
const fileName = activeEditor . document . fileName ;
98
-
99
- if ( ! fileDecorationMap . has ( fileName ) ) {
100
- fileDecorationMap . set ( fileName , [ ] ) ;
101
- }
102
-
103
- const currentFileDecorations = fileDecorationMap . get ( fileName ) || [ ] ;
104
- const currentLineCount = activeEditor . document . lineCount ;
105
- const diffLine = currentLineCount - lineCount ;
106
- console . log ( "diffLine" , diffLine ) ;
107
- lineCount = currentLineCount ;
75
+ let currentFileDecorations = fileDecorationMap . get ( fileName ) || [ ] ;
108
76
109
77
/**
110
78
* Dispose the line decoration between start and end
111
79
*/
112
- if (
113
- startLine &&
114
- endLine &&
115
- currentFileDecorations . length &&
116
- diffLine <= 0
117
- ) {
118
- for ( let i = startLine ; i <= endLine ; i ++ ) {
119
- for ( let j = 0 ; j < currentFileDecorations . length ; j ++ ) {
120
- if ( currentFileDecorations [ j ] . line === i ) {
121
- currentFileDecorations [ j ] . disposable . dispose ( ) ;
122
- console . log ( "dispose" , i ) ;
123
- currentFileDecorations . splice ( j -- , 1 ) ;
124
- }
125
- }
126
- }
80
+ if ( currentFileDecorations . length ) {
81
+ currentFileDecorations . forEach ( ( item ) => {
82
+ item . dispose ( ) ;
83
+ } ) ;
84
+ currentFileDecorations = [ ] ;
127
85
}
128
86
129
- // if (diffLine > 0) {
130
- // return;
131
- // }
132
-
133
87
fullTokenKeys . forEach ( ( key : string ) => {
134
88
if ( ! activeEditor ) {
135
89
return ;
@@ -152,47 +106,34 @@ export default function setupChangeEvent(
152
106
) ;
153
107
const currentLine = startPos . line ;
154
108
155
- if (
156
- ! isEdit ||
157
- // diffLine > 0 ||
158
- ( startLine &&
159
- endLine &&
160
- currentLine >= startLine &&
161
- currentLine <= endLine )
162
- ) {
163
- const value = String ( fullToken [ key ] ) ;
164
- const colorSpan = genMarkdownString ( value ) ;
165
- const markDownString = new vscode . MarkdownString (
166
- `<h3>antd design token: ${ match [ 0 ] } </h3>${ colorSpan } <code>${ value } </code><br></br>`
167
- ) ;
168
- markDownString . supportHtml = true ;
169
- markDownString . isTrusted = true ;
170
-
171
- const decoration = {
172
- range : new vscode . Range ( startPos , endPos ) ,
173
- hoverMessage : markDownString ,
174
- } ;
175
-
176
- const colorValue = getColorTokenValue ( fullToken [ key ] ) ;
177
- valueDecorations . push ( decoration ) ;
178
-
179
- decorationType = vscode . window . createTextEditorDecorationType ( {
180
- after : {
181
- contentText : colorValue ? `**` : `(${ String ( fullToken [ key ] ) } )` ,
182
- backgroundColor : colorValue || "" ,
183
- margin : "0 0 0 4px;" ,
184
- color : colorValue || "#1890ff" ,
185
- fontWeight : "bolder" ,
186
- } ,
187
- } ) ;
188
-
189
- currentFileDecorations . push ( {
190
- line : currentLine ,
191
- disposable : decorationType ,
192
- } ) ;
193
- console . log ( "setDecorations" , currentLine ) ;
194
- activeEditor . setDecorations ( decorationType , valueDecorations ) ;
195
- }
109
+ const value = String ( fullToken [ key ] ) ;
110
+ const colorSpan = genMarkdownString ( value ) ;
111
+ const markDownString = new vscode . MarkdownString (
112
+ `<h3>antd design token: ${ match [ 0 ] } </h3>${ colorSpan } <code>${ value } </code><br></br>`
113
+ ) ;
114
+ markDownString . supportHtml = true ;
115
+ markDownString . isTrusted = true ;
116
+
117
+ const decoration = {
118
+ range : new vscode . Range ( startPos , endPos ) ,
119
+ hoverMessage : markDownString ,
120
+ } ;
121
+
122
+ const colorValue = getColorTokenValue ( fullToken [ key ] ) ;
123
+ valueDecorations . push ( decoration ) ;
124
+
125
+ decorationType = vscode . window . createTextEditorDecorationType ( {
126
+ after : {
127
+ contentText : colorValue ? `**` : `(${ String ( fullToken [ key ] ) } )` ,
128
+ backgroundColor : colorValue || "" ,
129
+ margin : "0 0 0 4px;" ,
130
+ color : colorValue || "#1890ff" ,
131
+ fontWeight : "bolder" ,
132
+ } ,
133
+ } ) ;
134
+
135
+ currentFileDecorations . push ( decorationType ) ;
136
+ activeEditor . setDecorations ( decorationType , valueDecorations ) ;
196
137
}
197
138
} ) ;
198
139
fileDecorationMap . set ( fileName , currentFileDecorations ) ;
0 commit comments