Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Jul 30, 2024
2 parents e2c585d + 2a27a7f commit bed30e7
Show file tree
Hide file tree
Showing 65 changed files with 172 additions and 161 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
debug/
target/
/debug/
/target/
**/*.rs.bk
*.pdb
result
/result
*.html
*.log
*.svg
Expand Down
34 changes: 14 additions & 20 deletions extra/latex.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
#!/usr/bin/env bash
echo "Checking latex PDFs agree"
DIR="$(mktemp -d)"
cp -r ../tests/* $DIR
echo $DIR

# TODO remove block
rm $DIR/in/*
cp -r ../tests/in/higher* $DIR/in/
cp -r ../tests/in/cam* $DIR/in/
cp -r ../tests/in/quiver* $DIR/in/

cd $DIR
cp -r ../tests/* "$DIR"
echo "$DIR"
cd "$DIR" || exit

echo

for file in ./in/*.tex; do
f=$(basename $file .tex)
for file in ./source/*.tex; do
f=$(basename "$file" .tex)
echo "Running latex for $f.tex"
(cd ./in && latexmk -pdflua $f.tex )#>/dev/null 2>&1)
(cd ./out && latexmk -pdflua $f.tex )#>/dev/null 2>&1)
(cd ./in && pdftotext -q $f.pdf >/dev/null 2>&1)
(cd ./out && pdftotext -q $f.pdf >/dev/null 2>&1)
(cd ./source && latexmk -pdflua "$f.tex" >/dev/null 2>&1)
(cd ./target && latexmk -pdflua "$f.tex" >/dev/null 2>&1)
(cd ./source && pdftotext -q "$f.pdf" >/dev/null 2>&1)
(cd ./target && pdftotext -q "$f.pdf" >/dev/null 2>&1)
done

echo

for file in ./in/*.tex; do
f=$(basename $file .tex)
for file in ./source/*.tex; do
f=$(basename "$file" .tex)
echo "Checking PDF for $f.tex"
diff -u ./in/$f.txt ./out/$f.txt | diff-so-fancy
diff -u "source/$f.txt" "target/$f.txt" | diff-so-fancy
done

echo $DIR
echo "$DIR"
21 changes: 11 additions & 10 deletions extra/perf.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
echo "Getting performance metrics"
DIR="$(mktemp -d)"
cp -r ../tests/* $DIR
cp -r ../tests/* "$DIR"
cargo build --release

calc(){ awk "BEGIN { print "$*" }"; }
calc(){ awk "BEGIN { print ""$*"" }"; }

echo
echo -n "Test files: $(ls -l $DIR/in/* $DIR/out/* | wc -l) files, "
echo -n "$(wc -l --total=only $DIR/in/* $DIR/out/*) lines, "
echo "$(du -hs $DIR | cut -f 1)"
echo -n "Test files: $(find "$DIR"/*/* | wc -l) files, "
echo -n "$(wc -l --total=only "$DIR"/source/* "$DIR"/target/*) lines, "
du -hs "$DIR" | cut -f 1
echo

# tex-fmt
Expand All @@ -17,7 +18,7 @@ hyperfine --warmup 10 \
--export-csv $TEXFMTFILE \
--command-name "tex-fmt" \
--prepare "cp -r ../tests/* $DIR" \
"../target/release/tex-fmt $DIR/in/* $DIR/out/*"
"../target/release/tex-fmt $DIR/source/* $DIR/target/*"

# latexindent
LATEXINDENTFILE="hyperfine-latexindent.csv"
Expand All @@ -26,7 +27,7 @@ hyperfine --warmup 0 \
--runs 1 \
--command-name "latexindent" \
--prepare "cp -r ../tests/* $DIR" \
"latexindent $DIR/in/* $DIR/out/*"
"latexindent $DIR/source/* $DIR/target/*"

# latexindent -m
LATEXINDENTMFILE="hyperfine-latexindent-m.csv"
Expand All @@ -35,16 +36,16 @@ hyperfine --warmup 0 \
--runs 1 \
--command-name "latexindent -m" \
--prepare "cp -r ../tests/* $DIR" \
"latexindent -m $DIR/in/* $DIR/out/*"
"latexindent -m $DIR/source/* $DIR/target/*"

# print results
TEXFMT=$(cat $TEXFMTFILE | tail -n 1 | cut -d "," -f 2)
echo "tex-fmt: ${TEXFMT}s"

