@@ -7,9 +7,8 @@ import * as notifications from "./lspExt";
77
88let D_PARAM : vscode . TextEditorDecorationType ;
99let D_GLOBAL : vscode . TextEditorDecorationType ;
10- let D_DOC_TYPE : vscode . TextEditorDecorationType ;
10+ let D_LOCALS : vscode . TextEditorDecorationType [ ] ;
1111let D_UPVALUE : vscode . TextEditorDecorationType ;
12- let D_NOTUSE : vscode . TextEditorDecorationType ;
1312
1413function createDecoration ( key : string | undefined , config : vscode . DecorationRenderOptions = { } ) : vscode . TextEditorDecorationType {
1514 if ( key == undefined ) {
@@ -23,19 +22,29 @@ function createDecoration(key: string | undefined, config: vscode.DecorationRend
2322 return vscode . window . createTextEditorDecorationType ( config ) ;
2423}
2524
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+ } ) ) ;
32+ }
33+ return [ ] ;
34+ }
35+
2636function updateDecorations ( ) {
2737 // 各种方式更新时之前的decoration没有dispose导致重复渲染
2838 if ( D_PARAM ) {
2939 D_PARAM . dispose ( ) ;
3040 D_GLOBAL . dispose ( ) ;
31- D_DOC_TYPE . dispose ( ) ;
41+ D_LOCALS . forEach ( d => d . dispose ( ) ) ;
3242 D_UPVALUE . dispose ( ) ;
33- D_NOTUSE . dispose ( ) ;
3443 }
3544
3645 D_PARAM = createDecoration ( "colors.parameter" ) ;
3746 D_GLOBAL = createDecoration ( "colors.global" ) ;
38- D_DOC_TYPE = createDecoration ( "colors.doc_type " ) ;
47+ D_LOCALS = createDecorations ( "colors.local " ) ;
3948
4049 let upvalueColor = vscode . workspace . getConfiguration ( "emmylua" ) . get ( "colors.upvalue" ) ;
4150 if ( upvalueColor && upvalueColor != "" ) {
@@ -46,8 +55,6 @@ function updateDecorations() {
4655 else {
4756 D_UPVALUE = createDecoration ( undefined ) ;
4857 }
49-
50- D_NOTUSE = createDecoration ( "colors.not_use" , { } ) ;
5158}
5259
5360export function onDidChangeConfiguration ( client : LanguageClient ) {
@@ -70,60 +77,63 @@ function requestAnnotatorsImpl(editor: vscode.TextEditor, client: LanguageClient
7077
7178 let params : notifications . AnnotatorParams = { uri : editor . document . uri . toString ( ) } ;
7279 client . sendRequest < notifications . IAnnotator [ ] > ( "emmy/annotator" , params ) . then ( list => {
73- let map : Map < AnnotatorType , notifications . RenderRange [ ] > = new Map ( ) ;
74- map . set ( AnnotatorType . DocType , [ ] ) ;
80+ let map : Map < AnnotatorType , vscode . Range [ ] > = new Map ( ) ;
7581 map . set ( AnnotatorType . Param , [ ] ) ;
7682 map . set ( AnnotatorType . Global , [ ] ) ;
7783 map . set ( AnnotatorType . Upvalue , [ ] ) ;
78- map . set ( AnnotatorType . NotUse , [ ] ) ;
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+ }
89+
7990 if ( ! list ) {
8091 return ;
8192 }
8293
83- list . forEach ( data => {
84- let uri = vscode . Uri . parse ( data . uri ) ;
85- let uriSet = new Set < string > ( ) ;
86- // 而vscode 在diff,分屏以及其他一些情况下可以获得多个相同的uri
87- vscode . window . visibleTextEditors . forEach ( ( editor ) => {
88- let docUri = editor . document . uri ;
89- if ( uriSet . has ( docUri . path ) ) {
90- return ;
94+ let local_index = 0 ;
95+
96+ 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 ) ;
91107 }
92- uriSet . add ( docUri . path )
93-
94- if ( uri . path . toLowerCase ( ) === docUri . path . toLowerCase ( ) ) {
95- let list = map . get ( data . type ) ;
96- if ( list === undefined ) {
97- list = data . ranges ;
98- } else {
99- list = list . concat ( data . ranges ) ;
100- }
101- map . set ( data . type , list ) ;
108+ ranges . push ( ...annotation . ranges ) ;
109+ local_index ++ ;
110+ if ( local_index >= D_LOCALS . length ) {
111+ local_index = 0 ;
102112 }
103- } ) ;
113+ }
104114 } ) ;
105115 map . forEach ( ( v , k ) => {
106116 updateAnnotators ( editor , k , v ) ;
107117 } ) ;
118+
119+ local_maps . forEach ( ( v , i ) => {
120+ if ( i < D_LOCALS . length ) {
121+ editor . setDecorations ( D_LOCALS [ i ] , v ) ;
122+ }
123+ } ) ;
108124 } ) ;
109125}
110126
111- function updateAnnotators ( editor : vscode . TextEditor , type : AnnotatorType , renderRanges : notifications . RenderRange [ ] ) {
127+ function updateAnnotators ( editor : vscode . TextEditor , type : AnnotatorType , ranges : vscode . Range [ ] ) {
112128 switch ( type ) {
113129 case AnnotatorType . Param :
114- editor . setDecorations ( D_PARAM , renderRanges . map ( e => e . range ) ) ;
130+ editor . setDecorations ( D_PARAM , ranges ) ;
115131 break ;
116132 case AnnotatorType . Global :
117- editor . setDecorations ( D_GLOBAL , renderRanges . map ( e => e . range ) ) ;
118- break ;
119- case AnnotatorType . DocType :
120- editor . setDecorations ( D_DOC_TYPE , renderRanges . map ( e => e . range ) ) ;
133+ editor . setDecorations ( D_GLOBAL , ranges ) ;
121134 break ;
122135 case AnnotatorType . Upvalue :
123- editor . setDecorations ( D_UPVALUE , renderRanges . map ( e => e . range ) ) ;
124- break ;
125- case AnnotatorType . NotUse :
126- editor . setDecorations ( D_NOTUSE , renderRanges . map ( e => e . range ) ) ;
136+ editor . setDecorations ( D_UPVALUE , ranges ) ;
127137 break ;
128138 }
129139}
0 commit comments