make Docs handing more ideomatic

This commit is contained in:
Aleksey Kladov 2019-06-08 14:16:05 +03:00
parent 5dc2789895
commit 33026c654e
4 changed files with 13 additions and 23 deletions

View file

@ -12,7 +12,6 @@ use crate::{
ty::{TraitRef, InferenceResult, primitive::{IntTy, FloatTy, Signedness, IntBitness, FloatBitness}}, ty::{TraitRef, InferenceResult, primitive::{IntTy, FloatTy, Signedness, IntBitness, FloatBitness}},
adt::{EnumVariantId, StructFieldId, VariantDef}, adt::{EnumVariantId, StructFieldId, VariantDef},
generics::HasGenericParams, generics::HasGenericParams,
docs::{Documentation, Docs, docs_from_ast},
ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId, MacroDefId}, ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId, MacroDefId},
impl_block::ImplBlock, impl_block::ImplBlock,
resolve::Resolver, resolve::Resolver,

View file

@ -1,24 +1,24 @@
use ra_syntax::ast; use ra_syntax::ast;
use crate::{HirDatabase, Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union}; use crate::{HirDatabase, Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource};
/// Holds documentation /// Holds documentation
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Documentation(String); pub struct Documentation(String);
impl Documentation { impl Documentation {
pub fn new(s: &str) -> Self { fn new(s: &str) -> Documentation {
Self(s.into()) Documentation(s.into())
} }
pub fn contents(&self) -> &str { pub fn as_str(&self) -> &str {
&self.0 &self.0
} }
} }
impl Into<String> for Documentation { impl Into<String> for Documentation {
fn into(self) -> String { fn into(self) -> String {
self.contents().into() self.0.clone()
} }
} }

View file

@ -171,7 +171,7 @@ impl Conv for ra_ide_api::Documentation {
fn conv(self) -> Documentation { fn conv(self) -> Documentation {
Documentation::MarkupContent(MarkupContent { Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown, kind: MarkupKind::Markdown,
value: crate::markdown::sanitize_markdown(self).into(), value: crate::markdown::mark_fenced_blocks_as_rust(self.as_str()).into(),
}) })
} }
} }

View file

@ -1,26 +1,20 @@
use ra_ide_api::Documentation; pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
pub(crate) fn sanitize_markdown(docs: Documentation) -> Documentation {
let docs: String = docs.into();
// Massage markdown
let mut processed_lines = Vec::new(); let mut processed_lines = Vec::new();
let mut in_code_block = false; let mut in_code_block = false;
for line in docs.lines() { for line in src.lines() {
if line.starts_with("```") { if line.starts_with("```") {
in_code_block = !in_code_block; in_code_block ^= true
} }
let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
"```rust".into() "```rust"
} else { } else {
line.to_string() line
}; };
processed_lines.push(line); processed_lines.push(line);
} }
processed_lines.join("\n")
Documentation::new(&processed_lines.join("\n"))
} }
#[cfg(test)] #[cfg(test)]
@ -30,9 +24,6 @@ mod tests {
#[test] #[test]
fn test_codeblock_adds_rust() { fn test_codeblock_adds_rust() {
let comment = "```\nfn some_rust() {}\n```"; let comment = "```\nfn some_rust() {}\n```";
assert_eq!( assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```");
sanitize_markdown(Documentation::new(comment)).contents(),
"```rust\nfn some_rust() {}\n```"
);
} }
} }