Skip to content

Commit

Permalink
feat: (LSP) if in runtime code, always suggest functions that return …
Browse files Browse the repository at this point in the history
…Quoted as macro calls (#6098)

# Description

## Problem

When in non-comptime code, when suggesting a function that returns
`Quoted` it should never be suggested as a non-macro call.

## Summary


![lsp-suggest-macro-call](https://github.com/user-attachments/assets/bf870da8-59fe-4993-af73-60e865d141c2)

## Additional Context

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Sep 19, 2024
1 parent 30d7035 commit 4a160cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
14 changes: 14 additions & 0 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct NodeFinder<'a> {
/// 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 @@ impl<'a> NodeFinder<'a> {
nesting: 0,
auto_import_line: 0,
self_type: None,
in_comptime: false,
}
}

Expand Down Expand Up @@ -1056,8 +1058,12 @@ impl<'a> Visitor for NodeFinder<'a> {
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 @@ impl<'a> Visitor for NodeFinder<'a> {
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 @@ impl<'a> Visitor for NodeFinder<'a> {
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
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 @@ -2126,7 +2126,9 @@ mod completion_tests {
comptime fn foobar() -> Quoted {}
fn main() {
fooba>|<
comptime {
fooba>|<
}
}
"#;

Expand All @@ -2140,6 +2142,24 @@ mod completion_tests {
.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

0 comments on commit 4a160cb

Please sign in to comment.