Skip to content

Commit

Permalink
feat: (LSP) suggest $vars inside quote { ... } (#6114)
Browse files Browse the repository at this point in the history
# Description

## Problem

`$var` should be suggested inside `quote { ... }`: only local variables
should be suggested.

## Summary


![lsp-complete-dollar](https://github.com/user-attachments/assets/745494ac-2cac-4dcb-bc5e-03c2209456d5)

## 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 20, 2024
1 parent 871ddc8 commit 73245b3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
35 changes: 34 additions & 1 deletion tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use noirc_frontend::{
macros_api::{ModuleDefId, NodeInterner},
node_interner::ReferenceId,
parser::{Item, ItemKind, ParsedSubModule},
token::CustomAttribute,
token::{CustomAttribute, Token, Tokens},
ParsedModule, StructType, Type, TypeBinding,
};
use sort_text::underscore_sort_text;
Expand Down Expand Up @@ -1560,6 +1560,39 @@ impl<'a> Visitor for NodeFinder<'a> {

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

fn visit_quote(&mut self, tokens: &Tokens) {
let mut last_was_dollar = false;

for token in &tokens.0 {
let span = token.to_span();
if span.end() as usize > self.byte_index {
break;
}

let token = token.token();

if let Token::DollarSign = token {
if span.end() as usize == self.byte_index {
self.local_variables_completion("");
break;
}

last_was_dollar = true;
continue;
}

if span.end() as usize == self.byte_index {
let prefix = token.to_string();
if last_was_dollar {
self.local_variables_completion(&prefix);
}
break;
}

last_was_dollar = false;
}
}
}

fn get_field_type(typ: &Type, name: &str) -> Option<Type> {
Expand Down
48 changes: 48 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,4 +2174,52 @@ mod completion_tests {
assert_eq!(completions.len(), 1);
assert_eq!(completions[0].label, "unquote!(…)");
}

#[test]
async fn test_suggests_variable_in_quoted_after_dollar() {
let src = r#"
fn main() {
comptime {
let some_var = 1;
quote {
$>|<
}
}
}
"#;

assert_completion(
src,
vec![simple_completion_item(
"some_var",
CompletionItemKind::VARIABLE,
Some("Field".to_string()),
)],
)
.await;
}

#[test]
async fn test_suggests_variable_in_quoted_after_dollar_and_letters() {
let src = r#"
fn main() {
comptime {
let some_var = 1;
quote {
$s>|<
}
}
}
"#;

assert_completion(
src,
vec![simple_completion_item(
"some_var",
CompletionItemKind::VARIABLE,
Some("Field".to_string()),
)],
)
.await;
}
}
6 changes: 5 additions & 1 deletion tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ pub(crate) fn on_initialize(
)),
completion_provider: Some(lsp_types::OneOf::Right(lsp_types::CompletionOptions {
resolve_provider: None,
trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
trigger_characters: Some(vec![
".".to_string(), // For method calls
":".to_string(), // For paths
"$".to_string(), // For $var inside `quote { ... }`
]),
all_commit_characters: None,
work_done_progress_options: WorkDoneProgressOptions {
work_done_progress: None,
Expand Down

0 comments on commit 73245b3

Please sign in to comment.