Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
fix(rls-vfs): line loading inclusivity
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetthardwick authored and bennett-microsoft committed Dec 17, 2019
1 parent fed7a31 commit a6addc5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rls-vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl TextFile {
line_end: span::Row<span::ZeroIndexed>,
) -> 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;
}
Expand All @@ -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])
Expand Down
83 changes: 83 additions & 0 deletions rls-vfs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MockFileLoader, ()>::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::<MockFileLoader, ()>::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::<MockFileLoader, ()>::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");
}

0 comments on commit a6addc5

Please sign in to comment.