From 2a60c1caf93770d56a64b64e9496be2d5e24eccb Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sun, 10 Mar 2024 01:38:31 +0800 Subject: [PATCH] fix: call typst-ide with cursor values --- crates/tinymist-query/src/completion.rs | 11 +++++++---- crates/tinymist-query/src/goto_definition.rs | 5 +++-- crates/tinymist-query/src/hover.rs | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/crates/tinymist-query/src/completion.rs b/crates/tinymist-query/src/completion.rs index 6892884a..b6dadc4f 100644 --- a/crates/tinymist-query/src/completion.rs +++ b/crates/tinymist-query/src/completion.rs @@ -14,14 +14,17 @@ impl CompletionRequest { doc: Option>, position_encoding: PositionEncoding, ) -> Option { + let doc = doc.as_deref(); let source = get_suitable_source_in_workspace(world, &self.path).ok()?; - let typst_offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + let offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + // the typst's cursor is 1-based, so we need to add 1 to the offset + let cursor = offset + 1; - let (typst_start_offset, completions) = - typst_ide::autocomplete(world, doc.as_deref(), &source, typst_offset, self.explicit)?; + let (offset, completions) = + typst_ide::autocomplete(world, doc, &source, cursor, self.explicit)?; let lsp_start_position = - typst_to_lsp::offset_to_position(typst_start_offset, position_encoding, &source); + typst_to_lsp::offset_to_position(offset, position_encoding, &source); let replace_range = LspRange::new(lsp_start_position, self.position); Some(typst_to_lsp::completions(&completions, replace_range).into()) } diff --git a/crates/tinymist-query/src/goto_definition.rs b/crates/tinymist-query/src/goto_definition.rs index 2c46de98..ec8a507c 100644 --- a/crates/tinymist-query/src/goto_definition.rs +++ b/crates/tinymist-query/src/goto_definition.rs @@ -17,10 +17,11 @@ impl GotoDefinitionRequest { position_encoding: PositionEncoding, ) -> Option { let source = get_suitable_source_in_workspace(world, &self.path).ok()?; - let typst_offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + let offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + let cursor = offset + 1; let def = { - let ast_node = LinkedNode::new(source.root()).leaf_at(typst_offset + 1)?; + let ast_node = LinkedNode::new(source.root()).leaf_at(cursor)?; let t: &dyn World = world; find_definition(t.track(), source.id(), ast_node)? }; diff --git a/crates/tinymist-query/src/hover.rs b/crates/tinymist-query/src/hover.rs index 0a655924..6fd81a33 100644 --- a/crates/tinymist-query/src/hover.rs +++ b/crates/tinymist-query/src/hover.rs @@ -14,11 +14,13 @@ impl HoverRequest { position_encoding: PositionEncoding, ) -> Option { let source = get_suitable_source_in_workspace(world, &self.path).ok()?; - let typst_offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + let offset = lsp_to_typst::position(self.position, position_encoding, &source)?; + // the typst's cursor is 1-based, so we need to add 1 to the offset + let cursor = offset + 1; - let typst_tooltip = typst_ide::tooltip(world, doc.as_deref(), &source, typst_offset)?; + let typst_tooltip = typst_ide::tooltip(world, doc.as_deref(), &source, cursor)?; - let ast_node = LinkedNode::new(source.root()).leaf_at(typst_offset + 1)?; + let ast_node = LinkedNode::new(source.root()).leaf_at(cursor)?; let range = typst_to_lsp::range(ast_node.range(), &source, position_encoding); Some(Hover {