on master help sign in
lsp: edit events change ranges need to affect each other.
commitd5f9622e2ee82887904ff5d5d433438075228cd1 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 17 Feb 2021, 17:28 (6 years ago) parent9cac44c
2 files changed+82−20
helix-lsp/src/client.rs+41−6 helix-view/src/document.rs+41−14
helix-lsp/src/client.rs+41−6
@@ -247,19 +247,47 @@ impl Client {
247247 .await
248248 }
249249
250+ // TODO: this is dumb. TextEdit describes changes to the initial doc (concurrent), but
251+ // TextDocumentContentChangeEvent describes a series of changes (sequential).
252+ // So S -> S1 -> S2, meaning positioning depends on the previous edits.
253+ //
254+ // Calculation is therefore a bunch trickier.
250255 pub fn changeset_to_changes(
251256 old_text: &Rope,
257+ new_text: &Rope,
252258 changeset: &ChangeSet,
253259 ) -> Vec<lsp::TextDocumentContentChangeEvent> {
254260 let mut iter = changeset.changes().iter().peekable();
255261 let mut old_pos = 0;
262+ let mut new_pos = 0;
256263
257264 let mut changes = Vec::new();
258265
259266 use crate::util::pos_to_lsp_pos;
260267 use helix_core::Operation::*;
261268
269+ // TODO: stolen from syntax.rs, share
270+ use helix_core::RopeSlice;
271+ fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
272+ let lsp::Position {
273+ mut line,
274+ mut character,
275+ } = pos;
276+
277+ // TODO: there should be a better way here
278+ for ch in text.chars() {
279+ if ch == '\n' {
280+ line += 1;
281+ character = 0;
282+ } else {
283+ character += ch.len_utf16() as u32;
284+ }
285+ }
286+ lsp::Position { line, character }
287+ }
288+
262289 let old_text = old_text.slice(..);
290+ let new_text = new_text.slice(..);
263291
264292 // TODO: verify this function, specifically line num counting
265293
@@ -271,10 +299,12 @@ impl Client {
271299 let mut old_end = old_pos + len;
272300
273301 match change {
274− Retain(_) => {}
302+ Retain(i) => {
303+ new_pos += i;
304+ }
275305 Delete(_) => {
276− let start = pos_to_lsp_pos(&old_text, old_pos);
277− let end = pos_to_lsp_pos(&old_text, old_end);
306+ let start = pos_to_lsp_pos(&new_text, new_pos);
307+ let end = traverse(start, old_text.slice(old_pos..old_end));
278308
279309 // deletion
280310 changes.push(lsp::TextDocumentContentChangeEvent {
@@ -284,12 +314,14 @@ impl Client {
284314 });
285315 }
286316 Insert(s) => {
287− let start = pos_to_lsp_pos(&old_text, old_pos);
317+ let start = pos_to_lsp_pos(&new_text, new_pos);
318+
319+ new_pos += s.chars().count();
288320
289321 // a subsequent delete means a replace, consume it
290322 let end = if let Some(Delete(len)) = iter.peek() {
291323 old_end = old_pos + len;
292− let end = pos_to_lsp_pos(&old_text, old_end);
324+ let end = traverse(start, old_text.slice(old_pos..old_end));
293325
294326 iter.next();
295327
@@ -318,6 +350,7 @@ impl Client {
318350 &self,
319351 text_document: lsp::VersionedTextDocumentIdentifier,
320352 old_text: &Rope,
353+ new_text: &Rope,
321354 changes: &ChangeSet,
322355 ) -> Result<()> {
323356 // figure out what kind of sync the server supports
@@ -343,7 +376,9 @@ impl Client {
343376 text: "".to_string(),
344377 }] // TODO: probably need old_state here too?
345378 }
346− lsp::TextDocumentSyncKind::Incremental => Self::changeset_to_changes(old_text, changes),
379+ lsp::TextDocumentSyncKind::Incremental => {
380+ Self::changeset_to_changes(old_text, new_text, changes)
381+ }
347382 lsp::TextDocumentSyncKind::None => return Ok(()),
348383 };
349384
helix-view/src/document.rs+41−14
@@ -183,6 +183,7 @@ impl Document {
183183 let notify = language_server.text_document_did_change(
184184 self.versioned_identifier(),
185185 &old_doc,
186+ self.text(),
186187 transaction.changes(),
187188 );
188189
@@ -301,8 +302,8 @@ mod test {
301302
302303 let transaction = Transaction::insert(&doc.state, " world".into());
303304 let old_doc = doc.state.clone();
304− let changes = Client::changeset_to_changes(&old_doc.doc, transaction.changes());
305305 doc.apply(&transaction);
306+ let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
306307
307308 assert_eq!(
308309 changes,
@@ -320,8 +321,8 @@ mod test {
320321
321322 let transaction = transaction.invert(&old_doc);
322323 let old_doc = doc.state.clone();
323− let changes = Client::changeset_to_changes(&old_doc.doc, transaction.changes());
324324 doc.apply(&transaction);
325+ let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
325326
326327 // line: 0-based.
327328 // col: 0-based, gaps between chars.
@@ -343,22 +344,48 @@ mod test {
343344
344345 // replace
345346
347+ // also tests that changes are layered, positions depend on previous changes.
348+
346349 doc.state.selection = Selection::single(0, 5);
347− let transaction = Transaction::change_by_selection(&doc.state, |range| {
348− (range.from(), range.to(), Some("aeiou".into()))
349− });
350− let changes = Client::changeset_to_changes(&doc.state.doc, transaction.changes());
350+ let transaction = Transaction::change(
351+ &doc.state,
352+ vec![(0, 2, Some("aei".into())), (3, 5, Some("ou".into()))].into_iter(),
353+ );
354+ // aeilou
355+ doc.apply(&transaction);
356+ let changes =
357+ Client::changeset_to_changes(&doc.state.doc, doc.text(), transaction.changes());
351358
352359 assert_eq!(
353360 changes,
354− &[lsp::TextDocumentContentChangeEvent {
355− range: Some(lsp::Range::new(
356− lsp::Position::new(0, 0),
357− lsp::Position::new(0, 5)
358− )),
359− text: "aeiou".into(),
360− range_length: None,
361− }]
361+ &[
362+ // 0 1 2 3 4 5
363+ // |h|e|l|l|o|
364+ // ----
365+ //
366+ // aeillo
367+ lsp::TextDocumentContentChangeEvent {
368+ range: Some(lsp::Range::new(
369+ lsp::Position::new(0, 0),
370+ lsp::Position::new(0, 2)
371+ )),
372+ text: "aei".into(),
373+ range_length: None,
374+ },
375+ // 0 1 2 3 4 5 6
376+ // |a|e|i|l|l|o|
377+ // -----
378+ //
379+ // aeilou
380+ lsp::TextDocumentContentChangeEvent {
381+ range: Some(lsp::Range::new(
382+ lsp::Position::new(0, 4),
383+ lsp::Position::new(0, 6)
384+ )),
385+ text: "ou".into(),
386+ range_length: None,
387+ }
388+ ]
362389 );
363390 }
364391}