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 autocompletion for attributes #5963

Merged
merged 4 commits into from
Sep 6, 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
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
mod type_alias;
mod visitor;

pub use visitor::AttributeTarget;
pub use visitor::Visitor;

pub use expression::*;
Expand Down Expand Up @@ -498,7 +499,7 @@
Self::Public => write!(f, "pub"),
Self::Private => write!(f, "priv"),
Self::CallData(id) => write!(f, "calldata{id}"),
Self::ReturnData => write!(f, "returndata"),

Check warning on line 502 in compiler/noirc_frontend/src/ast/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (returndata)
}
}
}
55 changes: 50 additions & 5 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::{SecondaryAttribute, Tokens},
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 @@ -26,6 +26,13 @@
UnresolvedTypeExpression,
};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AttributeTarget {
Module,
Struct,
Function,
}

/// Implements the [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for Noir's AST.
///
/// In this implementation, methods must return a bool:
Expand Down Expand Up @@ -433,7 +440,15 @@
true
}

fn visit_secondary_attribute(&mut self, _: &SecondaryAttribute, _: Span) {}
fn visit_secondary_attribute(
&mut self,
_: &SecondaryAttribute,
_target: AttributeTarget,
) -> bool {
true
}

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

Check warning on line 451 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 @@ -484,14 +499,18 @@
module_declaration.accept(self.span, visitor);
}
ItemKind::InnerAttribute(attribute) => {
attribute.accept(self.span, visitor);
attribute.accept(AttributeTarget::Module, visitor);
}
}
}
}

impl ParsedSubModule {
pub fn accept(&self, span: Span, visitor: &mut impl Visitor) {
for attribute in &self.outer_attributes {
attribute.accept(AttributeTarget::Module, visitor);
}

if visitor.visit_parsed_submodule(self, span) {
self.accept_children(visitor);
}
Expand All @@ -510,6 +529,10 @@
}

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

for param in &self.def.parameters {
param.typ.accept(visitor);
}
Expand Down Expand Up @@ -674,6 +697,10 @@
}

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

for (_name, unresolved_type) in &self.fields {
unresolved_type.accept(visitor);
}
Expand All @@ -694,6 +721,10 @@

impl ModuleDeclaration {
pub fn accept(&self, span: Span, visitor: &mut impl Visitor) {
for attribute in &self.outer_attributes {
attribute.accept(AttributeTarget::Module, visitor);
}

visitor.visit_module_declaration(self, span);
}
}
Expand Down Expand Up @@ -1210,8 +1241,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 1244 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 1245 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 @@ -1295,8 +1326,22 @@
}

impl SecondaryAttribute {
pub fn accept(&self, span: Span, visitor: &mut impl Visitor) {
visitor.visit_secondary_attribute(self, span);
pub fn accept(&self, target: AttributeTarget, visitor: &mut impl Visitor) {
if visitor.visit_secondary_attribute(self, target) {
self.accept_children(target, visitor);
}
}

pub fn accept_children(&self, target: AttributeTarget, visitor: &mut impl Visitor) {
if let SecondaryAttribute::Custom(custom) = self {
custom.accept(target, visitor);
}
}
}

impl CustomAtrribute {

Check warning on line 1342 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
75 changes: 69 additions & 6 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use async_lsp::ResponseError;
use completion_items::{
crate_completion_item, field_completion_item, simple_completion_item,
crate_completion_item, field_completion_item, simple_completion_item, snippet_completion_item,
struct_field_completion_item,
};
use convert_case::{Case, Casing};
Expand All @@ -15,18 +15,19 @@
use noirc_errors::{Location, Span};
use noirc_frontend::{
ast::{
AsTraitPath, BlockExpression, CallExpression, ConstructorExpression, Expression,
ExpressionKind, ForLoopStatement, GenericTypeArgs, Ident, IfExpression, ItemVisibility,
Lambda, LetStatement, MemberAccessExpression, MethodCallExpression, NoirFunction,
NoirStruct, NoirTraitImpl, Path, PathKind, Pattern, Statement, TypeImpl, UnresolvedGeneric,
UnresolvedGenerics, UnresolvedType, UseTree, UseTreeKind, Visitor,
AsTraitPath, AttributeTarget, BlockExpression, CallExpression, ConstructorExpression,
Expression, ExpressionKind, ForLoopStatement, GenericTypeArgs, Ident, IfExpression,
ItemVisibility, Lambda, LetStatement, MemberAccessExpression, MethodCallExpression,
NoirFunction, NoirStruct, NoirTraitImpl, Path, PathKind, Pattern, Statement, TypeImpl,
UnresolvedGeneric, UnresolvedGenerics, UnresolvedType, UseTree, UseTreeKind, Visitor,
},
graph::{CrateId, Dependency},
hir::def_map::{CrateDefMap, LocalModuleId, ModuleId},
hir_def::traits::Trait,
macros_api::{ModuleDefId, NodeInterner},
node_interner::ReferenceId,
parser::{Item, ItemKind, ParsedSubModule},
token::CustomAtrribute,

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Atrribute)
ParsedModule, StructType, Type,
};
use sort_text::underscore_sort_text;
Expand All @@ -36,7 +37,7 @@
use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 40 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 @@ -218,7 +219,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 222 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 @@ -359,6 +360,7 @@
self.builtin_types_completion(&prefix);
self.type_parameters_completion(&prefix);
}
RequestedItems::OnlyAttributeFunctions(..) => (),
}
self.complete_auto_imports(&prefix, requested_items, function_completion_kind);
}
Expand Down Expand Up @@ -606,6 +608,7 @@
func_id,
function_completion_kind,
function_kind,
None, // attribute first type
self_prefix,
) {
self.completion_items.push(completion_item);
Expand All @@ -632,6 +635,7 @@
*func_id,
function_completion_kind,
function_kind,
None, // attribute first type
self_prefix,
) {
self.completion_items.push(completion_item);
Expand Down Expand Up @@ -786,6 +790,49 @@
None
}

