Skip to content

Commit

Permalink
Fix long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Oct 15, 2024
1 parent f73426c commit f842df1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Cli {
logs,
Error,
"",
"No files specified. Either provide filenames or provide --stdin.",
"No files specified. Provide filenames or pass --stdin.",
);
exit_code = 1;
}
Expand Down
16 changes: 10 additions & 6 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub fn format_file(
file,
&pattern,
) {
// Check if the line should be split because of a pattern that should begin on a new line.
// Check if the line should be split because of a pattern
// that should begin on a new line.
if needs_env_new_line(&line, &temp_state, &pattern) {
// Split the line into two ...
let (this_line, next_line) =
Expand All @@ -62,7 +63,8 @@ pub fn format_file(
line = this_line.to_string();
}

// Calculate the indent based on the current state and the patterns in the line.
// Calculate the indent based on the current state
// and the patterns in the line.
let indent = calculate_indent(
&line,
&mut temp_state,
Expand All @@ -75,7 +77,8 @@ pub fn format_file(
let indent_length = usize::try_from(indent.visual * args.tab)
.expect("Visual indent is non-negative.");

// Wrap the line before applying the indent, and loop back if the line needed wrapping.
// Wrap the line before applying the indent, and loop back
// if the line needed wrapping.
if needs_wrap(line.trim_start(), indent_length, args) {
let wrapped_lines = apply_wrap(
line.trim_start(),
Expand Down Expand Up @@ -122,8 +125,8 @@ pub fn format_file(
new_text
}

/// Sets the `ignore` and `verbatim` flags in the given [State] based on `line` and returns whether `line` should be
/// ignored by formatting.
/// Sets the `ignore` and `verbatim` flags in the given [State] based on
/// `line` and returns whether `line` should be ignored by formatting.
fn set_ignore_and_report(
line: &str,
temp_state: &mut State,
Expand All @@ -138,7 +141,8 @@ fn set_ignore_and_report(
temp_state.verbatim.visual || temp_state.ignore.visual
}

/// Cleans the given text by removing extra line breaks and trailing spaces, and tabs if they shouldn't be used.
/// Cleans the given text by removing extra line breaks and trailing spaces,
/// and also tabs if they shouldn't be used.
fn clean_text(text: &str, args: &Cli) -> String {
let mut text = remove_extra_newlines(text);

Expand Down
19 changes: 12 additions & 7 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ fn get_indent(line: &str, prev_indent: &Indent, pattern: &Pattern) -> Indent {
Indent { actual, visual }
}

/// Calculates the indent for `line` based on its contents. This functions saves the calculated [Indent], which might be
/// negative, to the given [State], and then ensures that the returned [Indent] is non-negative.
/// Calculates the indent for `line` based on its contents.
/// This functions saves the calculated [Indent], which might be
/// negative, to the given [State], and then ensures that the returned
/// [Indent] is non-negative.
pub fn calculate_indent(
line: &str,
state: &mut State,
Expand All @@ -117,8 +119,8 @@ pub fn calculate_indent(
args: &Cli,
pattern: &Pattern,
) -> Indent {
// Calculate the new indent by first removing the comment from the line (if there is one) to ignore diffs from
// characters in there.
// 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 line_strip = remove_comment(line, comment_index);
let mut indent = get_indent(line_strip, &state.indent, pattern);
Expand All @@ -139,11 +141,13 @@ pub fn calculate_indent(
);
}

// Save the indent to the state. Note, this indent might be negative; it is saved without correction so that this is
// Save the indent to the state. Note, this indent might be negative;
// it is saved without correction so that this is
// not forgotten for the next iterations.
state.indent = indent.clone();

// However, we can't negatively indent a line. So we log the negative indent and reset the values to 0.
// However, we can't negatively indent a line.
// So we log the negative indent and reset the values to 0.
if (indent.visual < 0) || (indent.actual < 0) {
record_line_log(
logs,
Expand Down Expand Up @@ -174,7 +178,8 @@ pub fn apply_indent(
// If the line is now empty, return a new empty String
if trimmed_line.is_empty() {
String::new()
// Otherwise, allocate enough memory to fit line with the added indentation and insert the appropriate string slices
// Otherwise, allocate enough memory to fit line with the added
// indentation and insert the appropriate string slices
} else {
let n_indent_chars = usize::try_from(indent.visual * args.tab).unwrap();
let mut new_line =
Expand Down
25 changes: 15 additions & 10 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ lazy_static! {
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\end\{)").unwrap();
pub static ref RE_ITEM_SHARED_LINE: Regex =
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\item)").unwrap();
// Regex that matches any splitting command with non-whitespace characters before it and catches the previous text
// in a group called "prev" and captures the command itself and the remaining text in a group called "env".
// Regex that matches any splitting command with non-whitespace
// characters before it and catches the previous text in a group called
// "prev" and captures the command itself and the remaining text
// in a group called "env".
pub static ref RE_ENV_ITEM_SHARED_LINE: Regex = Regex::new(
r"(?x) # Enable extended mode
(?P<prev>\S.*?) # <prev>: captures any number of characters starting with a non-whitespace
# character until the start of the next group;
(?P<env>( # <env>: captures any LaTeX command before which the line should be split
\\begin\{ # start of environments
|\\end\{ # end of environments
|\\item ) # list items (note the space before the closing bracket)
.*)" // and any characters that follow the command
r"(?x) # Enable extended mode
(?P<prev>\S.*?) # <prev>: captures any number of characters starting
# with a non-whitespace character until the start
# of the next group;
(?P<env>( # <env>: captures any LaTeX command before which the
# line should be split
\\begin\{ # start of environments
|\\end\{ # end of environments
|\\item ) # list items (note the space before the closing bracket)
.*) # and any characters that follow the command
"
)
.unwrap();
}
13 changes: 7 additions & 6 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,27 @@ pub fn needs_env_new_line(

// If we're not ignoring and we've matched an environment ...
if not_ignored_and_contains_env {
// ... return `true` if the comment index is `None` (which implies the split point must be in text), otherwise
// ... 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| {
if RE_ENV_ITEM_SHARED_LINE
.captures(line)
.unwrap() // Doesn't panic because we've matched split point.
.unwrap() // Matched split point so no panic.
.get(2)
.unwrap() // Doesn't panic because the regex has 4 groups so index 2 is in bounds.
.unwrap() // Regex has 4 groups so index 2 is in bounds.
.start()
> comment_index
{
// If the split point is past the comment index, then we don't split the line,
// If split point is past the comment index, don't split.
false
} else {
// otherwise, the split point is before the comment and we do split the line.
// Otherwise, split point is before comment and we do split.
true
}
})
} else {
// If we're ignoring or we didn't match an environment, we don't need a new line.
// If ignoring or didn't match an environment, don't need a new line.
false
}
}
Expand Down

0 comments on commit f842df1

Please sign in to comment.