aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/conv.rs
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-15 22:44:23 +0100
committerJeremy A. Kolb <[email protected]>2018-10-16 14:41:10 +0100
commit61f3a438d3a729a6be941bca1ff4c6a97a33f221 (patch)
tree6551967cc8c6e921b66071453ad7888a9121d326 /crates/ra_lsp_server/src/conv.rs
parent39cb6c6d3f78b193f5873c3492e530bbd24d5dd2 (diff)
Cargo Format
Run `cargo fmt` and ignore generated files
Diffstat (limited to 'crates/ra_lsp_server/src/conv.rs')
-rw-r--r--crates/ra_lsp_server/src/conv.rs66
1 files changed, 31 insertions, 35 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index a75b160c5..8325e8c1e 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -1,17 +1,12 @@
1use languageserver_types::{ 1use languageserver_types::{
2 Range, SymbolKind, Position, TextEdit, Location, Url, 2 Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3 TextDocumentIdentifier, VersionedTextDocumentIdentifier, TextDocumentItem, 3 TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier,
4 TextDocumentPositionParams, TextDocumentEdit,
5};
6use ra_editor::{LineIndex, LineCol, Edit, AtomEdit};
7use ra_syntax::{SyntaxKind, TextUnit, TextRange};
8use ra_analysis::{FileId, SourceChange, SourceFileEdit, FileSystemEdit};
9
10use crate::{
11 Result,
12 server_world::ServerWorld,
13 req,
14}; 4};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit};
6use ra_editor::{AtomEdit, Edit, LineCol, LineIndex};
7use ra_syntax::{SyntaxKind, TextRange, TextUnit};
8
9use crate::{req, server_world::ServerWorld, Result};
15 10
16pub trait Conv { 11pub trait Conv {
17 type Output; 12 type Output;
@@ -190,8 +185,12 @@ impl TryConvWith for SourceChange {
190 None => None, 185 None => None,
191 Some(pos) => { 186 Some(pos) => {
192 let line_index = world.analysis().file_line_index(pos.file_id); 187 let line_index = world.analysis().file_line_index(pos.file_id);
193 let edits = self.source_file_edits.iter().find(|it| it.file_id == pos.file_id) 188 let edits = self
194 .map(|it| it.edits.as_slice()).unwrap_or(&[]); 189 .source_file_edits
190 .iter()
191 .find(|it| it.file_id == pos.file_id)
192 .map(|it| it.edits.as_slice())
193 .unwrap_or(&[]);
195 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); 194 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
196 let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64); 195 let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64);
197 Some(TextDocumentPositionParams { 196 Some(TextDocumentPositionParams {
@@ -224,11 +223,11 @@ fn translate_offset_with_edit(
224 let fallback = pre_edit_index.line_col(offset); 223 let fallback = pre_edit_index.line_col(offset);
225 let edit = match edits.first() { 224 let edit = match edits.first() {
226 None => return fallback, 225 None => return fallback,
227 Some(edit) => edit 226 Some(edit) => edit,
228 }; 227 };
229 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert); 228 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
230 if !(edit.delete.start() <= offset && offset <= end_offset) { 229 if !(edit.delete.start() <= offset && offset <= end_offset) {
231 return fallback 230 return fallback;
232 } 231 }
233 let rel_offset = offset - edit.delete.start(); 232 let rel_offset = offset - edit.delete.start();
234 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset); 233 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
@@ -255,11 +254,11 @@ impl TryConvWith for SourceFileEdit {
255 version: None, 254 version: None,
256 }; 255 };
257 let line_index = world.analysis().file_line_index(self.file_id); 256 let line_index = world.analysis().file_line_index(self.file_id);
258 let edits = self.edits 257 let edits = self.edits.into_iter().map_conv_with(&line_index).collect();
259 .into_iter() 258 Ok(TextDocumentEdit {
260 .map_conv_with(&line_index) 259 text_document,
261 .collect(); 260 edits,
262 Ok(TextDocumentEdit { text_document, edits }) 261 })
263 } 262 }
264} 263}
265 264
@@ -273,13 +272,13 @@ impl TryConvWith for FileSystemEdit {
273 let path = &path.as_str()[3..]; // strip `../` b/c url is weird 272 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
274 let uri = uri.join(path)?; 273 let uri = uri.join(path)?;
275 req::FileSystemEdit::CreateFile { uri } 274 req::FileSystemEdit::CreateFile { uri }
276 }, 275 }
277 FileSystemEdit::MoveFile { file, path } => { 276 FileSystemEdit::MoveFile { file, path } => {
278 let src = world.file_id_to_uri(file)?; 277 let src = world.file_id_to_uri(file)?;
279 let path = &path.as_str()[3..]; // strip `../` b/c url is weird 278 let path = &path.as_str()[3..]; // strip `../` b/c url is weird
280 let dst = src.join(path)?; 279 let dst = src.join(path)?;
281 req::FileSystemEdit::MoveFile { src, dst } 280 req::FileSystemEdit::MoveFile { src, dst }
282 }, 281 }
283 }; 282 };
284 Ok(res) 283 Ok(res)
285 } 284 }
@@ -291,12 +290,9 @@ pub fn to_location(
291 world: &ServerWorld, 290 world: &ServerWorld,
292 line_index: &LineIndex, 291 line_index: &LineIndex,
293) -> Result<Location> { 292) -> Result<Location> {
294 let url = file_id.try_conv_with(world)?; 293 let url = file_id.try_conv_with(world)?;
295 let loc = Location::new( 294 let loc = Location::new(url, range.conv_with(line_index));
296 url, 295 Ok(loc)
297 range.conv_with(line_index),
298 );
299 Ok(loc)
300} 296}
301 297
302pub trait MapConvWith<'a>: Sized + 'a { 298pub trait MapConvWith<'a>: Sized + 'a {
@@ -309,8 +305,9 @@ pub trait MapConvWith<'a>: Sized + 'a {
309} 305}
310 306
311impl<'a, I> MapConvWith<'a> for I 307impl<'a, I> MapConvWith<'a> for I
312 where I: Iterator + 'a, 308where
313 I::Item: ConvWith 309 I: Iterator + 'a,
310 I::Item: ConvWith,
314{ 311{
315 type Ctx = <I::Item as ConvWith>::Ctx; 312 type Ctx = <I::Item as ConvWith>::Ctx;
316 type Output = <I::Item as ConvWith>::Output; 313 type Output = <I::Item as ConvWith>::Output;
@@ -322,9 +319,9 @@ pub struct ConvWithIter<'a, I, Ctx: 'a> {
322} 319}
323 320
324impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx> 321impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
325 where 322where
326 I: Iterator, 323 I: Iterator,
327 I::Item: ConvWith<Ctx=Ctx>, 324 I::Item: ConvWith<Ctx = Ctx>,
328{ 325{
329 type Item = <I::Item as ConvWith>::Output; 326 type Item = <I::Item as ConvWith>::Output;
330 327
@@ -332,4 +329,3 @@ impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
332 self.iter.next().map(|item| item.conv_with(self.ctx)) 329 self.iter.next().map(|item| item.conv_with(self.ctx))
333 } 330 }
334} 331}
335