From 422971c8b6f39e4ffe8f2e51c0015e765089f841 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:15:40 +0800 Subject: [PATCH] fix: skip code completion when editing length (#882) --- crates/tinymist-query/src/completion.rs | 21 +++++++++++++++++++ .../src/fixtures/completion/edit_length.typ | 2 ++ .../snaps/test@edit_length.typ.snap | 10 +++++++++ 3 files changed, 33 insertions(+) create mode 100644 crates/tinymist-query/src/fixtures/completion/edit_length.typ create mode 100644 crates/tinymist-query/src/fixtures/completion/snaps/test@edit_length.typ.snap diff --git a/crates/tinymist-query/src/completion.rs b/crates/tinymist-query/src/completion.rs index 21b91c1ab..2eb5ef8ec 100644 --- a/crates/tinymist-query/src/completion.rs +++ b/crates/tinymist-query/src/completion.rs @@ -101,6 +101,27 @@ impl StatefulRequest for CompletionRequest { } } + // Skip if an error node starts with number (e.g. `1pt`) + if matches!( + deref_target, + Some(DerefTarget::Callee(..) | DerefTarget::VarAccess(..) | DerefTarget::Normal(..),) + ) { + let node = LinkedNode::new(source.root()).leaf_at_compat(cursor)?; + if node.erroneous() { + let mut n = node.text().chars(); + + match n.next() { + Some(c) if c.is_numeric() => return None, + Some('.') => { + if matches!(n.next(), Some(c) if c.is_numeric()) { + return None; + } + } + _ => {} + } + } + } + // Do some completion specific to the deref target let mut ident_like = None; let mut completion_result = None; diff --git a/crates/tinymist-query/src/fixtures/completion/edit_length.typ b/crates/tinymist-query/src/fixtures/completion/edit_length.typ new file mode 100644 index 000000000..c64e6032a --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/edit_length.typ @@ -0,0 +1,2 @@ +/// contains: top-edge +#text(size: 1p/* range 0..1 */) \ No newline at end of file diff --git a/crates/tinymist-query/src/fixtures/completion/snaps/test@edit_length.typ.snap b/crates/tinymist-query/src/fixtures/completion/snaps/test@edit_length.typ.snap new file mode 100644 index 000000000..a7f1b0c10 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/completion/snaps/test@edit_length.typ.snap @@ -0,0 +1,10 @@ +--- +source: crates/tinymist-query/src/completion.rs +description: Completion on / (37..38) +expression: "JsonRepr::new_pure(results)" +input_file: crates/tinymist-query/src/fixtures/completion/edit_length.typ +snapshot_kind: text +--- +[ + null +]