fix: call typst-ide with cursor values

This commit is contained in:
Myriad-Dreamin 2024-03-10 01:38:31 +08:00
parent 888aecbe7e
commit 2a60c1caf9
3 changed files with 15 additions and 9 deletions

View file

@ -14,14 +14,17 @@ impl CompletionRequest {
doc: Option<Arc<TypstDocument>>,
position_encoding: PositionEncoding,
) -> Option<CompletionResponse> {
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())
}

View file

@ -17,10 +17,11 @@ impl GotoDefinitionRequest {
position_encoding: PositionEncoding,
) -> Option<GotoDefinitionResponse> {
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)?
};

View file

@ -14,11 +14,13 @@ impl HoverRequest {
position_encoding: PositionEncoding,
) -> Option<Hover> {
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 {