diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-20 18:27:23 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-20 18:27:23 +0000 |
commit | f133702f723203a60a1b4dade51418261cdbc133 (patch) | |
tree | 4d6d38d2e6185bb39b08ccfd5b89cdb5740d5ef8 /crates/ra_lsp_server/src | |
parent | b89f8b6b4929ec09be4f9b13f87ad56b3235bd39 (diff) | |
parent | fd8db14c2fcec7801edbdbb8e7f4d4c982a3da09 (diff) |
Merge #574
574: refactor completions to use TextEdit instead of InsertText r=matklad a=gfreezy
1. migrate from `insertText` to `TextEdit` from `CompleteItem`
2. use `insta` to test completions
Co-authored-by: gfreezy <[email protected]>
Diffstat (limited to 'crates/ra_lsp_server/src')
-rw-r--r-- | crates/ra_lsp_server/src/conv.rs | 38 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 6 |
2 files changed, 27 insertions, 17 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6e187d49e..23b226fac 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs | |||
@@ -1,13 +1,13 @@ | |||
1 | use lsp_types::{ | 1 | use lsp_types::{ |
2 | self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink, | 2 | self, CreateFile, DocumentChangeOperation, DocumentChanges, Location, LocationLink, |
3 | Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, | 3 | Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, |
4 | TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, | 4 | TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, |
5 | WorkspaceEdit, | 5 | WorkspaceEdit, |
6 | }; | 6 | }; |
7 | use ra_ide_api::{ | 7 | use ra_ide_api::{ |
8 | CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit, | 8 | CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit, |
9 | InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo, | 9 | NavigationTarget, SourceChange, SourceFileEdit, RangeInfo, |
10 | LineCol, LineIndex, translate_offset_with_edit | 10 | LineCol, LineIndex, translate_offset_with_edit, InsertTextFormat |
11 | }; | 11 | }; |
12 | use ra_syntax::{SyntaxKind, TextRange, TextUnit}; | 12 | use ra_syntax::{SyntaxKind, TextRange, TextUnit}; |
13 | use ra_text_edit::{AtomTextEdit, TextEdit}; | 13 | use ra_text_edit::{AtomTextEdit, TextEdit}; |
@@ -74,27 +74,33 @@ impl Conv for CompletionItemKind { | |||
74 | } | 74 | } |
75 | } | 75 | } |
76 | 76 | ||
77 | impl Conv for CompletionItem { | 77 | impl ConvWith for CompletionItem { |
78 | type Ctx = LineIndex; | ||
78 | type Output = ::lsp_types::CompletionItem; | 79 | type Output = ::lsp_types::CompletionItem; |
79 | 80 | ||
80 | fn conv(self) -> <Self as Conv>::Output { | 81 | fn conv_with(mut self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { |
81 | let mut res = ::lsp_types::CompletionItem { | 82 | let atom_text_edit = AtomTextEdit::replace(self.source_range(), self.insert_text()); |
83 | let text_edit = (&atom_text_edit).conv_with(ctx); | ||
84 | let additional_text_edits = if let Some(edit) = self.take_text_edit() { | ||
85 | Some(edit.conv_with(ctx)) | ||
86 | } else { | ||
87 | None | ||
88 | }; | ||
89 | |||
90 | let mut res = lsp_types::CompletionItem { | ||
82 | label: self.label().to_string(), | 91 | label: self.label().to_string(), |
83 | detail: self.detail().map(|it| it.to_string()), | 92 | detail: self.detail().map(|it| it.to_string()), |
84 | filter_text: Some(self.lookup().to_string()), | 93 | filter_text: Some(self.lookup().to_string()), |
85 | kind: self.kind().map(|it| it.conv()), | 94 | kind: self.kind().map(|it| it.conv()), |
95 | text_edit: Some(text_edit), | ||
96 | additional_text_edits, | ||
86 | ..Default::default() | 97 | ..Default::default() |
87 | }; | 98 | }; |
88 | match self.insert_text() { | 99 | res.insert_text_format = Some(match self.insert_text_format() { |
89 | InsertText::PlainText { text } => { | 100 | InsertTextFormat::Snippet => lsp_types::InsertTextFormat::Snippet, |
90 | res.insert_text = Some(text); | 101 | InsertTextFormat::PlainText => lsp_types::InsertTextFormat::PlainText, |
91 | res.insert_text_format = Some(InsertTextFormat::PlainText); | 102 | }); |
92 | } | 103 | |
93 | InsertText::Snippet { text } => { | ||
94 | res.insert_text = Some(text); | ||
95 | res.insert_text_format = Some(InsertTextFormat::Snippet); | ||
96 | } | ||
97 | } | ||
98 | res | 104 | res |
99 | } | 105 | } |
100 | } | 106 | } |
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 02393f728..497f819be 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs | |||
@@ -340,7 +340,11 @@ pub fn handle_completion( | |||
340 | None => return Ok(None), | 340 | None => return Ok(None), |
341 | Some(items) => items, | 341 | Some(items) => items, |
342 | }; | 342 | }; |
343 | let items = items.into_iter().map(|item| item.conv()).collect(); | 343 | let line_index = world.analysis().file_line_index(position.file_id); |
344 | let items = items | ||
345 | .into_iter() | ||
346 | .map(|item| item.conv_with(&line_index)) | ||
347 | .collect(); | ||
344 | 348 | ||
345 | Ok(Some(req::CompletionResponse::Array(items))) | 349 | Ok(Some(req::CompletionResponse::Array(items))) |
346 | } | 350 | } |