Skip to content

Commit

Permalink
Fixed 'Comment removed between type name and =' issue (#4448)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
whizsid authored Oct 9, 2020
1 parent 69d9a91 commit 7e31d5d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,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)));
Expand Down Expand Up @@ -1665,12 +1666,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)?;
Expand All @@ -1687,6 +1716,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(
Expand All @@ -1697,6 +1727,7 @@ pub(crate) fn rewrite_opaque_type(
generics,
Some(generic_bounds),
Some(&opaque_type_bounds),
span,
)
}

Expand Down Expand Up @@ -1954,6 +1985,7 @@ pub(crate) fn rewrite_type_alias(
context: &RewriteContext<'_>,
indent: Indent,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
rewrite_type(
context,
Expand All @@ -1963,6 +1995,7 @@ pub(crate) fn rewrite_type_alias(
generics,
generic_bounds_opt,
ty_opt,
span,
)
}

Expand Down Expand Up @@ -2012,8 +2045,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)),
Expand Down Expand Up @@ -3250,6 +3284,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)
Expand Down
4 changes: 4 additions & 0 deletions src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
&self.get_context(),
self.block_indent,
&item.vis,
item.span,
);
self.push_rewrite(item.span, rewrite);
}
Expand All @@ -617,6 +618,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
generic_bounds,
generics,
&item.vis,
item.span,
);
self.push_rewrite(item.span, rewrite);
}
Expand Down Expand Up @@ -685,6 +687,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
&self.get_context(),
self.block_indent,
&ti.vis,
ti.span,
);
self.push_rewrite(ti.span, rewrite);
}
Expand Down Expand Up @@ -734,6 +737,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
generics,
&self.get_context(),
self.block_indent,
ii.span,
)
};
let rewrite = match ty {
Expand Down
16 changes: 16 additions & 0 deletions tests/source/issue-4244.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions tests/target/issue-4244.rs
Original file line number Diff line number Diff line change
@@ -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>;

0 comments on commit 7e31d5d

Please sign in to comment.