1 use languageserver_types::{
2 self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink,
3 Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
4 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
8 CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
9 InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo,
10 LineCol, LineIndex, translate_offset_with_edit
12 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
13 use ra_text_edit::{AtomTextEdit, TextEdit};
15 use crate::{req, server_world::ServerWorld, Result};
19 fn conv(self) -> Self::Output;
25 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output;
28 pub trait TryConvWith {
31 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>;
34 impl Conv for SyntaxKind {
35 type Output = SymbolKind;
37 fn conv(self) -> <Self as Conv>::Output {
39 SyntaxKind::FN_DEF => SymbolKind::Function,
40 SyntaxKind::STRUCT_DEF => SymbolKind::Struct,
41 SyntaxKind::ENUM_DEF => SymbolKind::Enum,
42 SyntaxKind::TRAIT_DEF => SymbolKind::Interface,
43 SyntaxKind::MODULE => SymbolKind::Module,
44 SyntaxKind::TYPE_DEF => SymbolKind::TypeParameter,
45 SyntaxKind::STATIC_DEF => SymbolKind::Constant,
46 SyntaxKind::CONST_DEF => SymbolKind::Constant,
47 SyntaxKind::IMPL_BLOCK => SymbolKind::Object,
48 _ => SymbolKind::Variable,
53 impl Conv for CompletionItemKind {
54 type Output = ::languageserver_types::CompletionItemKind;
56 fn conv(self) -> <Self as Conv>::Output {
57 use languageserver_types::CompletionItemKind::*;
59 CompletionItemKind::Keyword => Keyword,
60 CompletionItemKind::Snippet => Snippet,
61 CompletionItemKind::Module => Module,
62 CompletionItemKind::Function => Function,
63 CompletionItemKind::Struct => Struct,
64 CompletionItemKind::Enum => Enum,
65 CompletionItemKind::EnumVariant => EnumMember,
66 CompletionItemKind::Binding => Variable,
67 CompletionItemKind::Field => Field,
72 impl Conv for CompletionItem {
73 type Output = ::languageserver_types::CompletionItem;
75 fn conv(self) -> <Self as Conv>::Output {
76 let mut res = ::languageserver_types::CompletionItem {
77 label: self.label().to_string(),
78 detail: self.detail().map(|it| it.to_string()),
79 filter_text: Some(self.lookup().to_string()),
80 kind: self.kind().map(|it| it.conv()),
83 match self.insert_text() {
84 InsertText::PlainText { text } => {
85 res.insert_text = Some(text);
86 res.insert_text_format = Some(InsertTextFormat::PlainText);
88 InsertText::Snippet { text } => {
89 res.insert_text = Some(text);
90 res.insert_text_format = Some(InsertTextFormat::Snippet);
97 impl ConvWith for Position {
99 type Output = TextUnit;
101 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
102 let line_col = LineCol {
103 line: self.line as u32,
104 col_utf16: self.character as u32,
106 line_index.offset(line_col)
110 impl ConvWith for TextUnit {
111 type Ctx = LineIndex;
112 type Output = Position;
114 fn conv_with(self, line_index: &LineIndex) -> Position {
115 let line_col = line_index.line_col(self);
116 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
120 impl ConvWith for TextRange {
121 type Ctx = LineIndex;
124 fn conv_with(self, line_index: &LineIndex) -> Range {
126 self.start().conv_with(line_index),
127 self.end().conv_with(line_index),
132 impl ConvWith for Range {
133 type Ctx = LineIndex;
134 type Output = TextRange;
136 fn conv_with(self, line_index: &LineIndex) -> TextRange {
138 self.start.conv_with(line_index),
139 self.end.conv_with(line_index),
144 impl ConvWith for TextEdit {
145 type Ctx = LineIndex;
146 type Output = Vec<languageserver_types::TextEdit>;
148 fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
151 .map_conv_with(line_index)
156 impl<'a> ConvWith for &'a AtomTextEdit {
157 type Ctx = LineIndex;
158 type Output = languageserver_types::TextEdit;
160 fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
161 languageserver_types::TextEdit {
162 range: self.delete.conv_with(line_index),
163 new_text: self.insert.clone(),
168 impl<T: ConvWith> ConvWith for Option<T> {
169 type Ctx = <T as ConvWith>::Ctx;
170 type Output = Option<<T as ConvWith>::Output>;
171 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
172 self.map(|x| ConvWith::conv_with(x, ctx))
176 impl<'a> TryConvWith for &'a Url {
177 type Ctx = ServerWorld;
178 type Output = FileId;
179 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
180 world.uri_to_file_id(self)
184 impl TryConvWith for FileId {
185 type Ctx = ServerWorld;
187 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
188 world.file_id_to_uri(self)
192 impl<'a> TryConvWith for &'a TextDocumentItem {
193 type Ctx = ServerWorld;
194 type Output = FileId;
195 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
196 self.uri.try_conv_with(world)
200 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
201 type Ctx = ServerWorld;
202 type Output = FileId;
203 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
204 self.uri.try_conv_with(world)
208 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
209 type Ctx = ServerWorld;
210 type Output = FileId;
211 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
212 world.uri_to_file_id(&self.uri)
216 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
217 type Ctx = ServerWorld;
218 type Output = FilePosition;
219 fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
220 let file_id = self.text_document.try_conv_with(world)?;
221 let line_index = world.analysis().file_line_index(file_id);
222 let offset = self.position.conv_with(&line_index);
223 Ok(FilePosition { file_id, offset })
227 impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
228 type Ctx = ServerWorld;
229 type Output = FileRange;
230 fn try_conv_with(self, world: &ServerWorld) -> Result<FileRange> {
231 let file_id = self.0.try_conv_with(world)?;
232 let line_index = world.analysis().file_line_index(file_id);
233 let range = self.1.conv_with(&line_index);
234 Ok(FileRange { file_id, range })
238 impl<T: TryConvWith> TryConvWith for Vec<T> {
239 type Ctx = <T as TryConvWith>::Ctx;
240 type Output = Vec<<T as TryConvWith>::Output>;
241 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
242 let mut res = Vec::with_capacity(self.len());
244 res.push(item.try_conv_with(ctx)?);
250 impl TryConvWith for SourceChange {
251 type Ctx = ServerWorld;
252 type Output = req::SourceChange;
253 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
254 let cursor_position = match self.cursor_position {
257 let line_index = world.analysis().file_line_index(pos.file_id);
261 .find(|it| it.file_id == pos.file_id)
263 let line_col = match edit {
264 Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
265 None => line_index.line_col(pos.offset),
268 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
269 Some(TextDocumentPositionParams {
270 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
275 let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
276 for resource_op in self.file_system_edits.try_conv_with(world)? {
277 document_changes.push(DocumentChangeOperation::Op(resource_op));
279 for text_document_edit in self.source_file_edits.try_conv_with(world)? {
280 document_changes.push(DocumentChangeOperation::Edit(text_document_edit));
282 let workspace_edit = WorkspaceEdit {
284 document_changes: Some(DocumentChanges::Operations(document_changes)),
286 Ok(req::SourceChange {
294 impl TryConvWith for SourceFileEdit {
295 type Ctx = ServerWorld;
296 type Output = TextDocumentEdit;
297 fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {
298 let text_document = VersionedTextDocumentIdentifier {
299 uri: self.file_id.try_conv_with(world)?,
302 let line_index = world.analysis().file_line_index(self.file_id);
307 .map_conv_with(&line_index)
309 Ok(TextDocumentEdit {
316 impl TryConvWith for FileSystemEdit {
317 type Ctx = ServerWorld;
318 type Output = ResourceOp;
319 fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
320 let res = match self {
321 FileSystemEdit::CreateFile { source_root, path } => {
322 let uri = world.path_to_uri(source_root, &path)?.to_string();
323 ResourceOp::Create(CreateFile { uri, options: None })
325 FileSystemEdit::MoveFile {
330 let old_uri = world.file_id_to_uri(src)?.to_string();
331 let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
332 ResourceOp::Rename(RenameFile {
343 impl TryConvWith for &NavigationTarget {
344 type Ctx = ServerWorld;
345 type Output = Location;
346 fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
347 let line_index = world.analysis().file_line_index(self.file_id());
348 let range = self.focus_range().unwrap_or(self.full_range());
349 to_location(self.file_id(), range, &world, &line_index)
353 pub fn to_location_link(
354 target: &RangeInfo<NavigationTarget>,
356 // line index for original range file
357 line_index: &LineIndex,
358 ) -> Result<LocationLink> {
359 let url = target.info.file_id().try_conv_with(world)?;
360 let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
362 let res = LocationLink {
363 origin_selection_range: Some(target.range.conv_with(line_index)),
364 target_uri: url.to_string(),
365 target_range: target.info.full_range().conv_with(&tgt_line_index),
366 target_selection_range: target
369 .map(|it| it.conv_with(&tgt_line_index)),
378 line_index: &LineIndex,
379 ) -> Result<Location> {
380 let url = file_id.try_conv_with(world)?;
381 let loc = Location::new(url, range.conv_with(line_index));
385 pub trait MapConvWith<'a>: Sized + 'a {
389 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
390 ConvWithIter { iter: self, ctx }
394 impl<'a, I> MapConvWith<'a> for I
399 type Ctx = <I::Item as ConvWith>::Ctx;
400 type Output = <I::Item as ConvWith>::Output;
403 pub struct ConvWithIter<'a, I, Ctx: 'a> {
408 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
411 I::Item: ConvWith<Ctx = Ctx>,
413 type Item = <I::Item as ConvWith>::Output;
415 fn next(&mut self) -> Option<Self::Item> {
416 self.iter.next().map(|item| item.conv_with(self.ctx))