Remove CharKind::Newline

This is just a character, and so it seems clearer to refer to it specifically when we want to know if a character is a newline. There was only one case where we relied on Newline being different from Whitespace, and we special-cased that instance. Changing this actually makes us match the behavior of VS Code when double-clicking runs of multiple newlines.

/cc @as-cii

Co-Authored-By: Keith Simmons <keith@the-simmons.net>
This commit is contained in:
Nathan Sobo 2022-03-21 14:14:55 -06:00
parent baeb7d27b8
commit 210fa4c443
3 changed files with 13 additions and 16 deletions

View file

@ -151,17 +151,14 @@ pub fn previous_word_start(map: &DisplaySnapshot, mut point: DisplayPoint) -> Di
let mut boundary = DisplayPoint::new(point.row(), 0);
let mut column = 0;
let mut prev_char_kind = CharKind::Newline;
let mut prev_char_kind = CharKind::Whitespace;
for c in map.chars_at(DisplayPoint::new(point.row(), 0)) {
if column >= point.column() {
break;
}
let char_kind = char_kind(c);
if char_kind != prev_char_kind
&& char_kind != CharKind::Whitespace
&& char_kind != CharKind::Newline
{
if char_kind != prev_char_kind && char_kind != CharKind::Whitespace && c != '\n' {
*boundary.column_mut() = column;
}
@ -179,10 +176,7 @@ pub fn next_word_end(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayP
if c == '\n' {
break;
}
if prev_char_kind != char_kind
&& prev_char_kind != CharKind::Whitespace
&& prev_char_kind != CharKind::Newline
{
if prev_char_kind != char_kind && prev_char_kind != CharKind::Whitespace {
break;
}
}
@ -441,7 +435,7 @@ mod tests {
.select_font(family_id, &Default::default())
.unwrap();
let font_size = 14.0;
let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit", cx);
let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit\n\n\n\n", cx);
let display_map = cx
.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, 1, 1, cx));
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
@ -502,5 +496,11 @@ mod tests {
surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7),
);
// Don't consider runs of multiple newlines to be a "word"
assert_eq!(
surrounding_word(&snapshot, DisplayPoint::new(3, 0)),
DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0),
);
}
}

View file

@ -1407,7 +1407,7 @@ impl MultiBufferSnapshot {
);
for ch in prev_chars {
if Some(char_kind(ch)) == word_kind {
if Some(char_kind(ch)) == word_kind && ch != '\n' {
start -= ch.len_utf8();
} else {
break;
@ -1415,7 +1415,7 @@ impl MultiBufferSnapshot {
}
for ch in next_chars {
if Some(char_kind(ch)) == word_kind {
if Some(char_kind(ch)) == word_kind && ch != '\n' {
end += ch.len_utf8();
} else {
break;

View file

@ -271,7 +271,6 @@ pub(crate) struct DiagnosticEndpoint {
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
pub enum CharKind {
Newline,
Punctuation,
Whitespace,
Word,
@ -2259,9 +2258,7 @@ pub fn contiguous_ranges(
}
pub fn char_kind(c: char) -> CharKind {
if c == '\n' {
CharKind::Newline
} else if c.is_whitespace() {
if c.is_whitespace() {
CharKind::Whitespace
} else if c.is_alphanumeric() || c == '_' {
CharKind::Word