From c8d5ce5e72f3171f24e4fe2b94ac098ba6d99ef6 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 22 Jan 2025 18:09:41 -0300 Subject: [PATCH] fix: LSP hover over function with `&mut self` (#7155) --- tooling/lsp/src/requests/hover.rs | 20 ++++++++++++++++++- .../test_programs/workspace/two/src/lib.nr | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tooling/lsp/src/requests/hover.rs b/tooling/lsp/src/requests/hover.rs index bfe2a2a2448..8d845dce13d 100644 --- a/tooling/lsp/src/requests/hover.rs +++ b/tooling/lsp/src/requests/hover.rs @@ -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(", "); } @@ -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)")); + } } diff --git a/tooling/lsp/test_programs/workspace/two/src/lib.nr b/tooling/lsp/test_programs/workspace/two/src/lib.nr index d18a663b276..aacc4508756 100644 --- a/tooling/lsp/test_programs/workspace/two/src/lib.nr +++ b/tooling/lsp/test_programs/workspace/two/src/lib.nr @@ -93,3 +93,6 @@ impl TraitWithDocs for Field { fn foo() {} } +impl Foo { + fn mut_self(&mut self) {} +}