diff --git a/src/cli.rs b/src/cli.rs index 252de45..a67e1b4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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; } diff --git a/src/format.rs b/src/format.rs index e7caeb9..b9e6f04 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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) = @@ -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, @@ -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(), @@ -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, @@ -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); diff --git a/src/indent.rs b/src/indent.rs index 0800af6..472db94 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -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, @@ -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); @@ -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, @@ -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 = diff --git a/src/regexes.rs b/src/regexes.rs index 53b1905..84a88a3 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -51,17 +51,22 @@ lazy_static! { Regex::new(r"(?P\S.*?)(?P\\end\{)").unwrap(); pub static ref RE_ITEM_SHARED_LINE: Regex = Regex::new(r"(?P\S.*?)(?P\\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\S.*?) # : captures any number of characters starting with a non-whitespace - # character until the start of the next group; - (?P( # : 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\S.*?) # : captures any number of characters starting + # with a non-whitespace character until the start + # of the next group; + (?P( # : 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(); } diff --git a/src/subs.rs b/src/subs.rs index 01a337f..8647ec5 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -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 } }