Better white-space-favoring chunk-splitting behavior.

This commit is contained in:
Nathan Vegdahl 2020-02-01 23:06:15 +09:00
parent db0ce6a4f6
commit 4e1bdc412c

View File

@ -195,26 +195,22 @@ pub trait LineFormatter {
// Finds the best break at or before the given char index, bounded by // Finds the best break at or before the given char index, bounded by
// the given `lower_limit`. // the given `lower_limit`.
pub fn find_good_break(slice: &RopeSlice, lower_limit: usize, char_idx: usize) -> usize { pub fn find_good_break(slice: &RopeSlice, lower_limit: usize, char_idx: usize) -> usize {
let char_idx = char_idx.min(slice.len_chars()); const WS_CHARS: &[char] = &[' ', ' ', '\t'];
let lower_limit = lower_limit.min(slice.len_chars());
let slice_len = slice.len_chars();
let char_idx = char_idx.min(slice_len);
let lower_limit = lower_limit.min(slice_len);
// Find a whitespace break, if any. // Find a whitespace break, if any.
let mut i = char_idx; let mut i = char_idx;
while i > lower_limit { while i > lower_limit {
match slice.char(i - 1) { if (i == slice_len || !WS_CHARS.contains(&slice.char(i)))
// Previous char is whitespace. && WS_CHARS.contains(&slice.char(i - 1))
'\u{0009}' | '\u{0020}' | '\u{00A0}' | '\u{2000}' | '\u{2001}' | '\u{2002}' {
| '\u{2003}' | '\u{2004}' | '\u{2005}' | '\u{2006}' | '\u{2007}' | '\u{2008}'
| '\u{2009}' | '\u{200A}' | '\u{202F}' | '\u{205F}' | '\u{3000}' => {
return i; return i;
} }
// Previous char is not whitespace.
_ => {
i -= 1; i -= 1;
} }
}
}
// Otherwise, at least try to find a grapheme break. // Otherwise, at least try to find a grapheme break.
let mut i = char_idx; let mut i = char_idx;