fix: complete parameters in scope (#146)

This commit is contained in:
Myriad-Dreamin 2024-04-01 10:40:05 +08:00 committed by GitHub
parent b43dfcf11e
commit de5df05b67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 314 additions and 5 deletions

View file

@ -0,0 +1,4 @@
#let aa(aab, aac, aabc) ={
aac(/* range -2..0 */);
}

View file

@ -0,0 +1,290 @@
---
source: crates/tinymist-query/src/completion.rs
description: Completion on c( (31..33)
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/func_params.typ
---
[
{
"isIncomplete": true,
"items": [
{
"kind": 7,
"label": "array",
"textEdit": {
"newText": "array",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "parbreak",
"textEdit": {
"newText": "parbreak()${1:}",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "smallcaps",
"textEdit": {
"newText": "smallcaps(${1:})",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "pagebreak",
"textEdit": {
"newText": "pagebreak(${1:})",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "metadata",
"textEdit": {
"newText": "metadata(${1:})",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 21,
"label": "aqua",
"textEdit": {
"newText": "aqua",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 3,
"label": "aa",
"textEdit": {
"newText": "aa",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 6,
"label": "aab",
"textEdit": {
"newText": "aab",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 6,
"label": "aabc",
"textEdit": {
"newText": "aabc",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 6,
"label": "aac",
"textEdit": {
"newText": "aac",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 15,
"label": "import package",
"textEdit": {
"newText": "import \"@${1:}\": ${2:items}",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 15,
"label": "include (package)",
"textEdit": {
"newText": "include \"@${1:}\"",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 15,
"label": "array literal",
"textEdit": {
"newText": "(${1:1, 2, 3})",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 15,
"label": "dictionary literal",
"textEdit": {
"newText": "(${1:a: 1, b: 2})",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
}
]
},
{
"isIncomplete": true,
"items": [
{
"kind": 6,
"label": "aabc",
"textEdit": {
"newText": "aabc",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
},
{
"kind": 6,
"label": "aac",
"textEdit": {
"newText": "aac",
"range": {
"end": {
"character": 5,
"line": 1
},
"start": {
"character": 2,
"line": 1
}
}
}
}
]
}
]

View file

@ -14,6 +14,10 @@ impl<'a> CompletionContext<'a> {
pub fn scope_completions_(&mut self, parens: bool, filter: impl Fn(&Value) -> bool) {
let mut defined = BTreeMap::new();
let mut try_insert = |name: EcoString, kind: CompletionKind| {
if name.is_empty() {
return;
}
if let std::collections::btree_map::Entry::Vacant(entry) = defined.entry(name) {
entry.insert(kind);
}
@ -74,11 +78,22 @@ impl<'a> CompletionContext<'a> {
}
}
}
if let Some(v) = parent.cast::<ast::ForLoop>() {
if node.prev_sibling_kind() != Some(SyntaxKind::In) {
let pattern = v.pattern();
for ident in pattern.bindings() {
try_insert(ident.get().clone(), CompletionKind::Variable);
if let Some(v) = node.cast::<ast::Closure>() {
for param in v.params().children() {
match param {
ast::Param::Pos(pattern) => {
for ident in pattern.bindings() {
try_insert(ident.get().clone(), CompletionKind::Variable);
}
}
ast::Param::Named(n) => {
try_insert(n.name().get().clone(), CompletionKind::Variable)
}
ast::Param::Spread(s) => {
if let Some(sink_ident) = s.sink_ident() {
try_insert(sink_ident.get().clone(), CompletionKind::Variable)
}
}
}
}
}