From 73d1d50029a41ffbdd446587aab607fdaae69f86 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Sat, 21 Sep 2024 22:56:23 +0100 Subject: [PATCH] Add variable tab size --- src/format.rs | 2 +- src/indent.rs | 2 +- src/parse.rs | 9 ++++++++- src/regexes.rs | 3 --- src/subs.rs | 4 ++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/format.rs b/src/format.rs index 317cbfb..6e4fe26 100644 --- a/src/format.rs +++ b/src/format.rs @@ -20,7 +20,7 @@ pub fn format_file( record_file_log(logs, Info, file, "Formatting started."); let mut old_text = remove_extra_newlines(text); old_text = environments_new_line(&old_text, file, args, logs); - old_text = remove_tabs(&old_text); + 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 cd0f657..00674a5 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -170,7 +170,7 @@ pub fn apply_indent( // apply indent new_line = line.trim_start().to_string(); if !new_line.is_empty() { - let n_spaces = indent.visual * TAB; + let n_spaces = indent.visual * args.tab; for _ in 0..n_spaces { new_line.insert(0, ' '); } diff --git a/src/parse.rs b/src/parse.rs index baccec4..daa5198 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -34,6 +34,12 @@ pub struct Cli { help = "Process STDIN as a single file, output formatted text to STDOUT" )] pub stdin: bool, + #[arg( + long, + help = "Number of spaces to use as tab size", + default_value_t = 2 + )] + pub tab: i8, } impl Cli { @@ -88,6 +94,7 @@ impl Cli { quiet: false, trace: false, files: Vec::::new(), + tab: 2, } } } @@ -112,7 +119,7 @@ pub fn read(file: &str, logs: &mut Vec) -> Option<(String, String)> { None } -/// Attempts to read from STDIN and return the filename `` and text +/// Attempt to read from STDIN, return filename `` and text pub fn read_stdin(logs: &mut Vec) -> Option<(String, String)> { let mut text = String::new(); match std::io::stdin().read_to_string(&mut text) { diff --git a/src/regexes.rs b/src/regexes.rs index 01196ae..346a1f1 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -4,9 +4,6 @@ use crate::LINE_END; use lazy_static::lazy_static; use regex::Regex; -/// Number of spaces for tabs and indents -pub const TAB: i8 = 2; - /// Match a LaTeX \item pub const ITEM: &str = "\\item"; /// Match a LaTeX \begin{document} diff --git a/src/subs.rs b/src/subs.rs index df1a235..a23b3ac 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -17,8 +17,8 @@ pub fn remove_extra_newlines(text: &str) -> String { } /// Replace tabs with spaces -pub fn remove_tabs(text: &str) -> String { - let replace = (0..TAB).map(|_| " ").collect::(); +pub fn remove_tabs(text: &str, args: &Cli) -> String { + let replace = (0..args.tab).map(|_| " ").collect::(); text.replace('\t', &replace) }