dev: identify let context completely (#255)

This commit is contained in:
Myriad-Dreamin 2024-05-07 19:26:31 +08:00 committed by GitHub
parent 806fb9bdfc
commit 7e59b9dbcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 11 deletions

View file

@ -83,17 +83,19 @@ impl StatefulRequest for CompletionRequest {
if let Some(d) = &deref_target {
let node = d.node();
// skip if is the let binding item, todo, check whether the pattern is exact
// todo: check if the pattern(span) is exact, instead of just checking the
// parent kind
if matches!(
(d, node.parent_kind()),
(
DerefTarget::VarAccess(..),
Some(SyntaxKind::LetBinding | SyntaxKind::Closure)
)
) {
return None;
// skip if is the let binding item *directly*
if matches!(d, DerefTarget::VarAccess(..)) {
match node.parent_kind() {
// complete the init part of the let binding
Some(SyntaxKind::LetBinding) => {
let parent = node.parent()?;
let parent_init = parent.cast::<ast::LetBinding>()?.init()?;
let parent_init = parent.find(parent_init.span())?;
parent_init.find(node.span())?;
}
Some(SyntaxKind::Closure) => return None,
_ => {}
}
}
}

View file

@ -0,0 +1,8 @@
// contains: a
#let a = 1;
#let b = /* range after 1..2 */ #();
#let add(x, y) = {
x + y
}

View file

@ -0,0 +1,33 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on (60..61)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/let-context.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 6,
"label": "a",
"labelDetails": {
"description": "1"
},
"textEdit": {
"newText": "a",
"range": {
"end": {
"character": 32,
"line": 3
},
"start": {
"character": 32,
"line": 3
}
}
}
}
]
}
]