diff options
Diffstat (limited to 'editors/code/src/commands/inlay_hints.ts')
-rw-r--r-- | editors/code/src/commands/inlay_hints.ts | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 3ba9da48b..5393a2bc9 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts | |||
@@ -22,53 +22,56 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ | |||
22 | export class HintsUpdater { | 22 | export class HintsUpdater { |
23 | private displayHints = true; | 23 | private displayHints = true; |
24 | 24 | ||
25 | public async loadHints( | ||
26 | editor: vscode.TextEditor | undefined | ||
27 | ): Promise<void> { | ||
28 | if ( | ||
29 | this.displayHints && | ||
30 | editor !== undefined && | ||
31 | this.isRustDocument(editor.document) | ||
32 | ) { | ||
33 | await this.updateDecorationsFromServer( | ||
34 | editor.document.uri.toString(), | ||
35 | editor | ||
36 | ); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | public async toggleHintsDisplay(displayHints: boolean): Promise<void> { | 25 | public async toggleHintsDisplay(displayHints: boolean): Promise<void> { |
41 | if (this.displayHints !== displayHints) { | 26 | if (this.displayHints !== displayHints) { |
42 | this.displayHints = displayHints; | 27 | this.displayHints = displayHints; |
43 | 28 | return this.refreshVisibleEditorsHints( | |
44 | if (displayHints) { | 29 | displayHints ? undefined : [] |
45 | return this.updateHints(); | 30 | ); |
46 | } else { | ||
47 | const editor = vscode.window.activeTextEditor; | ||
48 | if (editor != null) { | ||
49 | return editor.setDecorations(typeHintDecorationType, []); | ||
50 | } | ||
51 | } | ||
52 | } | 31 | } |
53 | } | 32 | } |
54 | 33 | ||
55 | public async updateHints(cause?: TextDocumentChangeEvent): Promise<void> { | 34 | public async refreshHintsForVisibleEditors( |
35 | cause?: TextDocumentChangeEvent | ||
36 | ): Promise<void> { | ||
56 | if (!this.displayHints) { | 37 | if (!this.displayHints) { |
57 | return; | 38 | return; |
58 | } | 39 | } |
59 | const editor = vscode.window.activeTextEditor; | 40 | if ( |
60 | if (editor == null) { | 41 | cause !== undefined && |
42 | (cause.contentChanges.length === 0 || | ||
43 | !this.isRustDocument(cause.document)) | ||
44 | ) { | ||
61 | return; | 45 | return; |
62 | } | 46 | } |
63 | const document = cause == null ? editor.document : cause.document; | 47 | return this.refreshVisibleEditorsHints(); |
64 | if (!this.isRustDocument(document)) { | 48 | } |
65 | return; | 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 | } | ||
66 | } | 70 | } |
67 | 71 | ||
68 | return await this.updateDecorationsFromServer( | 72 | for (const promise of promises) { |
69 | document.uri.toString(), | 73 | await promise; |
70 | editor | 74 | } |
71 | ); | ||
72 | } | 75 | } |
73 | 76 | ||
74 | private isRustDocument(document: vscode.TextDocument): boolean { | 77 | private isRustDocument(document: vscode.TextDocument): boolean { |
@@ -76,11 +79,10 @@ export class HintsUpdater { | |||
76 | } | 79 | } |
77 | 80 | ||
78 | private async updateDecorationsFromServer( | 81 | private async updateDecorationsFromServer( |
79 | documentUri: string, | ||
80 | editor: TextEditor | 82 | editor: TextEditor |
81 | ): Promise<void> { | 83 | ): Promise<void> { |
82 | const newHints = await this.queryHints(documentUri); | 84 | const newHints = await this.queryHints(editor.document.uri.toString()); |
83 | if (newHints != null) { | 85 | if (newHints !== null) { |
84 | const newDecorations = newHints.map(hint => ({ | 86 | const newDecorations = newHints.map(hint => ({ |
85 | range: hint.range, | 87 | range: hint.range, |
86 | renderOptions: { after: { contentText: `: ${hint.label}` } } | 88 | renderOptions: { after: { contentText: `: ${hint.label}` } } |