-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'one-pass' into develop
- Loading branch information
Showing
27 changed files
with
9,094 additions
and
8,550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,13 @@ | ||
#!/usr/bin/env bash | ||
echo "Making flamegraph profile" | ||
DIR="$(mktemp -d)" | ||
cp "../tests/source/phd_dissertation.tex" "$DIR" | ||
cp -r ../tests/* "$DIR" | ||
cargo build --release | ||
|
||
echo "Writing large test file" | ||
for _ in {1..5}; do | ||
cat "$DIR/phd_dissertation.tex" >> "$DIR/large.tex" | ||
printf "\n\n\n" >> "$DIR/large.tex" | ||
done | ||
echo | ||
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 | ||
|
||
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, " | ||
du -h "$f" | cut --fields 1 | ||
done | ||
|
||
flamegraph -F 10000 -- ../target/release/tex-fmt "$DIR"/*.tex | ||
hyperfine --warmup 2 -n "tex-fmt" "../target/release/tex-fmt $DIR/*.tex" | ||
flamegraph -F 10000 -- ../target/release/tex-fmt "$DIR/source/"* "$DIR/target/"* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ build: | |
test: | ||
@cargo test -r | ||
|
||
testignored: | ||
@cargo test -r -- --ignored | ||
|
||
clippy: | ||
@cargo clippy -r | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1 @@ | ||
#+title: tex-fmt | ||
* Tasks | ||
** Check other latex files | ||
** Tidy code | ||
** Documentation | ||
** Try wrapping spaceless lines | ||
* 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 | ||
** Parsing with pest | ||
** Parsing with nom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ pkgs.mkShell { | |
pillow | ||
]); | ||
in [ | ||
pkgs.bacon | ||
pkgs.cacert | ||
pkgs.cargo-flamegraph | ||
pkgs.cargo-edit | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,89 @@ | ||
use crate::ignore::*; | ||
use crate::indent::*; | ||
use crate::leave::*; | ||
use crate::logging::*; | ||
use crate::parse::*; | ||
use crate::subs::*; | ||
use crate::wrap::*; | ||
use crate::Cli; | ||
use log::Level::{Info, Warn}; | ||
|
||
const MAX_PASS: usize = 10; | ||
|
||
fn apply_passes( | ||
pub fn format_file( | ||
text: &str, | ||
file: &str, | ||
args: &Cli, | ||
logs: &mut Vec<Log>, | ||
) -> String { | ||
let mut new_text = apply_indent(text, file, args, logs, Some(1)); | ||
let mut finished = false; | ||
let mut pass = 2; | ||
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_trailing_spaces(&old_text); | ||
|
||
let mut state = State::new(); | ||
|
||
let mut old_lines = old_text.lines().enumerate(); | ||
|
||
let mut queue: Vec<(usize, String)> = vec![]; | ||
let mut new_text = String::with_capacity(text.len()); | ||
|
||
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_text == old_text { | ||
finished = true; | ||
loop { | ||
if let Some((linum_old, mut line)) = queue.pop() { | ||
let temp_state: State; | ||
(line, temp_state) = | ||
apply_indent(&line, linum_old, &state, logs, file, args); | ||
if needs_wrap(&line, &temp_state, args) { | ||
let wrapped_lines = | ||
apply_wrap(&line, &temp_state, file, args, logs); | ||
if wrapped_lines.is_some() { | ||
queue.push((linum_old, wrapped_lines.clone().unwrap().1)); | ||
queue.push((linum_old, wrapped_lines.clone().unwrap().0)); | ||
} else { | ||
new_text.push_str(&line); | ||
new_text.push('\n'); | ||
state.linum_new += 1; | ||
}; | ||
} else { | ||
state = temp_state; | ||
new_text.push_str(&line); | ||
new_text.push('\n'); | ||
} | ||
} else if let Some((linum_old, line)) = old_lines.next() { | ||
queue.push((linum_old, line.to_string())); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
record_log( | ||
logs, | ||
Info, | ||
None, | ||
file.to_string(), | ||
None, | ||
None, | ||
"Passes completed.".to_string(), | ||
); | ||
|
||
// check indents return to zero | ||
if new_text.lines().last().unwrap_or_default().starts_with(' ') { | ||
record_log( | ||
logs, | ||
Warn, | ||
None, | ||
file.to_string(), | ||
None, | ||
None, | ||
"Indent does not return to zero.".to_string(), | ||
); | ||
if !indents_return_to_zero(&new_text) { | ||
record_file_log(logs, Warn, file, "Indent does not return to zero."); | ||
} | ||
|
||
record_file_log(logs, Info, file, "Formatting complete."); | ||
|
||
new_text | ||
} | ||
|
||
pub fn format_file( | ||
text: &str, | ||
file: &str, | ||
args: &Cli, | ||
logs: &mut Vec<Log>, | ||
) -> String { | ||
if args.verbose { | ||
record_log( | ||
logs, | ||
Info, | ||
None, | ||
file.to_string(), | ||
None, | ||
None, | ||
"Begin formatting.".to_string(), | ||
); | ||
#[derive(Clone, Debug)] | ||
pub struct State { | ||
pub linum_old: usize, | ||
pub linum_new: usize, | ||
pub ignore: Ignore, | ||
pub indent: Indent, | ||
pub leave: Leave, | ||
} | ||
|
||
impl State { | ||
pub fn new() -> Self { | ||
State { | ||
linum_old: 0, | ||
linum_new: 0, | ||
ignore: Ignore::new(), | ||
indent: Indent::new(), | ||
leave: Leave::new(), | ||
} | ||
} | ||
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 | ||
} | ||
|
||
fn indents_return_to_zero(text: &str) -> bool { | ||
!text.lines().last().unwrap_or_default().starts_with(' ') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.