Skip to content

Commit

Permalink
fix: LSP hover over function with &mut self (#7155)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Jan 22, 2025
1 parent 7f9525d commit c8d5ce5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tooling/lsp/src/requests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,24 @@ fn format_function(id: FuncId, args: &ProcessRequestCallbackArgs) -> String {
string.push('(');
let parameters = &func_meta.parameters;
for (index, (pattern, typ, visibility)) in parameters.iter().enumerate() {
let is_self = pattern_is_self(pattern, args.interner);

// `&mut self` is represented as a mutable reference type, not as a mutable pattern
if is_self && matches!(typ, Type::MutableReference(..)) {
string.push_str("&mut ");
}

format_pattern(pattern, args.interner, &mut string);
if !pattern_is_self(pattern, args.interner) {

// Don't add type for `self` param
if !is_self {
string.push_str(": ");
if matches!(visibility, Visibility::Public) {
string.push_str("pub ");
}
string.push_str(&format!("{}", typ));
}

if index != parameters.len() - 1 {
string.push_str(", ");
}
Expand Down Expand Up @@ -1238,4 +1248,12 @@ mod hover_tests {
.await;
assert!(hover_text.contains("Some docs"));
}

#[test]
async fn hover_on_function_with_mut_self() {
let hover_text =
get_hover_text("workspace", "two/src/lib.nr", Position { line: 96, character: 10 })
.await;
assert!(hover_text.contains("fn mut_self(&mut self)"));
}
}
3 changes: 3 additions & 0 deletions tooling/lsp/test_programs/workspace/two/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ impl TraitWithDocs for Field {
fn foo() {}
}

impl<U> Foo<U> {
fn mut_self(&mut self) {}
}

0 comments on commit c8d5ce5

Please sign in to comment.