From ea1065c6038900e00f5050cd9d660b04f29085ba Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 9 Jan 2025 11:04:05 -0300 Subject: [PATCH 1/2] Use rpc feature in lsp project --- tooling/lsp/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tooling/lsp/Cargo.toml b/tooling/lsp/Cargo.toml index 04c8edf7ea9..65b59552b32 100644 --- a/tooling/lsp/Cargo.toml +++ b/tooling/lsp/Cargo.toml @@ -3,7 +3,7 @@ name = "noir_lsp" description = "Language server for Noir" version.workspace = true authors.workspace = true -edition.workspace = true# +edition.workspace = true rust-version.workspace = true license.workspace = true @@ -16,7 +16,7 @@ workspace = true acvm.workspace = true codespan-lsp.workspace = true lsp-types.workspace = true -nargo.workspace = true +nargo = { workspace = true, features = ["rpc"] } nargo_fmt.workspace = true nargo_toml.workspace = true noirc_driver.workspace = true From c137d8bcc954913fc724b6eac1af8ad0660b6a16 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 9 Jan 2025 11:04:36 -0300 Subject: [PATCH 2/2] feat(lsp): use trait method docs for trait impl method docs on hover --- tooling/lsp/src/requests/hover.rs | 32 +++++++++++++++++-- .../test_programs/workspace/two/src/lib.nr | 9 ++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/tooling/lsp/src/requests/hover.rs b/tooling/lsp/src/requests/hover.rs index e2e2d2881dc..9da24fd1c8a 100644 --- a/tooling/lsp/src/requests/hover.rs +++ b/tooling/lsp/src/requests/hover.rs @@ -410,8 +410,10 @@ fn format_function(id: FuncId, args: &ProcessRequestCallbackArgs) -> String { string.push_str("comptime "); } + let func_name = &func_name_definition_id.name; + string.push_str("fn "); - string.push_str(&func_name_definition_id.name); + string.push_str(func_name); format_generics(&func_meta.direct_generics, &mut string); string.push('('); let parameters = &func_meta.parameters; @@ -442,7 +444,20 @@ fn format_function(id: FuncId, args: &ProcessRequestCallbackArgs) -> String { string.push_str(&go_to_type_links(return_type, args.interner, args.files)); - append_doc_comments(args.interner, ReferenceId::Function(id), &mut string); + let had_doc_comments = + append_doc_comments(args.interner, ReferenceId::Function(id), &mut string); + if !had_doc_comments { + // If this function doesn't have doc comments, but it's a trait impl method, + // use the trait method doc comments. + if let Some(trait_impl_id) = func_meta.trait_impl { + let trait_impl = args.interner.get_trait_implementation(trait_impl_id); + let trait_impl = trait_impl.borrow(); + let trait_ = args.interner.get_trait(trait_impl.trait_id); + if let Some(func_id) = trait_.method_ids.get(func_name) { + append_doc_comments(args.interner, ReferenceId::Function(*func_id), &mut string); + } + } + } string } @@ -776,13 +791,16 @@ fn format_link(name: String, location: lsp_types::Location) -> String { ) } -fn append_doc_comments(interner: &NodeInterner, id: ReferenceId, string: &mut String) { +fn append_doc_comments(interner: &NodeInterner, id: ReferenceId, string: &mut String) -> bool { if let Some(doc_comments) = interner.doc_comments(id) { string.push_str("\n\n---\n\n"); for comment in doc_comments { string.push_str(comment); string.push('\n'); } + true + } else { + false } } @@ -1139,4 +1157,12 @@ mod hover_tests { pub fn bar_stuff(self)" )); } + + #[test] + async fn hover_on_trait_impl_method_uses_docs_from_trait_method() { + let hover_text = + get_hover_text("workspace", "two/src/lib.nr", Position { line: 92, character: 8 }) + .await; + assert!(hover_text.contains("Some docs")); + } } diff --git a/tooling/lsp/test_programs/workspace/two/src/lib.nr b/tooling/lsp/test_programs/workspace/two/src/lib.nr index 2dec902f327..d18a663b276 100644 --- a/tooling/lsp/test_programs/workspace/two/src/lib.nr +++ b/tooling/lsp/test_programs/workspace/two/src/lib.nr @@ -84,3 +84,12 @@ fn bar_stuff() { foo.bar_stuff(); } +trait TraitWithDocs { + /// Some docs + fn foo(); +} + +impl TraitWithDocs for Field { + fn foo() {} +} +