fn suggest_attributes(&mut self, prefix: &str, target: AttributeTarget) {
self.suggest_builtin_attributes(prefix, target);

let function_completion_kind = FunctionCompletionKind::NameAndParameters;
let requested_items = RequestedItems::OnlyAttributeFunctions(target);

self.complete_in_module(
self.module_id,
prefix,
PathKind::Plain,
true,
function_completion_kind,
requested_items,
);

self.complete_auto_imports(prefix, requested_items, function_completion_kind);
}

fn suggest_no_arguments_attributes(&mut self, prefix: &str, attributes: &[&str]) {
for name in attributes {
if name_matches(name, prefix) {
self.completion_items.push(simple_completion_item(
*name,
CompletionItemKind::METHOD,
None,
));
}
}
}

fn suggest_one_argument_attributes(&mut self, prefix: &str, attributes: &[&str]) {
for name in attributes {
if name_matches(name, prefix) {
self.completion_items.push(snippet_completion_item(
format!("{}(…)", name),
CompletionItemKind::METHOD,
format!("{}(${{1:name}})", name),
None,
));
}
}
}

fn try_set_self_type(&mut self, pattern: &Pattern) {
match pattern {
Pattern::Identifier(ident) => {
Expand Down Expand Up @@ -855,6 +902,10 @@
}

fn visit_noir_function(&mut self, noir_function: &NoirFunction, span: Span) -> bool {
for attribute in noir_function.secondary_attributes() {
attribute.accept(AttributeTarget::Function, self);
}

let old_type_parameters = self.type_parameters.clone();
self.collect_type_parameters_in_generics(&noir_function.def.generics);

Expand Down Expand Up @@ -915,6 +966,10 @@
}

fn visit_noir_struct(&mut self, noir_struct: &NoirStruct, _: Span) -> bool {
for attribute in &noir_struct.attributes {
attribute.accept(AttributeTarget::Struct, self);
}

self.type_parameters.clear();
self.collect_type_parameters_in_generics(&noir_struct.generics);

Expand Down Expand Up @@ -1263,6 +1318,14 @@
unresolved_types.accept(self);
false
}

fn visit_custom_attribute(&mut self, attribute: &CustomAtrribute, target: AttributeTarget) {

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Atrribute)
if self.byte_index != attribute.contents_span.end() as usize {
return;
}

self.suggest_attributes(&attribute.contents, target);
}
}

/// Returns true if name matches a prefix written in code.
Expand Down
36 changes: 35 additions & 1 deletion tooling/lsp/src/requests/completion/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lsp_types::CompletionItemKind;
use noirc_frontend::token::Keyword;
use noirc_frontend::{ast::AttributeTarget, token::Keyword};
use strum::IntoEnumIterator;

use super::{
Expand Down Expand Up @@ -84,6 +84,40 @@ impl<'a> NodeFinder<'a> {
}
}
}

pub(super) fn suggest_builtin_attributes(&mut self, prefix: &str, target: AttributeTarget) {
match target {
AttributeTarget::Module => (),
AttributeTarget::Struct => {
self.suggest_one_argument_attributes(prefix, &["abi"]);
}
AttributeTarget::Function => {
let no_arguments_attributes = &[
"contract_library_method",
"deprecated",
"export",
"fold",
"no_predicates",
"recursive",
"test",
"varargs",
];
self.suggest_no_arguments_attributes(prefix, no_arguments_attributes);

let one_argument_attributes = &["abi", "field", "foreign", "oracle"];
self.suggest_one_argument_attributes(prefix, one_argument_attributes);

if name_matches("test", prefix) || name_matches("should_fail_with", prefix) {
self.completion_items.push(snippet_completion_item(
"test(should_fail_with=\"...\")",
CompletionItemKind::METHOD,
"test(should_fail_with=\"${1:message}\")",
None,
));
}
}
}
}
}

pub(super) fn builtin_integer_types() -> [&'static str; 8] {
Expand Down
Loading
Loading