diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-15 10:24:51 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-15 10:24:51 +0000 |
commit | 876f92d547af5f39170350f3995647ec934f590b (patch) | |
tree | b2dfc9053f910d1f64f3d899628cdaf482953aee /editors | |
parent | bc8be6bcdb7ea9b23cc6723769e6071a705cb88b (diff) | |
parent | d854ad8f279e5a3a4b3908bf5b0afdaba3d37bc9 (diff) |
Merge #2843
2843: Add inlay parameter name hints for call expr r=matklad a=imtsuki
This patch adds Intellij-like parameter name hints for literal values in function calls.
<img width="624" alt="Screenshot" src="https://user-images.githubusercontent.com/8423594/72366533-68d7f800-3735-11ea-9279-cf193ca8ca2f.png">
Signed-off-by: imtsuki <[email protected]>
Co-authored-by: imtsuki <[email protected]>
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/package.json | 2 | ||||
-rw-r--r-- | editors/code/src/inlay_hints.ts | 51 |
2 files changed, 42 insertions, 11 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index 77d31a04b..d4c310734 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -228,7 +228,7 @@ | |||
228 | "rust-analyzer.displayInlayHints": { | 228 | "rust-analyzer.displayInlayHints": { |
229 | "type": "boolean", | 229 | "type": "boolean", |
230 | "default": true, | 230 | "default": true, |
231 | "description": "Display additional type information in the editor" | 231 | "description": "Display additional type and parameter information in the editor" |
232 | }, | 232 | }, |
233 | "rust-analyzer.maxInlayHintLength": { | 233 | "rust-analyzer.maxInlayHintLength": { |
234 | "type": "number", | 234 | "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({ | |||
38 | }, | 38 | }, |
39 | }); | 39 | }); |
40 | 40 | ||
41 | const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({ | ||
42 | before: { | ||
43 | color: new vscode.ThemeColor('rust_analyzer.inlayHint'), | ||
44 | } | ||
45 | }) | ||
46 | |||
41 | class HintsUpdater { | 47 | class HintsUpdater { |
42 | private pending: Map<string, vscode.CancellationTokenSource> = new Map(); | 48 | private pending: Map<string, vscode.CancellationTokenSource> = new Map(); |
43 | private ctx: Ctx; | 49 | private ctx: Ctx; |
@@ -55,7 +61,10 @@ class HintsUpdater { | |||
55 | if (this.enabled) { | 61 | if (this.enabled) { |
56 | await this.refresh(); | 62 | await this.refresh(); |
57 | } else { | 63 | } else { |
58 | this.allEditors.forEach(it => this.setDecorations(it, [])); | 64 | this.allEditors.forEach(it => { |
65 | this.setTypeDecorations(it, []); | ||
66 | this.setParameterDecorations(it, []); | ||
67 | }); | ||
59 | } | 68 | } |
60 | } | 69 | } |
61 | 70 | ||
@@ -68,15 +77,27 @@ class HintsUpdater { | |||
68 | private async refreshEditor(editor: vscode.TextEditor): Promise<void> { | 77 | private async refreshEditor(editor: vscode.TextEditor): Promise<void> { |
69 | const newHints = await this.queryHints(editor.document.uri.toString()); | 78 | const newHints = await this.queryHints(editor.document.uri.toString()); |
70 | if (newHints == null) return; | 79 | if (newHints == null) return; |
71 | const newDecorations = newHints.map(hint => ({ | 80 | const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint') |
72 | range: hint.range, | 81 | .map(hint => ({ |
73 | renderOptions: { | 82 | range: hint.range, |
74 | after: { | 83 | renderOptions: { |
75 | contentText: `: ${hint.label}`, | 84 | after: { |
85 | contentText: `: ${hint.label}`, | ||
86 | }, | ||
76 | }, | 87 | }, |
77 | }, | 88 | })); |
78 | })); | 89 | this.setTypeDecorations(editor, newTypeDecorations); |
79 | this.setDecorations(editor, newDecorations); | 90 | |
91 | const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint') | ||
92 | .map(hint => ({ | ||
93 | range: hint.range, | ||
94 | renderOptions: { | ||
95 | before: { | ||
96 | contentText: `${hint.label}: `, | ||
97 | }, | ||
98 | }, | ||
99 | })); | ||
100 | this.setParameterDecorations(editor, newParameterDecorations); | ||
80 | } | 101 | } |
81 | 102 | ||
82 | private get allEditors(): vscode.TextEditor[] { | 103 | private get allEditors(): vscode.TextEditor[] { |
@@ -85,7 +106,7 @@ class HintsUpdater { | |||
85 | ); | 106 | ); |
86 | } | 107 | } |
87 | 108 | ||
88 | private setDecorations( | 109 | private setTypeDecorations( |
89 | editor: vscode.TextEditor, | 110 | editor: vscode.TextEditor, |
90 | decorations: vscode.DecorationOptions[], | 111 | decorations: vscode.DecorationOptions[], |
91 | ) { | 112 | ) { |
@@ -95,6 +116,16 @@ class HintsUpdater { | |||
95 | ); | 116 | ); |
96 | } | 117 | } |
97 | 118 | ||
119 | private setParameterDecorations( | ||
120 | editor: vscode.TextEditor, | ||
121 | decorations: vscode.DecorationOptions[], | ||
122 | ) { | ||
123 | editor.setDecorations( | ||
124 | parameterHintDecorationType, | ||
125 | this.enabled ? decorations : [], | ||
126 | ); | ||
127 | } | ||
128 | |||
98 | private async queryHints(documentUri: string): Promise<InlayHint[] | null> { | 129 | private async queryHints(documentUri: string): Promise<InlayHint[] | null> { |
99 | let client = this.ctx.client; | 130 | let client = this.ctx.client; |
100 | if (!client) return null; | 131 | if (!client) return null; |