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())
}