From d84fc3beecd1a0776e9c2de88cabd0524ea3dd55 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 11 Feb 2022 16:18:06 -0800 Subject: [PATCH] Remove Buffer::apply_lsp_edits Always use `edits_from_lsp`. --- crates/language/src/buffer.rs | 40 ----------------------------------- crates/project/src/project.rs | 9 ++++++-- 2 files changed, 7 insertions(+), 42 deletions(-) diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 63b0e912f8..bcb6ea68d0 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1561,46 +1561,6 @@ impl Buffer { }) } - pub fn apply_lsp_edits( - &mut self, - edits: I, - version: Option, - cx: &mut ModelContext, - ) -> Result<()> - where - I: IntoIterator, - T: DoubleEndedIterator, - { - let mut anchored_edits = Vec::new(); - let snapshot = - if let Some((version, language_server)) = version.zip(self.language_server.as_mut()) { - language_server.snapshot_for_version(version as usize)? - } else { - self.deref() - }; - for edit in edits { - let range = range_from_lsp(edit.range); - if snapshot.clip_point_utf16(range.start, Bias::Left) != range.start - || snapshot.clip_point_utf16(range.end, Bias::Left) != range.end - { - return Err(anyhow!( - "invalid formatting edits received from language server" - )); - } else { - let start = snapshot.anchor_before(range.start); - let end = snapshot.anchor_before(range.end); - anchored_edits.push((start..end, edit.new_text)); - } - } - - self.start_transaction(); - for (range, new_text) in anchored_edits.into_iter().rev() { - self.edit([range], new_text, cx); - } - self.end_transaction(cx); - Ok(()) - } - fn did_edit( &mut self, old_version: &clock::Global, diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 74fdf7fc8e..9320eb7b34 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -1404,11 +1404,16 @@ impl Project { let resolved_completion = lang_server .request::(completion.lsp_completion) .await?; - if let Some(additional_edits) = resolved_completion.additional_text_edits { + if let Some(edits) = resolved_completion.additional_text_edits { + let edits = buffer_handle + .update(&mut cx, |buffer, cx| buffer.edits_from_lsp(edits, None, cx)) + .await?; buffer_handle.update(&mut cx, |buffer, cx| { buffer.finalize_last_transaction(); buffer.start_transaction(); - buffer.apply_lsp_edits(additional_edits, None, cx).log_err(); + for (range, text) in edits { + buffer.edit([range], text, cx); + } let transaction = if buffer.end_transaction(cx).is_some() { let transaction = buffer.finalize_last_transaction().unwrap().clone(); if !push_to_history {