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) if in runtime code, always suggest functions that return Quoted as macro calls #6098

Merged
merged 1 commit into from
Sep 19, 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
14 changes: 14 additions & 0 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
use super::process_request;

mod auto_import;
mod builtins;

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

impl<'a> NodeFinder<'a> {
Expand Down Expand Up @@ -156,6 +157,7 @@
nesting: 0,
auto_import_line: 0,
self_type: None,
in_comptime: false,
}
}

Expand Down Expand Up @@ -233,7 +235,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 238 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 @@ -1056,8 +1058,12 @@
self.collect_local_variables(&param.pattern);
}

let old_in_comptime = self.in_comptime;
self.in_comptime = noir_function.def.is_comptime;

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

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

Expand Down Expand Up @@ -1278,8 +1284,12 @@
let old_local_variables = self.local_variables.clone();
self.local_variables.clear();

let old_in_comptime = self.in_comptime;
self.in_comptime = true;

statement.accept(self);

self.in_comptime = old_in_comptime;
self.local_variables = old_local_variables;

false
Expand Down Expand Up @@ -1424,8 +1434,12 @@
let old_local_variables = self.local_variables.clone();
self.local_variables.clear();

let old_in_comptime = self.in_comptime;
self.in_comptime = true;

block_expression.accept(Some(span), self);

self.in_comptime = old_in_comptime;
self.local_variables = old_local_variables;

false
Expand Down Expand Up @@ -1598,8 +1612,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1615 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 1616 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: 7 additions & 1 deletion tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ impl<'a> NodeFinder<'a> {
if modifiers.is_comptime
&& matches!(func_meta.return_type(), Type::Quoted(QuotedType::Quoted))
{
vec![make_completion_item(false), make_completion_item(true)]
if self.in_comptime {
vec![make_completion_item(false), make_completion_item(true)]
} else {
// If not in a comptime block we can't operate with comptime values so the only thing
// we can do is call a macro.
vec![make_completion_item(true)]
}
} else {
vec![make_completion_item(false)]
}
Expand Down
22 changes: 21 additions & 1 deletion tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 136 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 138 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 143 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 @@ -303,7 +303,7 @@
mod bar {
mod something {}

use super::foob>|<

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

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

Check warning on line 1549 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 @@ -1559,7 +1559,7 @@
assert_eq!(items.len(), 1);

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

Check warning on line 1562 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 @@ -2126,7 +2126,9 @@
comptime fn foobar() -> Quoted {}

fn main() {
fooba>|<
comptime {
fooba>|<
}
}
"#;

Expand All @@ -2140,6 +2142,24 @@
.await;
}

#[test]
async fn test_suggests_only_macro_call_if_comptime_function_returns_quoted_and_outside_comptime(
) {
let src = r#"
comptime fn foobar() -> Quoted {}

fn main() {
fooba>|<
}
"#;

assert_completion_excluding_auto_import(
src,
vec![function_completion_item("foobar!()", "foobar!()", "fn() -> Quoted")],
)
.await;
}

#[test]
async fn test_only_suggests_macro_call_for_unquote() {
let src = r#"
Expand Down
Loading