Skip to content

Commit 3a7724e

Browse files
Merge #2875
2875: Improve parameter hints a bit & add emacs support r=matklad a=flodiebold - just include the name, not e.g. `mut` - don't return empty hints (or `_`) CC @brotzeit for the Emacs change Co-authored-by: Florian Diebold <[email protected]>
2 parents d1d91df + 18ec4e3 commit 3a7724e

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

crates/ra_ide/src/display/function_signature.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,22 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
169169
res.push(self_param.syntax().text().to_string())
170170
}
171171

172-
res.extend(param_list.params().map(|param| {
173-
param.pat().map(|pat| pat.syntax().text().to_string()).unwrap_or_default()
174-
}));
172+
res.extend(
173+
param_list
174+
.params()
175+
.map(|param| {
176+
Some(
177+
param
178+
.pat()?
179+
.syntax()
180+
.descendants()
181+
.find_map(ast::Name::cast)?
182+
.text()
183+
.to_string(),
184+
)
185+
})
186+
.map(|param| param.unwrap_or_default()),
187+
);
175188
}
176189
res
177190
}

crates/ra_ide/src/inlay_hints.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn get_param_name_hints(
116116
let hints = parameters
117117
.zip(args)
118118
.filter_map(|(param, arg)| {
119-
if arg.syntax().kind() == SyntaxKind::LITERAL {
119+
if arg.syntax().kind() == SyntaxKind::LITERAL && !param.is_empty() {
120120
Some((arg.syntax().text_range(), param))
121121
} else {
122122
None
@@ -683,12 +683,12 @@ fn main() {
683683
struct Test {}
684684
685685
impl Test {
686-
fn method(&self, param: i32) -> i32 {
686+
fn method(&self, mut param: i32) -> i32 {
687687
param * 2
688688
}
689689
}
690690
691-
fn test_func(foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
691+
fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
692692
foo + bar
693693
}
694694
@@ -704,37 +704,32 @@ fn main() {
704704
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###"
705705
[
706706
InlayHint {
707-
range: [207; 218),
707+
range: [215; 226),
708708
kind: TypeHint,
709709
label: "i32",
710710
},
711711
InlayHint {
712-
range: [251; 252),
712+
range: [259; 260),
713713
kind: ParameterHint,
714714
label: "foo",
715715
},
716716
InlayHint {
717-
range: [254; 255),
717+
range: [262; 263),
718718
kind: ParameterHint,
719719
label: "bar",
720720
},
721721
InlayHint {
722-
range: [257; 264),
722+
range: [265; 272),
723723
kind: ParameterHint,
724724
label: "msg",
725725
},
726726
InlayHint {
727-
range: [266; 267),
728-
kind: ParameterHint,
729-
label: "_",
730-
},
731-
InlayHint {
732-
range: [323; 326),
727+
range: [331; 334),
733728
kind: ParameterHint,
734729
label: "param",
735730
},
736731
InlayHint {
737-
range: [350; 354),
732+
range: [358; 362),
738733
kind: ParameterHint,
739734
label: "param",
740735
},

editors/emacs/rust-analyzer.el

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@
210210
;; inlay hints
211211
(defun rust-analyzer--update-inlay-hints (buffer)
212212
(if (and (rust-analyzer--initialized?) (eq buffer (current-buffer)))
213-
(lsp-send-request-async
214-
(lsp-make-request "rust-analyzer/inlayHints"
215-
(list :textDocument (lsp--text-document-identifier)))
213+
(lsp-request-async
214+
"rust-analyzer/inlayHints"
215+
(list :textDocument (lsp--text-document-identifier))
216216
(lambda (res)
217217
(remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t)
218218
(dolist (hint res)
@@ -221,9 +221,16 @@
221221
(overlay (make-overlay beg end)))
222222
(overlay-put overlay 'rust-analyzer--inlay-hint t)
223223
(overlay-put overlay 'evaporate t)
224-
(overlay-put overlay 'after-string (propertize (concat ": " label)
225-
'font-lock-face 'font-lock-comment-face)))))
226-
'tick))
224+
(cond
225+
((string= kind "TypeHint")
226+
(overlay-put overlay 'after-string (propertize (concat ": " label)
227+
'font-lock-face 'font-lock-comment-face)))
228+
((string= kind "ParameterHint")
229+
(overlay-put overlay 'before-string (propertize (concat label ": ")
230+
'font-lock-face 'font-lock-comment-face)))
231+
)
232+
)))
233+
:mode 'tick))
227234
nil)
228235

229236
(defvar-local rust-analyzer--inlay-hints-timer nil)

0 commit comments

Comments
 (0)