mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-07-23 04:35:00 +00:00
fix: handle the conversion of offset at the EOF (#325)
* fix: handle the conversion of offset at the EOF * fix: clippy error * fix: snapshot
This commit is contained in:
parent
27fa1beb26
commit
8d753d8c56
5 changed files with 37 additions and 5 deletions
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue