Fix word splitting with hyphens

Fixes #908
split_more_thoroughly
Wilfred Hughes 2025-10-23 09:50:06 +07:00
parent 648fe733ba
commit 84e9a9e673
2 changed files with 19 additions and 1 deletions

@ -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

@ -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";