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 now suggests self fields and methods #5955

Merged
merged 1 commit 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
78 changes: 70 additions & 8 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 39 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 @@ -106,6 +106,7 @@
nesting: usize,
/// The line where an auto_import must be inserted
auto_import_line: usize,
self_type: Option<Type>,
}

impl<'a> NodeFinder<'a> {
Expand Down Expand Up @@ -147,6 +148,7 @@
suggested_module_def_ids: HashSet::new(),
nesting: 0,
auto_import_line: 0,
self_type: None,
}
}

Expand Down Expand Up @@ -191,8 +193,9 @@
fields.remove(&field.0.contents);
}

let self_prefix = false;
for (field, typ) in fields {
self.completion_items.push(struct_field_completion_item(field, typ));
self.completion_items.push(struct_field_completion_item(field, typ, self_prefix));
}
}

Expand All @@ -215,7 +218,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 221 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 @@ -293,6 +296,7 @@
&prefix,
FunctionKind::Any,
function_completion_kind,
false, // self_prefix
);
return;
}
Expand All @@ -308,6 +312,7 @@
&prefix,
FunctionKind::Any,
function_completion_kind,
false, // self_prefix
);
return;
}
Expand Down Expand Up @@ -340,6 +345,15 @@
self.local_variables_completion(&prefix);
self.builtin_functions_completion(&prefix, function_completion_kind);
self.builtin_values_completion(&prefix);
if let Some(self_type) = &self.self_type {
let self_prefix = true;
self.complete_type_fields_and_methods(
&self_type.clone(),
&prefix,
function_completion_kind,
self_prefix,
);
}
}
RequestedItems::OnlyTypes => {
self.builtin_types_completion(&prefix);
Expand Down Expand Up @@ -518,16 +532,18 @@
typ: &Type,
prefix: &str,
function_completion_kind: FunctionCompletionKind,
self_prefix: bool,
) {
match typ {
Type::Struct(struct_type, generics) => {
self.complete_struct_fields(&struct_type.borrow(), generics, prefix);
self.complete_struct_fields(&struct_type.borrow(), generics, prefix, self_prefix);
}
Type::MutableReference(typ) => {
return self.complete_type_fields_and_methods(
typ,
prefix,
function_completion_kind,
self_prefix,
);
}
Type::Alias(type_alias, _) => {
Expand All @@ -536,10 +552,11 @@
&type_alias.typ,
prefix,
function_completion_kind,
self_prefix,
);
}
Type::Tuple(types) => {
self.complete_tuple_fields(types);
self.complete_tuple_fields(types, self_prefix);
}
Type::FieldElement
| Type::Array(_, _)
Expand All @@ -565,6 +582,7 @@
prefix,
FunctionKind::SelfType(typ),
function_completion_kind,
self_prefix,
);
}

