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:
Myriad-Dreamin 2024-06-15 03:37:39 +08:00 committed by GitHub
parent 27fa1beb26
commit 8d753d8c56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 5 deletions

View file

@ -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);

View file

@ -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)