@@ -21,91 +21,72 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
21
21
22
22
export class HintsUpdater {
23
23
private displayHints = true ;
24
- private decorationsSinceLastChange = new Map <
25
- string ,
26
- vscode . DecorationOptions [ ]
27
- > ( ) ;
28
-
29
- public async loadHints ( editor ?: vscode . TextEditor ) : Promise < void > {
30
- if ( this . displayHints ) {
31
- const documentUri = this . getEditorDocumentUri ( editor ) ;
32
- if ( documentUri !== null ) {
33
- const latestDecorations = this . decorationsSinceLastChange . get (
34
- documentUri . toString ( )
35
- ) ;
36
- if ( latestDecorations === undefined ) {
37
- await this . updateDecorationsFromServer (
38
- documentUri ,
39
- editor !
40
- ) ;
41
- } else {
42
- await editor ! . setDecorations (
43
- typeHintDecorationType ,
44
- latestDecorations
45
- ) ;
46
- }
47
- }
48
- }
49
- }
50
24
51
25
public async toggleHintsDisplay ( displayHints : boolean ) : Promise < void > {
52
26
if ( this . displayHints !== displayHints ) {
53
27
this . displayHints = displayHints ;
54
- this . decorationsSinceLastChange . clear ( ) ;
55
-
56
- if ( displayHints ) {
57
- return this . updateHints ( ) ;
58
- } else {
59
- const currentEditor = vscode . window . activeTextEditor ;
60
- if ( this . getEditorDocumentUri ( currentEditor ) !== null ) {
61
- return currentEditor ! . setDecorations (
62
- typeHintDecorationType ,
63
- [ ]
64
- ) ;
65
- }
66
- }
28
+ return this . refreshVisibleEditorsHints (
29
+ displayHints ? undefined : [ ]
30
+ ) ;
67
31
}
68
32
}
69
33
70
- public async updateHints ( cause ?: TextDocumentChangeEvent ) : Promise < void > {
34
+ public async refreshHintsForVisibleEditors (
35
+ cause ?: TextDocumentChangeEvent
36
+ ) : Promise < void > {
71
37
if ( ! this . displayHints ) {
72
38
return ;
73
39
}
74
- const editor = vscode . window . activeTextEditor ;
75
- if ( editor === undefined ) {
40
+ if (
41
+ cause !== undefined &&
42
+ ( cause . contentChanges . length === 0 ||
43
+ ! this . isRustDocument ( cause . document ) )
44
+ ) {
76
45
return ;
77
46
}
78
- const document = cause === undefined ? editor . document : cause . document ;
79
- if ( ! this . isRustDocument ( document ) ) {
80
- return ;
47
+ return this . refreshVisibleEditorsHints ( ) ;
48
+ }
49
+
50
+ private async refreshVisibleEditorsHints (
51
+ newDecorations ?: vscode . DecorationOptions [ ]
52
+ ) {
53
+ const promises : Array < Promise < void > > = [ ] ;
54
+
55
+ for ( const rustEditor of vscode . window . visibleTextEditors . filter (
56
+ editor => this . isRustDocument ( editor . document )
57
+ ) ) {
58
+ if ( newDecorations !== undefined ) {
59
+ promises . push (
60
+ Promise . resolve (
61
+ rustEditor . setDecorations (
62
+ typeHintDecorationType ,
63
+ newDecorations
64
+ )
65
+ )
66
+ ) ;
67
+ } else {
68
+ promises . push ( this . updateDecorationsFromServer ( rustEditor ) ) ;
69
+ }
81
70
}
82
71
83
- this . decorationsSinceLastChange . clear ( ) ;
84
- return await this . updateDecorationsFromServer ( document . uri , editor ) ;
72
+ for ( const promise of promises ) {
73
+ await promise ;
74
+ }
85
75
}
86
76
87
77
private isRustDocument ( document : vscode . TextDocument ) : boolean {
88
78
return document && document . languageId === 'rust' ;
89
79
}
90
80
91
81
private async updateDecorationsFromServer (
92
- documentUri : vscode . Uri ,
93
82
editor : TextEditor
94
83
) : Promise < void > {
95
- const newHints = await this . queryHints ( documentUri . toString ( ) ) ;
96
- if (
97
- newHints !== null &&
98
- this . getEditorDocumentUri ( vscode . window . activeTextEditor ) ===
99
- documentUri
100
- ) {
84
+ const newHints = await this . queryHints ( editor . document . uri . toString ( ) ) ;
85
+ if ( newHints !== null ) {
101
86
const newDecorations = newHints . map ( hint => ( {
102
87
range : hint . range ,
103
88
renderOptions : { after : { contentText : `: ${ hint . label } ` } }
104
89
} ) ) ;
105
- this . decorationsSinceLastChange . set (
106
- documentUri . toString ( ) ,
107
- newDecorations
108
- ) ;
109
90
return editor . setDecorations (
110
91
typeHintDecorationType ,
111
92
newDecorations
@@ -127,13 +108,4 @@ export class HintsUpdater {
127
108
)
128
109
) ;
129
110
}
130
-
131
- private getEditorDocumentUri (
132
- editor ?: vscode . TextEditor
133
- ) : vscode . Uri | null {
134
- if ( editor && this . isRustDocument ( editor . document ) ) {
135
- return editor . document . uri ;
136
- }
137
- return null ;
138
- }
139
111
}
0 commit comments