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: let LSP suggest traits in trait bounds #6370

Merged
merged 1 commit into from
Oct 28, 2024
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
54 changes: 51 additions & 3 deletions compiler/noirc_frontend/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

use super::{
ForBounds, FunctionReturnType, GenericTypeArgs, IntegerBitSize, ItemVisibility, Pattern,
Signedness, TraitImplItemKind, TypePath, UnresolvedGenerics, UnresolvedTraitConstraint,
UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
Signedness, TraitBound, TraitImplItemKind, TypePath, UnresolvedGenerics,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -438,6 +438,14 @@
true
}

fn visit_trait_bound(&mut self, _: &TraitBound) -> bool {
true
}

fn visit_unresolved_trait_constraint(&mut self, _: &UnresolvedTraitConstraint) -> bool {
true
}

fn visit_pattern(&mut self, _: &Pattern) -> bool {
true
}
Expand Down Expand Up @@ -555,6 +563,12 @@
param.typ.accept(visitor);
}

self.def.return_type.accept(visitor);

for constraint in &self.def.where_clause {
constraint.accept(visitor);
}

self.def.body.accept(None, visitor);
}
}
Expand Down Expand Up @@ -645,6 +659,14 @@
attribute.accept(AttributeTarget::Trait, visitor);
}

for bound in &self.bounds {
bound.accept(visitor);
}

for constraint in &self.where_clause {
constraint.accept(visitor);
}

for item in &self.items {
item.item.accept(visitor);
}
Expand Down Expand Up @@ -686,7 +708,7 @@
return_type.accept(visitor);

for unresolved_trait_constraint in where_clause {
unresolved_trait_constraint.typ.accept(visitor);
unresolved_trait_constraint.accept(visitor);
}

if let Some(body) = body {
Expand Down Expand Up @@ -1296,8 +1318,8 @@
UnresolvedTypeData::Unspecified => visitor.visit_unspecified_type(self.span),
UnresolvedTypeData::Quoted(typ) => visitor.visit_quoted_type(typ, self.span),
UnresolvedTypeData::FieldElement => visitor.visit_field_element_type(self.span),
UnresolvedTypeData::Integer(signdness, size) => {

Check warning on line 1321 in compiler/noirc_frontend/src/ast/visitor.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (signdness)
visitor.visit_integer_type(*signdness, *size, self.span);

Check warning on line 1322 in compiler/noirc_frontend/src/ast/visitor.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (signdness)
}
UnresolvedTypeData::Bool => visitor.visit_bool_type(self.span),
UnresolvedTypeData::Unit => visitor.visit_unit_type(self.span),
Expand Down Expand Up @@ -1346,6 +1368,32 @@
}
}

impl TraitBound {
pub fn accept(&self, visitor: &mut impl Visitor) {
if visitor.visit_trait_bound(self) {
self.accept_children(visitor);
}
}

pub fn accept_children(&self, visitor: &mut impl Visitor) {
self.trait_path.accept(visitor);
self.trait_generics.accept(visitor);
}
}

impl UnresolvedTraitConstraint {
pub fn accept(&self, visitor: &mut impl Visitor) {
if visitor.visit_unresolved_trait_constraint(self) {
self.accept_children(visitor);
}
}

pub fn accept_children(&self, visitor: &mut impl Visitor) {
self.typ.accept(visitor);
self.trait_bound.accept(visitor);
}
}

