Skip to content

Commit

Permalink
feat(lsp): use trait method docs for trait impl method docs on hover (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Jan 9, 2025
1 parent ae9f2ae commit 4d38a88
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions tooling/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
32 changes: 29 additions & 3 deletions tooling/lsp/src/requests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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"));
}
}
9 changes: 9 additions & 0 deletions tooling/lsp/test_programs/workspace/two/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ fn bar_stuff() {
foo.bar_stuff();
}

trait TraitWithDocs {
/// Some docs
fn foo();
}

impl TraitWithDocs for Field {
fn foo() {}
}

0 comments on commit 4d38a88

Please sign in to comment.