From 4c7a8416f6c939edc8149b3fc997a9ab4edd7431 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:18:14 +0100 Subject: [PATCH] Add flag to use tabs instead of spaces --- src/format.rs | 4 +++- src/indent.rs | 10 +++++++--- src/parse.rs | 3 +++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/format.rs b/src/format.rs index a86b713..a38dcc7 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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(); diff --git a/src/indent.rs b/src/indent.rs index 621a6b1..20402d1 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -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, ' '); + } } } } diff --git a/src/parse.rs b/src/parse.rs index daa5198..eb3353b 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -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 { @@ -95,6 +97,7 @@ impl Cli { trace: false, files: Vec::::new(), tab: 2, + usetabs: false, } } }