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) suggest $vars inside quote { ... } #6114

Merged
merged 1 commit into from
Sep 20, 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
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 @@
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 All @@ -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 @@ -235,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 @@ -1560,6 +1560,39 @@

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 Expand Up @@ -1612,8 +1645,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1648 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 1649 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
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 @@ -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 @@ -2174,4 +2174,52 @@
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
Loading