From 8ff11bc855262a1e16d8deb8d77f0425630f69c2 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Sat, 2 Nov 2024 12:14:41 +0000 Subject: [PATCH] More efficient implementation of apply_indent --- justfile | 3 ++ notes.org | 2 ++ shell.nix | 1 + src/indent.rs | 37 ++++++++++++----------- tests/source/cam-thesis.cls | 2 -- tests/source/higher_categories_thesis.tex | 1 - tests/target/cam-thesis.cls | 2 -- tests/target/higher_categories_thesis.tex | 1 - 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/justfile b/justfile index ea05c45..4d268dd 100644 --- a/justfile +++ b/justfile @@ -49,6 +49,9 @@ shellcheck: nix: @nix flake update +todo: + @rg -g '!justfile' todo + logo: @cd extra && python logo.py @cd extra && magick -background none logo.svg -resize 5000x5000 logo.png diff --git a/notes.org b/notes.org index e9afea6..7f87876 100644 --- a/notes.org +++ b/notes.org @@ -1,4 +1,6 @@ #+title: tex-fmt +* Tasks +** TODO Skipping a line seems to break linums * Release process ** Update version number in Cargo.toml ** Update Nix flake and lock diff --git a/shell.nix b/shell.nix index f44c434..336ea1b 100644 --- a/shell.nix +++ b/shell.nix @@ -19,6 +19,7 @@ pkgs.mkShell { pkgs.gh pkgs.hyperfine pkgs.poppler_utils + pkgs.ripgrep pkgs.rustfmt pkgs.shellcheck pkgs.texlive.combined.scheme-full diff --git a/src/indent.rs b/src/indent.rs index 1f6f8a2..0aa983f 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -179,25 +179,28 @@ pub fn apply_indent( args: &Cli, indent_char: &str, ) -> String { - // Remove white space from the start of the line - let trimmed_line = line.trim_start(); + let first_non_whitespace = line.chars().position(|c| !c.is_whitespace()); + + // If line is blank, return an empty line + if first_non_whitespace.is_none() { + return String::new(); + } + + // If line is correctly indented, return it directly + #[allow(clippy::cast_possible_wrap)] + let n_indent_chars = (indent.visual * args.tab as i8) as usize; + if first_non_whitespace == Some(n_indent_chars) { + return line.into(); + } - // If the line is now empty, return a new empty String - if trimmed_line.is_empty() { - String::new() // Otherwise, allocate enough memory to fit line with the added // indentation and insert the appropriate string slices - } else { - // TODO can we check if the indent is already correct and do nothing? - #[allow(clippy::cast_possible_wrap)] - let n_indent_chars = - usize::try_from(indent.visual * args.tab as i8).unwrap(); - let mut new_line = - String::with_capacity(trimmed_line.len() + n_indent_chars); - for idx in 0..n_indent_chars { - new_line.insert_str(idx, indent_char); - } - new_line.insert_str(n_indent_chars, trimmed_line); - new_line + let trimmed_line = line.trim_start(); + let mut new_line = + String::with_capacity(trimmed_line.len() + n_indent_chars); + for idx in 0..n_indent_chars { + new_line.insert_str(idx, indent_char); } + new_line.insert_str(n_indent_chars, trimmed_line); + new_line } diff --git a/tests/source/cam-thesis.cls b/tests/source/cam-thesis.cls index 03034d6..86c4746 100644 --- a/tests/source/cam-thesis.cls +++ b/tests/source/cam-thesis.cls @@ -284,8 +284,6 @@ for the degree of Doctor of Philosophy% %% %% Chapter and section numbering %% -% TODO: Check whether numbering up to the fourth level is okay. It is not -% specified in the guidelines. \setcounter{secnumdepth}{3} \setcounter{tocdepth}{3} diff --git a/tests/source/higher_categories_thesis.tex b/tests/source/higher_categories_thesis.tex index 933723d..d5f666f 100644 --- a/tests/source/higher_categories_thesis.tex +++ b/tests/source/higher_categories_thesis.tex @@ -216,7 +216,6 @@ \setlength\cellspacetoplimit{5pt} \setlength\cellspacebottomlimit{5pt} \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} -%\usepackage{todonotes} \usepackage{ebproof} \usepackage{mathpartir} \usepackage{subcaption} diff --git a/tests/target/cam-thesis.cls b/tests/target/cam-thesis.cls index f1d16ab..d7e70f0 100644 --- a/tests/target/cam-thesis.cls +++ b/tests/target/cam-thesis.cls @@ -291,8 +291,6 @@ %% %% Chapter and section numbering %% -% TODO: Check whether numbering up to the fourth level is okay. It is not -% specified in the guidelines. \setcounter{secnumdepth}{3} \setcounter{tocdepth}{3} diff --git a/tests/target/higher_categories_thesis.tex b/tests/target/higher_categories_thesis.tex index 12d013a..eab61c3 100644 --- a/tests/target/higher_categories_thesis.tex +++ b/tests/target/higher_categories_thesis.tex @@ -219,7 +219,6 @@ \setlength\cellspacetoplimit{5pt} \setlength\cellspacebottomlimit{5pt} \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} -%\usepackage{todonotes} \usepackage{ebproof} \usepackage{mathpartir} \usepackage{subcaption}