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:
parent
baeb7d27b8
commit
210fa4c443
3 changed files with 13 additions and 16 deletions
|
@ -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 boundary = DisplayPoint::new(point.row(), 0);
|
||||||
let mut column = 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)) {
|
for c in map.chars_at(DisplayPoint::new(point.row(), 0)) {
|
||||||
if column >= point.column() {
|
if column >= point.column() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let char_kind = char_kind(c);
|
let char_kind = char_kind(c);
|
||||||
if char_kind != prev_char_kind
|
if char_kind != prev_char_kind && char_kind != CharKind::Whitespace && c != '\n' {
|
||||||
&& char_kind != CharKind::Whitespace
|
|
||||||
&& char_kind != CharKind::Newline
|
|
||||||
{
|
|
||||||
*boundary.column_mut() = column;
|
*boundary.column_mut() = column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,10 +176,7 @@ pub fn next_word_end(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayP
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if prev_char_kind != char_kind
|
if prev_char_kind != char_kind && prev_char_kind != CharKind::Whitespace {
|
||||||
&& prev_char_kind != CharKind::Whitespace
|
|
||||||
&& prev_char_kind != CharKind::Newline
|
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -441,7 +435,7 @@ mod tests {
|
||||||
.select_font(family_id, &Default::default())
|
.select_font(family_id, &Default::default())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let font_size = 14.0;
|
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
|
let display_map = cx
|
||||||
.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, 1, 1, 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));
|
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||||
|
@ -502,5 +496,11 @@ mod tests {
|
||||||
surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
|
surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
|
||||||
DisplayPoint::new(1, 4)..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),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1407,7 +1407,7 @@ impl MultiBufferSnapshot {
|
||||||
);
|
);
|
||||||
|
|
||||||
for ch in prev_chars {
|
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();
|
start -= ch.len_utf8();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
@ -1415,7 +1415,7 @@ impl MultiBufferSnapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
for ch in next_chars {
|
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();
|
end += ch.len_utf8();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -271,7 +271,6 @@ pub(crate) struct DiagnosticEndpoint {
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
|
||||||
pub enum CharKind {
|
pub enum CharKind {
|
||||||
Newline,
|
|
||||||
Punctuation,
|
Punctuation,
|
||||||
Whitespace,
|
Whitespace,
|
||||||
Word,
|
Word,
|
||||||
|
@ -2259,9 +2258,7 @@ pub fn contiguous_ranges(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn char_kind(c: char) -> CharKind {
|
pub fn char_kind(c: char) -> CharKind {
|
||||||
if c == '\n' {
|
if c.is_whitespace() {
|
||||||
CharKind::Newline
|
|
||||||
} else if c.is_whitespace() {
|
|
||||||
CharKind::Whitespace
|
CharKind::Whitespace
|
||||||
} else if c.is_alphanumeric() || c == '_' {
|
} else if c.is_alphanumeric() || c == '_' {
|
||||||
CharKind::Word
|
CharKind::Word
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue