From 8d753d8c565636ab01011f9e22b1ef00d04ce617 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Sat, 15 Jun 2024 03:37:39 +0800 Subject: [PATCH] fix: handle the conversion of offset at the EOF (#325) * fix: handle the conversion of offset at the EOF * fix: clippy error * fix: snapshot --- .../tinymist-query/src/lsp_typst_boundary.rs | 29 +++++++++++++++++++ .../tinymist-query/src/semantic_tokens/mod.rs | 5 +++- crates/tinymist/src/actor/export.rs | 4 +-- crates/tinymist/src/actor/typ_client.rs | 2 +- crates/tinymist/src/lib.rs | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crates/tinymist-query/src/lsp_typst_boundary.rs b/crates/tinymist-query/src/lsp_typst_boundary.rs index 6e3b6ac3..a381a19a 100644 --- a/crates/tinymist-query/src/lsp_typst_boundary.rs +++ b/crates/tinymist-query/src/lsp_typst_boundary.rs @@ -213,6 +213,10 @@ pub mod typst_to_lsp { lsp_position_encoding: LspPositionEncoding, typst_source: &Source, ) -> LspPosition { + if typst_offset > typst_source.len_bytes() { + return LspPosition::new(typst_source.len_lines() as u32, 0); + } + let line_index = typst_source.byte_to_line(typst_offset).unwrap(); let column_index = typst_source.byte_to_column(typst_offset).unwrap(); @@ -362,6 +366,31 @@ mod test { } } + #[test] + fn overflow_offset_to_position() { + let source = Source::detached("test"); + + let offset = source.len_bytes(); + let position = typst_to_lsp::offset_to_position(offset, PositionEncoding::Utf16, &source); + assert_eq!( + position, + LspPosition { + line: 0, + character: 4 + } + ); + + let offset = source.len_bytes() + 1; + let position = typst_to_lsp::offset_to_position(offset, PositionEncoding::Utf16, &source); + assert_eq!( + position, + LspPosition { + line: 1, + character: 0 + } + ); + } + #[test] fn utf16_position_to_utf8_offset() { let source = Source::detached(ENCODING_TEST_STRING); diff --git a/crates/tinymist-query/src/semantic_tokens/mod.rs b/crates/tinymist-query/src/semantic_tokens/mod.rs index c46076a8..3524c54b 100644 --- a/crates/tinymist-query/src/semantic_tokens/mod.rs +++ b/crates/tinymist-query/src/semantic_tokens/mod.rs @@ -181,7 +181,10 @@ impl Tokenizer { token_modifiers_bitset: token.modifiers.bitset(), }); } else { - let final_line = self.source.byte_to_line(utf8_end).unwrap() as u32; + let final_line = self + .source + .byte_to_line(utf8_end) + .unwrap_or_else(|| self.source.len_lines()) as u32; let next_offset = self .source .line_to_byte((self.curr_pos.line + 1) as usize) diff --git a/crates/tinymist/src/actor/export.rs b/crates/tinymist/src/actor/export.rs index 443ebf1a..0c843972 100644 --- a/crates/tinymist/src/actor/export.rs +++ b/crates/tinymist/src/actor/export.rs @@ -27,7 +27,7 @@ pub struct ExportConfig { #[derive(Debug)] pub enum ExportRequest { OnTyped, - OnSaved(PathBuf), + OnSaved, Oneshot(Option, oneshot::Sender>), ChangeConfig(ExportConfig), ChangeExportPath(EntryState), @@ -61,7 +61,7 @@ impl ExportActor { ExportRequest::ChangeConfig(config) => self.config = config, ExportRequest::ChangeExportPath(entry) => self.entry = entry, ExportRequest::OnTyped => need_export |= self.config.mode == ExportMode::OnType, - ExportRequest::OnSaved(..) => match self.config.mode { + ExportRequest::OnSaved => match self.config.mode { ExportMode::OnSave => need_export = true, ExportMode::OnDocumentHasTitle => need_export |= doc.title.is_some(), _ => {} diff --git a/crates/tinymist/src/actor/typ_client.rs b/crates/tinymist/src/actor/typ_client.rs index b4a12520..6afbdf37 100644 --- a/crates/tinymist/src/actor/typ_client.rs +++ b/crates/tinymist/src/actor/typ_client.rs @@ -456,7 +456,7 @@ impl CompileClientActor { pub fn on_save_export(&self, path: PathBuf) -> anyhow::Result<()> { info!("CompileActor: on save export: {}", path.display()); - let _ = self.export_tx.send(ExportRequest::OnSaved(path)); + let _ = self.export_tx.send(ExportRequest::OnSaved); Ok(()) } diff --git a/crates/tinymist/src/lib.rs b/crates/tinymist/src/lib.rs index f27c071f..31846485 100644 --- a/crates/tinymist/src/lib.rs +++ b/crates/tinymist/src/lib.rs @@ -32,7 +32,7 @@ pub mod harness; mod resource; mod server; mod state; -mod tools; +pub mod tools; pub mod transport; mod utils; mod world;