From 90efb38c7e55d64cfd8fe90622ee9a58d163b3e9 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Mon, 5 May 2025 18:40:52 +0800 Subject: [PATCH] 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 --- .../src/analysis/completion/mode.rs | 23 ++++++++++---- .../src/fixtures/completion/comment_docs.typ | 5 ++++ .../fixtures/completion/comment_docs_far.typ | 6 ++++ .../snaps/test@comment_docs.typ.snap | 30 +++++++++++++++++++ .../snaps/test@comment_docs_far.typ.snap | 12 ++++++++ .../e2e-workspaces/simple-docs/paper.typ | 6 ++++ .../e2e-workspaces/simple-docs/references.bib | 8 +++++ 7 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 crates/tinymist-query/src/fixtures/completion/comment_docs.typ create mode 100644 crates/tinymist-query/src/fixtures/completion/comment_docs_far.typ create mode 100644 crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs.typ.snap create mode 100644 crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs_far.typ.snap create mode 100644 editors/vscode/e2e-workspaces/simple-docs/paper.typ create mode 100644 editors/vscode/e2e-workspaces/simple-docs/references.bib diff --git a/crates/tinymist-query/src/analysis/completion/mode.rs b/crates/tinymist-query/src/analysis/completion/mode.rs index 5dae6f1a..6205a659 100644 --- a/crates/tinymist-query/src/analysis/completion/mode.rs +++ b/crates/tinymist-query/src/analysis/completion/mode.rs @@ -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::(); 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 { diff --git a/crates/tinymist-query/src/fixtures/completion/comment_docs.typ b/crates/tinymist-query/src/fixtures/completion/comment_docs.typ new file mode 100644 index 00000000..8dbf8f06 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/comment_docs.typ @@ -0,0 +1,5 @@ +/// contains: Document function + +/* range after 4..5 */ +/// +#let f(x, y) = x + y diff --git a/crates/tinymist-query/src/fixtures/completion/comment_docs_far.typ b/crates/tinymist-query/src/fixtures/completion/comment_docs_far.typ new file mode 100644 index 00000000..d4029057 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/comment_docs_far.typ @@ -0,0 +1,6 @@ +/// contains: Document function + +/* range after 4..5 */ +/// + +#let f(x, y) = x + y diff --git a/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs.typ.snap b/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs.typ.snap new file mode 100644 index 00000000..d658d3a2 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs.typ.snap @@ -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 + } + } + } + } + ] + } +] diff --git a/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs_far.typ.snap b/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs_far.typ.snap new file mode 100644 index 00000000..841d466c --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/snaps/test@comment_docs_far.typ.snap @@ -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": [] + } +] diff --git a/editors/vscode/e2e-workspaces/simple-docs/paper.typ b/editors/vscode/e2e-workspaces/simple-docs/paper.typ new file mode 100644 index 00000000..8f93b3f4 --- /dev/null +++ b/editors/vscode/e2e-workspaces/simple-docs/paper.typ @@ -0,0 +1,6 @@ + +#ref(/* position after */ ) + +@Russell:1908 + +#bibliography("references.bib") diff --git a/editors/vscode/e2e-workspaces/simple-docs/references.bib b/editors/vscode/e2e-workspaces/simple-docs/references.bib new file mode 100644 index 00000000..d6e90243 --- /dev/null +++ b/editors/vscode/e2e-workspaces/simple-docs/references.bib @@ -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} \ No newline at end of file