From 94d96b60f334e662f516bd0f04cc4191d7a804e6 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sun, 20 Jan 2019 00:38:34 +0800 Subject: refactor to use `remove_range` and `replace_range` instead of TextEdit --- crates/ra_lsp_server/src/conv.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 76215a975..54a977b7a 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -13,6 +13,7 @@ use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_text_edit::{AtomTextEdit, TextEdit}; use crate::{req, server_world::ServerWorld, Result}; +use ra_text_edit::TextEditBuilder; pub trait Conv { type Output; @@ -78,19 +79,24 @@ impl ConvWith for CompletionItem { type Ctx = LineIndex; type Output = ::lsp_types::CompletionItem; - fn conv_with(mut self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { - let text_edit = self.text_edit().map(|t| t.conv_with(ctx)); - let additonal_text_edit = self - .take_additional_text_edits() - .map(|it| it.conv_with(ctx)); + fn conv_with(self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { + let atom_text_edit = AtomTextEdit::replace(self.replace_range(), self.insert_text()); + let text_edit = (&atom_text_edit).conv_with(ctx); + let additional_text_edits = if let Some(delete_range) = self.delete_range() { + let mut builder = TextEditBuilder::default(); + builder.delete(delete_range); + Some(builder.finish().conv_with(ctx)) + } else { + None + }; let mut res = lsp_types::CompletionItem { label: self.label().to_string(), detail: self.detail().map(|it| it.to_string()), filter_text: Some(self.lookup().to_string()), kind: self.kind().map(|it| it.conv()), - text_edit, - additional_text_edits: additonal_text_edit, + text_edit: Some(text_edit), + additional_text_edits, ..Default::default() }; res.insert_text_format = Some(match self.insert_text_format() { -- cgit v1.2.3