Skip to content

Commit

Permalink
Improved handling of ignored lines
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 16, 2024
1 parent a38c999 commit 215f72e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
57 changes: 55 additions & 2 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
pub fn is_ignored(line: &str) -> bool {
line.ends_with("%tex-fmt-ignore-line")
pub struct Ignore {
skip: bool,
block: bool,
}

impl Ignore {
pub fn new() -> Self {
Ignore {
skip: false,
block: false,
}
}
}

pub fn get_ignore(line: &str, prev_ignore: Ignore) -> Ignore {
let skip = contains_ignore_skip(line);
let start = contains_ignore_start(line);
let end = contains_ignore_end(line);
let mut block = prev_ignore.block;

if !prev_ignore.block {
// not currently in ignore block
if start {
block = true
}
if end {
// TODO ERROR
}
} else {
// currently in ignore block
if start {
// TODO ERROR
}
if end {
block = false
}
}

Ignore { skip, block }
}

pub fn is_ignored(ignore: &Ignore) -> bool {
ignore.skip || ignore.block
}

fn contains_ignore_skip(line: &str) -> bool {
line.ends_with("% tex-fmt: skip")
}

fn contains_ignore_start(line: &str) -> bool {
line.ends_with("% tex-fmt: off")
}

fn contains_ignore_end(line: &str) -> bool {
line.ends_with("% tex-fmt: on")
}
4 changes: 3 additions & 1 deletion src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
pub fn apply_indent(file: &str, args: &Cli) -> (String, Vec<(usize, Indent)>) {
log::info!("Indenting file");
let mut indent = Indent::new();
let mut ignore = Ignore::new();
let mut new_file = String::with_capacity(file.len());
let mut verbatim_count = 0;
let mut indent_errs = vec![];
Expand All @@ -113,7 +114,8 @@ pub fn apply_indent(file: &str, args: &Cli) -> (String, Vec<(usize, Indent)>) {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if verbatim_count == 0 && !is_ignored(line) {
ignore = get_ignore(line, ignore);
if verbatim_count == 0 && !is_ignored(&ignore) {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
Expand Down
4 changes: 3 additions & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ pub fn wrap(file: &str) -> String {
let mut new_file = "".to_string();
let mut new_line: String;
let mut verbatim_count = 0;
let mut ignore = Ignore::new();
for line in file.lines() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if line_needs_wrap(line) && verbatim_count == 0 && !is_ignored(line) {
ignore = get_ignore(line, ignore);
if line_needs_wrap(line) && verbatim_count == 0 && !is_ignored(&ignore) {
new_line = wrap_line(line);
new_file.push_str(&new_line);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ignore_in.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

\begin{document}

Lines which end with the ignore keyword are not indented or wrapped %tex-fmt-ignore-line
Lines which end with the ignore keyword are not indented or wrapped % tex-fmt: skip

\end{document}
2 changes: 1 addition & 1 deletion tests/ignore_out.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

\begin{document}

Lines which end with the ignore keyword are not indented or wrapped %tex-fmt-ignore-line
Lines which end with the ignore keyword are not indented or wrapped % tex-fmt: skip

\end{document}

0 comments on commit 215f72e

Please sign in to comment.