LATEXINDENT=$(cat $LATEXINDENTFILE | tail -n 1 | cut -d "," -f 2)
LATEXINDENTTIMES=$(calc $LATEXINDENT/$TEXFMT)
LATEXINDENTTIMES=$(calc "$LATEXINDENT"/"$TEXFMT")
echo "latexindent: ${LATEXINDENT}s, x$LATEXINDENTTIMES"

LATEXINDENTM=$(cat $LATEXINDENTMFILE | tail -n 1 | cut -d "," -f 2)
LATEXINDENTMTIMES=$(calc $LATEXINDENTM/$TEXFMT)
LATEXINDENTMTIMES=$(calc "$LATEXINDENTM"/"$TEXFMT")
echo "latexindent -m: ${LATEXINDENTM}s, x$LATEXINDENTMTIMES"
21 changes: 11 additions & 10 deletions extra/prof.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#!/usr/bin/env bash
echo "Making flamegraph profile"
DIR="$(mktemp -d)"
cp ../tests/in/phd_dissertation.tex $DIR
cp "../tests/source/phd_dissertation.tex" "$DIR"
cargo build --release

echo "Writing large test file"
for i in {1..5}; do
cat $DIR/phd_dissertation.tex >> $DIR/large.tex
echo "\n\n\n" >> $DIR/large.tex
for _ in {1..5}; do
cat "$DIR/phd_dissertation.tex" >> "$DIR/large.tex"
printf "\n\n\n" >> "$DIR/large.tex"
done

rm $DIR/phd_dissertation.tex
rm "$DIR/phd_dissertation.tex"

