1
-
2
1
import * as vscode from 'vscode' ;
3
2
import { AnnotatorType } from './lspExt' ;
4
3
import { LanguageClient } from 'vscode-languageclient/node' ;
@@ -7,57 +6,60 @@ import * as notifications from "./lspExt";
7
6
8
7
let D_PARAM : vscode . TextEditorDecorationType ;
9
8
let 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 ;
12
12
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 = { }
17
15
let color = vscode . workspace . getConfiguration ( "emmylua" ) . get ( key ) ;
18
16
if ( typeof ( color ) === 'string' ) {
19
- config . light = { color : color } ;
20
- config . dark = { color : color } ;
17
+ config . light = { color } ;
18
+ config . dark = { color } ;
21
19
}
22
20
return vscode . window . createTextEditorDecorationType ( config ) ;
23
21
}
24
22
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
+ } ;
32
42
}
33
- return [ ] ;
43
+ return vscode . window . createTextEditorDecorationType ( config ) ;
44
+ }
45
+
46
+ function disposeDecorations ( ...decorations : ( vscode . TextEditorDecorationType | undefined ) [ ] ) {
47
+ decorations . forEach ( d => d && d . dispose ( ) ) ;
34
48
}
35
49
36
50
function updateDecorations ( ) {
37
- // 各种方式更新时之前的decoration没有dispose导致重复渲染
38
51
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 ) ;
43
53
}
44
54
45
55
D_PARAM = createDecoration ( "colors.parameter" ) ;
46
56
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" ) ;
58
60
}
59
61
60
- export function onDidChangeConfiguration ( client : LanguageClient ) {
62
+ export function onDidChangeConfiguration ( ) {
61
63
updateDecorations ( ) ;
62
64
}
63
65
@@ -78,62 +80,44 @@ function requestAnnotatorsImpl(editor: vscode.TextEditor, client: LanguageClient
78
80
let params : notifications . AnnotatorParams = { uri : editor . document . uri . toString ( ) } ;
79
81
client . sendRequest < notifications . IAnnotator [ ] > ( "emmy/annotator" , params ) . then ( list => {
80
82
let map : Map < AnnotatorType , vscode . Range [ ] > = new Map ( ) ;
81
- map . set ( AnnotatorType . Param , [ ] ) ;
83
+ map . set ( AnnotatorType . ReadOnlyParam , [ ] ) ;
82
84
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 , [ ] ) ;
89
88
90
89
if ( ! list ) {
91
90
return ;
92
91
}
93
92
94
- let local_index = 0 ;
95
-
96
93
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 ) {
108
96
ranges . push ( ...annotation . ranges ) ;
109
- local_index ++ ;
110
- if ( local_index >= D_LOCALS . length ) {
111
- local_index = 0 ;
112
- }
113
97
}
114
98
} ) ;
115
99
map . forEach ( ( v , k ) => {
116
100
updateAnnotators ( editor , k , v ) ;
117
101
} ) ;
118
-
119
- local_maps . forEach ( ( v , i ) => {
120
- if ( i < D_LOCALS . length ) {
121
- editor . setDecorations ( D_LOCALS [ i ] , v ) ;
122
- }
123
- } ) ;
124
102
} ) ;
125
103
}
126
104
127
105
function updateAnnotators ( editor : vscode . TextEditor , type : AnnotatorType , ranges : vscode . Range [ ] ) {
128
106
switch ( type ) {
129
- case AnnotatorType . Param :
107
+ case AnnotatorType . ReadOnlyParam :
130
108
editor . setDecorations ( D_PARAM , ranges ) ;
131
109
break ;
132
110
case AnnotatorType . Global :
133
111
editor . setDecorations ( D_GLOBAL , ranges ) ;
134
112
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 ) ;
137
121
break ;
138
122
}
139
123
}
0 commit comments