Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doc of generic items formmating error #5124

Merged
merged 5 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,10 @@ impl Spanned for ast::Param {

impl Spanned for ast::GenericParam {
fn span(&self) -> Span {
let lo = if let ast::GenericParamKind::Const {
ty: _,
kw_span,
default: _,
} = self.kind
{
kw_span.lo()
} else if self.attrs.is_empty() {
self.ident.span.lo()
} else {
self.attrs[0].span.lo()
let lo = match self.kind {
_ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
_ => self.ident.span.lo(),
};
let hi = if self.bounds.is_empty() {
self.ident.span.hi()
Expand Down
11 changes: 10 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,16 @@ impl Rewrite for ast::GenericParam {
let mut result = String::with_capacity(128);
// FIXME: If there are more than one attributes, this will force multiline.
match self.attrs.rewrite(context, shape) {
Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
Some(ref rw) if !rw.is_empty() => {
result.push_str(rw);
// When rewriting generic params, an extra newline should be put
// if the attributes end with a doc comment
if let Some(true) = self.attrs.last().map(|a| a.is_doc_comment()) {
result.push_str(&shape.indent.to_string_with_newline(context.config));
} else {
result.push(' ');
}
}
_ => (),
}

Expand Down
14 changes: 14 additions & 0 deletions tests/target/doc-of-generic-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Non-doc pre-comment of Foo
/// doc of Foo
// Non-doc post-comment of Foo
struct Foo<
// Non-doc pre-comment of 'a
/// doc of 'a
'a,
// Non-doc pre-comment of T
/// doc of T
T,
// Non-doc pre-comment of N
/// doc of N
const N: item,
>;