Expand All @@ -574,6 +592,7 @@
prefix: &str,
function_kind: FunctionKind,
function_completion_kind: FunctionCompletionKind,
self_prefix: bool,
) {
let Some(methods_by_name) = self.interner.get_type_methods(typ) else {
return;
Expand All @@ -587,6 +606,7 @@
func_id,
function_completion_kind,
function_kind,
self_prefix,
) {
self.completion_items.push(completion_item);
self.suggested_module_def_ids.insert(ModuleDefId::FunctionId(func_id));
Expand All @@ -603,13 +623,16 @@
function_kind: FunctionKind,
function_completion_kind: FunctionCompletionKind,
) {
let self_prefix = false;

for (name, func_id) in &trait_.method_ids {
if name_matches(name, prefix) {
if let Some(completion_item) = self.function_completion_item(
name,
*func_id,
function_completion_kind,
function_kind,
self_prefix,
) {
self.completion_items.push(completion_item);
self.suggested_module_def_ids.insert(ModuleDefId::FunctionId(*func_id));
Expand All @@ -623,17 +646,19 @@
struct_type: &StructType,
generics: &[Type],
prefix: &str,
self_prefix: bool,
) {
for (name, typ) in &struct_type.get_fields(generics) {
if name_matches(name, prefix) {
self.completion_items.push(struct_field_completion_item(name, typ));
self.completion_items.push(struct_field_completion_item(name, typ, self_prefix));
}
}
}

fn complete_tuple_fields(&mut self, types: &[Type]) {
fn complete_tuple_fields(&mut self, types: &[Type], self_prefix: bool) {
for (index, typ) in types.iter().enumerate() {
self.completion_items.push(field_completion_item(&index.to_string(), typ.to_string()));
let name = index.to_string();
self.completion_items.push(field_completion_item(&name, typ.to_string(), self_prefix));
}
}

Expand Down Expand Up @@ -761,6 +786,23 @@
None
}

fn try_set_self_type(&mut self, pattern: &Pattern) {
match pattern {
Pattern::Identifier(ident) => {
if ident.0.contents == "self" {
let location = Location::new(ident.span(), self.file);
if let Some(ReferenceId::Local(definition_id)) =
self.interner.find_referenced(location)
{
self.self_type = Some(self.interner.definition_type(definition_id));
}
}
}
Pattern::Mutable(pattern, ..) => self.try_set_self_type(pattern),
Pattern::Tuple(..) | Pattern::Struct(..) => (),
}
}

fn includes_span(&self, span: Span) -> bool {
span.start() as usize <= self.byte_index && self.byte_index <= span.end() as usize
}
Expand Down Expand Up @@ -817,6 +859,7 @@
self.collect_type_parameters_in_generics(&noir_function.def.generics);

for param in &noir_function.def.parameters {
self.try_set_self_type(&param.pattern);
param.typ.accept(self);
}

Expand All @@ -830,6 +873,7 @@
noir_function.def.body.accept(Some(span), self);

self.type_parameters = old_type_parameters;
self.self_type = None;

false
}
Expand Down Expand Up @@ -945,7 +989,13 @@
if let Some(typ) = self.interner.type_at_location(location) {
let typ = typ.follow_bindings();
let prefix = "";
self.complete_type_fields_and_methods(&typ, prefix, FunctionCompletionKind::Name);
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
prefix,
FunctionCompletionKind::Name,
self_prefix,
);
return false;
}
}
Expand Down Expand Up @@ -973,7 +1023,13 @@
let offset =
self.byte_index - method_call_expression.method_name.span().start() as usize;
let prefix = prefix[0..offset].to_string();
self.complete_type_fields_and_methods(&typ, &prefix, FunctionCompletionKind::Name);
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
&prefix,
FunctionCompletionKind::Name,
self_prefix,
);
return false;
}
}
Expand Down Expand Up @@ -1042,10 +1098,12 @@
{
let typ = self.interner.definition_type(definition_id);
let prefix = "";
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
prefix,
FunctionCompletionKind::NameAndParameters,
self_prefix,
);
}
}
Expand All @@ -1072,10 +1130,12 @@
if let Some(typ) = self.interner.type_at_location(location) {
let typ = typ.follow_bindings();
let prefix = "";
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
prefix,
FunctionCompletionKind::NameAndParameters,
self_prefix,
);
}
}
Expand Down Expand Up @@ -1136,10 +1196,12 @@
if let Some(typ) = self.interner.type_at_location(location) {
let typ = typ.follow_bindings();
let prefix = ident.to_string().to_case(Case::Snake);
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
&prefix,
FunctionCompletionKind::NameAndParameters,
self_prefix,
);
return false;
}
Expand Down Expand Up @@ -1211,8 +1273,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1276 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 1277 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
24 changes: 20 additions & 4 deletions tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<'a> NodeFinder<'a> {
func_id,
function_completion_kind,
function_kind,
false, // self_prefix
),
ModuleDefId::TypeId(..) => Some(self.struct_completion_item(name)),
ModuleDefId::TypeAliasId(..) => Some(self.type_alias_completion_item(name)),
Expand Down Expand Up @@ -77,6 +78,7 @@ impl<'a> NodeFinder<'a> {
func_id: FuncId,
function_completion_kind: FunctionCompletionKind,
function_kind: FunctionKind,
self_prefix: bool,
) -> Option<CompletionItem> {
let func_meta = self.interner.function_meta(&func_id);

Expand Down Expand Up @@ -135,6 +137,8 @@ impl<'a> NodeFinder<'a> {
} else {
false
};
let name = if self_prefix { format!("self.{}", name) } else { name.clone() };
let name = &name;
let description = func_meta_type_to_string(func_meta, func_self_type.is_some());

let completion_item = match function_completion_kind {
Expand Down Expand Up @@ -294,12 +298,24 @@ fn type_to_self_string(typ: &Type, string: &mut String) {
}
}

pub(super) fn struct_field_completion_item(field: &str, typ: &Type) -> CompletionItem {
field_completion_item(field, typ.to_string())
pub(super) fn struct_field_completion_item(
field: &str,
typ: &Type,
self_type: bool,
) -> CompletionItem {
field_completion_item(field, typ.to_string(), self_type)
}

pub(super) fn field_completion_item(field: &str, typ: impl Into<String>) -> CompletionItem {
simple_completion_item(field, CompletionItemKind::FIELD, Some(typ.into()))
pub(super) fn field_completion_item(
field: &str,
typ: impl Into<String>,
self_type: bool,
) -> CompletionItem {
if self_type {
simple_completion_item(format!("self.{field}"), CompletionItemKind::FIELD, Some(typ.into()))
} else {
simple_completion_item(field, CompletionItemKind::FIELD, Some(typ.into()))
}
}

pub(super) fn simple_completion_item(
Expand Down
33 changes: 31 additions & 2 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
completion_items::{
completion_item_with_sort_text,
completion_item_with_trigger_parameter_hints_command, crate_completion_item,
field_completion_item, module_completion_item, simple_completion_item,
snippet_completion_item,
module_completion_item, simple_completion_item, snippet_completion_item,
},
sort_text::{auto_import_sort_text, self_mismatch_sort_text},
},
Expand Down Expand Up @@ -116,17 +115,21 @@
))
}

fn field_completion_item(field: &str, typ: impl Into<String>) -> CompletionItem {
crate::requests::completion::field_completion_item(field, typ, false)
}

#[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 @@ -296,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 @@ -1538,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 All @@ -1551,7 +1554,7 @@
assert_eq!(items.len(), 1);

let item = &items[0];
assert_eq!(item.label, "barbaz");

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
assert_eq!(
item.label_details,
Some(CompletionItemLabelDetails {
Expand Down Expand Up @@ -1888,4 +1891,30 @@
Some("(use super::barbaz)".to_string()),
);
}

#[test]
async fn test_suggests_self_fields_and_methods() {
let src = r#"
struct Foo {
foobar: Field,
}

impl Foo {
fn foobarbaz(self) {}

fn some_method(self) {
foob>|<
}
}
"#;

assert_completion_excluding_auto_import(
src,
vec![
field_completion_item("self.foobar", "Field"),
function_completion_item("self.foobarbaz()", "self.foobarbaz()", "fn(self)"),
],
)
.await;
}
}
Loading