From 3970748f59376381f360e42d8f703b2f0f7bbde7 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Mon, 19 Oct 2015 21:41:18 +0200 Subject: [PATCH] Remove overflow indentation --- src/comment.rs | 25 ++++++++++++++++--------- src/expr.rs | 11 +++++------ src/missed_spans.rs | 2 +- src/rewrite.rs | 16 ---------------- src/visitor.rs | 2 -- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index d9b2d1e111e..e0910ba093e 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -291,6 +291,10 @@ impl Iterator for CharClasses 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 Iterator for CharClasses 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 { 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()); } diff --git a/src/expr.rs b/src/expr.rs index b69989aa761..8592ad47b9e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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>(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)); } diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 6cb4a88d160..53819bfdd8f 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -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 diff --git a/src/rewrite.rs b/src/rewrite.rs index 6517bbdf37b..cf8be8004e1 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -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, } } diff --git a/src/visitor.rs b/src/visitor.rs index 6ea296a36b7..17f197ec880 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -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(), } } }