Fixed 'Comment removed between type name and =' issue (#4448)
* Fixed Comment removed between type name and = issue * Fixed where clause issue and pass the full span * has_where condition inline * Fixed indentation error on where clause * Removed tmp file
This commit is contained in:
parent
ea712f1a2c
commit
6455e9de0e
4 changed files with 78 additions and 3 deletions
41
src/items.rs
41
src/items.rs
|
|
@ -1495,6 +1495,7 @@ fn rewrite_type<R: Rewrite>(
|
|||
generics: &ast::Generics,
|
||||
generic_bounds_opt: Option<&ast::GenericBounds>,
|
||||
rhs: Option<&R>,
|
||||
span: Span,
|
||||
) -> Option<String> {
|
||||
let mut result = String::with_capacity(128);
|
||||
result.push_str(&format!("{}type ", format_visibility(context, vis)));
|
||||
|
|
@ -1541,12 +1542,40 @@ fn rewrite_type<R: Rewrite>(
|
|||
if let Some(ty) = rhs {
|
||||
// If there's a where clause, add a newline before the assignment. Otherwise just add a
|
||||
// space.
|
||||
if !generics.where_clause.predicates.is_empty() {
|
||||
let has_where = !generics.where_clause.predicates.is_empty();
|
||||
if has_where {
|
||||
result.push_str(&indent.to_string_with_newline(context.config));
|
||||
} else {
|
||||
result.push(' ');
|
||||
}
|
||||
let lhs = format!("{}=", result);
|
||||
|
||||
let comment_span = context
|
||||
.snippet_provider
|
||||
.opt_span_before(span, "=")
|
||||
.map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo));
|
||||
|
||||
let lhs = match comment_span {
|
||||
Some(comment_span)
|
||||
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
|
||||
{
|
||||
let comment_shape = if has_where {
|
||||
Shape::indented(indent, context.config)
|
||||
} else {
|
||||
Shape::indented(indent, context.config)
|
||||
.block_left(context.config.tab_spaces())?
|
||||
};
|
||||
|
||||
combine_strs_with_missing_comments(
|
||||
context,
|
||||
result.trim_end(),
|
||||
"=",
|
||||
comment_span,
|
||||
comment_shape,
|
||||
true,
|
||||
)?
|
||||
}
|
||||
_ => format!("{}=", result),
|
||||
};
|
||||
|
||||
// 1 = `;`
|
||||
let shape = Shape::indented(indent, context.config).sub_width(1)?;
|
||||
|
|
@ -1563,6 +1592,7 @@ pub(crate) fn rewrite_opaque_type(
|
|||
generic_bounds: &ast::GenericBounds,
|
||||
generics: &ast::Generics,
|
||||
vis: &ast::Visibility,
|
||||
span: Span,
|
||||
) -> Option<String> {
|
||||
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
|
||||
rewrite_type(
|
||||
|
|
@ -1573,6 +1603,7 @@ pub(crate) fn rewrite_opaque_type(
|
|||
generics,
|
||||
Some(generic_bounds),
|
||||
Some(&opaque_type_bounds),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -1801,6 +1832,7 @@ pub(crate) fn rewrite_type_alias(
|
|||
context: &RewriteContext<'_>,
|
||||
indent: Indent,
|
||||
vis: &ast::Visibility,
|
||||
span: Span,
|
||||
) -> Option<String> {
|
||||
rewrite_type(
|
||||
context,
|
||||
|
|
@ -1810,6 +1842,7 @@ pub(crate) fn rewrite_type_alias(
|
|||
generics,
|
||||
generic_bounds_opt,
|
||||
ty_opt,
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -1859,8 +1892,9 @@ pub(crate) fn rewrite_associated_impl_type(
|
|||
generics: &ast::Generics,
|
||||
context: &RewriteContext<'_>,
|
||||
indent: Indent,
|
||||
span: Span,
|
||||
) -> Option<String> {
|
||||
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?;
|
||||
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?;
|
||||
|
||||
match defaultness {
|
||||
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
|
||||
|
|
@ -3118,6 +3152,7 @@ impl Rewrite for ast::ForeignItem {
|
|||
&context,
|
||||
shape.indent,
|
||||
&self.vis,
|
||||
self.span,
|
||||
),
|
||||
ast::ForeignItemKind::MacCall(ref mac) => {
|
||||
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
|
||||
|
|
|
|||
|
|
@ -572,6 +572,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
&self.get_context(),
|
||||
self.block_indent,
|
||||
&item.vis,
|
||||
item.span,
|
||||
);
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
|
|
@ -583,6 +584,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
generic_bounds,
|
||||
generics,
|
||||
&item.vis,
|
||||
item.span,
|
||||
);
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
|
|
@ -649,6 +651,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
&self.get_context(),
|
||||
self.block_indent,
|
||||
&ti.vis,
|
||||
ti.span,
|
||||
);
|
||||
self.push_rewrite(ti.span, rewrite);
|
||||
}
|
||||
|
|
@ -695,6 +698,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
&generics,
|
||||
&self.get_context(),
|
||||
self.block_indent,
|
||||
ii.span,
|
||||
)
|
||||
};
|
||||
let rewrite = match ty {
|
||||
|
|
|
|||
16
tests/source/issue-4244.rs
Normal file
16
tests/source/issue-4244.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
pub struct SS {}
|
||||
|
||||
pub type A /* A Comment */ = SS;
|
||||
|
||||
pub type B // Comment
|
||||
// B
|
||||
= SS;
|
||||
|
||||
pub type C
|
||||
/* Comment C */ = SS;
|
||||
|
||||
pub trait D <T> {
|
||||
type E /* Comment E */ = SS;
|
||||
}
|
||||
|
||||
type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq where T: 'static + Copy /* x */ = Vec<u8>;
|
||||
20
tests/target/issue-4244.rs
Normal file
20
tests/target/issue-4244.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
pub struct SS {}
|
||||
|
||||
pub type A /* A Comment */ = SS;
|
||||
|
||||
pub type B // Comment
|
||||
// B
|
||||
= SS;
|
||||
|
||||
pub type C
|
||||
/* Comment C */
|
||||
= SS;
|
||||
|
||||
pub trait D<T> {
|
||||
type E /* Comment E */ = SS;
|
||||
}
|
||||
|
||||
type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq
|
||||
where
|
||||
T: 'static + Copy, /* x */
|
||||
= Vec<u8>;
|
||||
Loading…
Add table
Add a link
Reference in a new issue