minor: Simplify implementation of apply_document_changes
While reading through the code base, I stumbled across a piece of code that I found hard to read despite its simple purpose. This is my attempt at making the code easier to understand for future readers. I won't be offended if this is too minor and not worth your time.
This commit is contained in:
parent
e1e4626ff5
commit
b9933fdaaa
1 changed files with 12 additions and 23 deletions
|
@ -171,30 +171,19 @@ pub(crate) fn apply_document_changes(
|
||||||
file_contents: impl FnOnce() -> String,
|
file_contents: impl FnOnce() -> String,
|
||||||
mut content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
|
mut content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
|
||||||
) -> String {
|
) -> String {
|
||||||
// Skip to the last full document change, as it invalidates all previous changes anyways.
|
// If at least one of the changes is a full document change, use the last
|
||||||
let mut start = content_changes
|
// of them as the starting point and ignore all previous changes.
|
||||||
.iter()
|
let (mut text, content_changes) =
|
||||||
.rev()
|
match content_changes.iter().rposition(|change| change.range.is_none()) {
|
||||||
.position(|change| change.range.is_none())
|
Some(idx) => {
|
||||||
.map(|idx| content_changes.len() - idx - 1)
|
let text = mem::take(&mut content_changes[idx].text);
|
||||||
.unwrap_or(0);
|
(text, &content_changes[idx + 1..])
|
||||||
|
|
||||||
let mut text: String = match content_changes.get_mut(start) {
|
|
||||||
// peek at the first content change as an optimization
|
|
||||||
Some(lsp_types::TextDocumentContentChangeEvent { range: None, text, .. }) => {
|
|
||||||
let text = mem::take(text);
|
|
||||||
start += 1;
|
|
||||||
|
|
||||||
// The only change is a full document update
|
|
||||||
if start == content_changes.len() {
|
|
||||||
return text;
|
|
||||||
}
|
}
|
||||||
text
|
None => (file_contents(), &content_changes[..]),
|
||||||
}
|
};
|
||||||
Some(_) => file_contents(),
|
if content_changes.is_empty() {
|
||||||
// we received no content changes
|
return text;
|
||||||
None => return file_contents(),
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let mut line_index = LineIndex {
|
let mut line_index = LineIndex {
|
||||||
// the index will be overwritten in the bottom loop's first iteration
|
// the index will be overwritten in the bottom loop's first iteration
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue