diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-12-05 01:06:31 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-05 06:06:31 +0000 |
commit | 73571485466055335401990d8c47b8621f3f4f7e (patch) | |
tree | 788302cd3477653554110bd03aa15c24c1b53f20 | |
parent | beb700f88974420d36cad494b1fac80de3e01093 (diff) |
Fix whitespace errors under keep-plus-minus-markers (#425)
* Add failing test of added empty line with keep-plus-minus-markers
* Fix whitespace error flagging with keep-plus-minus-markers
Fixes #388
* Possibly more efficient implementation
* Fix clippy
* Fix clippy II
* Increase tarpaulin timeout
* Increase tarpaulin timeout
* Add failing test of line with one non-whitespace character
* Fix test of line with one non-whitespace character
* Revert tarpaulin changes
-rw-r--r-- | src/paint.rs | 24 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 42 |
2 files changed, 57 insertions, 9 deletions
diff --git a/src/paint.rs b/src/paint.rs index 7bb19f06..27582194 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -1,5 +1,3 @@ -use lazy_static::lazy_static; -use regex::Regex; use std::io::Write; use itertools::Itertools; @@ -586,18 +584,26 @@ fn style_sections_contain_more_than_one_style(sections: &[(Style, &str)]) -> boo } } -lazy_static! { - static ref NON_WHITESPACE_REGEX: Regex = Regex::new(r"\S").unwrap(); -} - /// True iff the line represented by `sections` constitutes a whitespace error. +// Note that a space is always present as the first character in the line (it was put there as a +// replacement for the leading +/- marker; see paint::prepare()). A line is a whitespace error iff, +// beyond the initial space character, (a) there are more characters and (b) they are all +// whitespace characters. // TODO: Git recognizes blank lines at end of file (blank-at-eof) as a whitespace error but delta // does not yet. // https://git-scm.com/docs/git-config#Documentation/git-config.txt-corewhitespace fn is_whitespace_error(sections: &[(Style, &str)]) -> bool { - !sections - .iter() - .any(|(_, s)| NON_WHITESPACE_REGEX.is_match(s)) + let mut any_chars = false; + for c in sections.iter().flat_map(|(_, s)| s.chars()).skip(1) { + if c == '\n' { + return any_chars; + } else if c != ' ' && c != '\t' { + return false; + } else { + any_chars = true; + } + } + false } mod superimpose_style_sections { diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index 4fd2184f..6668acce 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1303,6 +1303,32 @@ impl<'a> Alignment<'a> { │ } #[test] + fn test_added_empty_line_is_not_whitespace_error() { + let plus_style = "bold yellow red ul"; + let config = integration_test_utils::make_config_from_args(&[ + "--light", + "--keep-plus-minus-markers", + "--plus-style", + plus_style, + ]); + let output = integration_test_utils::run_delta(DIFF_WITH_ADDED_EMPTY_LINE, &config); + ansi_test_utils::assert_line_has_style(&output, 6, "", plus_style, &config) + } + + #[test] + fn test_single_character_line_is_not_whitespace_error() { + let plus_style = "bold yellow red ul"; + let config = integration_test_utils::make_config_from_args(&[ + "--light", + "--keep-plus-minus-markers", + "--plus-style", + plus_style, + ]); + let output = integration_test_utils::run_delta(DIFF_WITH_SINGLE_CHARACTER_LINE, &config); + ansi_test_utils::assert_line_has_style(&output, 12, "+}", plus_style, &config) + } + + #[test] fn test_color_only() { let config = integration_test_utils::make_config_from_args(&["--color-only"]); let output = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, &config); @@ -1850,6 +1876,22 @@ index e69de29..8b13789 100644 + "; + const DIFF_WITH_SINGLE_CHARACTER_LINE: &str = r" +diff --git a/Person.java b/Person.java +new file mode 100644 +index 0000000..c6c830c +--- /dev/null ++++ b/Person.java +@@ -0,0 +1,7 @@ ++import lombok.Data; ++ ++@Data ++public class Person { ++ private Long id; ++ private String name; ++} +"; + const DIFF_WITH_WHITESPACE_ERROR: &str = r" diff --git c/a i/a new file mode 100644 |