Skip to content

Commit

Permalink
Handle tabs inside words in split_words
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoohey31 committed Nov 2, 2022
1 parent 484c29b commit 603f4cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions src/word_splitters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl WordSplitter {
pub fn split_words<'a, I>(
words: I,
word_splitter: &'a WordSplitter,
tab_width: u8,
) -> impl Iterator<Item = Word<'a>>
where
I: IntoIterator<Item = Word<'a>>,
Expand All @@ -193,10 +194,7 @@ where
let need_hyphen = !word[..idx].ends_with('-');
let w = Word {
word: &word.word[prev..idx],
// word[prev..idx] is a subset of the original word.word,
// which is trimmed of whitespace in Word::from, so we
// don't need an accurate tab_width.
width: display_width(&word[prev..idx], 0),
width: display_width(&word[prev..idx], tab_width),
whitespace: "",
penalty: if need_hyphen { "-" } else { "" },
};
Expand Down Expand Up @@ -234,37 +232,44 @@ mod tests {

#[test]
fn split_words_no_words() {
assert_iter_eq!(split_words(vec![], &WordSplitter::HyphenSplitter), vec![]);
assert_iter_eq!(
split_words(vec![], &WordSplitter::HyphenSplitter, 0),
vec![]
);
}

#[test]
fn split_words_empty_word() {
assert_iter_eq!(
split_words(vec![Word::from(" ")], &WordSplitter::HyphenSplitter),
split_words(vec![Word::from(" ")], &WordSplitter::HyphenSplitter, 0),
vec![Word::from(" ")]
);
}

#[test]
fn split_words_single_word() {
assert_iter_eq!(
split_words(vec![Word::from("foobar")], &WordSplitter::HyphenSplitter),
split_words(vec![Word::from("foobar")], &WordSplitter::HyphenSplitter, 0),
vec![Word::from("foobar")]
);
}

#[test]
fn split_words_hyphen_splitter() {
assert_iter_eq!(
split_words(vec![Word::from("foo-bar")], &WordSplitter::HyphenSplitter),
split_words(
vec![Word::from("foo-bar")],
&WordSplitter::HyphenSplitter,
0
),
vec![Word::from("foo-"), Word::from("bar")]
);
}

#[test]
fn split_words_no_hyphenation() {
assert_iter_eq!(
split_words(vec![Word::from("foo-bar")], &WordSplitter::NoHyphenation),
split_words(vec![Word::from("foo-bar")], &WordSplitter::NoHyphenation, 0),
vec![Word::from("foo-bar")]
);
}
Expand All @@ -276,7 +281,8 @@ mod tests {
assert_iter_eq!(
split_words(
vec![Word::from("foobar")].into_iter(),
&WordSplitter::Custom(fixed_split_point)
&WordSplitter::Custom(fixed_split_point),
0,
),
vec![
Word {
Expand All @@ -297,7 +303,8 @@ mod tests {
assert_iter_eq!(
split_words(
vec![Word::from("fo-bar")].into_iter(),
&WordSplitter::Custom(fixed_split_point)
&WordSplitter::Custom(fixed_split_point),
0
),
vec![
Word {
Expand Down
2 changes: 1 addition & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub(crate) fn wrap_single_line_slow_path<'a>(
let line_widths = [initial_width, subsequent_width];

let words = options.word_separator.find_words(line);
let split_words = split_words(words, &options.word_splitter);
let split_words = split_words(words, &options.word_splitter, options.tab_width);
let broken_words = if options.break_words {
let mut broken_words = break_words(split_words, line_widths[1], options.tab_width);
if !options.initial_indent.is_empty() {
Expand Down

0 comments on commit 603f4cf

Please sign in to comment.