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

Commit

Permalink
Merge pull request #1600 from bennetthardwick/fix/load-lines
Browse files Browse the repository at this point in the history
Fix rls-vfs line loading inclusivity
  • Loading branch information
Xanewok committed Jun 20, 2022
2 parents df11a04 + e27abd1 commit 6ae8421
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 10 deletions.
13 changes: 3 additions & 10 deletions rls-vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,7 @@ impl TextFile {
}

fn load_line(&self, line: span::Row<span::ZeroIndexed>) -> Result<&str, Error> {
let start = *try_opt_loc!(self.line_indices.get(line.0 as usize));
let end = *try_opt_loc!(self.line_indices.get(line.0 as usize + 1));

if (end as usize) <= self.text.len() && start <= end {
Ok(&self.text[start as usize..end as usize])
} else {
Err(Error::BadLocation)
}
self.load_lines(line, line)
}

fn load_lines(
Expand All @@ -770,7 +763,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 +788,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 6ae8421

Please sign in to comment.