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, CompletionItem, CompletionItemKind, InsertText};
6 use ra_editor::{LineCol, LineIndex};
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::Binding => Variable,
63 impl Conv for CompletionItem {
64 type Output = ::languageserver_types::CompletionItem;
66 fn conv(self) -> <Self as Conv>::Output {
67 let mut res = ::languageserver_types::CompletionItem {
68 label: self.label().to_string(),
69 filter_text: Some(self.lookup().to_string()),
70 kind: self.kind().map(|it| it.conv()),
73 match self.insert_text() {
74 InsertText::PlainText { text } => {
75 res.insert_text = Some(text);
76 res.insert_text_format = Some(InsertTextFormat::PlainText);
78 InsertText::Snippet { text } => {
79 res.insert_text = Some(text);
80 res.insert_text_format = Some(InsertTextFormat::Snippet);
81 res.kind = Some(languageserver_types::CompletionItemKind::Keyword);
88 impl ConvWith for Position {
90 type Output = TextUnit;
92 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
93 let line_col = LineCol {
94 line: self.line as u32,
95 col_utf16: self.character as u32,
97 line_index.offset(line_col)
101 impl ConvWith for TextUnit {
102 type Ctx = LineIndex;
103 type Output = Position;
105 fn conv_with(self, line_index: &LineIndex) -> Position {
106 let line_col = line_index.line_col(self);
107 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
111 impl ConvWith for TextRange {
112 type Ctx = LineIndex;
115 fn conv_with(self, line_index: &LineIndex) -> Range {
117 self.start().conv_with(line_index),
118 self.end().conv_with(line_index),
123 impl ConvWith for Range {
124 type Ctx = LineIndex;
125 type Output = TextRange;
127 fn conv_with(self, line_index: &LineIndex) -> TextRange {
129 self.start.conv_with(line_index),
130 self.end.conv_with(line_index),
135 impl ConvWith for TextEdit {
136 type Ctx = LineIndex;
137 type Output = Vec<languageserver_types::TextEdit>;
139 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
142 .map_conv_with(line_index)
147 impl<'a> ConvWith for &'a AtomTextEdit {
148 type Ctx = LineIndex;
149 type Output = languageserver_types::TextEdit;
151 fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
152 languageserver_types::TextEdit {
153 range: self.delete.conv_with(line_index),
154 new_text: self.insert.clone(),
159 impl<T: ConvWith> ConvWith for Option<T> {
160 type Ctx = <T as ConvWith>::Ctx;
161 type Output = Option<<T as ConvWith>::Output>;
162 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
163 self.map(|x| ConvWith::conv_with(x, ctx))
167 impl<'a> TryConvWith for &'a Url {
168 type Ctx = ServerWorld;
169 type Output = FileId;
170 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
171 world.uri_to_file_id(self)
175 impl TryConvWith for FileId {
176 type Ctx = ServerWorld;
178 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
179 world.file_id_to_uri(self)
183 impl<'a> TryConvWith for &'a TextDocumentItem {
184 type Ctx = ServerWorld;
185 type Output = FileId;
186 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
187 self.uri.try_conv_with(world)
191 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
192 type Ctx = ServerWorld;
193 type Output = FileId;
194 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
195 self.uri.try_conv_with(world)
199 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
200 type Ctx = ServerWorld;
201 type Output = FileId;
202 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
203 world.uri_to_file_id(&self.uri)
207 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
208 type Ctx = ServerWorld;
209 type Output = FilePosition;
210 fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
211 let file_id = self.text_document.try_conv_with(world)?;
212 let line_index = world.analysis().file_line_index(file_id);
213 let offset = self.position.conv_with(&line_index);
214 Ok(FilePosition { file_id, offset })
218 impl<T: TryConvWith> TryConvWith for Vec<T> {
219 type Ctx = <T as TryConvWith>::Ctx;
220 type Output = Vec<<T as TryConvWith>::Output>;
221 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
222 let mut res = Vec::with_capacity(self.len());
224 res.push(item.try_conv_with(ctx)?);
230 impl TryConvWith for SourceChange {
231 type Ctx = ServerWorld;
232 type Output = req::SourceChange;
233 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
234 let cursor_position = match self.cursor_position {
237 let line_index = world.analysis().file_line_index(pos.file_id);
241 .find(|it| it.file_id == pos.file_id)
242 .map(|it| it.edit.as_atoms())
244 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
246 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
247 Some(TextDocumentPositionParams {
248 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
253 let source_file_edits = self.source_file_edits.try_conv_with(world)?;
254 let file_system_edits = self.file_system_edits.try_conv_with(world)?;
255 Ok(req::SourceChange {
264 // HACK: we should translate offset to line/column using linde_index *with edits applied*.
265 // A naive version of this function would be to apply `edits` to the original text,
266 // construct a new line index and use that, but it would be slow.
268 // Writing fast & correct version is issue #105, let's use a quick hack in the meantime
269 fn translate_offset_with_edit(
270 pre_edit_index: &LineIndex,
272 edits: &[AtomTextEdit],
274 let fallback = pre_edit_index.line_col(offset);
275 let edit = match edits.first() {
276 None => return fallback,
279 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
280 if !(edit.delete.start() <= offset && offset <= end_offset) {
283 let rel_offset = offset - edit.delete.start();
284 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
285 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
286 if in_edit_line_col.line == 0 {
288 line: edit_line_col.line,
289 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
293 line: edit_line_col.line + in_edit_line_col.line,
294 col_utf16: in_edit_line_col.col_utf16,
299 impl TryConvWith for SourceFileEdit {
300 type Ctx = ServerWorld;
301 type Output = TextDocumentEdit;
302 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
303 let text_document = VersionedTextDocumentIdentifier {
304 uri: self.file_id.try_conv_with(world)?,
307 let line_index = world.analysis().file_line_index(self.file_id);
312 .map_conv_with(&line_index)
314 Ok(TextDocumentEdit {
321 impl TryConvWith for FileSystemEdit {
322 type Ctx = ServerWorld;
323 type Output = req::FileSystemEdit;
324 fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
325 let res = match self {
326 FileSystemEdit::CreateFile { source_root, path } => {
327 let uri = world.path_to_uri(source_root, &path)?;
328 req::FileSystemEdit::CreateFile { uri }
330 FileSystemEdit::MoveFile {
335 let src = world.file_id_to_uri(src)?;
336 let dst = world.path_to_uri(dst_source_root, &dst_path)?;
337 req::FileSystemEdit::MoveFile { src, dst }
348 line_index: &LineIndex,
349 ) -> Result<Location> {
350 let url = file_id.try_conv_with(world)?;
351 let loc = Location::new(url, range.conv_with(line_index));
355 pub trait MapConvWith<'a>: Sized + 'a {
359 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
360 ConvWithIter { iter: self, ctx }
364 impl<'a, I> MapConvWith<'a> for I
369 type Ctx = <I::Item as ConvWith>::Ctx;
370 type Output = <I::Item as ConvWith>::Output;
373 pub struct ConvWithIter<'a, I, Ctx: 'a> {
378 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
381 I::Item: ConvWith<Ctx = Ctx>,
383 type Item = <I::Item as ConvWith>::Output;
385 fn next(&mut self) -> Option<Self::Item> {
386 self.iter.next().map(|item| item.conv_with(self.ctx))