aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/conv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/conv.rs')
-rw-r--r--crates/ra_lsp_server/src/conv.rs61
1 files changed, 43 insertions, 18 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 0d6e62727..7230fb101 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -1,11 +1,16 @@
1use languageserver_types::{ 1use languageserver_types::{
2 self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, 2 self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location,
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, 3 Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
4 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
5 WorkspaceEdit,
4}; 6};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange, CompletionItem, CompletionItemKind, InsertText}; 7use ra_analysis::{
6use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; 8 CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
7use ra_text_edit::{AtomTextEdit, TextEdit}; 9 InsertText, NavigationTarget, SourceChange, SourceFileEdit,
10};
11use ra_editor::{translate_offset_with_edit, LineCol, LineIndex};
8use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 12use ra_syntax::{SyntaxKind, TextRange, TextUnit};
13use ra_text_edit::{AtomTextEdit, TextEdit};
9 14
10use crate::{req, server_world::ServerWorld, Result}; 15use crate::{req, server_world::ServerWorld, Result};
11 16
@@ -49,7 +54,7 @@ impl Conv for CompletionItemKind {
49 type Output = ::languageserver_types::CompletionItemKind; 54 type Output = ::languageserver_types::CompletionItemKind;
50 55
51 fn conv(self) -> <Self as Conv>::Output { 56 fn conv(self) -> <Self as Conv>::Output {
52 use ::languageserver_types::CompletionItemKind::*; 57 use languageserver_types::CompletionItemKind::*;
53 match self { 58 match self {
54 CompletionItemKind::Keyword => Keyword, 59 CompletionItemKind::Keyword => Keyword,
55 CompletionItemKind::Snippet => Snippet, 60 CompletionItemKind::Snippet => Snippet,
@@ -82,7 +87,6 @@ impl Conv for CompletionItem {
82 InsertText::Snippet { text } => { 87 InsertText::Snippet { text } => {
83 res.insert_text = Some(text); 88 res.insert_text = Some(text);
84 res.insert_text_format = Some(InsertTextFormat::Snippet); 89 res.insert_text_format = Some(InsertTextFormat::Snippet);
85 res.kind = Some(languageserver_types::CompletionItemKind::Keyword);
86 } 90 }
87 } 91 }
88 res 92 res
@@ -267,12 +271,20 @@ impl TryConvWith for SourceChange {
267 }) 271 })
268 } 272 }
269 }; 273 };
270 let source_file_edits = self.source_file_edits.try_conv_with(world)?; 274 let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
271 let file_system_edits = self.file_system_edits.try_conv_with(world)?; 275 for resource_op in self.file_system_edits.try_conv_with(world)? {
276 document_changes.push(DocumentChangeOperation::Op(resource_op));
277 }
278 for text_document_edit in self.source_file_edits.try_conv_with(world)? {
279 document_changes.push(DocumentChangeOperation::Edit(text_document_edit));
280 }
281 let workspace_edit = WorkspaceEdit {
282 changes: None,
283 document_changes: Some(DocumentChanges::Operations(document_changes)),
284 };
272 Ok(req::SourceChange { 285 Ok(req::SourceChange {
273 label: self.label, 286 label: self.label,
274 source_file_edits, 287 workspace_edit,
275 file_system_edits,
276 cursor_position, 288 cursor_position,
277 }) 289 })
278 } 290 }
@@ -302,27 +314,40 @@ impl TryConvWith for SourceFileEdit {
302 314
303impl TryConvWith for FileSystemEdit { 315impl TryConvWith for FileSystemEdit {
304 type Ctx = ServerWorld; 316 type Ctx = ServerWorld;
305 type Output = req::FileSystemEdit; 317 type Output = ResourceOp;
306 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> { 318 fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
307 let res = match self { 319 let res = match self {
308 FileSystemEdit::CreateFile { source_root, path } => { 320 FileSystemEdit::CreateFile { source_root, path } => {
309 let uri = world.path_to_uri(source_root, &path)?; 321 let uri = world.path_to_uri(source_root, &path)?.to_string();
310 req::FileSystemEdit::CreateFile { uri } 322 ResourceOp::Create(CreateFile { uri, options: None })
311 } 323 }
312 FileSystemEdit::MoveFile { 324 FileSystemEdit::MoveFile {
313 src, 325 src,
314 dst_source_root, 326 dst_source_root,
315 dst_path, 327 dst_path,
316 } => { 328 } => {
317 let src = world.file_id_to_uri(src)?; 329 let old_uri = world.file_id_to_uri(src)?.to_string();
318 let dst = world.path_to_uri(dst_source_root, &dst_path)?; 330 let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
319 req::FileSystemEdit::MoveFile { src, dst } 331 ResourceOp::Rename(RenameFile {
332 old_uri,
333 new_uri,
334 options: None,
335 })
320 } 336 }
321 }; 337 };
322 Ok(res) 338 Ok(res)
323 } 339 }
324} 340}
325 341
342impl TryConvWith for &NavigationTarget {
343 type Ctx = ServerWorld;
344 type Output = Location;
345 fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
346 let line_index = world.analysis().file_line_index(self.file_id());
347 to_location(self.file_id(), self.range(), &world, &line_index)
348 }
349}
350
326pub fn to_location( 351pub fn to_location(
327 file_id: FileId, 352 file_id: FileId,
328 range: TextRange, 353 range: TextRange,