Skip to content

Commit 2e72e74

Browse files
committed
Revert "Restore line index micro-optimization"
This reverts commit 83a87fc.
1 parent 33e53d4 commit 2e72e74

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

crates/rust-analyzer/src/lsp_utils.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Utilities for LSP-related boilerplate code.
2-
use std::{borrow::Cow, error::Error, ops::Range};
2+
use std::{error::Error, ops::Range};
33

44
use lsp_server::Notification;
55
use ra_db::Canceled;
@@ -84,8 +84,8 @@ impl GlobalState {
8484
pub(crate) fn apply_document_changes(
8585
old_text: &mut String,
8686
content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
87-
mut line_index: Cow<'_, LineIndex>,
8887
) {
88+
let mut line_index = LineIndex::new(old_text);
8989
// The changes we got must be applied sequentially, but can cross lines so we
9090
// have to keep our line index updated.
9191
// Some clients (e.g. Code) sort the ranges in reverse. As an optimization, we
@@ -110,7 +110,7 @@ pub(crate) fn apply_document_changes(
110110
match change.range {
111111
Some(range) => {
112112
if !index_valid.covers(range.end.line) {
113-
line_index = Cow::Owned(LineIndex::new(old_text));
113+
line_index = LineIndex::new(&old_text);
114114
}
115115
index_valid = IndexValid::UpToLineExclusive(range.start.line);
116116
let range = from_proto::text_range(&line_index, range);
@@ -145,15 +145,10 @@ mod tests {
145145
};
146146
}
147147

148-
fn run(text: &mut String, changes: Vec<TextDocumentContentChangeEvent>) {
149-
let line_index = Cow::Owned(LineIndex::new(&text));
150-
super::apply_document_changes(text, changes, line_index);
151-
}
152-
153148
let mut text = String::new();
154-
run(&mut text, vec![]);
149+
apply_document_changes(&mut text, vec![]);
155150
assert_eq!(text, "");
156-
run(
151+
apply_document_changes(
157152
&mut text,
158153
vec![TextDocumentContentChangeEvent {
159154
range: None,
@@ -162,36 +157,39 @@ mod tests {
162157
}],
163158
);
164159
assert_eq!(text, "the");
165-
run(&mut text, c![0, 3; 0, 3 => " quick"]);
160+
apply_document_changes(&mut text, c![0, 3; 0, 3 => " quick"]);
166161
assert_eq!(text, "the quick");
167-
run(&mut text, c![0, 0; 0, 4 => "", 0, 5; 0, 5 => " foxes"]);
162+
apply_document_changes(&mut text, c![0, 0; 0, 4 => "", 0, 5; 0, 5 => " foxes"]);
168163
assert_eq!(text, "quick foxes");
169-
run(&mut text, c![0, 11; 0, 11 => "\ndream"]);
164+
apply_document_changes(&mut text, c![0, 11; 0, 11 => "\ndream"]);
170165
assert_eq!(text, "quick foxes\ndream");
171-
run(&mut text, c![1, 0; 1, 0 => "have "]);
166+
apply_document_changes(&mut text, c![1, 0; 1, 0 => "have "]);
172167
assert_eq!(text, "quick foxes\nhave dream");
173-
run(&mut text, c![0, 0; 0, 0 => "the ", 1, 4; 1, 4 => " quiet", 1, 16; 1, 16 => "s\n"]);
168+
apply_document_changes(
169+
&mut text,
170+
c![0, 0; 0, 0 => "the ", 1, 4; 1, 4 => " quiet", 1, 16; 1, 16 => "s\n"],
171+
);
174172
assert_eq!(text, "the quick foxes\nhave quiet dreams\n");
175-
run(&mut text, c![0, 15; 0, 15 => "\n", 2, 17; 2, 17 => "\n"]);
173+
apply_document_changes(&mut text, c![0, 15; 0, 15 => "\n", 2, 17; 2, 17 => "\n"]);
176174
assert_eq!(text, "the quick foxes\n\nhave quiet dreams\n\n");
177-
run(
175+
apply_document_changes(
178176
&mut text,
179177
c![1, 0; 1, 0 => "DREAM", 2, 0; 2, 0 => "they ", 3, 0; 3, 0 => "DON'T THEY?"],
180178
);
181179
assert_eq!(text, "the quick foxes\nDREAM\nthey have quiet dreams\nDON'T THEY?\n");
182-
run(&mut text, c![0, 10; 1, 5 => "", 2, 0; 2, 12 => ""]);
180+
apply_document_changes(&mut text, c![0, 10; 1, 5 => "", 2, 0; 2, 12 => ""]);
183181
assert_eq!(text, "the quick \nthey have quiet dreams\n");
184182

185183
text = String::from("❤️");
186-
run(&mut text, c![0, 0; 0, 0 => "a"]);
184+
apply_document_changes(&mut text, c![0, 0; 0, 0 => "a"]);
187185
assert_eq!(text, "a❤️");
188186

189187
text = String::from("a\nb");
190-
run(&mut text, c![0, 1; 1, 0 => "\nțc", 0, 1; 1, 1 => "d"]);
188+
apply_document_changes(&mut text, c![0, 1; 1, 0 => "\nțc", 0, 1; 1, 1 => "d"]);
191189
assert_eq!(text, "adcb");
192190

193191
text = String::from("a\nb");
194-
run(&mut text, c![0, 1; 1, 0 => \nc", 0, 2; 0, 2 => "c"]);
192+
apply_document_changes(&mut text, c![0, 1; 1, 0 => \nc", 0, 2; 0, 2 => "c"]);
195193
assert_eq!(text, "ațc\ncb");
196194
}
197195
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//! The main loop of `rust-analyzer` responsible for dispatching LSP
22
//! requests/replies and notifications back to the client.
33
use std::{
4-
borrow::Cow,
54
env, fmt, panic,
65
time::{Duration, Instant},
76
};
87

98
use crossbeam_channel::{select, Receiver};
109
use lsp_server::{Connection, Notification, Request, Response};
11-
use lsp_types::{notification::Notification as _, DidChangeTextDocumentParams};
10+
use lsp_types::notification::Notification as _;
1211
use ra_db::VfsPath;
1312
use ra_ide::{Canceled, FileId};
1413
use ra_prof::profile;
@@ -422,20 +421,15 @@ impl GlobalState {
422421
})?
423422
.on::<lsp_types::notification::DidChangeTextDocument>(|this, params| {
424423
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
425-
let DidChangeTextDocumentParams { text_document, content_changes } = params;
424+
let doc = this.mem_docs.get_mut(&path).unwrap();
426425
let vfs = &mut this.vfs.write().0;
427-
let world = this.snapshot();
428426
let file_id = vfs.file_id(&path).unwrap();
429-
430-
// let file_id = vfs.file_id(&path).unwrap();
431427
let mut text = String::from_utf8(vfs.file_contents(file_id).to_vec()).unwrap();
432-
let line_index = world.analysis.file_line_index(file_id)?;
433-
apply_document_changes(&mut text, content_changes, Cow::Borrowed(&line_index));
428+
apply_document_changes(&mut text, params.content_changes);
434429

435430
// The version passed in DidChangeTextDocument is the version after all edits are applied
436431
// so we should apply it before the vfs is notified.
437-
let doc = this.mem_docs.get_mut(&path).unwrap();
438-
doc.version = text_document.version;
432+
doc.version = params.text_document.version;
439433

440434
vfs.set_file_contents(path.clone(), Some(text.into_bytes()));
441435
}

0 commit comments

Comments
 (0)