Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Alternative support for len field in ReplaceText event #14

Merged
merged 4 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rls-vfs"
version = "0.1.0"
version = "0.2.0"
authors = ["Nick Cameron <[email protected]>"]
description = "Virtual File System for the RLS"
license = "Apache-2.0/MIT"
Expand Down
31 changes: 20 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ pub enum Change {
},
/// Changes in-memory contents of the previously added file.
ReplaceText {
/// Span of the text to be replaced defined in col/row terms.
span: Span,
/// Length in chars of the text to be replaced. If present,
/// used to calculate replacement range instead of
/// span's row_end/col_end fields. Needed for editors that
/// can't properly calculate the latter fields.
/// Span's row_start/col_start are still assumed valid.
len: Option<u64>,
/// Text to replace specified text range with.
text: String,
},
}
Expand Down Expand Up @@ -414,22 +422,23 @@ impl<U> File<U> {
fn make_change(&mut self, changes: &[&Change]) -> Result<(), Error> {
for c in changes {
let new_text = match **c {
Change::ReplaceText { ref span, ref text } => {
Change::ReplaceText { ref span, ref len, ref text } => {
let range = {
let first_line = self.load_line(span.range.row_start).unwrap();
let last_line = self.load_line(span.range.row_end).unwrap();

let byte_start = self.line_indices[span.range.row_start.0 as usize] +
byte_in_str(first_line, span.range.col_start).unwrap() as u32;
let byte_end = if let Some(len) = span.range.len {
// NOTE: The rest of the file is treated as a continous line here, so we
// can use the `bytes_in_str` method to count up the bytes instead of
// rolling our own. Counting up is neccessary since there might be
// characters that span multiple bytes
byte_start
+ byte_in_str(&self.text[byte_start as usize..], span::Column::new_zero_indexed(len as u32)).unwrap() as u32

let byte_end = if let &Some(len) = len {
// if `len` exists, the replaced portion of text
// is `len` chars starting from row_start/col_start.
byte_start + byte_in_str(
&self.text[byte_start as usize..],
span::Column::new_zero_indexed(len as u32)
).unwrap() as u32
} else {
// if we don't have a range set, use the `end` position instead.
// if no `len`, fall back to using row_end/col_end
// for determining the tail end of replaced text.
let last_line = self.load_line(span.range.row_end).unwrap();
self.line_indices[span.range.row_end.0 as usize] +
byte_in_str(last_line, span.range.col_end).unwrap() as u32
};
Expand Down