mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Pass Documentation up to LSP and add "rust" to our codeblocks there
This commit is contained in:
parent
48d2acb297
commit
b88ba007cc
8 changed files with 103 additions and 89 deletions
|
@ -87,13 +87,6 @@ impl ConvWith for CompletionItem {
|
|||
None
|
||||
};
|
||||
|
||||
let documentation = self.documentation().map(|value| {
|
||||
Documentation::MarkupContent(MarkupContent {
|
||||
kind: MarkupKind::Markdown,
|
||||
value: value.to_string(),
|
||||
})
|
||||
});
|
||||
|
||||
let mut res = lsp_types::CompletionItem {
|
||||
label: self.label().to_string(),
|
||||
detail: self.detail().map(|it| it.to_string()),
|
||||
|
@ -101,7 +94,7 @@ impl ConvWith for CompletionItem {
|
|||
kind: self.kind().map(|it| it.conv()),
|
||||
text_edit: Some(text_edit),
|
||||
additional_text_edits,
|
||||
documentation: documentation,
|
||||
documentation: self.documentation().map(|it| it.conv()),
|
||||
..Default::default()
|
||||
};
|
||||
res.insert_text_format = Some(match self.insert_text_format() {
|
||||
|
@ -160,6 +153,16 @@ impl ConvWith for Range {
|
|||
}
|
||||
}
|
||||
|
||||
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<lsp_types::TextEdit>;
|
||||
|
|
|
@ -2,6 +2,7 @@ mod caps;
|
|||
mod cargo_target_spec;
|
||||
mod conv;
|
||||
mod main_loop;
|
||||
mod markdown;
|
||||
mod project_model;
|
||||
pub mod req;
|
||||
mod server_world;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use gen_lsp_server::ErrorCode;
|
||||
use lsp_types::{
|
||||
CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity,
|
||||
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, Documentation, FoldingRange,
|
||||
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange,
|
||||
FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent,
|
||||
MarkupKind, ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range,
|
||||
RenameParams, SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
||||
|
@ -401,12 +401,9 @@ pub fn handle_signature_help(
|
|||
documentation: None,
|
||||
})
|
||||
.collect();
|
||||
let documentation = call_info.doc.map(|value| {
|
||||
Documentation::MarkupContent(MarkupContent {
|
||||
kind: MarkupKind::Markdown,
|
||||
value,
|
||||
})
|
||||
});
|
||||
|
||||
let documentation = call_info.doc.map(|it| it.conv());
|
||||
|
||||
let sig_info = SignatureInformation {
|
||||
label: call_info.label,
|
||||
documentation,
|
||||
|
|
38
crates/ra_lsp_server/src/markdown.rs
Normal file
38
crates/ra_lsp_server/src/markdown.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use ra_ide_api::Documentation;
|
||||
|
||||
pub(crate) fn sanitize_markdown(docs: Documentation) -> Documentation {
|
||||
let docs: String = docs.into();
|
||||
|
||||
// Massage markdown
|
||||
let mut processed_lines = Vec::new();
|
||||
let mut in_code_block = false;
|
||||
for line in docs.lines() {
|
||||
if line.starts_with("```") {
|
||||
in_code_block = !in_code_block;
|
||||
}
|
||||
|
||||
let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
|
||||
"```rust".into()
|
||||
} else {
|
||||
line.to_string()
|
||||
};
|
||||
|
||||
processed_lines.push(line);
|
||||
}
|
||||
|
||||
Documentation::new(&processed_lines.join("\n"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_codeblock_adds_rust() {
|
||||
let comment = "```\nfn some_rust() {}\n```";
|
||||
assert_eq!(
|
||||
sanitize_markdown(Documentation::new(comment)).contents(),
|
||||
"```rust\nfn some_rust() {}\n```"
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue