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): suggest trait methods from where clauses #6915

Merged
merged 2 commits into from
Jan 2, 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
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@
.def_interner
.push_function_definition(func_id, modifiers, trait_id.0, location);

let referenced = ReferenceId::Function(func_id);
context.def_interner.add_definition_location(referenced, Some(trait_id.0));

if !trait_item.doc_comments.is_empty() {
context.def_interner.set_doc_comments(
ReferenceId::Function(func_id),
Expand Down Expand Up @@ -869,7 +872,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

Check warning on line 875 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(
Expand Down
57 changes: 55 additions & 2 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
},
hir_def::traits::Trait,
node_interner::{NodeInterner, ReferenceId, StructId},
node_interner::{FuncId, NodeInterner, ReferenceId, StructId},
parser::{Item, ItemKind, ParsedSubModule},
token::{MetaAttribute, Token, Tokens},
Kind, ParsedModule, StructType, Type, TypeBinding,
Expand All @@ -46,7 +46,7 @@
use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 49 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (builtins)
mod completion_items;
mod kinds;
mod sort_text;
Expand Down Expand Up @@ -120,6 +120,8 @@
use_segment_positions: UseSegmentPositions,
self_type: Option<Type>,
in_comptime: bool,
/// The function we are in, if any
func_id: Option<FuncId>,
}

impl<'a> NodeFinder<'a> {
Expand Down Expand Up @@ -165,6 +167,7 @@
use_segment_positions: UseSegmentPositions::default(),
self_type: None,
in_comptime: false,
func_id: None,
}
}

Expand Down Expand Up @@ -242,7 +245,7 @@
let mut idents: Vec<Ident> = Vec::new();

// Find in which ident we are in, and in which part of it
// (it could be that we are completting in the middle of an ident)

Check warning on line 248 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (completting)
for segment in &path.segments {
let ident = &segment.ident;

Expand Down Expand Up @@ -639,6 +642,13 @@
function_completion_kind: FunctionCompletionKind,
self_prefix: bool,
) {
self.complete_trait_constraints_methods(
typ,
prefix,
function_kind,
function_completion_kind,
);

let Some(methods_by_name) = self.interner.get_type_methods(typ) else {
return;
};
Expand Down Expand Up @@ -697,6 +707,31 @@
}
}

fn complete_trait_constraints_methods(
&mut self,
typ: &Type,
prefix: &str,
function_kind: FunctionKind,
function_completion_kind: FunctionCompletionKind,
) {
let Some(func_id) = self.func_id else {
return;
};

let func_meta = self.interner.function_meta(&func_id);
for constraint in &func_meta.trait_constraints {
if *typ == constraint.typ {
let trait_ = self.interner.get_trait(constraint.trait_bound.trait_id);
self.complete_trait_methods(
trait_,
prefix,
function_kind,
function_completion_kind,
);
}
}
}

fn complete_trait_methods(
&mut self,
trait_: &Trait,
Expand Down Expand Up @@ -1125,8 +1160,17 @@
let old_in_comptime = self.in_comptime;
self.in_comptime = noir_function.def.is_comptime;

if let Some(ReferenceId::Function(func_id)) = self
.interner
.reference_at_location(Location::new(noir_function.name_ident().span(), self.file))
{
self.func_id = Some(func_id);
}

noir_function.def.body.accept(Some(span), self);

self.func_id = None;

self.in_comptime = old_in_comptime;
self.type_parameters = old_type_parameters;
self.self_type = None;
Expand Down Expand Up @@ -1207,7 +1251,7 @@

fn visit_trait_item_function(
&mut self,
_name: &Ident,
name: &Ident,
generics: &UnresolvedGenerics,
parameters: &[(Ident, UnresolvedType)],
return_type: &noirc_frontend::ast::FunctionReturnType,
Expand All @@ -1232,7 +1276,16 @@
for (name, _) in parameters {
self.local_variables.insert(name.to_string(), name.span());
}

if let Some(ReferenceId::Function(func_id)) =
self.interner.reference_at_location(Location::new(name.span(), self.file))
{
self.func_id = Some(func_id);
}

body.accept(None, self);

self.func_id = None;
};

self.type_parameters = old_type_parameters;
Expand Down Expand Up @@ -1767,8 +1820,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1823 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
/// name_matches("compute_merkle_root", "merk_ro") == true

Check warning on line 1824 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
///
/// // "ro" matches "root", but "merkle" comes before it, so no match
/// name_matches("compute_merkle_root", "ro_mer") == false
Expand Down
18 changes: 18 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 137 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<

Check warning on line 139 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
"#;

assert_completion(
Expand Down Expand Up @@ -2861,4 +2861,22 @@
assert_eq!(items.len(), 1);
assert!(items[0].label == "bar_baz()");
}

#[test]
async fn test_suggests_trait_method_from_where_clause_in_function() {
let src = r#"
trait Foo {
fn foo(self) -> i32;
}

fn something<T>(x: T) -> i32
where
T: Foo,
{
x.fo>|<
}
"#;
let items = get_completions(src).await;
assert_eq!(items.len(), 1);
}
}
Loading