]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/conv.rs
Merge #408
[rust.git] / crates / ra_lsp_server / src / conv.rs
1 use languageserver_types::{
2     self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3     TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
4 };
5 use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange,  CompletionItem, CompletionItemKind, InsertText, NavigationTarget};
6 use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
7 use ra_text_edit::{AtomTextEdit, TextEdit};
8 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
9
10 use crate::{req, server_world::ServerWorld, Result};
11
12 pub trait Conv {
13     type Output;
14     fn conv(self) -> Self::Output;
15 }
16
17 pub trait ConvWith {
18     type Ctx;
19     type Output;
20     fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
21 }
22
23 pub trait TryConvWith {
24     type Ctx;
25     type Output;
26     fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
27 }
28
29 impl Conv for SyntaxKind {
30     type Output = SymbolKind;
31
32     fn conv(self) -> <Self as Conv>::Output {
33         match self {
34             SyntaxKind::FN_DEF => SymbolKind::Function,
35             SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
36             SyntaxKind::ENUM_DEF => SymbolKind::Enum,
37             SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
38             SyntaxKind::MODULE => SymbolKind::Module,
39             SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
40             SyntaxKind::STATIC_DEF => SymbolKind::Constant,
41             SyntaxKind::CONST_DEF => SymbolKind::Constant,
42             SyntaxKind::IMPL_ITEM => SymbolKind::Object,
43             _ => SymbolKind::Variable,
44         }
45     }
46 }
47
48 impl Conv for CompletionItemKind {
49     type Output = ::languageserver_types::CompletionItemKind;
50
51     fn conv(self) -> <Self as Conv>::Output {
52         use ::languageserver_types::CompletionItemKind::*;
53         match self {
54             CompletionItemKind::Keyword => Keyword,
55             CompletionItemKind::Snippet => Snippet,
56             CompletionItemKind::Module => Module,
57             CompletionItemKind::Function => Function,
58             CompletionItemKind::Struct => Struct,
59             CompletionItemKind::Enum => Enum,
60             CompletionItemKind::EnumVariant => EnumMember,
61             CompletionItemKind::Binding => Variable,
62             CompletionItemKind::Field => Field,
63         }
64     }
65 }
66
67 impl Conv for CompletionItem {
68     type Output = ::languageserver_types::CompletionItem;
69
70     fn conv(self) -> <Self as Conv>::Output {
71         let mut res = ::languageserver_types::CompletionItem {
72             label: self.label().to_string(),
73             filter_text: Some(self.lookup().to_string()),
74             kind: self.kind().map(|it| it.conv()),
75             ..Default::default()
76         };
77         match self.insert_text() {
78             InsertText::PlainText { text } => {
79                 res.insert_text = Some(text);
80                 res.insert_text_format = Some(InsertTextFormat::PlainText);
81             }
82             InsertText::Snippet { text } => {
83                 res.insert_text = Some(text);
84                 res.insert_text_format = Some(InsertTextFormat::Snippet);
85             }
86         }
87         res
88     }
89 }
90
91 impl ConvWith for Position {
92     type Ctx = LineIndex;
93     type Output = TextUnit;
94
95     fn conv_with(self, line_index: &LineIndex) -> TextUnit {
96         let line_col = LineCol {
97             line: self.line as u32,
98             col_utf16: self.character as u32,
99         };
100         line_index.offset(line_col)
101     }
102 }
103
104 impl ConvWith for TextUnit {
105     type Ctx = LineIndex;
106     type Output = Position;
107
108     fn conv_with(self, line_index: &LineIndex) -> Position {
109         let line_col = line_index.line_col(self);
110         Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
111     }
112 }
113
114 impl ConvWith for TextRange {
115     type Ctx = LineIndex;
116     type Output = Range;
117
118     fn conv_with(self, line_index: &LineIndex) -> Range {
119         Range::new(
120             self.start().conv_with(line_index),
121             self.end().conv_with(line_index),
122         )
123     }
124 }
125
126 impl ConvWith for Range {
127     type Ctx = LineIndex;
128     type Output = TextRange;
129
130     fn conv_with(self, line_index: &LineIndex) -> TextRange {
131         TextRange::from_to(
132             self.start.conv_with(line_index),
133             self.end.conv_with(line_index),
134         )
135     }
136 }
137
138 impl ConvWith for TextEdit {
139     type Ctx = LineIndex;
140     type Output = Vec<languageserver_types::TextEdit>;
141
142     fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
143         self.as_atoms()
144             .into_iter()
145             .map_conv_with(line_index)
146             .collect()
147     }
148 }
149
150 impl<'a> ConvWith for &'a AtomTextEdit {
151     type Ctx = LineIndex;
152     type Output = languageserver_types::TextEdit;
153
154     fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
155         languageserver_types::TextEdit {
156             range: self.delete.conv_with(line_index),
157             new_text: self.insert.clone(),
158         }
159     }
160 }
161
162 impl<T: ConvWith> ConvWith for Option<T> {
163     type Ctx = <T as ConvWith>::Ctx;
164     type Output = Option<<T as ConvWith>::Output>;
165     fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
166         self.map(|x| ConvWith::conv_with(x, ctx))
167     }
168 }
169
170 impl<'a> TryConvWith for &'a Url {
171     type Ctx = ServerWorld;
172     type Output = FileId;
173     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
174         world.uri_to_file_id(self)
175     }
176 }
177
178 impl TryConvWith for FileId {
179     type Ctx = ServerWorld;
180     type Output = Url;
181     fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
182         world.file_id_to_uri(self)
183     }
184 }
185
186 impl<'a> TryConvWith for &'a TextDocumentItem {
187     type Ctx = ServerWorld;
188     type Output = FileId;
189     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
190         self.uri.try_conv_with(world)
191     }
192 }
193
194 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
195     type Ctx = ServerWorld;
196     type Output = FileId;
197     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
198         self.uri.try_conv_with(world)
199     }
200 }
201
202 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
203     type Ctx = ServerWorld;
204     type Output = FileId;
205     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
206         world.uri_to_file_id(&self.uri)
207     }
208 }
209
210 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
211     type Ctx = ServerWorld;
212     type Output = FilePosition;
213     fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
214         let file_id = self.text_document.try_conv_with(world)?;
215         let line_index = world.analysis().file_line_index(file_id);
216         let offset = self.position.conv_with(&line_index);
217         Ok(FilePosition { file_id, offset })
218     }
219 }
220
221 impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
222     type Ctx = ServerWorld;
223     type Output = FileRange;
224     fn try_conv_with(self, world: &ServerWorld) -> Result<FileRange> {
225         let file_id = self.0.try_conv_with(world)?;
226         let line_index = world.analysis().file_line_index(file_id);
227         let range = self.1.conv_with(&line_index);
228         Ok(FileRange { file_id, range })
229     }
230 }
231
232 impl<T: TryConvWith> TryConvWith for Vec<T> {
233     type Ctx = <T as TryConvWith>::Ctx;
234     type Output = Vec<<T as TryConvWith>::Output>;
235     fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
236         let mut res = Vec::with_capacity(self.len());
237         for item in self {
238             res.push(item.try_conv_with(ctx)?);
239         }
240         Ok(res)
241     }
242 }
243
244 impl TryConvWith for SourceChange {
245     type Ctx = ServerWorld;
246     type Output = req::SourceChange;
247     fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
248         let cursor_position = match self.cursor_position {
249             None => None,
250             Some(pos) => {
251                 let line_index = world.analysis().file_line_index(pos.file_id);
252                 let edit = self
253                     .source_file_edits
254                     .iter()
255                     .find(|it| it.file_id == pos.file_id)
256                     .map(|it| &it.edit);
257                 let line_col = match edit {
258                     Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
259                     None => line_index.line_col(pos.offset),
260                 };
261                 let position =
262                     Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
263                 Some(TextDocumentPositionParams {
264                     text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
265                     position,
266                 })
267             }
268         };
269         let source_file_edits = self.source_file_edits.try_conv_with(world)?;
270         let file_system_edits = self.file_system_edits.try_conv_with(world)?;
271         Ok(req::SourceChange {
272             label: self.label,
273             source_file_edits,
274             file_system_edits,
275             cursor_position,
276         })
277     }
278 }
279
280 impl TryConvWith for SourceFileEdit {
281     type Ctx = ServerWorld;
282     type Output = TextDocumentEdit;
283     fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
284         let text_document = VersionedTextDocumentIdentifier {
285             uri: self.file_id.try_conv_with(world)?,
286             version: None,
287         };
288         let line_index = world.analysis().file_line_index(self.file_id);
289         let edits = self
290             .edit
291             .as_atoms()
292             .iter()
293             .map_conv_with(&line_index)
294             .collect();
295         Ok(TextDocumentEdit {
296             text_document,
297             edits,
298         })
299     }
300 }
301
302 impl TryConvWith for FileSystemEdit {
303     type Ctx = ServerWorld;
304     type Output = req::FileSystemEdit;
305     fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
306         let res = match self {
307             FileSystemEdit::CreateFile { source_root, path } => {
308                 let uri = world.path_to_uri(source_root, &path)?;
309                 req::FileSystemEdit::CreateFile { uri }
310             }
311             FileSystemEdit::MoveFile {
312                 src,
313                 dst_source_root,
314                 dst_path,
315             } => {
316                 let src = world.file_id_to_uri(src)?;
317                 let dst = world.path_to_uri(dst_source_root, &dst_path)?;
318                 req::FileSystemEdit::MoveFile { src, dst }
319             }
320         };
321         Ok(res)
322     }
323 }
324
325 impl TryConvWith for &NavigationTarget {
326     type Ctx = ServerWorld;
327     type Output = Location;
328     fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
329         let line_index = world.analysis().file_line_index(self.file_id());
330         to_location(self.file_id(), self.range(), &world, &line_index)
331     }
332 }
333
334 pub fn to_location(
335     file_id: FileId,
336     range: TextRange,
337     world: &ServerWorld,
338     line_index: &LineIndex,
339 ) -> Result<Location> {
340     let url = file_id.try_conv_with(world)?;
341     let loc = Location::new(url, range.conv_with(line_index));
342     Ok(loc)
343 }
344
345 pub trait MapConvWith<'a>: Sized + 'a {
346     type Ctx;
347     type Output;
348
349     fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
350         ConvWithIter { iter: self, ctx }
351     }
352 }
353
354 impl<'a, I> MapConvWith<'a> for I
355 where
356     I: Iterator + 'a,
357     I::Item: ConvWith,
358 {
359     type Ctx = <I::Item as ConvWith>::Ctx;
360     type Output = <I::Item as ConvWith>::Output;
361 }
362
363 pub struct ConvWithIter<'a, I, Ctx: 'a> {
364     iter: I,
365     ctx: &'a Ctx,
366 }
367
368 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
369 where
370     I: Iterator,
371     I::Item: ConvWith<Ctx = Ctx>,
372 {
373     type Item = <I::Item as ConvWith>::Output;
374
375     fn next(&mut self) -> Option<Self::Item> {
376         self.iter.next().map(|item| item.conv_with(self.ctx))
377     }
378 }