Merge branch 'main' into allow-following-outside-of-projects

This commit is contained in:
Max Brunsfeld 2023-09-29 14:15:33 -07:00
commit c718b810f6
109 changed files with 5271 additions and 1675 deletions

View file

@ -16,6 +16,7 @@ fn normal_before(_: &mut Workspace, action: &NormalBefore, cx: &mut ViewContext<
vim.stop_recording_immediately(action.boxed_clone());
if count <= 1 || vim.workspace_state.replaying {
vim.update_active_editor(cx, |editor, cx| {
editor.cancel(&Default::default(), cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, mut cursor, _| {
*cursor.column_mut() = cursor.column().saturating_sub(1);

View file

@ -467,6 +467,22 @@ impl Motion {
(_, selection.end) = map.next_line_boundary(selection.end.to_point(map));
} else {
// Another special case: When using the "w" motion in combination with an
// operator and the last word moved over is at the end of a line, the end of
// that word becomes the end of the operated text, not the first word in the
// next line.
if let Motion::NextWordStart {
ignore_punctuation: _,
} = self
{
let start_row = selection.start.to_point(&map).row;
if selection.end.to_point(&map).row > start_row {
selection.end =
Point::new(start_row, map.buffer_snapshot.line_len(start_row))
.to_display_point(&map)
}
}
// If the motion is exclusive and the end of the motion is in column 1, the
// end of the motion is moved to the end of the previous line and the motion
// becomes inclusive. Example: "}" moves to the first line after a paragraph,

View file

@ -1,6 +1,7 @@
mod case;
mod change;
mod delete;
mod increment;
mod paste;
pub(crate) mod repeat;
mod scroll;
@ -56,6 +57,7 @@ pub fn init(cx: &mut AppContext) {
scroll::init(cx);
search::init(cx);
substitute::init(cx);
increment::init(cx);
cx.add_action(insert_after);
cx.add_action(insert_before);

View file

@ -76,12 +76,6 @@ pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut Windo
// word does not include the following white space. {Vi: "cw" when on a blank
// followed by other blanks changes only the first blank; this is probably a
// bug, because "dw" deletes all the blanks}
//
// NOT HANDLED YET
// Another special case: When using the "w" motion in combination with an
// operator and the last word moved over is at the end of a line, the end of
// that word becomes the end of the operated text, not the first word in the
// next line.
fn expand_changed_word_selection(
map: &DisplaySnapshot,
selection: &mut Selection<DisplayPoint>,

View file

@ -2,6 +2,7 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}
use collections::{HashMap, HashSet};
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
use gpui::WindowContext;
use language::Point;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.stop_recording();
@ -14,6 +15,27 @@ pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &m
let original_head = selection.head();
original_columns.insert(selection.id, original_head.column());
motion.expand_selection(map, selection, times, true);
// Motion::NextWordStart on an empty line should delete it.
if let Motion::NextWordStart {
ignore_punctuation: _,
} = motion
{
if selection.is_empty()
&& map
.buffer_snapshot
.line_len(selection.start.to_point(&map).row)
== 0
{
selection.end = map
.buffer_snapshot
.clip_point(
Point::new(selection.start.to_point(&map).row + 1, 0),
Bias::Left,
)
.to_display_point(map)
}
}
});
});
copy_selections_content(editor, motion.linewise(), cx);
@ -129,28 +151,44 @@ mod test {
#[gpui::test]
async fn test_delete_w(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await.binding(["d", "w"]);
cx.assert("Teˇst").await;
cx.assert("Tˇest test").await;
cx.assert(indoc! {"
Test teˇst
test"})
.await;
cx.assert(indoc! {"
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_neovim_compatible(
indoc! {"
Test tesˇt
test"})
.await;
cx.assert_exempted(
test"},
["d", "w"],
)
.await;
cx.assert_neovim_compatible("Teˇst", ["d", "w"]).await;
cx.assert_neovim_compatible("Tˇest test", ["d", "w"]).await;
cx.assert_neovim_compatible(
indoc! {"
Test teˇst
test"},
["d", "w"],
)
.await;
cx.assert_neovim_compatible(
indoc! {"
Test tesˇt
test"},
["d", "w"],
)
.await;
cx.assert_neovim_compatible(
indoc! {"
Test test
ˇ
test"},
ExemptionFeatures::DeleteWordOnEmptyLine,
["d", "w"],
)
.await;
let mut cx = cx.binding(["d", "shift-w"]);
cx.assert("Test teˇst-test test").await;
cx.assert_neovim_compatible("Test teˇst-test test", ["d", "shift-w"])
.await;
}
#[gpui::test]

View file

@ -0,0 +1,262 @@
use std::ops::Range;
use editor::{scroll::autoscroll::Autoscroll, MultiBufferSnapshot, ToOffset, ToPoint};
use gpui::{impl_actions, AppContext, WindowContext};
use language::{Bias, Point};
use serde::Deserialize;
use workspace::Workspace;
use crate::{state::Mode, Vim};
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Increment {
#[serde(default)]
step: bool,
}
#[derive(Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Decrement {
#[serde(default)]
step: bool,
}
impl_actions!(vim, [Increment, Decrement]);
pub fn init(cx: &mut AppContext) {
cx.add_action(|_: &mut Workspace, action: &Increment, cx| {
Vim::update(cx, |vim, cx| {
vim.record_current_action(cx);
let count = vim.take_count(cx).unwrap_or(1);
let step = if action.step { 1 } else { 0 };
increment(vim, count as i32, step, cx)
})
});
cx.add_action(|_: &mut Workspace, action: &Decrement, cx| {
Vim::update(cx, |vim, cx| {
vim.record_current_action(cx);
let count = vim.take_count(cx).unwrap_or(1);
let step = if action.step { -1 } else { 0 };
increment(vim, count as i32 * -1, step, cx)
})
});
}
fn increment(vim: &mut Vim, mut delta: i32, step: i32, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, cx| {
let mut edits = Vec::new();
let mut new_anchors = Vec::new();
let snapshot = editor.buffer().read(cx).snapshot(cx);
for selection in editor.selections.all_adjusted(cx) {
if !selection.is_empty() {
if vim.state().mode != Mode::VisualBlock || new_anchors.is_empty() {
new_anchors.push((true, snapshot.anchor_before(selection.start)))
}
}
for row in selection.start.row..=selection.end.row {
let start = if row == selection.start.row {
selection.start
} else {
Point::new(row, 0)
};
if let Some((range, num, radix)) = find_number(&snapshot, start) {
if let Ok(val) = i32::from_str_radix(&num, radix) {
let result = val + delta;
delta += step;
let replace = match radix {
10 => format!("{}", result),
16 => {
if num.to_ascii_lowercase() == num {
format!("{:x}", result)
} else {
format!("{:X}", result)
}
}
2 => format!("{:b}", result),
_ => unreachable!(),
};
if selection.is_empty() {
new_anchors.push((false, snapshot.anchor_after(range.end)))
}
edits.push((range, replace));
}
}
}
}
editor.transact(cx, |editor, cx| {
editor.edit(edits, cx);
let snapshot = editor.buffer().read(cx).snapshot(cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
let mut new_ranges = Vec::new();
for (visual, anchor) in new_anchors.iter() {
let mut point = anchor.to_point(&snapshot);
if !*visual && point.column > 0 {
point.column -= 1;
point = snapshot.clip_point(point, Bias::Left)
}
new_ranges.push(point..point);
}
s.select_ranges(new_ranges)
})
});
});
vim.switch_mode(Mode::Normal, true, cx)
}
fn find_number(
snapshot: &MultiBufferSnapshot,
start: Point,
) -> Option<(Range<Point>, String, u32)> {
let mut offset = start.to_offset(snapshot);
// go backwards to the start of any number the selection is within
for ch in snapshot.reversed_chars_at(offset) {
if ch.is_ascii_digit() || ch == '-' || ch == 'b' || ch == 'x' {
offset -= ch.len_utf8();
continue;
}
break;
}
let mut begin = None;
let mut end = None;
let mut num = String::new();
let mut radix = 10;
let mut chars = snapshot.chars_at(offset).peekable();
// find the next number on the line (may start after the original cursor position)
while let Some(ch) = chars.next() {
if num == "0" && ch == 'b' && chars.peek().is_some() && chars.peek().unwrap().is_digit(2) {
radix = 2;
begin = None;
num = String::new();
}
if num == "0" && ch == 'x' && chars.peek().is_some() && chars.peek().unwrap().is_digit(16) {
radix = 16;
begin = None;
num = String::new();
}
if ch.is_digit(radix)
|| (begin.is_none()
&& ch == '-'
&& chars.peek().is_some()
&& chars.peek().unwrap().is_digit(radix))
{
if begin.is_none() {
begin = Some(offset);
}
num.push(ch);
} else {
if begin.is_some() {
end = Some(offset);
break;
} else if ch == '\n' {
break;
}
}
offset += ch.len_utf8();
}
if let Some(begin) = begin {
let end = end.unwrap_or(offset);
Some((begin.to_point(snapshot)..end.to_point(snapshot), num, radix))
} else {
None
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
use crate::test::NeovimBackedTestContext;
#[gpui::test]
async fn test_increment(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_shared_state(indoc! {"
1ˇ2
"})
.await;
cx.simulate_shared_keystrokes(["ctrl-a"]).await;
cx.assert_shared_state(indoc! {"
1ˇ3
"})
.await;
cx.simulate_shared_keystrokes(["ctrl-x"]).await;
cx.assert_shared_state(indoc! {"
1ˇ2
"})
.await;
cx.simulate_shared_keystrokes(["9", "9", "ctrl-a"]).await;
cx.assert_shared_state(indoc! {"
11ˇ1
"})
.await;
cx.simulate_shared_keystrokes(["1", "1", "1", "ctrl-x"])
.await;
cx.assert_shared_state(indoc! {"
ˇ0
"})
.await;
cx.simulate_shared_keystrokes(["."]).await;
cx.assert_shared_state(indoc! {"
-11ˇ1
"})
.await;
}
#[gpui::test]
async fn test_increment_radix(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_matches_neovim("ˇ total: 0xff", ["ctrl-a"], " total: 0x10ˇ0")
.await;
cx.assert_matches_neovim("ˇ total: 0xff", ["ctrl-x"], " total: 0xfˇe")
.await;
cx.assert_matches_neovim("ˇ total: 0xFF", ["ctrl-x"], " total: 0xFˇE")
.await;
cx.assert_matches_neovim("(ˇ0b10f)", ["ctrl-a"], "(0b1ˇ1f)")
.await;
cx.assert_matches_neovim("ˇ-1", ["ctrl-a"], "ˇ0").await;
}
#[gpui::test]
async fn test_increment_steps(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_shared_state(indoc! {"
ˇ1
1
1 2
1
1"})
.await;
cx.simulate_shared_keystrokes(["j", "v", "shift-g", "g", "ctrl-a"])
.await;
cx.assert_shared_state(indoc! {"
1
ˇ2
3 2
4
5"})
.await;
cx.simulate_shared_keystrokes(["shift-g", "ctrl-v", "g", "g", "g", "ctrl-x"])
.await;
cx.assert_shared_state(indoc! {"
ˇ0
0
0 2
0
0"})
.await;
}
}

View file

@ -23,8 +23,6 @@ pub enum ExemptionFeatures {
// MOTIONS
// When an operator completes at the end of the file, an extra newline is left
OperatorLastNewlineRemains,
// Deleting a word on an empty line doesn't remove the newline
DeleteWordOnEmptyLine,
// OBJECTS
// Resulting position after the operation is slightly incorrect for unintuitive reasons.
@ -363,6 +361,17 @@ impl<'a> NeovimBackedTestContext<'a> {
self.assert_state_matches().await;
}
pub async fn assert_matches_neovim<const COUNT: usize>(
&mut self,
marked_positions: &str,
keystrokes: [&str; COUNT],
result: &str,
) {
self.set_shared_state(marked_positions).await;
self.simulate_shared_keystrokes(keystrokes).await;
self.assert_shared_state(result).await;
}
pub async fn assert_binding_matches_all_exempted<const COUNT: usize>(
&mut self,
keystrokes: [&str; COUNT],

View file

@ -195,6 +195,9 @@ impl Vim {
if editor_mode == EditorMode::Full
&& !newest_selection_empty
&& self.state().mode == Mode::Normal
// if leader_replica_id is set, then you're following someone else's cursor
// don't switch vim mode.
&& editor.leader_replica_id().is_none()
{
self.switch_mode(Mode::Visual, true, cx);
}

View file

@ -20,10 +20,3 @@
{"Key":"0"}
{"Key":"enter"}
{"Get":{"state":"aa\ndd\nˇcc","mode":"Normal"}}
{"Key":":"}
{"Key":"%"}
{"Key":"s"}
{"Key":"/"}
{"Key":"/"}
{"Key":"/"}
{"Key":"enter"}

View file

@ -1,3 +1,7 @@
{"Put":{"state":"Test tesˇt\n test"}}
{"Key":"d"}
{"Key":"w"}
{"Get":{"state":"Test teˇs\n test","mode":"Normal"}}
{"Put":{"state":"Teˇst"}}
{"Key":"d"}
{"Key":"w"}
@ -14,6 +18,10 @@
{"Key":"d"}
{"Key":"w"}
{"Get":{"state":"Test teˇs\ntest","mode":"Normal"}}
{"Put":{"state":"Test test\nˇ\ntest"}}
{"Key":"d"}
{"Key":"w"}
{"Get":{"state":"Test test\nˇtest","mode":"Normal"}}
{"Put":{"state":"Test teˇst-test test"}}
{"Key":"d"}
{"Key":"shift-w"}

View file

@ -0,0 +1,16 @@
{"Put":{"state":"1ˇ2\n"}}
{"Key":"ctrl-a"}
{"Get":{"state":"1ˇ3\n","mode":"Normal"}}
{"Key":"ctrl-x"}
{"Get":{"state":"1ˇ2\n","mode":"Normal"}}
{"Key":"9"}
{"Key":"9"}
{"Key":"ctrl-a"}
{"Get":{"state":"11ˇ1\n","mode":"Normal"}}
{"Key":"1"}
{"Key":"1"}
{"Key":"1"}
{"Key":"ctrl-x"}
{"Get":{"state":"ˇ0\n","mode":"Normal"}}
{"Key":"."}
{"Get":{"state":"-11ˇ1\n","mode":"Normal"}}

View file

@ -0,0 +1,15 @@
{"Put":{"state":"ˇ total: 0xff"}}
{"Key":"ctrl-a"}
{"Get":{"state":" total: 0x10ˇ0","mode":"Normal"}}
{"Put":{"state":"ˇ total: 0xff"}}
{"Key":"ctrl-x"}
{"Get":{"state":" total: 0xfˇe","mode":"Normal"}}
{"Put":{"state":"ˇ total: 0xFF"}}
{"Key":"ctrl-x"}
{"Get":{"state":" total: 0xFˇE","mode":"Normal"}}
{"Put":{"state":"(ˇ0b10f)"}}
{"Key":"ctrl-a"}
{"Get":{"state":"(0b1ˇ1f)","mode":"Normal"}}
{"Put":{"state":"ˇ-1"}}
{"Key":"ctrl-a"}
{"Get":{"state":"ˇ0","mode":"Normal"}}

View file

@ -0,0 +1,14 @@
{"Put":{"state":"ˇ1\n1\n1 2\n1\n1"}}
{"Key":"j"}
{"Key":"v"}
{"Key":"shift-g"}
{"Key":"g"}
{"Key":"ctrl-a"}
{"Get":{"state":"1\nˇ2\n3 2\n4\n5","mode":"Normal"}}
{"Key":"shift-g"}
{"Key":"ctrl-v"}
{"Key":"g"}
{"Key":"g"}
{"Key":"g"}
{"Key":"ctrl-x"}
{"Get":{"state":"ˇ0\n0\n0 2\n0\n0","mode":"Normal"}}