11import * as vscode from "vscode" ;
22import { genMarkdownString , getColorTokenValue } from "./utils" ;
33
4- interface DecorationItem {
5- line : number ;
6- disposable : vscode . TextEditorDecorationType ;
7- }
8-
94export default function setupChangeEvent (
105 context : vscode . ExtensionContext ,
116 fullToken : any
127) {
138 let timeout : NodeJS . Timer | undefined = undefined ;
149 let activeEditor = vscode . window . activeTextEditor ;
1510 const fullTokenKeys = Object . keys ( fullToken ) ;
16- const fileDecorationMap = new Map < string , DecorationItem [ ] > ( ) ;
11+ const fileDecorationMap = new Map <
12+ string ,
13+ vscode . TextEditorDecorationType [ ]
14+ > ( ) ;
1715 const openedFileNameSet = new Set < string > ( ) ;
18- let lineCount = 0 ;
1916
2017 if ( activeEditor ) {
21- lineCount = activeEditor . document . lineCount ;
2218 const isOpened = checkOpenedFile ( activeEditor . document . fileName ) ;
2319
2420 if ( ! isOpened ) {
@@ -32,19 +28,11 @@ export default function setupChangeEvent(
3228 return ;
3329 }
3430
35- const startLine = event . contentChanges [ 0 ] . range . start . line ;
36- let endLine = event . contentChanges [ 0 ] . range . end . line ;
37-
3831 if ( activeEditor && event . document === activeEditor . document ) {
3932 /**
4033 * As undo (reason === 1) or redo (reason === 2) are very fast, do it very fast too.
41- * Same as `delete` some code
4234 */
43- const throttle =
44- event . reason !== undefined
45- ? false
46- : event . document . lineCount - lineCount >= 0 ;
47- triggerUpdateDecorations ( throttle , true , startLine , endLine ) ;
35+ triggerUpdateDecorations ( true ) ;
4836 }
4937 } ,
5038 null ,
@@ -55,7 +43,6 @@ export default function setupChangeEvent(
5543 ( editor ) => {
5644 activeEditor = editor ;
5745 if ( editor ) {
58- lineCount = editor . document . lineCount ;
5946 const isOpened = checkOpenedFile ( editor . document . fileName ) ;
6047
6148 if ( ! isOpened ) {
@@ -67,69 +54,36 @@ export default function setupChangeEvent(
6754 context . subscriptions
6855 ) ;
6956
70- function triggerUpdateDecorations (
71- throttle = false ,
72- isEdit = false ,
73- startLine ?: number ,
74- endLine ?: number
75- ) {
57+ function triggerUpdateDecorations ( throttle = false ) {
7658 if ( timeout ) {
7759 clearTimeout ( timeout ) ;
7860 timeout = undefined ;
7961 }
8062 if ( throttle ) {
8163 timeout = setTimeout ( ( ) => {
82- updateDecorations ( isEdit , startLine , endLine ) ;
64+ updateDecorations ( ) ;
8365 } , 500 ) ;
8466 } else {
85- updateDecorations ( isEdit , startLine , endLine ) ;
67+ updateDecorations ( ) ;
8668 }
8769 }
8870
89- function updateDecorations (
90- isEdit : boolean ,
91- startLine ?: number ,
92- endLine ?: number
93- ) {
71+ function updateDecorations ( ) {
9472 if ( activeEditor ) {
95- console . log ( "!!!!!!update!!!!" , startLine , endLine ) ;
9673 const text = activeEditor . document . getText ( ) ;
9774 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 ) || [ ] ;
10876
10977 /**
11078 * Dispose the line decoration between start and end
11179 */
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 = [ ] ;
12785 }
12886
129- // if (diffLine > 0) {
130- // return;
131- // }
132-
13387 fullTokenKeys . forEach ( ( key : string ) => {
13488 if ( ! activeEditor ) {
13589 return ;
@@ -152,47 +106,34 @@ export default function setupChangeEvent(
152106 ) ;
153107 const currentLine = startPos . line ;
154108
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 ) ;
196137 }
197138 } ) ;
198139 fileDecorationMap . set ( fileName , currentFileDecorations ) ;
0 commit comments