Skip to content

Commit

Permalink
Add comment checking to patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Dec 24, 2024
1 parent 910db8f commit 47902dd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Utilities for finding, extracting and removing LaTeX comments
use crate::format::*;

/// Find the location where a comment begins in a line
pub fn find_comment_index(line: &str) -> Option<usize> {
pub fn find_comment_index(line: &str, pattern: &Pattern) -> Option<usize> {
// often there is no '%' so check this first
if line.contains('%') {
if pattern.contains_comment {
let mut prev_c = ' ';
for (i, c) in line.char_indices() {
if c == '%' && prev_c != '\\' {
Expand Down
7 changes: 6 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn format_file(
file,
args,
logs,
&pattern,
);
if let Some([this_line, next_line_start, next_line]) =
wrapped_lines
Expand Down Expand Up @@ -207,25 +208,29 @@ pub struct Pattern {
pub contains_item: bool,
/// Whether a splitting pattern is present
pub contains_splitting: bool,
/// Whether a comment is present
pub contains_comment: bool,
}

impl Pattern {
/// Check if a string contains patterns
pub fn new(s: &str) -> Self {
// If splitting does not match, no patterns are present
// If splitting does not match, most patterns are not present
if RE_SPLITTING.is_match(s) {
Self {
contains_env_begin: s.contains(ENV_BEGIN),
contains_env_end: s.contains(ENV_END),
contains_item: s.contains(ITEM),
contains_splitting: true,
contains_comment: s.contains('%'),
}
} else {
Self {
contains_env_begin: false,
contains_env_end: false,
contains_item: false,
contains_splitting: false,
contains_comment: s.contains('%'),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn calculate_indent(
) -> Indent {
// Calculate the new indent by first removing the comment from the line
// (if there is one) to ignore diffs from characters in there.
let comment_index = find_comment_index(line);
let comment_index = find_comment_index(line, pattern);
let line_strip = remove_comment(line, comment_index);
let mut indent = get_indent(line_strip, &state.indent, pattern, state);

Expand Down
2 changes: 1 addition & 1 deletion src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn needs_split(line: &str, pattern: &Pattern) -> bool {
// ... return `true` if the comment index is `None`
// (which implies the split point must be in text), otherwise
// compare the index of the comment with the split point.
find_comment_index(line).map_or(true, |comment_index| {
find_comment_index(line, pattern).map_or(true, |comment_index| {
if RE_SPLITTING_SHARED_LINE_CAPTURE
.captures(line)
.unwrap() // Matched split point so no panic.
Expand Down
3 changes: 2 additions & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn apply_wrap<'a>(
file: &str,
args: &Args,
logs: &mut Vec<Log>,
pattern: &Pattern,
) -> Option<[&'a str; 3]> {
if args.verbosity == LevelFilter::Trace {
record_line_log(
Expand All @@ -70,7 +71,7 @@ pub fn apply_wrap<'a>(
);
}
let wrap_point = find_wrap_point(line, indent_length, args);
let comment_index = find_comment_index(line);
let comment_index = find_comment_index(line, pattern);

match wrap_point {
Some(p) if p <= args.wraplen.into() => {}
Expand Down

0 comments on commit 47902dd

Please sign in to comment.