echo "Test file:"
for f in $DIR/*.tex; do
echo -n " $(basename $f), "
echo -n "$(wc -l $f | cut --delimiter=" " --fields 1) lines, "
echo "$(ls -sh $f | cut --delimiter=" " --fields 1)"
for f in "$DIR"/*.tex; do
echo -n " $(basename "$f"), "
echo -n "$(wc -l "$f" | cut --delimiter=" " --fields 1) lines, "
du -h "$f" | cut --fields 1
done

flamegraph -F 10000 -- ../target/release/tex-fmt $DIR/*.tex
flamegraph -F 10000 -- ../target/release/tex-fmt "$DIR"/*.tex
hyperfine --warmup 2 -n "tex-fmt" "../target/release/tex-fmt $DIR/*.tex"
7 changes: 5 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default: test clippy format
default: test clippy format shellcheck

all: default latex perf prof
all: default prof perf latex

alias t := test
alias l := latex
Expand All @@ -24,3 +24,6 @@ perf:

prof:
@cd extra && bash prof.sh

shellcheck:
@shellcheck extra/*.sh
16 changes: 15 additions & 1 deletion notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
** Tidy code
** Documentation
** Try wrapping spaceless lines
** Use "file" for file names and "text" for contents
* Features
** Aim for a single pass of wrapping and indenting
*** Three lists of strings
**** 1: lines of original file
**** 2: lines in processing queue
**** 3: lines of formatted file
*** Logic flow
**** If the queue is empty
***** Take a line from the original file and put it in the queue
**** Else
***** Indent the first line in the queue with the global indentation state
***** Keep the new indentation state in a temporary variable
***** If it then needs wrapping
****** Wrap it into several lines and push them all back onto the queue
***** Else
****** Move it from the queue to the output file
****** Update the global indentation state from the temporary variable
** Parsing with texlab
*** https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/syntax.md
*** https://github.com/latex-lsp/texlab/tree/master/crates/parser
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pkgs.mkShell {
texlive.combined.scheme-full
diff-so-fancy
poppler_utils
shellcheck
];
}
40 changes: 20 additions & 20 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use log::Level::{Info, Warn};
const MAX_PASS: usize = 10;

fn apply_passes(
text: &str,
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> String {
let mut new_file = apply_indent(file, filename, args, logs, Some(1));
let mut new_text = apply_indent(text, file, args, logs, Some(1));
let mut finished = false;
let mut pass = 2;

while !finished && needs_wrap(&new_file) && pass < MAX_PASS + 2 {
let old_file = new_file.clone();
new_file = wrap(&new_file, filename, logs, Some(pass), args);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, filename, args, logs, Some(pass));
while !finished && needs_wrap(&new_text) && pass < MAX_PASS + 2 {
let old_text = new_text.clone();
new_text = wrap(&new_text, file, logs, Some(pass), args);
new_text = remove_trailing_spaces(&new_text);
new_text = apply_indent(&new_text, file, args, logs, Some(pass));
pass += 1;
if new_file == old_file {
if new_text == old_text {
finished = true;
}
}
Expand All @@ -32,31 +32,31 @@ fn apply_passes(
logs,
Info,
None,
filename.to_string(),
file.to_string(),
None,
None,
"Passes completed.".to_string(),
);

// check indents return to zero
if new_file.lines().last().unwrap_or_default().starts_with(' ') {
if new_text.lines().last().unwrap_or_default().starts_with(' ') {
record_log(
logs,
Warn,
None,
filename.to_string(),
file.to_string(),
None,
None,
"Indent does not return to zero.".to_string(),
);
}

new_file
new_text
}

pub fn format_file(
text: &str,
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> String {
Expand All @@ -65,16 +65,16 @@ pub fn format_file(
logs,
Info,
None,
filename.to_string(),
file.to_string(),
None,
None,
"Begin formatting.".to_string(),
);
}
let mut new_file = remove_extra_newlines(file);
new_file = environments_new_line(&new_file, filename, args, logs);
new_file = remove_tabs(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_passes(&new_file, filename, args, logs);
new_file
let mut new_text = remove_extra_newlines(text);
new_text = environments_new_line(&new_text, file, args, logs);
new_text = remove_tabs(&new_text);
new_text = remove_trailing_spaces(&new_text);
new_text = apply_passes(&new_text, file, args, logs);
new_text
}
6 changes: 3 additions & 3 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get_ignore(
line: &str,
linum: usize,
ignore: Ignore,
filename: &str,
file: &str,
logs: &mut Vec<Log>,
pass: Option<usize>,
warn: bool,
Expand All @@ -41,7 +41,7 @@ pub fn get_ignore(
logs,
Warn,
pass,
filename.to_string(),
file.to_string(),
Some(linum),
Some(line.to_string()),
"Cannot begin ignore block:".to_string(),
Expand All @@ -55,7 +55,7 @@ pub fn get_ignore(
logs,
Warn,
pass,
filename.to_string(),
file.to_string(),
Some(linum),
Some(line.to_string()),
"No ignore block to end:".to_string(),
Expand Down
24 changes: 12 additions & 12 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
}

pub fn apply_indent(
text: &str,
file: &str,
filename: &str,
args: &Cli,
logs: &mut Vec<Log>,
pass: Option<usize>,
Expand All @@ -118,7 +118,7 @@ pub fn apply_indent(
logs,
Info,
pass,
filename.to_string(),
file.to_string(),
None,
None,
format!("Indent on pass {}.", pass.unwrap_or_default()),
Expand All @@ -128,11 +128,11 @@ pub fn apply_indent(
let mut indent = Indent::new();
let mut ignore = Ignore::new();
let mut leave = Leave::new();
let mut new_file = String::with_capacity(file.len());
let mut new_text = String::with_capacity(text.len());

for (linum, line) in file.lines().enumerate() {
ignore = get_ignore(line, linum, ignore, filename, logs, pass, true);
leave = get_leave(line, linum, leave, filename, logs, pass, true);
for (linum, line) in text.lines().enumerate() {
ignore = get_ignore(line, linum, ignore, file, logs, pass, true);
leave = get_leave(line, linum, leave, file, logs, pass, true);

if !leave.visual && !ignore.visual {
// calculate indent
Expand All @@ -144,7 +144,7 @@ pub fn apply_indent(
logs,
Trace,
pass,
filename.to_string(),
file.to_string(),
Some(linum),
Some(line.to_string()),
format!(
Expand All @@ -159,7 +159,7 @@ pub fn apply_indent(
logs,
Warn,
pass,
filename.to_string(),
file.to_string(),
Some(linum),
Some(line.to_string()),
"Indent is negative.".to_string(),
Expand All @@ -176,12 +176,12 @@ pub fn apply_indent(
new_line.insert(0, ' ');
}
}
new_file.push_str(&new_line);
new_text.push_str(&new_line);
} else {
new_file.push_str(line);
new_text.push_str(line);
}
new_file.push('\n');
new_text.push('\n');
}

new_file
new_text
}
Loading

0 comments on commit bed30e7

Please sign in to comment.