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

fix: suggest trait attributes in LSP #5972

Merged
merged 2 commits into from
Sep 9, 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
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
QuotedTypeId,
},
parser::{Item, ItemKind, ParsedSubModule},
token::{CustomAtrribute, SecondaryAttribute, Tokens},

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Atrribute)
ParsedModule, QuotedType,
};

Expand All @@ -30,6 +30,7 @@
pub enum AttributeTarget {
Module,
Struct,
Trait,
Function,
}

Expand Down Expand Up @@ -448,7 +449,7 @@
true
}

fn visit_custom_attribute(&mut self, _: &CustomAtrribute, _target: AttributeTarget) {}

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Atrribute)
}

impl ParsedModule {
Expand Down Expand Up @@ -615,6 +616,10 @@
}

pub fn accept_children(&self, visitor: &mut impl Visitor) {
for attribute in &self.attributes {
attribute.accept(AttributeTarget::Trait, visitor);
}

for item in &self.items {
item.item.accept(visitor);
}
Expand Down Expand Up @@ -1241,8 +1246,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 1249 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 1250 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 @@ -1339,7 +1344,7 @@
}
}

impl CustomAtrribute {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Atrribute)
pub fn accept(&self, target: AttributeTarget, visitor: &mut impl Visitor) {
visitor.visit_custom_attribute(self, target);
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/lsp/src/requests/completion/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> NodeFinder<'a> {

pub(super) fn suggest_builtin_attributes(&mut self, prefix: &str, target: AttributeTarget) {
match target {
AttributeTarget::Module => (),
AttributeTarget::Module | AttributeTarget::Trait => (),
AttributeTarget::Struct => {
self.suggest_one_argument_attributes(prefix, &["abi"]);
}
Expand Down
11 changes: 8 additions & 3 deletions tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<'a> NodeFinder<'a> {
match target {
AttributeTarget::Module => Some(Type::Quoted(QuotedType::Module)),
AttributeTarget::Struct => Some(Type::Quoted(QuotedType::StructDefinition)),
AttributeTarget::Trait => Some(Type::Quoted(QuotedType::TraitDefinition)),
AttributeTarget::Function => Some(Type::Quoted(QuotedType::FunctionDefinition)),
}
} else {
Expand Down Expand Up @@ -226,10 +227,14 @@ impl<'a> NodeFinder<'a> {
function_kind,
skip_first_argument,
);
let label = if insert_text.ends_with("()") {
format!("{}()", name)
let (label, insert_text) = if insert_text.ends_with("()") {
if skip_first_argument {
(name.to_string(), insert_text.strip_suffix("()").unwrap().to_string())
} else {
(format!("{}()", name), insert_text)
}
} else {
format!("{}(…)", name)
(format!("{}(…)", name), insert_text)
};

snippet_completion_item(label, kind, insert_text, Some(description))
Expand Down
36 changes: 36 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 125 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 127 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 132 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 @@ -299,7 +299,7 @@
mod bar {
mod something {}

use super::foob>|<

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -1541,7 +1541,7 @@
async fn test_auto_import_suggests_modules_too() {
let src = r#"
mod foo {
mod barbaz {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand Down Expand Up @@ -1952,4 +1952,40 @@
)
.await;
}

#[test]
async fn test_suggests_function_attribute_no_arguments() {
let src = r#"
#[some>|<]
fn foo() {}

fn some_attr(f: FunctionDefinition) {}
"#;

assert_completion_excluding_auto_import(
src,
vec![function_completion_item("some_attr", "some_attr", "fn(FunctionDefinition)")],
)
.await;
}

#[test]
async fn test_suggests_trait_attribute() {
let src = r#"
#[some>|<]
trait SomeTrait {}

fn some_attr(t: TraitDefinition, x: Field) {}
"#;

assert_completion_excluding_auto_import(
src,
vec![function_completion_item(
"some_attr(…)",
"some_attr(${1:x})",
"fn(TraitDefinition, Field)",
)],
)
.await;
}
}
Loading