Skip to content

Commit

Permalink
More efficient implementation of apply_indent
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Nov 2, 2024
1 parent c0658bb commit 8ff11bc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions notes.org
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pkgs.mkShell {
pkgs.gh
pkgs.hyperfine
pkgs.poppler_utils
pkgs.ripgrep
pkgs.rustfmt
pkgs.shellcheck
pkgs.texlive.combined.scheme-full
Expand Down
37 changes: 20 additions & 17 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions tests/source/cam-thesis.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
1 change: 0 additions & 1 deletion tests/source/higher_categories_thesis.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 0 additions & 2 deletions tests/target/cam-thesis.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
1 change: 0 additions & 1 deletion tests/target/higher_categories_thesis.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 8ff11bc

Please sign in to comment.