1 use languageserver_types::{
2 self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
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};
10 use crate::{req, server_world::ServerWorld, Result};
14 fn conv(self) -> Self::Output;
20 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
23 pub trait TryConvWith {
26 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
29 impl Conv for SyntaxKind {
30 type Output = SymbolKind;
32 fn conv(self) -> <Self as Conv>::Output {
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,
48 impl Conv for CompletionItemKind {
49 type Output = ::languageserver_types::CompletionItemKind;
51 fn conv(self) -> <Self as Conv>::Output {
52 use ::languageserver_types::CompletionItemKind::*;
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,
67 impl Conv for CompletionItem {
68 type Output = ::languageserver_types::CompletionItem;
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()),
77 match self.insert_text() {
78 InsertText::PlainText { text } => {
79 res.insert_text = Some(text);
80 res.insert_text_format = Some(InsertTextFormat::PlainText);
82 InsertText::Snippet { text } => {
83 res.insert_text = Some(text);
84 res.insert_text_format = Some(InsertTextFormat::Snippet);
91 impl ConvWith for Position {
93 type Output = TextUnit;
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,
100 line_index.offset(line_col)
104 impl ConvWith for TextUnit {
105 type Ctx = LineIndex;
106 type Output = Position;
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))
114 impl ConvWith for TextRange {
115 type Ctx = LineIndex;
118 fn conv_with(self, line_index: &LineIndex) -> Range {
120 self.start().conv_with(line_index),
121 self.end().conv_with(line_index),
126 impl ConvWith for Range {
127 type Ctx = LineIndex;
128 type Output = TextRange;
130 fn conv_with(self, line_index: &LineIndex) -> TextRange {
132 self.start.conv_with(line_index),
133 self.end.conv_with(line_index),
138 impl ConvWith for TextEdit {
139 type Ctx = LineIndex;
140 type Output = Vec<languageserver_types::TextEdit>;
142 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
145 .map_conv_with(line_index)
150 impl<'a> ConvWith for &'a AtomTextEdit {
151 type Ctx = LineIndex;
152 type Output = languageserver_types::TextEdit;
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(),
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))
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)
178 impl TryConvWith for FileId {
179 type Ctx = ServerWorld;
181 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
182 world.file_id_to_uri(self)
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)
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)
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)
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 })
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 })
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());
238 res.push(item.try_conv_with(ctx)?);
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 {
251 let line_index = world.analysis().file_line_index(pos.file_id);
255 .find(|it| it.file_id == pos.file_id)
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),
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)?),
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 {
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)?,
288 let line_index = world.analysis().file_line_index(self.file_id);
293 .map_conv_with(&line_index)
295 Ok(TextDocumentEdit {
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 }
311 FileSystemEdit::MoveFile {
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 }
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)
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));
345 pub trait MapConvWith<'a>: Sized + 'a {
349 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
350 ConvWithIter { iter: self, ctx }
354 impl<'a, I> MapConvWith<'a> for I
359 type Ctx = <I::Item as ConvWith>::Ctx;
360 type Output = <I::Item as ConvWith>::Output;
363 pub struct ConvWithIter<'a, I, Ctx: 'a> {
368 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
371 I::Item: ConvWith<Ctx = Ctx>,
373 type Item = <I::Item as ConvWith>::Output;
375 fn next(&mut self) -> Option<Self::Item> {
376 self.iter.next().map(|item| item.conv_with(self.ctx))