Skip to content

Commit

Permalink
Fixed bug with indenting and wrapping verbatim environments
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 8, 2024
1 parent 9ede3d8 commit b92034b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
6 changes: 3 additions & 3 deletions notes.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#+title: tex-fmt
* Tests
* Tasks
** Look for problem cases in other latex documents
** justfile
** Tidy code
* Features
** Implement new flags
** Ignore source lines
Expand All @@ -10,9 +12,7 @@
** Find todos
** Better file backup
* Bugs
** Do not wrap lines in verbatim environment
** Better errors including line numbers in source file
** Handle list items and comments properly
** Handle errors due to unwrappable lines
** Do not break line at \<space>
** Prefer to break line after punctuation
43 changes: 27 additions & 16 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,37 @@ pub fn get_indent(line: &str, prev_indent: Indent) -> Indent {
pub fn apply_indent(file: &str, debug: bool) -> String {
let mut indent = Indent::new();
let mut new_file = "".to_owned();
let mut verbatim_count = 0;

for (i, line) in file.lines().enumerate() {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
if !debug {
assert!(indent.actual >= 0, "line {}: {}", i, line);
assert!(indent.visual >= 0, "line {}: {}", i, line);
};

// apply indent
let mut new_line = line.trim_start().to_string();
if !new_line.is_empty() {
let n_spaces = indent.visual * TAB;
let spaces: String = (0..n_spaces).map(|_| " ").collect();
new_line.insert_str(0, &spaces);
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if verbatim_count == 0 {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
if !debug {
assert!(indent.actual >= 0, "line {}: {}", i, line);
assert!(indent.visual >= 0, "line {}: {}", i, line);
};

// apply indent
let mut new_line = line.trim_start().to_string();
if !new_line.is_empty() {
let n_spaces = indent.visual * TAB;
let spaces: String = (0..n_spaces).map(|_| " ").collect();
new_line.insert_str(0, &spaces);
}
new_file.push_str(&new_line);
} else {
new_file.push_str(line);
}
new_file.push_str(&new_line);
new_file.push('\n');
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
}

// check indents return to zero
Expand Down
4 changes: 4 additions & 0 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ lazy_static! {
Regex::new(r".*\\begin\{document\}.*").unwrap();
pub static ref RE_DOCUMENT_END: Regex =
Regex::new(r".*\\end\{document\}.*").unwrap();
pub static ref RE_VERBATIM_BEGIN: Regex =
Regex::new(r".*\\begin\{verbatim\}.*").unwrap();
pub static ref RE_VERBATIM_END: Regex =
Regex::new(r".*\\end\{verbatim\}.*").unwrap();
pub static ref RE_ENV_BEGIN: Regex =
Regex::new(r".*\\begin\{[a-zA-Z0-9\*]*\}.*").unwrap();
pub static ref RE_ENV_END: Regex =
Expand Down
10 changes: 9 additions & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::comments::*;
use crate::regexes::*;

const WRAP: usize = 80;

Expand Down Expand Up @@ -63,14 +64,21 @@ pub fn wrap_line(line: &str) -> String {

pub fn wrap(file: &str) -> String {
let mut new_file = "".to_string();
let mut verbatim_count = 0;
for line in file.lines() {
if line_needs_wrap(line) {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if line_needs_wrap(line) && verbatim_count == 0 {
let new_line = wrap_line(line);
new_file.push_str(&new_line);
} else {
new_file.push_str(line);
}
new_file.push('\n');
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
}
new_file
}
Expand Down
2 changes: 2 additions & 0 deletions tests/verbatim_in.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
code code code code code code code code code code code code code code code code code code
code code code code code code code code code code code code code code code code code code
\end{verbatim}

\end{document}
5 changes: 3 additions & 2 deletions tests/verbatim_out.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

\begin{verbatim}
code code code code code code code code code code code code code code code
code code code
code code code code code code code code code code code code code code code code code code
code code code code code code code code code code code code code code code code code code
\end{verbatim}

Expand Down

0 comments on commit b92034b

Please sign in to comment.