feat: only complete if the next line is a function definition (#1740)

* test: add `paper.typ` for e2e testing

* feat: only complete if the next line is a function definition
This commit is contained in:
Myriad-Dreamin 2025-05-05 18:40:52 +08:00 committed by GitHub
parent 8f0d9c25d7
commit 90efb38c7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 85 additions and 5 deletions

View file

@ -9,15 +9,28 @@ impl CompletionPair<'_, '_, '_> {
if_chain! {
if text == "///" || text == "/// ";
// hash node
if let Some(next) = self.cursor.leaf.next_leaf();
if let Some(hash_node) = self.cursor.leaf.next_leaf();
// let node
if let Some(next_next) = next.next_leaf();
if let Some(next_next) = next_next.next_leaf();
if matches!(next_next.parent_kind(), Some(SyntaxKind::Closure));
if let Some(closure) = next_next.parent();
if let Some(let_node) = hash_node.next_leaf();
if let Some(let_closure) = let_node.next_leaf();
if matches!(let_closure.parent_kind(), Some(SyntaxKind::Closure));
if let Some(closure) = let_closure.parent();
if let Some(closure) = closure.cast::<ast::Expr>();
if let ast::Expr::Closure(c) = closure;
then {
// Only completes if the next line is a function definition
let rng = self.cursor.leaf.offset()..hash_node.offset();
let text_between = &self.cursor.source.text()[rng];
let mut line_count = 0;
for ch in text_between.chars() {
if ch == '\n' {
line_count += 1;
}
if line_count > 1 {
return false;
}
}
let mut doc_snippet: String = if text == "///" {
" $0\n///".to_string()
} else {

View file

@ -0,0 +1,5 @@
/// contains: Document function
/* range after 4..5 */
///
#let f(x, y) = x + y

View file

@ -0,0 +1,6 @@
/// contains: Document function
/* range after 4..5 */
///
#let f(x, y) = x + y

View file

@ -0,0 +1,30 @@
---
source: crates/tinymist-query/src/completion.rs
description: "Completion on \n (59..60)"
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/comment_docs.typ
---
[
{
"isIncomplete": false,
"items": [
{
"kind": 21,
"label": "Document function",
"textEdit": {
"newText": " $0\n///\n/// - x ($1): $2\n/// - y ($3): $4\n/// -> $5",
"range": {
"end": {
"character": 3,
"line": 3
},
"start": {
"character": 3,
"line": 3
}
}
}
}
]
}
]

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/completion.rs
description: "Completion on \n (59..60)"
expression: "JsonRepr::new_pure(results)"
input_file: crates/tinymist-query/src/fixtures/completion/comment_docs_far.typ
---
[
{
"isIncomplete": false,
"items": []
}
]

View file

@ -0,0 +1,6 @@
#ref(/* position after */ <Russell:1908>)
@Russell:1908
#bibliography("references.bib")

View file

@ -0,0 +1,8 @@
@article{Russell:1908,
Author = {Bertand Russell},
Journal = {American Journal of Mathematics},
Pages = {222--262},
Title = {Mathematical logic based on the theory of types},
Volume = 30,
Year = 1908}