Skip to content

Commit ce2c4f6

Browse files
committed
Merge pull request #290 from SiegeLord/tabs
Initial implementation of hard tab indentation.
2 parents 4e1fff8 + 01bdcd0 commit ce2c4f6

File tree

17 files changed

+514
-210
lines changed

17 files changed

+514
-210
lines changed

src/chains.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
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};
23-
use utils::{first_line_width, make_indent};
24+
use utils::first_line_width;
2425
use expr::rewrite_call;
2526

2627
use syntax::{ast, ptr};
@@ -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{}", indent.to_string(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: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212

1313
use std::iter;
1414

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

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

2127
// Edge case: block comments. Let's not trim their lines (for now).
@@ -33,11 +39,12 @@ pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usiz
3339
line_start: line_start,
3440
line_end: "",
3541
width: max_chars,
36-
offset: offset + opener.len() - line_start.len(),
42+
offset: offset + (opener.len() - line_start.len()),
3743
trim_end: true,
44+
config: config,
3845
};
3946

40-
let indent_str = make_indent(offset);
47+
let indent_str = offset.to_string(config);
4148
let line_breaks = s.chars().filter(|&c| c == '\n').count();
4249

4350
let (_, mut s) = s.lines()
@@ -288,27 +295,32 @@ impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
288295
mod test {
289296
use super::{CharClasses, CodeCharKind, contains_comment, rewrite_comment, FindUncommented};
290297

291-
// FIXME(#217): prevent string literal from going over the limit.
298+
use Indent;
292299
#[test]
293300
#[rustfmt_skip]
294301
fn format_comments() {
295-
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, 100));
296-
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, 0));
302+
let config = Default::default();
303+
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
304+
&config));
305+
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
306+
Indent::empty(), &config));
297307

298308
assert_eq!("// A multi line comment\n // between args.",
299309
rewrite_comment("// A multi line comment\n // between args.",
300310
false,
301311
60,
302-
12));
312+
Indent::new(0, 12),
313+
&config));
303314

304315
let input = "// comment";
305316
let expected =
306317
"/* com\n \
307318
* men\n \
308319
* t */";
309-
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
320+
assert_eq!(expected, rewrite_comment(input, true, 9, Indent::new(0, 69), &config));
310321

311-
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
322+
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100,
323+
Indent::new(0, 100), &config));
312324
}
313325

314326
// 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
@@ -246,6 +246,7 @@ create_config! {
246246
format_strings: bool, "Format string literals, or leave as is",
247247
chains_overflow_last: bool, "Allow last call in method chain to break the line",
248248
take_source_hints: bool, "Retain some formatting characteristics from the source code",
249+
hard_tabs: bool, "Use tab characters for indentation, spaces for alignment",
249250
}
250251

251252
impl Default for Config {
@@ -279,6 +280,7 @@ impl Default for Config {
279280
format_strings: true,
280281
chains_overflow_last: true,
281282
take_source_hints: true,
283+
hard_tabs: false,
282284
}
283285
}
284286
}

0 commit comments

Comments
 (0)