diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-06 16:50:49 +0000 |
|---|---|---|
| committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-06 16:50:49 +0000 |
| commit | 7e12422fa2c8ba7b3df63b4e4297464a0071a359 (patch) | |
| tree | f88de34c48e08ea8cf481a29f5764f13492a9ac9 /editors/code/src | |
| parent | 811492aa546d83daf56f61d334d6ee295651f111 (diff) | |
| parent | c5598d9ade92e9ec4474a14229bb34a44a4edad5 (diff) | |
| download | rust-7e12422fa2c8ba7b3df63b4e4297464a0071a359.tar.gz rust-7e12422fa2c8ba7b3df63b4e4297464a0071a359.zip | |
Merge #1652
1652: Improve type hints behavior r=matklad a=SomeoneToIgnore This PR fixed the following type hints issues: * Restructures the `InlayKind` enum contents based on the discussion here: https://github.com/rust-analyzer/rust-analyzer/pull/1606#issuecomment-515968055 * Races described in #1639 * Caches the latest decorations received for each file to show them the next time the file is opened (instead of a new server request) Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
Diffstat (limited to 'editors/code/src')
| -rw-r--r-- | editors/code/src/commands/inlay_hints.ts | 76 | ||||
| -rw-r--r-- | editors/code/src/extension.ts | 20 |
2 files changed, 55 insertions, 41 deletions
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 3ba9da48b66..5393a2bc928 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({ export class HintsUpdater { private displayHints = true; - public async loadHints( - editor: vscode.TextEditor | undefined - ): Promise<void> { - if ( - this.displayHints && - editor !== undefined && - this.isRustDocument(editor.document) - ) { - await this.updateDecorationsFromServer( - editor.document.uri.toString(), - editor - ); - } - } - public async toggleHintsDisplay(displayHints: boolean): Promise<void> { if (this.displayHints !== displayHints) { this.displayHints = displayHints; - - if (displayHints) { - return this.updateHints(); - } else { - const editor = vscode.window.activeTextEditor; - if (editor != null) { - return editor.setDecorations(typeHintDecorationType, []); - } - } + return this.refreshVisibleEditorsHints( + displayHints ? undefined : [] + ); } } - public async updateHints(cause?: TextDocumentChangeEvent): Promise<void> { + public async refreshHintsForVisibleEditors( + cause?: TextDocumentChangeEvent + ): Promise<void> { if (!this.displayHints) { return; } - const editor = vscode.window.activeTextEditor; - if (editor == null) { + if ( + cause !== undefined && + (cause.contentChanges.length === 0 || + !this.isRustDocument(cause.document)) + ) { return; } - const document = cause == null ? editor.document : cause.document; - if (!this.isRustDocument(document)) { - return; + return this.refreshVisibleEditorsHints(); + } + + private async refreshVisibleEditorsHints( + newDecorations?: vscode.DecorationOptions[] + ) { + const promises: Array<Promise<void>> = []; + + for (const rustEditor of vscode.window.visibleTextEditors.filter( + editor => this.isRustDocument(editor.document) + )) { + if (newDecorations !== undefined) { + promises.push( + Promise.resolve( + rustEditor.setDecorations( + typeHintDecorationType, + newDecorations + ) + ) + ); + } else { + promises.push(this.updateDecorationsFromServer(rustEditor)); + } } - return await this.updateDecorationsFromServer( - document.uri.toString(), - editor - ); + for (const promise of promises) { + await promise; + } } private isRustDocument(document: vscode.TextDocument): boolean { @@ -76,11 +79,10 @@ export class HintsUpdater { } private async updateDecorationsFromServer( - documentUri: string, editor: TextEditor ): Promise<void> { - const newHints = await this.queryHints(documentUri); - if (newHints != null) { + const newHints = await this.queryHints(editor.document.uri.toString()); + if (newHints !== null) { const newDecorations = newHints.map(hint => ({ range: hint.range, renderOptions: { after: { contentText: `: ${hint.label}` } } diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index c6efc2e7e04..39fe6efd889 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -151,15 +151,27 @@ export function activate(context: vscode.ExtensionContext) { if (Server.config.displayInlayHints) { const hintsUpdater = new HintsUpdater(); - hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => { + hintsUpdater.refreshHintsForVisibleEditors().then(() => { + // vscode may ignore top level hintsUpdater.refreshHintsForVisibleEditors() + // so update the hints once when the focus changes to guarantee their presence + let editorChangeDisposable: vscode.Disposable | null = null; + editorChangeDisposable = vscode.window.onDidChangeActiveTextEditor( + _ => { + if (editorChangeDisposable !== null) { + editorChangeDisposable.dispose(); + } + return hintsUpdater.refreshHintsForVisibleEditors(); + } + ); + disposeOnDeactivation( - vscode.window.onDidChangeActiveTextEditor(editor => - hintsUpdater.loadHints(editor) + vscode.window.onDidChangeVisibleTextEditors(_ => + hintsUpdater.refreshHintsForVisibleEditors() ) ); disposeOnDeactivation( vscode.workspace.onDidChangeTextDocument(e => - hintsUpdater.updateHints(e) + hintsUpdater.refreshHintsForVisibleEditors(e) ) ); disposeOnDeactivation( |
