Merge pull request #1619 from topecongiro/config/format_if_else_cond_comment
Add format_if_else_cond_comment config option
This commit is contained in:
commit
3e25e628a1
@ -666,6 +666,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||
|
||||
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
|
||||
visitor.block_indent = shape.indent;
|
||||
visitor.is_if_else_block = context.is_if_else_block;
|
||||
|
||||
let prefix = match self.rules {
|
||||
ast::BlockCheckMode::Unsafe(..) => {
|
||||
@ -965,7 +966,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||
width: block_width,
|
||||
..shape
|
||||
};
|
||||
let block_str = try_opt!(self.block.rewrite(context, block_shape));
|
||||
let mut block_context = context.clone();
|
||||
block_context.is_if_else_block = self.else_block.is_some();
|
||||
|
||||
let block_str = try_opt!(self.block.rewrite(&block_context, block_shape));
|
||||
|
||||
let cond_span = if let Some(cond) = self.cond {
|
||||
cond.span
|
||||
|
@ -27,7 +27,11 @@ pub struct RewriteContext<'a> {
|
||||
pub codemap: &'a CodeMap,
|
||||
pub config: &'a Config,
|
||||
pub inside_macro: bool,
|
||||
// Force block indent style even if we are using visual indent style.
|
||||
pub use_block: bool,
|
||||
// When `format_if_else_cond_comment` is true, unindent the comment on top
|
||||
// of the `else` or `else if`.
|
||||
pub is_if_else_block: bool,
|
||||
}
|
||||
|
||||
impl<'a> RewriteContext<'a> {
|
||||
|
@ -56,6 +56,7 @@ pub struct FmtVisitor<'a> {
|
||||
pub block_indent: Indent,
|
||||
pub config: &'a Config,
|
||||
pub failed: bool,
|
||||
pub is_if_else_block: bool,
|
||||
}
|
||||
|
||||
impl<'a> FmtVisitor<'a> {
|
||||
@ -117,9 +118,22 @@ pub fn visit_block(&mut self, b: &ast::Block) {
|
||||
}
|
||||
}
|
||||
|
||||
let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
|
||||
if unindent_comment {
|
||||
let end_pos = source!(self, b.span).hi - brace_compensation;
|
||||
let snippet = self.get_context()
|
||||
.snippet(codemap::mk_sp(self.last_pos, end_pos));
|
||||
unindent_comment = snippet.contains("//") || snippet.contains("/*");
|
||||
}
|
||||
// FIXME: we should compress any newlines here to just one
|
||||
if unindent_comment {
|
||||
self.block_indent = self.block_indent.block_unindent(self.config);
|
||||
}
|
||||
self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation);
|
||||
self.close_block();
|
||||
if unindent_comment {
|
||||
self.block_indent = self.block_indent.block_indent(self.config);
|
||||
}
|
||||
self.close_block(unindent_comment);
|
||||
self.last_pos = source!(self, b.span).hi;
|
||||
}
|
||||
|
||||
@ -127,9 +141,11 @@ pub fn visit_block(&mut self, b: &ast::Block) {
|
||||
// item in the block and the closing brace to the block's level.
|
||||
// The closing brace itself, however, should be indented at a shallower
|
||||
// level.
|
||||
fn close_block(&mut self) {
|
||||
fn close_block(&mut self, unindent_comment: bool) {
|
||||
let total_len = self.buffer.len;
|
||||
let chars_too_many = if self.config.hard_tabs() {
|
||||
let chars_too_many = if unindent_comment {
|
||||
0
|
||||
} else if self.config.hard_tabs() {
|
||||
1
|
||||
} else {
|
||||
self.config.tab_spaces()
|
||||
@ -484,6 +500,7 @@ pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisi
|
||||
block_indent: Indent::empty(),
|
||||
config: config,
|
||||
failed: false,
|
||||
is_if_else_block: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -587,7 +604,7 @@ fn format_mod(&mut self, m: &ast::Mod, vis: &ast::Visibility, s: Span, ident: as
|
||||
self.block_indent = self.block_indent.block_indent(self.config);
|
||||
self.walk_mod_items(m);
|
||||
self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
|
||||
self.close_block();
|
||||
self.close_block(false);
|
||||
}
|
||||
self.last_pos = source!(self, m.inner).hi;
|
||||
} else {
|
||||
@ -611,6 +628,7 @@ pub fn get_context(&self) -> RewriteContext {
|
||||
config: self.config,
|
||||
inside_macro: false,
|
||||
use_block: false,
|
||||
is_if_else_block: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
27
tests/target/unindent_if_else_cond_comment.rs
Normal file
27
tests/target/unindent_if_else_cond_comment.rs
Normal file
@ -0,0 +1,27 @@
|
||||
// Comments on else block. See #1575.
|
||||
|
||||
fn example() {
|
||||
// `if` comment
|
||||
if x {
|
||||
foo();
|
||||
// `else if` comment
|
||||
} else if y {
|
||||
foo();
|
||||
// Comment on `else if`.
|
||||
// Comment on `else if`.
|
||||
} else if z {
|
||||
bar();
|
||||
/*
|
||||
* Multi line comment on `else if`
|
||||
*/
|
||||
} else if xx {
|
||||
bar();
|
||||
/* Single line comment on `else if` */
|
||||
} else if yy {
|
||||
foo();
|
||||
// `else` comment
|
||||
} else {
|
||||
foo();
|
||||
// Comment at the end of `else` block
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user