Skip to content

Commit

Permalink
Merge #2875
Browse files Browse the repository at this point in the history
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 <flodiebold@gmail.com>
  • Loading branch information
bors[bot] and flodiebold authored Jan 18, 2020
2 parents d1d91df + 18ec4e3 commit 3a7724e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
19 changes: 16 additions & 3 deletions crates/ra_ide/src/display/function_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,22 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
res.push(self_param.syntax().text().to_string())
}

res.extend(param_list.params().map(|param| {
param.pat().map(|pat| pat.syntax().text().to_string()).unwrap_or_default()
}));
res.extend(
param_list
.params()
.map(|param| {
Some(
param
.pat()?
.syntax()
.descendants()
.find_map(ast::Name::cast)?
.text()
.to_string(),
)
})
.map(|param| param.unwrap_or_default()),
);
}
res
}
Expand Down
23 changes: 9 additions & 14 deletions crates/ra_ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn get_param_name_hints(
let hints = parameters
.zip(args)
.filter_map(|(param, arg)| {
if arg.syntax().kind() == SyntaxKind::LITERAL {
if arg.syntax().kind() == SyntaxKind::LITERAL && !param.is_empty() {
Some((arg.syntax().text_range(), param))
} else {
None
Expand Down Expand Up @@ -683,12 +683,12 @@ fn main() {
struct Test {}
impl Test {
fn method(&self, param: i32) -> i32 {
fn method(&self, mut param: i32) -> i32 {
param * 2
}
}
fn test_func(foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
foo + bar
}
Expand All @@ -704,37 +704,32 @@ fn main() {
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###"
[
InlayHint {
range: [207; 218),
range: [215; 226),
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [251; 252),
range: [259; 260),
kind: ParameterHint,
label: "foo",
},
InlayHint {
range: [254; 255),
range: [262; 263),
kind: ParameterHint,
label: "bar",
},
InlayHint {
range: [257; 264),
range: [265; 272),
kind: ParameterHint,
label: "msg",
},
InlayHint {
range: [266; 267),
kind: ParameterHint,
label: "_",
},
InlayHint {
range: [323; 326),
range: [331; 334),
kind: ParameterHint,
label: "param",
},
InlayHint {
range: [350; 354),
range: [358; 362),
kind: ParameterHint,
label: "param",
},
Expand Down
19 changes: 13 additions & 6 deletions editors/emacs/rust-analyzer.el
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@
;; inlay hints
(defun rust-analyzer--update-inlay-hints (buffer)
(if (and (rust-analyzer--initialized?) (eq buffer (current-buffer)))
(lsp-send-request-async
(lsp-make-request "rust-analyzer/inlayHints"
(list :textDocument (lsp--text-document-identifier)))
(lsp-request-async
"rust-analyzer/inlayHints"
(list :textDocument (lsp--text-document-identifier))
(lambda (res)
(remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t)
(dolist (hint res)
Expand All @@ -221,9 +221,16 @@
(overlay (make-overlay beg end)))
(overlay-put overlay 'rust-analyzer--inlay-hint t)
(overlay-put overlay 'evaporate t)
(overlay-put overlay 'after-string (propertize (concat ": " label)
'font-lock-face 'font-lock-comment-face)))))
'tick))
(cond
((string= kind "TypeHint")
(overlay-put overlay 'after-string (propertize (concat ": " label)
'font-lock-face 'font-lock-comment-face)))
((string= kind "ParameterHint")
(overlay-put overlay 'before-string (propertize (concat label ": ")
'font-lock-face 'font-lock-comment-face)))
)
)))
:mode 'tick))
nil)

(defvar-local rust-analyzer--inlay-hints-timer nil)
Expand Down

0 comments on commit 3a7724e

Please sign in to comment.