]> git.lizzy.rs Git - rust.git/blobdiff - crates/ra_lsp_server/src/conv.rs
Pass Documentation up to LSP and add "rust" to our codeblocks there
[rust.git] / crates / ra_lsp_server / src / conv.rs
index 76fa98cbeb9b89d4a05315e624035bde75b45c56..c033ecdeaa10a1c570b9b4e4bec72b7dee60dca4 100644 (file)
@@ -1,13 +1,13 @@
-use languageserver_types::{
-    self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink,
-    Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
+use lsp_types::{
+    self, CreateFile, Documentation, DocumentChangeOperation, DocumentChanges, Location, LocationLink,
+    MarkupContent, MarkupKind, Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
     TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
     WorkspaceEdit,
 };
 use ra_ide_api::{
     CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
-    InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo,
-    LineCol, LineIndex, translate_offset_with_edit
+    NavigationTarget, SourceChange, SourceFileEdit, RangeInfo,
+    LineCol, LineIndex, translate_offset_with_edit, InsertTextFormat
 };
 use ra_syntax::{SyntaxKind, TextRange, TextUnit};
 use ra_text_edit::{AtomTextEdit, TextEdit};
@@ -51,10 +51,10 @@ fn conv(self) -> <Self as Conv>::Output {
 }
 
 impl Conv for CompletionItemKind {
-    type Output = ::languageserver_types::CompletionItemKind;
+    type Output = ::lsp_types::CompletionItemKind;
 
     fn conv(self) -> <Self as Conv>::Output {
-        use languageserver_types::CompletionItemKind::*;
+        use lsp_types::CompletionItemKind::*;
         match self {
             CompletionItemKind::Keyword => Keyword,
             CompletionItemKind::Snippet => Snippet,
@@ -65,31 +65,43 @@ fn conv(self) -> <Self as Conv>::Output {
             CompletionItemKind::EnumVariant => EnumMember,
             CompletionItemKind::Binding => Variable,
             CompletionItemKind::Field => Field,
+            CompletionItemKind::Trait => Interface,
+            CompletionItemKind::TypeAlias => Struct,
+            CompletionItemKind::Const => Constant,
+            CompletionItemKind::Static => Value,
+            CompletionItemKind::Method => Method,
         }
     }
 }
 
-impl Conv for CompletionItem {
-    type Output = ::languageserver_types::CompletionItem;
+impl ConvWith for CompletionItem {
+    type Ctx = LineIndex;
+    type Output = ::lsp_types::CompletionItem;
+
+    fn conv_with(mut self, ctx: &LineIndex) -> ::lsp_types::CompletionItem {
+        let atom_text_edit = AtomTextEdit::replace(self.source_range(), self.insert_text());
+        let text_edit = (&atom_text_edit).conv_with(ctx);
+        let additional_text_edits = if let Some(edit) = self.take_text_edit() {
+            Some(edit.conv_with(ctx))
+        } else {
+            None
+        };
 
-    fn conv(self) -> <Self as Conv>::Output {
-        let mut res = ::languageserver_types::CompletionItem {
+        let mut res = lsp_types::CompletionItem {
             label: self.label().to_string(),
             detail: self.detail().map(|it| it.to_string()),
             filter_text: Some(self.lookup().to_string()),
             kind: self.kind().map(|it| it.conv()),
+            text_edit: Some(text_edit),
+            additional_text_edits,
+            documentation: self.documentation().map(|it| it.conv()),
             ..Default::default()
         };
-        match self.insert_text() {
-            InsertText::PlainText { text } => {
-                res.insert_text = Some(text);
-                res.insert_text_format = Some(InsertTextFormat::PlainText);
-            }
-            InsertText::Snippet { text } => {
-                res.insert_text = Some(text);
-                res.insert_text_format = Some(InsertTextFormat::Snippet);
-            }
-        }
+        res.insert_text_format = Some(match self.insert_text_format() {
+            InsertTextFormat::Snippet => lsp_types::InsertTextFormat::Snippet,
+            InsertTextFormat::PlainText => lsp_types::InsertTextFormat::PlainText,
+        });
+
         res
     }
 }
@@ -141,11 +153,21 @@ fn conv_with(self, line_index: &LineIndex) -> TextRange {
     }
 }
 
+impl Conv for ra_ide_api::Documentation {
+    type Output = lsp_types::Documentation;
+    fn conv(self) -> Documentation {
+        Documentation::MarkupContent(MarkupContent {
+            kind: MarkupKind::Markdown,
+            value: crate::markdown::sanitize_markdown(self).into(),
+        })
+    }
+}
+
 impl ConvWith for TextEdit {
     type Ctx = LineIndex;
-    type Output = Vec<languageserver_types::TextEdit>;
+    type Output = Vec<lsp_types::TextEdit>;
 
-    fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
+    fn conv_with(self, line_index: &LineIndex) -> Vec<lsp_types::TextEdit> {
         self.as_atoms()
             .into_iter()
             .map_conv_with(line_index)
@@ -155,10 +177,10 @@ fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit
 
 impl<'a> ConvWith for &'a AtomTextEdit {
     type Ctx = LineIndex;
-    type Output = languageserver_types::TextEdit;
+    type Output = lsp_types::TextEdit;
 
-    fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
-        languageserver_types::TextEdit {
+    fn conv_with(self, line_index: &LineIndex) -> lsp_types::TextEdit {
+        lsp_types::TextEdit {
             range: self.delete.conv_with(line_index),
             new_text: self.insert.clone(),
         }
@@ -319,7 +341,7 @@ impl TryConvWith for FileSystemEdit {
     fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
         let res = match self {
             FileSystemEdit::CreateFile { source_root, path } => {
-                let uri = world.path_to_uri(source_root, &path)?.to_string();
+                let uri = world.path_to_uri(source_root, &path)?;
                 ResourceOp::Create(CreateFile { uri, options: None })
             }
             FileSystemEdit::MoveFile {
@@ -327,8 +349,8 @@ fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
                 dst_source_root,
                 dst_path,
             } => {
-                let old_uri = world.file_id_to_uri(src)?.to_string();
-                let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
+                let old_uri = world.file_id_to_uri(src)?;
+                let new_uri = world.path_to_uri(dst_source_root, &dst_path)?;
                 ResourceOp::Rename(RenameFile {
                     old_uri,
                     new_uri,
@@ -356,17 +378,22 @@ pub fn to_location_link(
     // line index for original range file
     line_index: &LineIndex,
 ) -> Result<LocationLink> {
-    let url = target.info.file_id().try_conv_with(world)?;
+    let target_uri = target.info.file_id().try_conv_with(world)?;
     let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
 
+    let target_range = target.info.full_range().conv_with(&tgt_line_index);
+
+    let target_selection_range = target
+        .info
+        .focus_range()
+        .map(|it| it.conv_with(&tgt_line_index))
+        .unwrap_or(target_range);
+
     let res = LocationLink {
         origin_selection_range: Some(target.range.conv_with(line_index)),
-        target_uri: url.to_string(),
-        target_range: target.info.full_range().conv_with(&tgt_line_index),
-        target_selection_range: target
-            .info
-            .focus_range()
-            .map(|it| it.conv_with(&tgt_line_index)),
+        target_uri,
+        target_range,
+        target_selection_range: target_selection_range,
     };
     Ok(res)
 }