6743: Don't insert blank lines between doc attributes r=Veykril a=Veykril

Fixes #6742.
Doc attributes should be concatenated via a single linebreak as written in the [rustdoc book](https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html).
Also changed the loop to use an iterator to get rid of the `docs.trim_end_matches("\n\n").to_owned()` part using `Itertools::intersperse`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-12-07 15:12:53 +00:00 committed by GitHub
commit 9a88332452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View file

@ -6,7 +6,8 @@
use std::sync::Arc;
use either::Either;
use syntax::ast;
use itertools::Itertools;
use syntax::{ast, SmolStr};
use crate::{
db::DefDatabase,
@ -93,7 +94,7 @@ fn merge_doc_comments_and_attrs(
) -> Option<String> {
match (doc_comment_text, doc_attr_text) {
(Some(mut comment_text), Some(attr_text)) => {
comment_text.push_str("\n\n");
comment_text.push_str("\n");
comment_text.push_str(&attr_text);
Some(comment_text)
}
@ -105,17 +106,16 @@ fn merge_doc_comments_and_attrs(
fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
let mut docs = String::new();
for attr in owner.attrs() {
if let Some(("doc", value)) =
attr.as_simple_key_value().as_ref().map(|(k, v)| (k.as_str(), v.as_str()))
{
docs.push_str(value);
docs.push_str("\n\n");
}
}
owner
.attrs()
.filter_map(|attr| attr.as_simple_key_value().filter(|(key, _)| key == "doc"))
.map(|(_, value)| value)
.intersperse(SmolStr::new_inline("\n"))
// No FromIterator<SmolStr> for String
.for_each(|s| docs.push_str(s.as_str()));
if docs.is_empty() {
None
} else {
Some(docs.trim_end_matches("\n\n").to_owned())
Some(docs)
}
}

View file

@ -1525,9 +1525,7 @@ fn foo() { let bar = Ba<|>r; }
---
bar docs 0
bar docs 1
bar docs 2
"#]],
);