diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dd435c1c..9fe47899b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and R parsers. Improved handling of variable names `$foo` in shell scripts. +### Diffing + +Improved subword highlighting for words with hyphens. + ### Display Difftastic is now smarter about calculating the display width for diff --git a/src/words.rs b/src/words.rs index 6c1bea6f5..f108a6202 100644 --- a/src/words.rs +++ b/src/words.rs @@ -47,7 +47,7 @@ pub(crate) fn split_words_and_numbers(s: &str) -> Vec<&str> { for (idx, c) in s.char_indices() { match word_start { Some((start, start_c)) => { - if c.is_alphanumeric() || c == '_' { + if c.is_alphanumeric() || c == '-' || c == '_' { // Word character, add to the current word if it's // not a number. if c.is_ascii_digit() == start_c.is_ascii_digit() { @@ -93,6 +93,13 @@ mod tests { assert_eq!(res, vec!["example", ".", "com"]) } + #[test] + fn test_split_words_hyphens() { + let s = "foo -bar-baz-"; + let res = split_words(s); + assert_eq!(res, vec!["foo", " ", "-bar-baz-"]) + } + #[test] fn test_split_words_punctuation() { let s = "example.."; @@ -142,6 +149,13 @@ mod tests { assert_eq!(res, vec!["a", "123", "b"]) } + #[test] + fn test_split_words_and_numbers_hyphens() { + let s = "a-b -c-"; + let res = split_words_and_numbers(s); + assert_eq!(res, vec!["a-b", " ", "-c-"]) + } + #[test] fn test_split_words_and_numbers_spaces() { let s = "foo bar";