From 0063f03e86f4222a5027720142eb20db4adc485d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 11:24:16 +0300 Subject: hide atom edits a bit --- crates/ra_lsp_server/src/conv.rs | 15 ++++++++++----- crates/ra_lsp_server/src/main_loop/handlers.rs | 11 +++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 198dbfc49..3531b727e 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -97,21 +97,21 @@ impl ConvWith for TextEdit { type Output = Vec; fn conv_with(self, line_index: &LineIndex) -> Vec { - self.into_atoms() + self.as_atoms() .into_iter() .map_conv_with(line_index) .collect() } } -impl ConvWith for AtomTextEdit { +impl<'a> ConvWith for &'a AtomTextEdit { type Ctx = LineIndex; type Output = languageserver_types::TextEdit; fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit { languageserver_types::TextEdit { range: self.delete.conv_with(line_index), - new_text: self.insert, + new_text: self.insert.clone(), } } } @@ -199,7 +199,7 @@ impl TryConvWith for SourceChange { .source_file_edits .iter() .find(|it| it.file_id == pos.file_id) - .map(|it| it.edits.as_slice()) + .map(|it| it.edit.as_atoms()) .unwrap_or(&[]); let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); let position = @@ -265,7 +265,12 @@ impl TryConvWith for SourceFileEdit { version: None, }; let line_index = world.analysis().file_line_index(self.file_id); - let edits = self.edits.into_iter().map_conv_with(&line_index).collect(); + let edits = self + .edit + .as_atoms() + .iter() + .map_conv_with(&line_index) + .collect(); Ok(TextDocumentEdit { text_document, edits, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 801966304..1751d7fa8 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -107,9 +107,16 @@ pub fn handle_on_type_formatting( }; let edits = match world.analysis().on_eq_typed(position) { None => return Ok(None), - Some(mut action) => action.source_file_edits.pop().unwrap().edits, + Some(mut action) => action + .source_file_edits + .pop() + .unwrap() + .edit + .as_atoms() + .iter() + .map_conv_with(&line_index) + .collect(), }; - let edits = edits.into_iter().map_conv_with(&line_index).collect(); Ok(Some(edits)) } -- cgit v1.2.3 From b5b44659a42cf982590519317ede9ead354f9c4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 12:18:14 +0300 Subject: edits use source-root API --- crates/ra_lsp_server/src/conv.rs | 17 +++++++++-------- crates/ra_lsp_server/src/server_world.rs | 10 +++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 3531b727e..218ded4ee 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -283,16 +283,17 @@ impl TryConvWith for FileSystemEdit { type Output = req::FileSystemEdit; fn try_conv_with(self, world: &ServerWorld) -> Result { let res = match self { - FileSystemEdit::CreateFile { anchor, path } => { - let uri = world.file_id_to_uri(anchor)?; - let path = &path.as_str()[3..]; // strip `../` b/c url is weird - let uri = uri.join(path)?; + FileSystemEdit::CreateFile { source_root, path } => { + let uri = world.path_to_uri(source_root, &path)?; req::FileSystemEdit::CreateFile { uri } } - FileSystemEdit::MoveFile { file, path } => { - let src = world.file_id_to_uri(file)?; - let path = &path.as_str()[3..]; // strip `../` b/c url is weird - let dst = src.join(path)?; + FileSystemEdit::MoveFile { + src, + dst_source_root, + dst_path, + } => { + let src = world.file_id_to_uri(src)?; + let dst = world.path_to_uri(dst_source_root, &dst_path)?; req::FileSystemEdit::MoveFile { src, dst } } }; diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index 785877c4b..73cccc9dd 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -8,7 +8,7 @@ use ra_analysis::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId }; -use ra_vfs::{Vfs, VfsChange, VfsFile}; +use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot}; use rustc_hash::FxHashMap; use relative_path::RelativePathBuf; use parking_lot::RwLock; @@ -183,4 +183,12 @@ impl ServerWorld { .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; Ok(url) } + + pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result { + let base = self.vfs.read().root2path(VfsRoot(root.0)); + let path = path.to_path(base); + let url = Url::from_file_path(&path) + .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; + Ok(url) + } } -- cgit v1.2.3