Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): use trait method docs for trait impl method docs on hover #7003

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 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 @@ -838,7 +856,7 @@
"two/src/lib.nr",
Position { line: 6, character: 9 },
r#" one
mod subone"#,

Check warning on line 859 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
)
.await;
}
Expand All @@ -849,7 +867,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 9, character: 20 },
r#" one::subone

Check warning on line 870 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
struct SubOneStruct {
some_field: i32,
some_other_field: Field,
Expand All @@ -864,7 +882,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 46, character: 17 },
r#" one::subone

Check warning on line 885 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
struct GenericStruct<A, B> {
}"#,
)
Expand All @@ -877,7 +895,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 9, character: 35 },
r#" one::subone::SubOneStruct

Check warning on line 898 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
some_field: i32"#,
)
.await;
Expand All @@ -889,7 +907,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 12, character: 17 },
r#" one::subone

Check warning on line 910 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
trait SomeTrait"#,
)
.await;
Expand Down Expand Up @@ -1139,4 +1157,12 @@
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() {}
}

Loading