From d08e81cdd818dd3378c292767e15a38e6bbc6f6c Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sat, 19 Jan 2019 22:02:50 +0800 Subject: refactor completions to use TextEdit instead of InsertText --- crates/ra_lsp_server/src/conv.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'crates/ra_lsp_server/src/conv.rs') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6e187d49e..76215a975 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -1,13 +1,13 @@ use lsp_types::{ - self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink, + self, CreateFile, DocumentChangeOperation, DocumentChanges, Location, LocationLink, Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, WorkspaceEdit, }; use ra_ide_api::{ CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit, - InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo, - LineCol, LineIndex, translate_offset_with_edit + NavigationTarget, SourceChange, SourceFileEdit, RangeInfo, + LineCol, LineIndex, translate_offset_with_edit, InsertTextFormat }; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_text_edit::{AtomTextEdit, TextEdit}; @@ -74,27 +74,30 @@ impl Conv for CompletionItemKind { } } -impl Conv for CompletionItem { +impl ConvWith for CompletionItem { + type Ctx = LineIndex; type Output = ::lsp_types::CompletionItem; - fn conv(self) -> ::Output { - let mut res = ::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)); + + 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, ..Default::default() }; - match self.insert_text() { - InsertText::PlainText { text } => { - res.insert_text = Some(text); - res.insert_text_format = Some(InsertTextFormat::PlainText); - } - InsertText::Snippet { text } => { - res.insert_text = Some(text); - res.insert_text_format = Some(InsertTextFormat::Snippet); - } - } + res.insert_text_format = Some(match self.insert_text_format() { + InsertTextFormat::Snippet => lsp_types::InsertTextFormat::Snippet, + InsertTextFormat::PlainText => lsp_types::InsertTextFormat::PlainText, + }); + res } } -- cgit v1.2.3 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/src/conv.rs') 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 From 2a43638052213d1faa690e6d68bd5702e44fa027 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sun, 20 Jan 2019 12:02:00 +0800 Subject: use a combination of `source_change` and `text_edit` for `CompleteItem` --- crates/ra_lsp_server/src/conv.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'crates/ra_lsp_server/src/conv.rs') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 54a977b7a..23b226fac 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -13,7 +13,6 @@ 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; @@ -79,13 +78,11 @@ impl ConvWith for CompletionItem { type Ctx = LineIndex; type Output = ::lsp_types::CompletionItem; - fn conv_with(self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { - let atom_text_edit = AtomTextEdit::replace(self.replace_range(), self.insert_text()); + fn conv_with(mut self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { + let atom_text_edit = AtomTextEdit::replace(self.source_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)) + let additional_text_edits = if let Some(edit) = self.take_text_edit() { + Some(edit.conv_with(ctx)) } else { None }; -- cgit v1.2.3