Skip to content

Commit d5ad66e

Browse files
committed
Add position encoding conversion tests
1 parent 9c6595a commit d5ad66e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

crates/ark/src/lsp/document.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,53 @@ mod tests {
334334

335335
assert!(!document.parse.has_error());
336336
}
337+
338+
#[test]
339+
fn test_tree_sitter_point_from_lsp_position_wide_encoding() {
340+
// The emoji is 4 UTF-8 bytes and 2 UTF-16 bytes
341+
let mut document = Document::new("😃a", None);
342+
document.position_encoding = PositionEncoding::Wide(biome_line_index::WideEncoding::Utf16);
343+
344+
let point = document
345+
.tree_sitter_point_from_lsp_position(lsp_types::Position::new(0, 2))
346+
.unwrap();
347+
assert_eq!(point, Point::new(0, 4));
348+
349+
let point = document
350+
.tree_sitter_point_from_lsp_position(lsp_types::Position::new(0, 3))
351+
.unwrap();
352+
assert_eq!(point, Point::new(0, 5));
353+
}
354+
355+
#[test]
356+
fn test_lsp_position_from_tree_sitter_point_wide_encoding() {
357+
let mut document = Document::new("😃a", None);
358+
document.position_encoding = PositionEncoding::Wide(biome_line_index::WideEncoding::Utf16);
359+
360+
let position = document
361+
.lsp_position_from_tree_sitter_point(Point::new(0, 4))
362+
.unwrap();
363+
assert_eq!(position, lsp_types::Position::new(0, 2));
364+
365+
let position = document
366+
.lsp_position_from_tree_sitter_point(Point::new(0, 5))
367+
.unwrap();
368+
assert_eq!(position, lsp_types::Position::new(0, 3));
369+
}
370+
371+
#[test]
372+
fn test_utf8_position_roundtrip_multibyte() {
373+
// `é` is 2 bytes
374+
let mut document = Document::new(\n", None);
375+
document.position_encoding = PositionEncoding::Utf8;
376+
377+
let lsp_position = lsp_types::Position::new(0, 2);
378+
let point = document
379+
.tree_sitter_point_from_lsp_position(lsp_position)
380+
.unwrap();
381+
assert_eq!(point, Point::new(0, 2));
382+
383+
let roundtrip_position = document.lsp_position_from_tree_sitter_point(point).unwrap();
384+
assert_eq!(roundtrip_position, lsp_position);
385+
}
337386
}

0 commit comments

Comments
 (0)