aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/conv.rs36
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs2
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs18
-rw-r--r--crates/ra_lsp_server/src/server_world.rs10
4 files changed, 44 insertions, 22 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 7467f472c..218ded4ee 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -2,7 +2,7 @@ use languageserver_types::{
2 self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, 2 self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, 3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
4}; 4};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition}; 5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition};
6use ra_editor::{LineCol, LineIndex}; 6use ra_editor::{LineCol, LineIndex};
7use ra_text_edit::{AtomTextEdit, TextEdit}; 7use ra_text_edit::{AtomTextEdit, TextEdit};
8use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 8use ra_syntax::{SyntaxKind, TextRange, TextUnit};
@@ -97,21 +97,21 @@ impl ConvWith for TextEdit {
97 type Output = Vec<languageserver_types::TextEdit>; 97 type Output = Vec<languageserver_types::TextEdit>;
98 98
99 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> { 99 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
100 self.into_atoms() 100 self.as_atoms()
101 .into_iter() 101 .into_iter()
102 .map_conv_with(line_index) 102 .map_conv_with(line_index)
103 .collect() 103 .collect()
104 } 104 }
105} 105}
106 106
107impl ConvWith for AtomTextEdit { 107impl<'a> ConvWith for &'a AtomTextEdit {
108 type Ctx = LineIndex; 108 type Ctx = LineIndex;
109 type Output = languageserver_types::TextEdit; 109 type Output = languageserver_types::TextEdit;
110 110
111 fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit { 111 fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
112 languageserver_types::TextEdit { 112 languageserver_types::TextEdit {
113 range: self.delete.conv_with(line_index), 113 range: self.delete.conv_with(line_index),
114 new_text: self.insert, 114 new_text: self.insert.clone(),
115 } 115 }
116 } 116 }
117} 117}
@@ -199,7 +199,7 @@ impl TryConvWith for SourceChange {
199 .source_file_edits 199 .source_file_edits
200 .iter() 200 .iter()
201 .find(|it| it.file_id == pos.file_id) 201 .find(|it| it.file_id == pos.file_id)
202 .map(|it| it.edits.as_slice()) 202 .map(|it| it.edit.as_atoms())
203 .unwrap_or(&[]); 203 .unwrap_or(&[]);
204 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); 204 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
205 let position = 205 let position =
@@ -256,7 +256,7 @@ fn translate_offset_with_edit(
256 } 256 }
257} 257}
258 258
259impl TryConvWith for SourceFileNodeEdit { 259impl TryConvWith for SourceFileEdit {
260 type Ctx = ServerWorld; 260 type Ctx = ServerWorld;
261 type Output = TextDocumentEdit; 261 type Output = TextDocumentEdit;
262 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> { 262 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
@@ -265,7 +265,12 @@ impl TryConvWith for SourceFileNodeEdit {
265 version: None, 265 version: None,
266 }; 266 };
267 let line_index = world.analysis().file_line_index(self.file_id); 267 let line_index = world.analysis().file_line_index(self.file_id);
268 let edits = self.edits.into_iter().map_conv_with(&line_index).collect(); 268 let edits = self
269 .edit
270 .as_atoms()
271 .iter()
272 .map_conv_with(&line_index)
273 .collect();
269 Ok(TextDocumentEdit { 274 Ok(TextDocumentEdit {
270 text_document, 275 text_document,
271 edits, 276 edits,
@@ -278,16 +283,17 @@ impl TryConvWith for FileSystemEdit {
278 type Output = req::FileSystemEdit; 283 type Output = req::FileSystemEdit;
279 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> { 284 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
280 let res = match self { 285 let res = match self {
281 FileSystemEdit::CreateFile { anchor, path } => { 286 FileSystemEdit::CreateFile { source_root, path } => {
282 let uri = world.file_id_to_uri(anchor)?; 287 let uri = world.path_to_uri(source_root, &path)?;
283 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
284 let uri = uri.join(path)?;
285 req::FileSystemEdit::CreateFile { uri } 288 req::FileSystemEdit::CreateFile { uri }
286 } 289 }
287 FileSystemEdit::MoveFile { file, path } => { 290 FileSystemEdit::MoveFile {
288 let src = world.file_id_to_uri(file)?; 291 src,
289 let path = &path.as_str()[3..]; // strip `../` b/c url is weird 292 dst_source_root,
290 let dst = src.join(path)?; 293 dst_path,
294 } => {
295 let src = world.file_id_to_uri(src)?;
296 let dst = world.path_to_uri(dst_source_root, &dst_path)?;
291 req::FileSystemEdit::MoveFile { src, dst } 297 req::FileSystemEdit::MoveFile { src, dst }
292 } 298 }
293 }; 299 };
diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs
index 1d6e3e5d6..afe0fec89 100644
--- a/crates/ra_lsp_server/src/main_loop.rs
+++ b/crates/ra_lsp_server/src/main_loop.rs
@@ -387,7 +387,7 @@ impl<'a> PoolDispatcher<'a> {
387 RawResponse::err( 387 RawResponse::err(
388 id, 388 id,
389 ErrorCode::ContentModified as i32, 389 ErrorCode::ContentModified as i32,
390 e.to_string(), 390 format!("content modified: {}", e),
391 ) 391 )
392 } else { 392 } else {
393 RawResponse::err( 393 RawResponse::err(
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 572ae7fb5..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(
107 }; 107 };
108 let edits = match world.analysis().on_eq_typed(position) { 108 let edits = match world.analysis().on_eq_typed(position) {
109 None => return Ok(None), 109 None => return Ok(None),
110 Some(mut action) => action.source_file_edits.pop().unwrap().edits, 110 Some(mut action) => action
111 .source_file_edits
112 .pop()
113 .unwrap()
114 .edit
115 .as_atoms()
116 .iter()
117 .map_conv_with(&line_index)
118 .collect(),
111 }; 119 };
112 let edits = edits.into_iter().map_conv_with(&line_index).collect();
113 Ok(Some(edits)) 120 Ok(Some(edits))
114} 121}
115 122
@@ -446,8 +453,9 @@ pub fn handle_folding_range(
446 .into_iter() 453 .into_iter()
447 .map(|fold| { 454 .map(|fold| {
448 let kind = match fold.kind { 455 let kind = match fold.kind {
449 FoldKind::Comment => FoldingRangeKind::Comment, 456 FoldKind::Comment => Some(FoldingRangeKind::Comment),
450 FoldKind::Imports => FoldingRangeKind::Imports, 457 FoldKind::Imports => Some(FoldingRangeKind::Imports),
458 FoldKind::Block => None,
451 }; 459 };
452 let range = fold.range.conv_with(&line_index); 460 let range = fold.range.conv_with(&line_index);
453 FoldingRange { 461 FoldingRange {
@@ -455,7 +463,7 @@ pub fn handle_folding_range(
455 start_character: Some(range.start.character), 463 start_character: Some(range.start.character),
456 end_line: range.end.line, 464 end_line: range.end.line,
457 end_character: Some(range.start.character), 465 end_character: Some(range.start.character),
458 kind: Some(kind), 466 kind,
459 } 467 }
460 }) 468 })
461 .collect(), 469 .collect(),
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::{
8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, 8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
9 SourceRootId 9 SourceRootId
10}; 10};
11use ra_vfs::{Vfs, VfsChange, VfsFile}; 11use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
12use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
13use relative_path::RelativePathBuf; 13use relative_path::RelativePathBuf;
14use parking_lot::RwLock; 14use parking_lot::RwLock;
@@ -183,4 +183,12 @@ impl ServerWorld {
183 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; 183 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
184 Ok(url) 184 Ok(url)
185 } 185 }
186
187 pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
188 let base = self.vfs.read().root2path(VfsRoot(root.0));
189 let path = path.to_path(base);
190 let url = Url::from_file_path(&path)
191 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?;
192 Ok(url)
193 }
186} 194}