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};
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);
85 res.kind = Some(languageserver_types::CompletionItemKind::Keyword);
92 impl ConvWith for Position {
94 type Output = TextUnit;
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,
101 line_index.offset(line_col)
105 impl ConvWith for TextUnit {
106 type Ctx = LineIndex;
107 type Output = Position;
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))
115 impl ConvWith for TextRange {
116 type Ctx = LineIndex;
119 fn conv_with(self, line_index: &LineIndex) -> Range {
121 self.start().conv_with(line_index),
122 self.end().conv_with(line_index),
127 impl ConvWith for Range {
128 type Ctx = LineIndex;
129 type Output = TextRange;
131 fn conv_with(self, line_index: &LineIndex) -> TextRange {
133 self.start.conv_with(line_index),
134 self.end.conv_with(line_index),
139 impl ConvWith for TextEdit {
140 type Ctx = LineIndex;
141 type Output = Vec<languageserver_types::TextEdit>;
143 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
146 .map_conv_with(line_index)
151 impl<'a> ConvWith for &'a AtomTextEdit {
152 type Ctx = LineIndex;
153 type Output = languageserver_types::TextEdit;
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(),
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))
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)
179 impl TryConvWith for FileId {
180 type Ctx = ServerWorld;
182 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
183 world.file_id_to_uri(self)
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)
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)
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)
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 })
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 })
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());
239 res.push(item.try_conv_with(ctx)?);
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 {
252 let line_index = world.analysis().file_line_index(pos.file_id);
256 .find(|it| it.file_id == pos.file_id)
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),
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)?),
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 {
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)?,
289 let line_index = world.analysis().file_line_index(self.file_id);
294 .map_conv_with(&line_index)
296 Ok(TextDocumentEdit {
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 }
312 FileSystemEdit::MoveFile {
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 }
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));
337 pub trait MapConvWith<'a>: Sized + 'a {
341 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
342 ConvWithIter { iter: self, ctx }
346 impl<'a, I> MapConvWith<'a> for I
351 type Ctx = <I::Item as ConvWith>::Ctx;
352 type Output = <I::Item as ConvWith>::Output;
355 pub struct ConvWithIter<'a, I, Ctx: 'a> {
360 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
363 I::Item: ConvWith<Ctx = Ctx>,
365 type Item = <I::Item as ConvWith>::Output;
367 fn next(&mut self) -> Option<Self::Item> {
368 self.iter.next().map(|item| item.conv_with(self.ctx))