Skip to content

Commit

Permalink
Add flag to use tabs instead of spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Oct 1, 2024
1 parent 46c3c80 commit 4c7a841
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub fn format_file(
) -> String {
record_file_log(logs, Info, file, "Formatting started.");
let mut old_text = remove_extra_newlines(text);
old_text = remove_tabs(&old_text, args);
if !args.usetabs {
old_text = remove_tabs(&old_text, args);
}
old_text = remove_trailing_spaces(&old_text);

let mut state = State::new();
Expand Down
10 changes: 7 additions & 3 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ pub fn apply_indent(
// apply indent
new_line = line.trim_start().to_string();
if !new_line.is_empty() {
let n_spaces = indent.visual * args.tab;
for _ in 0..n_spaces {
new_line.insert(0, ' ');
let n_indent_chars = indent.visual * args.tab;
for _ in 0..n_indent_chars {
if args.usetabs {
new_line.insert(0, '\t');
} else {
new_line.insert(0, ' ');
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Cli {
default_value_t = 2
)]
pub tab: i8,
#[arg(long, help = "Use tabs instead of spaces for indentation")]
pub usetabs: bool,
}

impl Cli {
Expand Down Expand Up @@ -95,6 +97,7 @@ impl Cli {
trace: false,
files: Vec::<String>::new(),
tab: 2,
usetabs: false,
}
}
}
Expand Down

0 comments on commit 4c7a841

Please sign in to comment.