1-
21import * as vscode from 'vscode' ;
32import { AnnotatorType } from './lspExt' ;
43import { LanguageClient } from 'vscode-languageclient/node' ;
@@ -7,57 +6,60 @@ import * as notifications from "./lspExt";
76
87let D_PARAM : vscode . TextEditorDecorationType ;
98let D_GLOBAL : vscode . TextEditorDecorationType ;
10- let D_LOCALS : vscode . TextEditorDecorationType [ ] ;
11- let D_UPVALUE : vscode . TextEditorDecorationType ;
9+ let D_LOCAL : vscode . TextEditorDecorationType ;
10+ let D_MUT_LOCAL : vscode . TextEditorDecorationType ;
11+ let D_MUT_PARAM : vscode . TextEditorDecorationType ;
1212
13- function createDecoration ( key : string | undefined , config : vscode . DecorationRenderOptions = { } ) : vscode . TextEditorDecorationType {
14- if ( key == undefined ) {
15- return vscode . window . createTextEditorDecorationType ( config ) ;
16- }
13+ function createDecoration ( key : string ) : vscode . TextEditorDecorationType {
14+ let config : vscode . DecorationRenderOptions = { }
1715 let color = vscode . workspace . getConfiguration ( "emmylua" ) . get ( key ) ;
1816 if ( typeof ( color ) === 'string' ) {
19- config . light = { color : color } ;
20- config . dark = { color : color } ;
17+ config . light = { color } ;
18+ config . dark = { color } ;
2119 }
2220 return vscode . window . createTextEditorDecorationType ( config ) ;
2321}
2422
25- function createDecorations ( key : string , config : vscode . DecorationRenderOptions = { } ) : vscode . TextEditorDecorationType [ ] {
26- let colors = vscode . workspace . getConfiguration ( "emmylua" ) . get ( key ) ;
27- if ( colors instanceof Array ) {
28- return colors . map ( color => vscode . window . createTextEditorDecorationType ( {
29- light : { color : color } ,
30- dark : { color : color }
31- } ) ) ;
23+ function createDecorationUnderline ( key : string ) : vscode . TextEditorDecorationType {
24+ let config : vscode . DecorationRenderOptions = { }
25+ let color = vscode . workspace . getConfiguration ( "emmylua" ) . get ( key ) ;
26+ if ( typeof ( color ) === 'string' ) {
27+ config . light = {
28+ color,
29+ textDecoration : `underline;text-decoration-color:${ color } ;`
30+ } ;
31+ config . dark = {
32+ color,
33+ textDecoration : `underline;text-decoration-color:${ color } ;`
34+ } ;
35+ } else {
36+ config . light = {
37+ textDecoration : `underline;`
38+ } ;
39+ config . dark = {
40+ textDecoration : `underline;`
41+ } ;
3242 }
33- return [ ] ;
43+ return vscode . window . createTextEditorDecorationType ( config ) ;
44+ }
45+
46+ function disposeDecorations ( ...decorations : ( vscode . TextEditorDecorationType | undefined ) [ ] ) {
47+ decorations . forEach ( d => d && d . dispose ( ) ) ;
3448}
3549
3650function updateDecorations ( ) {
37- // 各种方式更新时之前的decoration没有dispose导致重复渲染
3851 if ( D_PARAM ) {
39- D_PARAM . dispose ( ) ;
40- D_GLOBAL . dispose ( ) ;
41- D_LOCALS . forEach ( d => d . dispose ( ) ) ;
42- D_UPVALUE . dispose ( ) ;
52+ disposeDecorations ( D_PARAM , D_GLOBAL , D_LOCAL , D_MUT_LOCAL , D_MUT_PARAM ) ;
4353 }
4454
4555 D_PARAM = createDecoration ( "colors.parameter" ) ;
4656 D_GLOBAL = createDecoration ( "colors.global" ) ;
47- D_LOCALS = createDecorations ( "colors.local" ) ;
48-
49- let upvalueColor = vscode . workspace . getConfiguration ( "emmylua" ) . get ( "colors.upvalue" ) ;
50- if ( upvalueColor && upvalueColor != "" ) {
51- D_UPVALUE = createDecoration ( undefined , {
52- textDecoration : `underline;text-decoration-color:${ upvalueColor } ;`
53- } ) ;
54- }
55- else {
56- D_UPVALUE = createDecoration ( undefined ) ;
57- }
57+ D_LOCAL = createDecoration ( "colors.local" ) ;
58+ D_MUT_LOCAL = createDecorationUnderline ( "colors.local" ) ;
59+ D_MUT_PARAM = createDecorationUnderline ( "colors.parameter" ) ;
5860}
5961
60- export function onDidChangeConfiguration ( client : LanguageClient ) {
62+ export function onDidChangeConfiguration ( ) {
6163 updateDecorations ( ) ;
6264}
6365
@@ -78,62 +80,44 @@ function requestAnnotatorsImpl(editor: vscode.TextEditor, client: LanguageClient
7880 let params : notifications . AnnotatorParams = { uri : editor . document . uri . toString ( ) } ;
7981 client . sendRequest < notifications . IAnnotator [ ] > ( "emmy/annotator" , params ) . then ( list => {
8082 let map : Map < AnnotatorType , vscode . Range [ ] > = new Map ( ) ;
81- map . set ( AnnotatorType . Param , [ ] ) ;
83+ map . set ( AnnotatorType . ReadOnlyParam , [ ] ) ;
8284 map . set ( AnnotatorType . Global , [ ] ) ;
83- map . set ( AnnotatorType . Upvalue , [ ] ) ;
84-
85- let local_maps = new Map < number , vscode . Range [ ] > ( ) ;
86- for ( let i = 0 ; i < D_LOCALS . length ; i ++ ) {
87- local_maps . set ( i , [ ] ) ;
88- }
85+ map . set ( AnnotatorType . ReadOnlyLocal , [ ] ) ;
86+ map . set ( AnnotatorType . MutLocal , [ ] ) ;
87+ map . set ( AnnotatorType . MutParam , [ ] ) ;
8988
9089 if ( ! list ) {
9190 return ;
9291 }
9392
94- let local_index = 0 ;
95-
9693 list . forEach ( annotation => {
97- if ( annotation . type !== AnnotatorType . Local ) {
98- let ranges = map . get ( annotation . type ) ;
99- if ( ranges ) {
100- ranges . push ( ...annotation . ranges ) ;
101- }
102- } else if ( D_LOCALS . length > 0 ) {
103- let ranges = local_maps . get ( local_index ) ;
104- if ( ! ranges ) {
105- ranges = [ ] ;
106- local_maps . set ( local_index , ranges ) ;
107- }
94+ let ranges = map . get ( annotation . type ) ;
95+ if ( ranges ) {
10896 ranges . push ( ...annotation . ranges ) ;
109- local_index ++ ;
110- if ( local_index >= D_LOCALS . length ) {
111- local_index = 0 ;
112- }
11397 }
11498 } ) ;
11599 map . forEach ( ( v , k ) => {
116100 updateAnnotators ( editor , k , v ) ;
117101 } ) ;
118-
119- local_maps . forEach ( ( v , i ) => {
120- if ( i < D_LOCALS . length ) {
121- editor . setDecorations ( D_LOCALS [ i ] , v ) ;
122- }
123- } ) ;
124102 } ) ;
125103}
126104
127105function updateAnnotators ( editor : vscode . TextEditor , type : AnnotatorType , ranges : vscode . Range [ ] ) {
128106 switch ( type ) {
129- case AnnotatorType . Param :
107+ case AnnotatorType . ReadOnlyParam :
130108 editor . setDecorations ( D_PARAM , ranges ) ;
131109 break ;
132110 case AnnotatorType . Global :
133111 editor . setDecorations ( D_GLOBAL , ranges ) ;
134112 break ;
135- case AnnotatorType . Upvalue :
136- editor . setDecorations ( D_UPVALUE , ranges ) ;
113+ case AnnotatorType . ReadOnlyLocal :
114+ editor . setDecorations ( D_LOCAL , ranges ) ;
115+ break ;
116+ case AnnotatorType . MutLocal :
117+ editor . setDecorations ( D_MUT_LOCAL , ranges ) ;
118+ break ;
119+ case AnnotatorType . MutParam :
120+ editor . setDecorations ( D_MUT_PARAM , ranges ) ;
137121 break ;
138122 }
139123}
0 commit comments