impl Pattern {
pub fn accept(&self, visitor: &mut impl Visitor) {
if visitor.visit_pattern(self) {
Expand Down
20 changes: 15 additions & 5 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
Expression, ExpressionKind, ForLoopStatement, GenericTypeArgs, Ident, IfExpression,
IntegerBitSize, ItemVisibility, LValue, Lambda, LetStatement, MemberAccessExpression,
MethodCallExpression, NoirFunction, NoirStruct, NoirTraitImpl, Path, PathKind, Pattern,
Signedness, Statement, TraitImplItemKind, TypeImpl, TypePath, UnresolvedGeneric,
UnresolvedGenerics, UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression, UseTree,
UseTreeKind, Visitor,
Signedness, Statement, TraitBound, TraitImplItemKind, TypeImpl, TypePath,
UnresolvedGeneric, UnresolvedGenerics, UnresolvedType, UnresolvedTypeData,
UnresolvedTypeExpression, UseTree, UseTreeKind, Visitor,
},
graph::{CrateId, Dependency},
hir::{
Expand All @@ -45,7 +45,7 @@
use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 48 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 @@ -241,7 +241,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 244 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 @@ -384,7 +384,7 @@
self.builtin_types_completion(&prefix);
self.type_parameters_completion(&prefix);
}
RequestedItems::OnlyAttributeFunctions(..) => (),
RequestedItems::OnlyTraits | RequestedItems::OnlyAttributeFunctions(..) => (),
}
self.complete_auto_imports(&prefix, requested_items, function_completion_kind);
}
Expand Down Expand Up @@ -1124,6 +1124,10 @@

noir_function.def.return_type.accept(self);

for constraint in &noir_function.def.where_clause {
constraint.accept(self);
}

self.local_variables.clear();
for param in &noir_function.def.parameters {
self.collect_local_variables(&param.pattern);
Expand Down Expand Up @@ -1231,7 +1235,7 @@
return_type.accept(self);

for unresolved_trait_constraint in where_clause {
unresolved_trait_constraint.typ.accept(self);
unresolved_trait_constraint.accept(self);
}

if let Some(body) = body {
Expand Down Expand Up @@ -1702,6 +1706,12 @@
last_was_dollar = false;
}
}

fn visit_trait_bound(&mut self, trait_bound: &TraitBound) -> bool {
self.find_in_path(&trait_bound.trait_path, RequestedItems::OnlyTraits);
trait_bound.trait_generics.accept(self);
false
}
}

fn get_field_type(typ: &Type, name: &str) -> Option<Type> {
Expand Down Expand Up @@ -1766,8 +1776,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1779 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 1780 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
8 changes: 8 additions & 0 deletions tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ impl<'a> NodeFinder<'a> {
| ModuleDefId::TypeAliasId(_)
| ModuleDefId::TraitId(_) => (),
},
RequestedItems::OnlyTraits => match module_def_id {
ModuleDefId::FunctionId(_) | ModuleDefId::GlobalId(_) | ModuleDefId::TypeId(_) => {
return Vec::new()
}
ModuleDefId::ModuleId(_)
| ModuleDefId::TypeAliasId(_)
| ModuleDefId::TraitId(_) => (),
},
RequestedItems::OnlyAttributeFunctions(..) => {
if !matches!(module_def_id, ModuleDefId::FunctionId(..)) {
return Vec::new();
Expand Down
4 changes: 3 additions & 1 deletion tooling/lsp/src/requests/completion/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub(super) enum FunctionKind<'a> {
pub(super) enum RequestedItems {
// Suggest any items (types, functions, etc.).
AnyItems,
// Only suggest types.
// Only suggest types (and modules, because they can contain types).
OnlyTypes,
// Only suggest traits (and modules, because they can contain traits).
OnlyTraits,
// Only attribute functions
OnlyAttributeFunctions(AttributeTarget),
}
38 changes: 38 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@
#[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(
src,
vec![module_completion_item("foobaz"), module_completion_item("foobar")],

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
)
.await;
}
Expand Down Expand Up @@ -304,7 +304,7 @@
mod bar {
mod something {}

use super::foob>|<

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -2719,4 +2719,42 @@

assert_completion(src, Vec::new()).await;
}

#[test]
async fn test_suggests_trait_in_trait_parent_bounds() {
let src = r#"
trait Foobar {}
struct Foobarbaz {}

trait Bar: Foob>|< {}
"#;
assert_completion(
src,
vec![simple_completion_item(
"Foobar",
CompletionItemKind::INTERFACE,
Some("Foobar".to_string()),
)],
)
.await;
}

#[test]
async fn test_suggests_trait_in_function_where_clause() {
let src = r#"
trait Foobar {}
struct Foobarbaz {}

fn foo<T>() where T: Foob>|< {}
"#;
assert_completion(
src,
vec![simple_completion_item(
"Foobar",
CompletionItemKind::INTERFACE,
Some("Foobar".to_string()),
)],
)
.await;
}
}
Loading