diff --git a/rls-vfs/src/lib.rs b/rls-vfs/src/lib.rs index 637bca34000..5e4764eb378 100644 --- a/rls-vfs/src/lib.rs +++ b/rls-vfs/src/lib.rs @@ -770,7 +770,7 @@ impl TextFile { line_end: span::Row, ) -> Result<&str, Error> { let line_start = line_start.0 as usize; - let mut line_end = line_end.0 as usize; + let mut line_end = line_end.0 as usize + 1; if line_end >= self.line_indices.len() { line_end = self.line_indices.len() - 1; } @@ -795,7 +795,7 @@ impl TextFile { let start = (*try_opt_loc!(self.line_indices.get(line_start))) as usize; let start = start + range.col_start.0 as usize; let end = (*try_opt_loc!(self.line_indices.get(line_end))) as usize; - let end = end + range.col_end.0 as usize; + let end = end + range.col_end.0 as usize + 1; if (end) <= self.text.len() && start <= end { Ok(&self.text[start..end]) diff --git a/rls-vfs/src/test.rs b/rls-vfs/src/test.rs index c81cb40b12b..1bcb9f96636 100644 --- a/rls-vfs/src/test.rs +++ b/rls-vfs/src/test.rs @@ -354,3 +354,86 @@ fn test_wide_utf16() { assert_eq!(vfs.load_file(&Path::new("foo")).unwrap(), FileContents::Text("".to_owned()),); } + +#[test] +fn test_load_span() { + let vfs = VfsInternal::::new(); + let changes = [Change::AddFile { + file: PathBuf::from("foo"), + text: String::from("hello\nfrom\nthe\nother\nside"), + }]; + + vfs.on_changes(&changes).unwrap(); + + assert_eq!( + vfs.load_span(Span::from_positions( + Position::new(Row::new_zero_indexed(0), Column::new_zero_indexed(0)), + Position::new(Row::new_zero_indexed(4), Column::new_zero_indexed(3)), + "foo", + ),) + .unwrap(), + "hello\nfrom\nthe\nother\nside" + ); + + assert_eq!( + vfs.load_span(Span::from_positions( + Position::new(Row::new_zero_indexed(0), Column::new_zero_indexed(2)), + Position::new(Row::new_zero_indexed(4), Column::new_zero_indexed(2)), + "foo", + ),) + .unwrap(), + "llo\nfrom\nthe\nother\nsid" + ); + + assert_eq!( + vfs.load_span(Span::from_positions( + Position::new(Row::new_zero_indexed(2), Column::new_zero_indexed(1)), + Position::new(Row::new_zero_indexed(2), Column::new_zero_indexed(2)), + "foo", + ),) + .unwrap(), + "he" + ); +} + +#[test] +fn test_load_lines() { + let vfs = VfsInternal::::new(); + let changes = [Change::AddFile { + file: PathBuf::from("foo"), + text: String::from("hello\nfrom\nthe\nother\nside"), + }]; + + vfs.on_changes(&changes).unwrap(); + + assert_eq!( + vfs.load_lines(&PathBuf::from("foo"), Row::new_zero_indexed(0), Row::new_zero_indexed(0)) + .unwrap(), + "hello\n" + ); + assert_eq!( + vfs.load_lines(&PathBuf::from("foo"), Row::new_zero_indexed(0), Row::new_zero_indexed(4)) + .unwrap(), + "hello\nfrom\nthe\nother\nside" + ); + assert_eq!( + vfs.load_lines(&PathBuf::from("foo"), Row::new_zero_indexed(2), Row::new_zero_indexed(4)) + .unwrap(), + "the\nother\nside" + ); +} + +#[test] +fn test_load_line() { + let vfs = VfsInternal::::new(); + let changes = [Change::AddFile { + file: PathBuf::from("foo"), + text: String::from("hello\nfrom\nthe\nother\nside"), + }]; + + vfs.on_changes(&changes).unwrap(); + + assert_eq!(vfs.load_line(&PathBuf::from("foo"), Row::new_zero_indexed(0)).unwrap(), "hello\n"); + assert_eq!(vfs.load_line(&PathBuf::from("foo"), Row::new_zero_indexed(2)).unwrap(), "the\n"); + assert_eq!(vfs.load_line(&PathBuf::from("foo"), Row::new_zero_indexed(4)).unwrap(), "side"); +}