From c390e92fdd25ced46c589bfbff94e4b0bc4d9c38 Mon Sep 17 00:00:00 2001 From: imtsuki Date: Wed, 15 Jan 2020 01:02:01 +0800 Subject: Add inlay parameter name hints for function calls Signed-off-by: imtsuki --- editors/code/package.json | 2 +- editors/code/src/inlay_hints.ts | 51 +++++++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 11 deletions(-) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index 7c22d21d3..ed637d114 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -228,7 +228,7 @@ "rust-analyzer.displayInlayHints": { "type": "boolean", "default": true, - "description": "Display additional type information in the editor" + "description": "Display additional type and parameter information in the editor" }, "rust-analyzer.maxInlayHintLength": { "type": "number", diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 078d18f0f..c4206cf5b 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -38,6 +38,12 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ }, }); +const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({ + before: { + color: new vscode.ThemeColor('rust_analyzer.inlayHint'), + } +}) + class HintsUpdater { private pending: Map = new Map(); private ctx: Ctx; @@ -55,7 +61,10 @@ class HintsUpdater { if (this.enabled) { await this.refresh(); } else { - this.allEditors.forEach(it => this.setDecorations(it, [])); + this.allEditors.forEach(it => { + this.setTypeDecorations(it, []); + this.setParameterDecorations(it, []); + }); } } @@ -68,15 +77,27 @@ class HintsUpdater { private async refreshEditor(editor: vscode.TextEditor): Promise { const newHints = await this.queryHints(editor.document.uri.toString()); if (newHints == null) return; - const newDecorations = newHints.map(hint => ({ - range: hint.range, - renderOptions: { - after: { - contentText: `: ${hint.label}`, + const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint') + .map(hint => ({ + range: hint.range, + renderOptions: { + after: { + contentText: `: ${hint.label}`, + }, }, - }, - })); - this.setDecorations(editor, newDecorations); + })); + this.setTypeDecorations(editor, newTypeDecorations); + + const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint') + .map(hint => ({ + range: hint.range, + renderOptions: { + before: { + contentText: `${hint.label}: `, + }, + }, + })); + this.setParameterDecorations(editor, newParameterDecorations); } private get allEditors(): vscode.TextEditor[] { @@ -85,7 +106,7 @@ class HintsUpdater { ); } - private setDecorations( + private setTypeDecorations( editor: vscode.TextEditor, decorations: vscode.DecorationOptions[], ) { @@ -95,6 +116,16 @@ class HintsUpdater { ); } + private setParameterDecorations( + editor: vscode.TextEditor, + decorations: vscode.DecorationOptions[], + ) { + editor.setDecorations( + parameterHintDecorationType, + this.enabled ? decorations : [], + ); + } + private async queryHints(documentUri: string): Promise { let client = this.ctx.client; if (!client) return null; -- cgit v1.2.3