Skip to content

Initial implementation of hard tab indentation. #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
// we put each subexpression on a separate, much like the (default) function
// argument function argument strategy.

use Indent;
use rewrite::{Rewrite, RewriteContext};
use utils::{first_line_width, make_indent};
use utils::first_line_width;
use expr::rewrite_call;

use syntax::{ast, ptr};
Expand All @@ -30,7 +31,7 @@ use syntax::print::pprust;
pub fn rewrite_chain(mut expr: &ast::Expr,
context: &RewriteContext,
width: usize,
offset: usize)
offset: Indent)
-> Option<String> {
let total_span = expr.span;
let mut subexpr_list = vec![expr];
Expand Down Expand Up @@ -116,7 +117,7 @@ pub fn rewrite_chain(mut expr: &ast::Expr,
let connector = if fits_single_line {
String::new()
} else {
format!("\n{}", make_indent(indent))
format!("\n{}", indent.to_string(context.config))
};

let first_connector = if extend {
Expand Down Expand Up @@ -145,7 +146,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
span: Span,
context: &RewriteContext,
width: usize,
offset: usize)
offset: Indent)
-> Option<String> {
match expr.node {
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => {
Expand Down Expand Up @@ -179,7 +180,7 @@ fn rewrite_method_call(method_name: ast::Ident,
span: Span,
context: &RewriteContext,
width: usize,
offset: usize)
offset: Indent)
-> Option<String> {
let type_str = if types.is_empty() {
String::new()
Expand Down
32 changes: 22 additions & 10 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@

use std::iter;

use Indent;
use config::Config;
use string::{StringFormat, rewrite_string};
use utils::make_indent;

pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usize) -> String {
pub fn rewrite_comment(orig: &str,
block_style: bool,
width: usize,
offset: Indent,
config: &Config)
-> String {
let s = orig.trim();

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

let indent_str = make_indent(offset);
let indent_str = offset.to_string(config);
let line_breaks = s.chars().filter(|&c| c == '\n').count();

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

// FIXME(#217): prevent string literal from going over the limit.
use Indent;
#[test]
#[rustfmt_skip]
fn format_comments() {
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, 100));
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, 0));
let config = Default::default();
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
&config));
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
Indent::empty(), &config));

assert_eq!("// A multi line comment\n // between args.",
rewrite_comment("// A multi line comment\n // between args.",
false,
60,
12));
Indent::new(0, 12),
&config));

let input = "// comment";
let expected =
"/* com\n \
* men\n \
* t */";
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
assert_eq!(expected, rewrite_comment(input, true, 9, Indent::new(0, 69), &config));

assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100,
Indent::new(0, 100), &config));
}

// This is probably intended to be a non-test fn, but it is not used. I'm
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ create_config! {
format_strings: bool, "Format string literals, or leave as is",
chains_overflow_last: bool, "Allow last call in method chain to break the line",
take_source_hints: bool, "Retain some formatting characteristics from the source code",
hard_tabs: bool, "Use tab characters for indentation, spaces for alignment",
}

impl Default for Config {
Expand Down Expand Up @@ -279,6 +280,7 @@ impl Default for Config {
format_strings: true,
chains_overflow_last: true,
take_source_hints: true,
hard_tabs: false,
}
}
}
Loading