mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
Merge #662
662: Preserve indentation in doc comments r=matklad a=kjeremy Fixes #502 Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
This commit is contained in:
commit
ffcf618842
@ -27,10 +27,5 @@ pub trait Docs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
|
pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
|
||||||
let comments = node.doc_comment_text();
|
node.doc_comment_text().map(|it| Documentation::new(&it))
|
||||||
if comments.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Documentation::new(&comments))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -126,8 +126,7 @@ impl CallInfo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut doc = None;
|
let mut doc = None;
|
||||||
let docs = node.doc_comment_text();
|
if let Some(docs) = node.doc_comment_text() {
|
||||||
if !docs.is_empty() {
|
|
||||||
// Massage markdown
|
// 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;
|
||||||
|
@ -100,12 +100,7 @@ impl NavigationTarget {
|
|||||||
fn docs(&self, db: &RootDatabase) -> Option<String> {
|
fn docs(&self, db: &RootDatabase) -> Option<String> {
|
||||||
let node = self.node(db)?;
|
let node = self.node(db)?;
|
||||||
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
|
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
|
||||||
let comments = node.doc_comment_text();
|
node.doc_comment_text()
|
||||||
if comments.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(comments)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
visitor()
|
visitor()
|
||||||
|
@ -115,21 +115,38 @@ pub trait DocCommentsOwner: AstNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the textual content of a doc comment block as a single string.
|
/// Returns the textual content of a doc comment block as a single string.
|
||||||
/// That is, strips leading `///` and joins lines
|
/// That is, strips leading `///` (+ optional 1 character of whitespace)
|
||||||
fn doc_comment_text(&self) -> std::string::String {
|
/// and joins lines.
|
||||||
self.doc_comments()
|
fn doc_comment_text(&self) -> Option<std::string::String> {
|
||||||
|
let docs = self
|
||||||
|
.doc_comments()
|
||||||
.filter(|comment| comment.is_doc_comment())
|
.filter(|comment| comment.is_doc_comment())
|
||||||
.map(|comment| {
|
.map(|comment| {
|
||||||
let prefix = comment.prefix();
|
let prefix_len = comment.prefix().len();
|
||||||
let trimmed = comment
|
|
||||||
.text()
|
let line = comment.text().as_str();
|
||||||
.as_str()
|
|
||||||
.trim()
|
// Determine if the prefix or prefix + 1 char is stripped
|
||||||
.trim_start_matches(prefix)
|
let pos = if line
|
||||||
.trim_start();
|
.chars()
|
||||||
trimmed.to_owned()
|
.nth(prefix_len)
|
||||||
|
.map(|c| c.is_whitespace())
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
prefix_len + 1
|
||||||
|
} else {
|
||||||
|
prefix_len
|
||||||
|
};
|
||||||
|
|
||||||
|
line[pos..].to_owned()
|
||||||
})
|
})
|
||||||
.join("\n")
|
.join("\n");
|
||||||
|
|
||||||
|
if docs.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(docs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,6 +720,18 @@ impl BindPat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_doc_comment_none() {
|
||||||
|
let file = SourceFile::parse(
|
||||||
|
r#"
|
||||||
|
// non-doc
|
||||||
|
mod foo {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
||||||
|
assert!(module.doc_comment_text().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_comment_of_items() {
|
fn test_doc_comment_of_items() {
|
||||||
let file = SourceFile::parse(
|
let file = SourceFile::parse(
|
||||||
@ -713,5 +742,25 @@ fn test_doc_comment_of_items() {
|
|||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
||||||
assert_eq!("doc", module.doc_comment_text());
|
assert_eq!("doc", module.doc_comment_text().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_doc_comment_preserves_indents() {
|
||||||
|
let file = SourceFile::parse(
|
||||||
|
r#"
|
||||||
|
/// doc1
|
||||||
|
/// ```
|
||||||
|
/// fn foo() {
|
||||||
|
/// // ...
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
mod foo {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
"doc1\n```\nfn foo() {\n // ...\n}\n```",
|
||||||
|
module.doc_comment_text().unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user