Skip to content

Assignment indentation after one line comment #4550

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

Merged
merged 4 commits into from
Dec 14, 2020
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
71 changes: 30 additions & 41 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::formatting::{
FindUncommented,
},
expr::{
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_expr,
rewrite_assign_rhs_with, rewrite_assign_rhs_with_comments, RhsTactics,
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
rewrite_assign_rhs_with_comments, RhsTactics,
},
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator},
macros::{rewrite_macro, MacroPosition},
Expand Down Expand Up @@ -121,61 +121,50 @@ impl Rewrite for ast::Local {
mk_sp(self.pat.span.hi(), self.span.hi())
};

if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
let base_span_lo = base_span.lo();
let offset = context.snippet(base_span).find_uncommented("=")?;
let base_span_lo = base_span.lo();

let assign_lo = base_span_lo + BytePos(offset as u32);
let comment_start_pos = if let Some(ref ty) = self.ty {
ty.span.hi()
} else {
self.pat.span.hi()
};
let comment_before_assign =
context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
let assign_lo = base_span_lo + BytePos(offset as u32);
let comment_start_pos = if let Some(ref ty) = self.ty {
ty.span.hi()
} else {
self.pat.span.hi()
};
let comment_before_assign = context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();

let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
let rhs_span_lo = ex.span.lo();
let comment_end_pos = if ex.attrs.is_empty() {
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
let rhs_span_lo = ex.span.lo();
let comment_end_pos = if ex.attrs.is_empty() {
rhs_span_lo
} else {
let attr_span_lo = ex.attrs.first().unwrap().span.lo();
// for the case using block
// ex. let x = { #![my_attr]do_something(); }
if rhs_span_lo < attr_span_lo {
rhs_span_lo
} else {
let attr_span_lo = ex.attrs.first().unwrap().span.lo();
// for the case using block
// ex. let x = { #![my_attr]do_something(); }
if rhs_span_lo < attr_span_lo {
rhs_span_lo
} else {
attr_span_lo
}
};
let comment_after_assign =
context.snippet(mk_sp(assign_hi, comment_end_pos)).trim();

if !comment_before_assign.is_empty() {
let new_indent_str = &pat_shape
.block_indent(0)
.to_string_with_newline(context.config);
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
attr_span_lo
}
};

if !comment_after_assign.is_empty() {
let new_indent_str =
&shape.block_indent(0).to_string_with_newline(context.config);
result.push_str(new_indent_str);
result.push_str(comment_after_assign);
result.push_str(new_indent_str);
}
if !comment_before_assign.is_empty() {
let new_indent_str = &pat_shape
.block_indent(0)
.to_string_with_newline(context.config);
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
}

// 1 = trailing semicolon;
let nested_shape = shape.sub_width(1)?;
let rhs = rewrite_assign_rhs_expr(
result = rewrite_assign_rhs_with_comments(
context,
&result,
&**ex,
nested_shape,
RhsTactics::Default,
mk_sp(assign_hi, comment_end_pos),
true,
)?;
result = result + &rhs;
}

result.push(';');
Expand Down
3 changes: 2 additions & 1 deletion tests/source/issue-3851.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ fn main() {
if true {
1.0
};
}
}

5 changes: 5 additions & 0 deletions tests/source/issue-4502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let after =
// after_comment
1.0;
}
16 changes: 8 additions & 8 deletions tests/target/issue-3851.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ fn main() {
};

let after =
// after_comment
if true {
1.0
};
// after_comment
if true {
1.0
};

// before_comment
let both =
// after_comment
if true {
1.0
};
// after_comment
if true {
1.0
};
}
5 changes: 5 additions & 0 deletions tests/target/issue-4502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let after =
// after_comment
1.0;
}