Remove overflow indentation

This commit is contained in:
Marcus Klaas 2015-10-19 21:41:18 +02:00
parent cf0f5ca814
commit 3970748f59
5 changed files with 22 additions and 34 deletions

View File

@ -291,6 +291,10 @@ impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
'\n' => CharClassesStatus::Normal,
_ => CharClassesStatus::LineComment,
};
// let code_char_kind = match chr {
// '\n' => CodeCharKind::Normal,
// _ => CodeCharKind::Comment,
// };
return Some((CodeCharKind::Comment, item));
}
};
@ -298,14 +302,14 @@ impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
}
}
struct CommentCodeSlices<'a> {
pub struct CommentCodeSlices<'a> {
slice: &'a str,
last_slice_type: CodeCharKind,
last_slice_end: usize,
}
impl<'a> CommentCodeSlices<'a> {
fn new(slice: &'a str) -> CommentCodeSlices<'a> {
pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
CommentCodeSlices {
slice: slice,
last_slice_type: CodeCharKind::Comment,
@ -315,7 +319,7 @@ impl<'a> CommentCodeSlices<'a> {
}
impl<'a> Iterator for CommentCodeSlices<'a> {
type Item = (CodeCharKind, &'a str);
type Item = (CodeCharKind, usize, &'a str);
fn next(&mut self) -> Option<Self::Item> {
if self.last_slice_end == self.slice.len() {
@ -341,11 +345,13 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
// This was the last subslice.
self.last_slice_end = self.slice.len();
Some((kind, &self.slice[sub_slice_end..]))
Some((kind, sub_slice_end, &self.slice[sub_slice_end..]))
} else {
let res = &self.slice[self.last_slice_end..sub_slice_end];
let res = (kind,
self.last_slice_end,
&self.slice[self.last_slice_end..sub_slice_end]);
self.last_slice_end = sub_slice_end;
Some((kind, res))
Some(res)
}
}
}
@ -362,9 +368,10 @@ mod test {
let mut iter = CommentCodeSlices::new(input);
assert_eq!((CodeCharKind::Normal, "code(); "), iter.next().unwrap());
assert_eq!((CodeCharKind::Comment, "/* test */"), iter.next().unwrap());
assert_eq!((CodeCharKind::Normal, " 1 + 1"), iter.next().unwrap());
assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
assert_eq!((CodeCharKind::Comment, 8, "/* test */"),
iter.next().unwrap());
assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
assert_eq!(None, iter.next());
}

View File

@ -423,7 +423,7 @@ impl Rewrite for ast::Block {
}
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
visitor.block_indent = context.block_indent + context.overflow_indent;
visitor.block_indent = context.block_indent;
let prefix = match self.rules {
ast::BlockCheckMode::UnsafeBlock(..) => {
@ -751,7 +751,7 @@ fn rewrite_match(context: &RewriteContext,
let mut result = format!("match {} {{", cond_str);
let nested_context = context.nested_context();
let arm_indent = nested_context.block_indent + context.overflow_indent;
let arm_indent = nested_context.block_indent;
let arm_indent_str = arm_indent.to_string(context.config);
let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
@ -795,7 +795,7 @@ fn rewrite_match(context: &RewriteContext,
&arm_indent_str));
result.push_str(&comment);
result.push('\n');
result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config));
result.push_str(&context.block_indent.to_string(context.config));
result.push('}');
Some(result)
}
@ -1537,9 +1537,8 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
// FIXME: we probably should related max_width to width instead of config.max_width
// where is the 1 coming from anyway?
let max_width = try_opt!(context.config.max_width.checked_sub(new_offset.width() + 1));
let rhs_indent = Indent::new(context.config.tab_spaces, 0);
let overflow_context = context.overflow_context(rhs_indent);
let rhs = ex.rewrite(&overflow_context, max_width, new_offset);
let inner_context = context.nested_context();
let rhs = ex.rewrite(&inner_context, max_width, new_offset);
result.push_str(&&try_opt!(rhs));
}

View File

@ -11,7 +11,7 @@
use visitor::FmtVisitor;
use syntax::codemap::{self, BytePos, Span};
use comment::{CharClasses, CodeCharKind};
use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment};
impl<'a> FmtVisitor<'a> {
// TODO these format_missing methods are ugly. Refactor and add unit tests

View File

@ -29,14 +29,8 @@ pub trait Rewrite {
pub struct RewriteContext<'a> {
pub codemap: &'a CodeMap,
pub config: &'a Config,
// Indentation due to nesting of blocks.
pub block_indent: Indent,
// *Extra* indentation due to overflowing to the next line, e.g.,
// let foo =
// bar();
// The extra 4 spaces when formatting `bar()` is overflow_indent.
pub overflow_indent: Indent,
}
impl<'a> RewriteContext<'a> {
@ -45,16 +39,6 @@ impl<'a> RewriteContext<'a> {
codemap: self.codemap,
config: self.config,
block_indent: self.block_indent.block_indent(self.config),
overflow_indent: self.overflow_indent,
}
}
pub fn overflow_context(&self, overflow: Indent) -> RewriteContext<'a> {
RewriteContext {
codemap: self.codemap,
config: self.config,
block_indent: self.block_indent,
overflow_indent: overflow,
}
}

View File

@ -387,7 +387,6 @@ impl<'a> FmtVisitor<'a> {
codemap: self.codemap,
config: self.config,
block_indent: self.block_indent,
overflow_indent: Indent::empty(),
};
// 1 = ";"
match vp.rewrite(&context, self.config.max_width - offset.width() - 1, offset) {
@ -419,7 +418,6 @@ impl<'a> FmtVisitor<'a> {
codemap: self.codemap,
config: self.config,
block_indent: self.block_indent,
overflow_indent: Indent::empty(),
}
}
}