Files
oak-gpui/crates/vim/src/object.rs
T

463 lines
15 KiB
Rust

use std::ops::Range;
use editor::{char_kind, display_map::DisplaySnapshot, movement, Bias, CharKind, DisplayPoint};
use gpui::{actions, impl_actions, MutableAppContext};
use language::Selection;
use serde::Deserialize;
use workspace::Workspace;
use crate::{motion, normal::normal_object, state::Mode, visual::visual_object, Vim};
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Object {
Word { ignore_punctuation: bool },
Sentence,
}
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Word {
#[serde(default)]
ignore_punctuation: bool,
}
actions!(vim, [Sentence]);
impl_actions!(vim, [Word]);
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(
|_: &mut Workspace, &Word { ignore_punctuation }: &Word, cx: _| {
object(Object::Word { ignore_punctuation }, cx)
},
);
cx.add_action(|_: &mut Workspace, _: &Sentence, cx: _| object(Object::Sentence, cx));
}
fn object(object: Object, cx: &mut MutableAppContext) {
match Vim::read(cx).state.mode {
Mode::Normal => normal_object(object, cx),
Mode::Visual { .. } => visual_object(object, cx),
Mode::Insert => {
// Shouldn't execute a text object in insert mode. Ignoring
}
}
}
impl Object {
pub fn range(
self,
map: &DisplaySnapshot,
relative_to: DisplayPoint,
around: bool,
) -> Range<DisplayPoint> {
match self {
Object::Word { ignore_punctuation } => {
if around {
around_word(map, relative_to, ignore_punctuation)
} else {
in_word(map, relative_to, ignore_punctuation)
}
}
Object::Sentence => sentence(map, relative_to, around),
}
}
pub fn expand_selection(
self,
map: &DisplaySnapshot,
selection: &mut Selection<DisplayPoint>,
around: bool,
) {
let range = self.range(map, selection.head(), around);
selection.start = range.start;
selection.end = range.end;
}
}
/// Return a range that surrounds the word relative_to is in
/// If relative_to is at the start of a word, return the word.
/// If relative_to is between words, return the space between
fn in_word(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
ignore_punctuation: bool,
) -> Range<DisplayPoint> {
// Use motion::right so that we consider the character under the cursor when looking for the start
let start = movement::find_preceding_boundary_in_line(
map,
motion::right(map, relative_to),
|left, right| {
char_kind(left).coerce_punctuation(ignore_punctuation)
!= char_kind(right).coerce_punctuation(ignore_punctuation)
},
);
let end = movement::find_boundary_in_line(map, relative_to, |left, right| {
char_kind(left).coerce_punctuation(ignore_punctuation)
!= char_kind(right).coerce_punctuation(ignore_punctuation)
});
start..end
}
/// Return a range that surrounds the word and following whitespace
/// relative_to is in.
/// If relative_to is at the start of a word, return the word and following whitespace.
/// If relative_to is between words, return the whitespace back and the following word
/// if in word
/// delete that word
/// if there is whitespace following the word, delete that as well
/// otherwise, delete any preceding whitespace
/// otherwise
/// delete whitespace around cursor
/// delete word following the cursor
fn around_word(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
ignore_punctuation: bool,
) -> Range<DisplayPoint> {
let in_word = map
.chars_at(relative_to)
.next()
.map(|(c, _)| char_kind(c) != CharKind::Whitespace)
.unwrap_or(false);
if in_word {
around_containing_word(map, relative_to, ignore_punctuation)
} else {
around_next_word(map, relative_to, ignore_punctuation)
}
}
fn around_containing_word(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
ignore_punctuation: bool,
) -> Range<DisplayPoint> {
expand_to_include_whitespace(map, in_word(map, relative_to, ignore_punctuation), true)
}
fn around_next_word(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
ignore_punctuation: bool,
) -> Range<DisplayPoint> {
// Get the start of the word
let start = movement::find_preceding_boundary_in_line(
map,
motion::right(map, relative_to),
|left, right| {
char_kind(left).coerce_punctuation(ignore_punctuation)
!= char_kind(right).coerce_punctuation(ignore_punctuation)
},
);
let mut word_found = false;
let end = movement::find_boundary(map, relative_to, |left, right| {
let left_kind = char_kind(left).coerce_punctuation(ignore_punctuation);
let right_kind = char_kind(right).coerce_punctuation(ignore_punctuation);
let found = (word_found && left_kind != right_kind) || right == '\n' && left == '\n';
if right_kind != CharKind::Whitespace {
word_found = true;
}
found
});
start..end
}
fn sentence(map: &DisplaySnapshot, relative_to: DisplayPoint, around: bool) -> Range<DisplayPoint> {
let mut start = None;
let mut previous_end = relative_to;
let mut chars = map.chars_at(relative_to).peekable();
// Search backwards for the previous sentence end or current sentence start. Include the character under relative_to
for (char, point) in chars
.peek()
.cloned()
.into_iter()
.chain(map.reverse_chars_at(relative_to))
{
if is_sentence_end(map, point) {
break;
}
if is_possible_sentence_start(char) {
start = Some(point);
}
previous_end = point;
}
// Search forward for the end of the current sentence or if we are between sentences, the start of the next one
let mut end = relative_to;
for (char, point) in chars {
if start.is_none() && is_possible_sentence_start(char) {
if around {
start = Some(point);
continue;
} else {
end = point;
break;
}
}
end = point;
*end.column_mut() += char.len_utf8() as u32;
end = map.clip_point(end, Bias::Left);
if is_sentence_end(map, end) {
break;
}
}
let mut range = start.unwrap_or(previous_end)..end;
if around {
range = expand_to_include_whitespace(map, range, false);
}
range
}
fn is_possible_sentence_start(character: char) -> bool {
!character.is_whitespace() && character != '.'
}
const SENTENCE_END_PUNCTUATION: &[char] = &['.', '!', '?'];
const SENTENCE_END_FILLERS: &[char] = &[')', ']', '"', '\''];
const SENTENCE_END_WHITESPACE: &[char] = &[' ', '\t', '\n'];
fn is_sentence_end(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
let mut next_chars = map.chars_at(point).peekable();
if let Some((char, _)) = next_chars.next() {
// We are at a double newline. This position is a sentence end.
if char == '\n' && next_chars.peek().map(|(c, _)| c == &'\n').unwrap_or(false) {
return true;
}
// The next text is not a valid whitespace. This is not a sentence end
if !SENTENCE_END_WHITESPACE.contains(&char) {
return false;
}
}
for (char, _) in map.reverse_chars_at(point) {
if SENTENCE_END_PUNCTUATION.contains(&char) {
return true;
}
if !SENTENCE_END_FILLERS.contains(&char) {
return false;
}
}
return false;
}
/// Expands the passed range to include whitespace on one side or the other in a line. Attempts to add the
/// whitespace to the end first and falls back to the start if there was none.
fn expand_to_include_whitespace(
map: &DisplaySnapshot,
mut range: Range<DisplayPoint>,
stop_at_newline: bool,
) -> Range<DisplayPoint> {
let mut whitespace_included = false;
let mut chars = map.chars_at(range.end).peekable();
while let Some((char, point)) = chars.next() {
if char == '\n' && stop_at_newline {
break;
}
if char.is_whitespace() {
// Set end to the next display_point or the character position after the current display_point
range.end = chars.peek().map(|(_, point)| *point).unwrap_or_else(|| {
let mut end = point;
*end.column_mut() += char.len_utf8() as u32;
map.clip_point(end, Bias::Left)
});
if char != '\n' {
whitespace_included = true;
}
} else {
// Found non whitespace. Quit out.
break;
}
}
if !whitespace_included {
for (char, point) in map.reverse_chars_at(range.start) {
if char == '\n' && stop_at_newline {
break;
}
if !char.is_whitespace() {
break;
}
range.start = point;
}
}
range
}
#[cfg(test)]
mod test {
use indoc::indoc;
use crate::test::NeovimBackedTestContext;
const WORD_LOCATIONS: &'static str = indoc! {"
The quick ˇbrowˇnˇ
fox ˇjuˇmpsˇ over
the lazy dogˇ
ˇ
ˇ
ˇ
Thˇeˇ-ˇquˇickˇ ˇbrownˇ
ˇ
ˇ
ˇ fox-jumpˇs over
the lazy dogˇ
ˇ
"};
#[gpui::test]
async fn test_change_word_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_binding_matches_all(["c", "i", "w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["c", "i", "shift-w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["c", "a", "w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["c", "a", "shift-w"], WORD_LOCATIONS)
.await;
}
#[gpui::test]
async fn test_delete_word_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_binding_matches_all(["d", "i", "w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["d", "i", "shift-w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["d", "a", "w"], WORD_LOCATIONS)
.await;
cx.assert_binding_matches_all(["d", "a", "shift-w"], WORD_LOCATIONS)
.await;
}
#[gpui::test]
async fn test_visual_word_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_binding_matches_all(["v", "i", "w"], WORD_LOCATIONS)
.await;
// Visual text objects are slightly broken when used with non empty selections
// cx.assert_binding_matches_all(["v", "h", "i", "w"], WORD_LOCATIONS)
// .await;
// cx.assert_binding_matches_all(["v", "l", "i", "w"], WORD_LOCATIONS)
// .await;
cx.assert_binding_matches_all(["v", "i", "shift-w"], WORD_LOCATIONS)
.await;
// Visual text objects are slightly broken when used with non empty selections
// cx.assert_binding_matches_all(["v", "i", "h", "shift-w"], WORD_LOCATIONS)
// .await;
// cx.assert_binding_matches_all(["v", "i", "l", "shift-w"], WORD_LOCATIONS)
// .await;
// Visual around words is somewhat broken right now when it comes to newlines
// cx.assert_binding_matches_all(["v", "a", "w"], WORD_LOCATIONS)
// .await;
// cx.assert_binding_matches_all(["v", "a", "shift-w"], WORD_LOCATIONS)
// .await;
}
const SENTENCE_EXAMPLES: &[&'static str] = &[
"ˇThe quick ˇbrownˇ?ˇ ˇFox Jˇumpsˇ!ˇ Ovˇer theˇ lazyˇ.",
indoc! {"
ˇThe quick ˇbrownˇ
fox jumps over
the lazy doˇgˇ.ˇ ˇThe quick ˇ
brown fox jumps over
"},
// Position of the cursor after deletion between lines isn't quite right.
// Deletion in a sentence at the start of a line with whitespace is incorrect.
// indoc! {"
// The quick brown fox jumps.
// Over the lazy dog
// ˇ
// ˇ
// ˇ fox-jumpˇs over
// the lazy dog.ˇ
// ˇ
// "},
r#"ˇThe ˇquick brownˇ.)ˇ]ˇ'ˇ" Brown ˇfox jumpsˇ.ˇ "#,
];
#[gpui::test]
async fn test_change_sentence_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx)
.await
.binding(["c", "i", "s"]);
for sentence_example in SENTENCE_EXAMPLES {
cx.assert_all(sentence_example).await;
}
let mut cx = cx.binding(["c", "a", "s"]);
// Resulting position is slightly incorrect for unintuitive reasons.
cx.add_initial_state_exemption("The quick brown?ˇ Fox Jumps! Over the lazy.");
// Changing around the sentence at the end of the line doesn't remove whitespace.'
cx.add_initial_state_exemption("The quick brown.)]\'\" Brown fox jumps.ˇ ");
for sentence_example in SENTENCE_EXAMPLES {
cx.assert_all(sentence_example).await;
}
}
#[gpui::test]
async fn test_delete_sentence_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx)
.await
.binding(["d", "i", "s"]);
for sentence_example in SENTENCE_EXAMPLES {
cx.assert_all(sentence_example).await;
}
let mut cx = cx.binding(["d", "a", "s"]);
// Resulting position is slightly incorrect for unintuitive reasons.
cx.add_initial_state_exemption("The quick brown?ˇ Fox Jumps! Over the lazy.");
// Changing around the sentence at the end of the line doesn't remove whitespace.'
cx.add_initial_state_exemption("The quick brown.)]\'\" Brown fox jumps.ˇ ");
for sentence_example in SENTENCE_EXAMPLES {
cx.assert_all(sentence_example).await;
}
}
#[gpui::test]
async fn test_visual_sentence_object(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx)
.await
.binding(["v", "i", "s"]);
for sentence_example in SENTENCE_EXAMPLES {
cx.assert_all(sentence_example).await;
}
// Visual around sentences is somewhat broken right now when it comes to newlines
// let mut cx = cx.binding(["d", "a", "s"]);
// for sentence_example in SENTENCE_EXAMPLES {
// cx.assert_all(sentence_example).await;
// }
}
}