]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/conv.rs
Merge #366
[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};
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                 res.kind = Some(languageserver_types::CompletionItemKind::Keyword);
86             }
87         }
88         res
89     }
90 }
91
92 impl ConvWith for Position {
93     type Ctx = LineIndex;
94     type Output = TextUnit;
95
96     fn conv_with(self, line_index: &LineIndex) -> TextUnit {
97         let line_col = LineCol {
98             line: self.line as u32,
99             col_utf16: self.character as u32,
100         };
101         line_index.offset(line_col)
102     }
103 }
104
105 impl ConvWith for TextUnit {
106     type Ctx = LineIndex;
107     type Output = Position;
108
109     fn conv_with(self, line_index: &LineIndex) -> Position {
110         let line_col = line_index.line_col(self);
111         Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
112     }
113 }
114
115 impl ConvWith for TextRange {
116     type Ctx = LineIndex;
117     type Output = Range;
118
119     fn conv_with(self, line_index: &LineIndex) -> Range {
120         Range::new(
121             self.start().conv_with(line_index),
122             self.end().conv_with(line_index),
123         )
124     }
125 }
126
127 impl ConvWith for Range {
128     type Ctx = LineIndex;
129     type Output = TextRange;
130
131     fn conv_with(self, line_index: &LineIndex) -> TextRange {
132         TextRange::from_to(
133             self.start.conv_with(line_index),
134             self.end.conv_with(line_index),
135         )
136     }
137 }
138
139 impl ConvWith for TextEdit {
140     type Ctx = LineIndex;
141     type Output = Vec<languageserver_types::TextEdit>;
142
143     fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
144         self.as_atoms()
145             .into_iter()
146             .map_conv_with(line_index)
147             .collect()
148     }
149 }
150
151 impl<'a> ConvWith for &'a AtomTextEdit {
152     type Ctx = LineIndex;
153     type Output = languageserver_types::TextEdit;
154
155     fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
156         languageserver_types::TextEdit {
157             range: self.delete.conv_with(line_index),
158             new_text: self.insert.clone(),
159         }
160     }
161 }
162
163 impl<T: ConvWith> ConvWith for Option<T> {
164     type Ctx = <T as ConvWith>::Ctx;
165     type Output = Option<<T as ConvWith>::Output>;
166     fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
167         self.map(|x| ConvWith::conv_with(x, ctx))
168     }
169 }
170
171 impl<'a> TryConvWith for &'a Url {
172     type Ctx = ServerWorld;
173     type Output = FileId;
174     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
175         world.uri_to_file_id(self)
176     }
177 }
178
179 impl TryConvWith for FileId {
180     type Ctx = ServerWorld;
181     type Output = Url;
182     fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
183         world.file_id_to_uri(self)
184     }
185 }
186
187 impl<'a> TryConvWith for &'a TextDocumentItem {
188     type Ctx = ServerWorld;
189     type Output = FileId;
190     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
191         self.uri.try_conv_with(world)
192     }
193 }
194
195 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
196     type Ctx = ServerWorld;
197     type Output = FileId;
198     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
199         self.uri.try_conv_with(world)
200     }
201 }
202
203 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
204     type Ctx = ServerWorld;
205     type Output = FileId;
206     fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
207         world.uri_to_file_id(&self.uri)
208     }
209 }
210
211 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
212     type Ctx = ServerWorld;
213     type Output = FilePosition;
214     fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
215         let file_id = self.text_document.try_conv_with(world)?;
216         let line_index = world.analysis().file_line_index(file_id);
217         let offset = self.position.conv_with(&line_index);
218         Ok(FilePosition { file_id, offset })
219     }
220 }
221
222 impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
223     type Ctx = ServerWorld;
224     type Output = FileRange;
225     fn try_conv_with(self, world: &ServerWorld) -> Result<FileRange> {
226         let file_id = self.0.try_conv_with(world)?;
227         let line_index = world.analysis().file_line_index(file_id);
228         let range = self.1.conv_with(&line_index);
229         Ok(FileRange { file_id, range })
230     }
231 }
232
233 impl<T: TryConvWith> TryConvWith for Vec<T> {
234     type Ctx = <T as TryConvWith>::Ctx;
235     type Output = Vec<<T as TryConvWith>::Output>;
236     fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
237         let mut res = Vec::with_capacity(self.len());
238         for item in self {
239             res.push(item.try_conv_with(ctx)?);
240         }
241         Ok(res)
242     }
243 }
244
245 impl TryConvWith for SourceChange {
246     type Ctx = ServerWorld;
247     type Output = req::SourceChange;
248     fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
249         let cursor_position = match self.cursor_position {
250             None => None,
251             Some(pos) => {
252                 let line_index = world.analysis().file_line_index(pos.file_id);
253                 let edit = self
254                     .source_file_edits
255                     .iter()
256                     .find(|it| it.file_id == pos.file_id)
257                     .map(|it| &it.edit);
258                 let line_col = match edit {
259                     Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
260                     None => line_index.line_col(pos.offset),
261                 };
262                 let position =
263                     Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
264                 Some(TextDocumentPositionParams {
265                     text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
266                     position,
267                 })
268             }
269         };
270         let source_file_edits = self.source_file_edits.try_conv_with(world)?;
271         let file_system_edits = self.file_system_edits.try_conv_with(world)?;
272         Ok(req::SourceChange {
273             label: self.label,
274             source_file_edits,
275             file_system_edits,
276             cursor_position,
277         })
278     }
279 }
280
281 impl TryConvWith for SourceFileEdit {
282     type Ctx = ServerWorld;
283     type Output = TextDocumentEdit;
284     fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
285         let text_document = VersionedTextDocumentIdentifier {
286             uri: self.file_id.try_conv_with(world)?,
287             version: None,
288         };
289         let line_index = world.analysis().file_line_index(self.file_id);
290         let edits = self
291             .edit
292             .as_atoms()
293             .iter()
294             .map_conv_with(&line_index)
295             .collect();
296         Ok(TextDocumentEdit {
297             text_document,
298             edits,
299         })
300     }
301 }
302
303 impl TryConvWith for FileSystemEdit {
304     type Ctx = ServerWorld;
305     type Output = req::FileSystemEdit;
306     fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
307         let res = match self {
308             FileSystemEdit::CreateFile { source_root, path } => {
309                 let uri = world.path_to_uri(source_root, &path)?;
310                 req::FileSystemEdit::CreateFile { uri }
311             }
312             FileSystemEdit::MoveFile {
313                 src,
314                 dst_source_root,
315                 dst_path,
316             } => {
317                 let src = world.file_id_to_uri(src)?;
318                 let dst = world.path_to_uri(dst_source_root, &dst_path)?;
319                 req::FileSystemEdit::MoveFile { src, dst }
320             }
321         };
322         Ok(res)
323     }
324 }
325
326 pub fn to_location(
327     file_id: FileId,
328     range: TextRange,
329     world: &ServerWorld,
330     line_index: &LineIndex,
331 ) -> Result<Location> {
332     let url = file_id.try_conv_with(world)?;
333     let loc = Location::new(url, range.conv_with(line_index));
334     Ok(loc)
335 }
336
337 pub trait MapConvWith<'a>: Sized + 'a {
338     type Ctx;
339     type Output;
340
341     fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
342         ConvWithIter { iter: self, ctx }
343     }
344 }
345
346 impl<'a, I> MapConvWith<'a> for I
347 where
348     I: Iterator + 'a,
349     I::Item: ConvWith,
350 {
351     type Ctx = <I::Item as ConvWith>::Ctx;
352     type Output = <I::Item as ConvWith>::Output;
353 }
354
355 pub struct ConvWithIter<'a, I, Ctx: 'a> {
356     iter: I,
357     ctx: &'a Ctx,
358 }
359
360 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
361 where
362     I: Iterator,
363     I::Item: ConvWith<Ctx = Ctx>,
364 {
365     type Item = <I::Item as ConvWith>::Output;
366
367     fn next(&mut self) -> Option<Self::Item> {
368         self.iter.next().map(|item| item.conv_with(self.ctx))
369     }
370 }