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 = ::lsp_types::CompletionItemKind;
56 fn conv(self) -> <Self as Conv>::Output {
57 use lsp_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,
68 CompletionItemKind::Trait => Interface,
69 CompletionItemKind::TypeAlias => Struct,
70 CompletionItemKind::Const => Constant,
71 CompletionItemKind::Static => Value,
72 CompletionItemKind::Method => Method,
77 impl Conv for CompletionItem {
78 type Output = ::lsp_types::CompletionItem;
80 fn conv(self) -> <Self as Conv>::Output {
81 let mut res = ::lsp_types::CompletionItem {
82 label: self.label().to_string(),
83 detail: self.detail().map(|it| it.to_string()),
84 filter_text: Some(self.lookup().to_string()),
85 kind: self.kind().map(|it| it.conv()),
88 match self.insert_text() {
89 InsertText::PlainText { text } => {
90 res.insert_text = Some(text);
91 res.insert_text_format = Some(InsertTextFormat::PlainText);
93 InsertText::Snippet { text } => {
94 res.insert_text = Some(text);
95 res.insert_text_format = Some(InsertTextFormat::Snippet);
102 impl ConvWith for Position {
103 type Ctx = LineIndex;
104 type Output = TextUnit;
106 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
107 let line_col = LineCol {
108 line: self.line as u32,
109 col_utf16: self.character as u32,
111 line_index.offset(line_col)
115 impl ConvWith for TextUnit {
116 type Ctx = LineIndex;
117 type Output = Position;
119 fn conv_with(self, line_index: &LineIndex) -> Position {
120 let line_col = line_index.line_col(self);
121 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16))
125 impl ConvWith for TextRange {
126 type Ctx = LineIndex;
129 fn conv_with(self, line_index: &LineIndex) -> Range {
131 self.start().conv_with(line_index),
132 self.end().conv_with(line_index),
137 impl ConvWith for Range {
138 type Ctx = LineIndex;
139 type Output = TextRange;
141 fn conv_with(self, line_index: &LineIndex) -> TextRange {
143 self.start.conv_with(line_index),
144 self.end.conv_with(line_index),
149 impl ConvWith for TextEdit {
150 type Ctx = LineIndex;
151 type Output = Vec<lsp_types::TextEdit>;
153 fn conv_with(self, line_index: &LineIndex) -> Vec<lsp_types::TextEdit> {
156 .map_conv_with(line_index)
161 impl<'a> ConvWith for &'a AtomTextEdit {
162 type Ctx = LineIndex;
163 type Output = lsp_types::TextEdit;
165 fn conv_with(self, line_index: &LineIndex) -> lsp_types::TextEdit {
166 lsp_types::TextEdit {
167 range: self.delete.conv_with(line_index),
168 new_text: self.insert.clone(),
173 impl<T: ConvWith> ConvWith for Option<T> {
174 type Ctx = <T as ConvWith>::Ctx;
175 type Output = Option<<T as ConvWith>::Output>;
176 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
177 self.map(|x| ConvWith::conv_with(x, ctx))
181 impl<'a> TryConvWith for &'a Url {
182 type Ctx = ServerWorld;
183 type Output = FileId;
184 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
185 world.uri_to_file_id(self)
189 impl TryConvWith for FileId {
190 type Ctx = ServerWorld;
192 fn try_conv_with(self, world: &ServerWorld) -> Result<Url> {
193 world.file_id_to_uri(self)
197 impl<'a> TryConvWith for &'a TextDocumentItem {
198 type Ctx = ServerWorld;
199 type Output = FileId;
200 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
201 self.uri.try_conv_with(world)
205 impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier {
206 type Ctx = ServerWorld;
207 type Output = FileId;
208 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
209 self.uri.try_conv_with(world)
213 impl<'a> TryConvWith for &'a TextDocumentIdentifier {
214 type Ctx = ServerWorld;
215 type Output = FileId;
216 fn try_conv_with(self, world: &ServerWorld) -> Result<FileId> {
217 world.uri_to_file_id(&self.uri)
221 impl<'a> TryConvWith for &'a TextDocumentPositionParams {
222 type Ctx = ServerWorld;
223 type Output = FilePosition;
224 fn try_conv_with(self, world: &ServerWorld) -> Result<FilePosition> {
225 let file_id = self.text_document.try_conv_with(world)?;
226 let line_index = world.analysis().file_line_index(file_id);
227 let offset = self.position.conv_with(&line_index);
228 Ok(FilePosition { file_id, offset })
232 impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
233 type Ctx = ServerWorld;
234 type Output = FileRange;
235 fn try_conv_with(self, world: &ServerWorld) -> Result<FileRange> {
236 let file_id = self.0.try_conv_with(world)?;
237 let line_index = world.analysis().file_line_index(file_id);
238 let range = self.1.conv_with(&line_index);
239 Ok(FileRange { file_id, range })
243 impl<T: TryConvWith> TryConvWith for Vec<T> {
244 type Ctx = <T as TryConvWith>::Ctx;
245 type Output = Vec<<T as TryConvWith>::Output>;
246 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
247 let mut res = Vec::with_capacity(self.len());
249 res.push(item.try_conv_with(ctx)?);
255 impl TryConvWith for SourceChange {
256 type Ctx = ServerWorld;
257 type Output = req::SourceChange;
258 fn try_conv_with(self, world: &ServerWorld) -> Result<req::SourceChange> {
259 let cursor_position = match self.cursor_position {
262 let line_index = world.analysis().file_line_index(pos.file_id);
266 .find(|it| it.file_id == pos.file_id)
268 let line_col = match edit {
269 Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
270 None => line_index.line_col(pos.offset),
273 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
274 Some(TextDocumentPositionParams {
275 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
280 let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
281 for resource_op in self.file_system_edits.try_conv_with(world)? {
282 document_changes.push(DocumentChangeOperation::Op(resource_op));
284 for text_document_edit in self.source_file_edits.try_conv_with(world)? {
285 document_changes.push(DocumentChangeOperation::Edit(text_document_edit));
287 let workspace_edit = WorkspaceEdit {
289 document_changes: Some(DocumentChanges::Operations(document_changes)),
291 Ok(req::SourceChange {
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 = ResourceOp;
324 fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
325 let res = match self {
326 FileSystemEdit::CreateFile { source_root, path } => {
327 let uri = world.path_to_uri(source_root, &path)?;
328 ResourceOp::Create(CreateFile { uri, options: None })
330 FileSystemEdit::MoveFile {
335 let old_uri = world.file_id_to_uri(src)?;
336 let new_uri = world.path_to_uri(dst_source_root, &dst_path)?;
337 ResourceOp::Rename(RenameFile {
348 impl TryConvWith for &NavigationTarget {
349 type Ctx = ServerWorld;
350 type Output = Location;
351 fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
352 let line_index = world.analysis().file_line_index(self.file_id());
353 let range = self.focus_range().unwrap_or(self.full_range());
354 to_location(self.file_id(), range, &world, &line_index)
358 pub fn to_location_link(
359 target: &RangeInfo<NavigationTarget>,
361 // line index for original range file
362 line_index: &LineIndex,
363 ) -> Result<LocationLink> {
364 let target_uri = target.info.file_id().try_conv_with(world)?;
365 let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
367 let target_range = target.info.full_range().conv_with(&tgt_line_index);
369 let target_selection_range = target
372 .map(|it| it.conv_with(&tgt_line_index))
373 .unwrap_or(target_range);
375 let res = LocationLink {
376 origin_selection_range: Some(target.range.conv_with(line_index)),
379 target_selection_range: Some(target_selection_range),
388 line_index: &LineIndex,
389 ) -> Result<Location> {
390 let url = file_id.try_conv_with(world)?;
391 let loc = Location::new(url, range.conv_with(line_index));
395 pub trait MapConvWith<'a>: Sized + 'a {
399 fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> {
400 ConvWithIter { iter: self, ctx }
404 impl<'a, I> MapConvWith<'a> for I
409 type Ctx = <I::Item as ConvWith>::Ctx;
410 type Output = <I::Item as ConvWith>::Output;
413 pub struct ConvWithIter<'a, I, Ctx: 'a> {
418 impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx>
421 I::Item: ConvWith<Ctx = Ctx>,
423 type Item = <I::Item as ConvWith>::Output;
425 fn next(&mut self) -> Option<Self::Item> {
426 self.iter.next().map(|item| item.conv_with(self.ctx))