Skip to content

Commit 1e8429e

Browse files
SiegeLordExSiegeLord
authored andcommitted
Initial implementation of hard tab indentation.
1 parent 0b7b3c8 commit 1e8429e

14 files changed

+361
-207
lines changed

src/chains.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// we put each subexpression on a separate, much like the (default) function
2020
// argument function argument strategy.
2121

22+
use Indent;
2223
use rewrite::{Rewrite, RewriteContext};
2324
use utils::{first_line_width, make_indent};
2425
use expr::rewrite_call;
@@ -30,7 +31,7 @@ use syntax::print::pprust;
3031
pub fn rewrite_chain(mut expr: &ast::Expr,
3132
context: &RewriteContext,
3233
width: usize,
33-
offset: usize)
34+
offset: Indent)
3435
-> Option<String> {
3536
let total_span = expr.span;
3637
let mut subexpr_list = vec![expr];
@@ -116,7 +117,7 @@ pub fn rewrite_chain(mut expr: &ast::Expr,
116117
let connector = if fits_single_line {
117118
String::new()
118119
} else {
119-
format!("\n{}", make_indent(indent))
120+
format!("\n{}", make_indent(indent, context.config))
120121
};
121122

122123
let first_connector = if extend {
@@ -145,7 +146,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
145146
span: Span,
146147
context: &RewriteContext,
147148
width: usize,
148-
offset: usize)
149+
offset: Indent)
149150
-> Option<String> {
150151
match expr.node {
151152
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => {
@@ -179,7 +180,7 @@ fn rewrite_method_call(method_name: ast::Ident,
179180
span: Span,
180181
context: &RewriteContext,
181182
width: usize,
182-
offset: usize)
183+
offset: Indent)
183184
-> Option<String> {
184185
let type_str = if types.is_empty() {
185186
String::new()

src/comment.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@
1212

1313
use std::iter;
1414

15+
use Indent;
16+
use config::Config;
1517
use string::{StringFormat, rewrite_string};
1618
use utils::make_indent;
1719

18-
pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usize) -> String {
20+
pub fn rewrite_comment(orig: &str,
21+
block_style: bool,
22+
width: usize,
23+
offset: Indent,
24+
config: &Config)
25+
-> String {
1926
let s = orig.trim();
2027

2128
// Edge case: block comments. Let's not trim their lines (for now).
@@ -33,11 +40,12 @@ pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usiz
3340
line_start: line_start,
3441
line_end: "",
3542
width: max_chars,
36-
offset: offset + opener.len() - line_start.len(),
43+
offset: offset + (opener.len() - line_start.len()),
3744
trim_end: true,
45+
config: config,
3846
};
3947

40-
let indent_str = make_indent(offset);
48+
let indent_str = make_indent(offset, config);
4149
let line_breaks = s.chars().filter(|&c| c == '\n').count();
4250

4351
let (_, mut s) = s.lines()
@@ -288,24 +296,33 @@ impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
288296
mod test {
289297
use super::{CharClasses, CodeCharKind, contains_comment, rewrite_comment, FindUncommented};
290298

299+
use std::default::Default;
300+
301+
use Indent;
302+
291303
#[test]
292304
fn format_comments() {
293-
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, 100));
294-
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, 0));
305+
let config = Default::default();
306+
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
307+
&config));
308+
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
309+
Indent::new(0, 0), &config));
295310

296311
assert_eq!("// A multi line comment\n // between args.",
297312
rewrite_comment("// A multi line comment\n // between args.",
298313
false,
299314
60,
300-
12));
315+
Indent::new(0, 12),
316+
&config));
301317

302318
let input = "// comment";
303319
let expected = "/* com\n \
304320
* men\n \
305321
* t */";
306-
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
322+
assert_eq!(expected, rewrite_comment(input, true, 9, Indent::new(0, 69), &config));
307323

308-
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
324+
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100,
325+
Indent::new(0, 100), &config));
309326
}
310327

311328
// This is probably intended to be a non-test fn, but it is not used. I'm

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ create_config! {
152152
chains_overflow_last: bool,
153153
take_source_hints: bool, // Retain some formatting characteristics from
154154
// the source code.
155+
hard_tabs: bool,
155156
}
156157

157158
impl Default for Config {
@@ -185,6 +186,7 @@ impl Default for Config {
185186
format_strings: true,
186187
chains_overflow_last: true,
187188
take_source_hints: true,
189+
hard_tabs: false,
188190
}
189191
}
190192
}

0 commit comments

Comments
 (0)