Skip to content

Commit

Permalink
Initial implementation of hard tab indentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Sep 7, 2015
1 parent 2a61a98 commit 9114843
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 205 deletions.
33 changes: 25 additions & 8 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@

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 @@ -34,11 +41,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 = make_indent(offset, config);
let line_breaks = s.chars().filter(|&c| c == '\n').count();

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

use std::default::Default;

use Indent;

#[test]
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::new(0, 0), &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 @@ -130,6 +130,7 @@ create_config! {
closure_indent_style: BlockIndentStyle,
single_line_if_else: bool,
format_strings: bool,
hard_tabs: bool,
}

impl Default for Config {
Expand Down Expand Up @@ -163,6 +164,7 @@ impl Default for Config {
closure_indent_style: BlockIndentStyle::Visual,
single_line_if_else: false,
format_strings: true,
hard_tabs: false,
}
}

Expand Down
Loading

0 comments on commit 9114843

Please sign in to comment.