Detailed changes
@@ -137,7 +137,7 @@
}
},
{
- "context": "Editor && vim_mode == normal && vim_operator == none && !VimWaiting",
+ "context": "Editor && vim_mode == normal && (vim_operator == none || vim_operator == n) && !VimWaiting",
"bindings": {
"c": [
"vim::PushOperator",
@@ -220,7 +220,8 @@
"r": [
"vim::PushOperator",
"Replace"
- ]
+ ],
+ "s": "vim::Substitute"
}
},
{
@@ -307,6 +308,7 @@
"x": "vim::VisualDelete",
"y": "vim::VisualYank",
"p": "vim::VisualPaste",
+ "s": "vim::Substitute",
"r": [
"vim::PushOperator",
"Replace"
@@ -41,13 +41,7 @@ use collections::HashMap;
use core::panic;
use json::ToJson;
use smallvec::SmallVec;
-use std::{
- any::Any,
- borrow::Cow,
- marker::PhantomData,
- mem,
- ops::{Deref, DerefMut, Range},
-};
+use std::{any::Any, borrow::Cow, mem, ops::Range};
pub trait Element<V: View>: 'static {
type LayoutState;
@@ -567,90 +561,6 @@ impl<V: View> RootElement<V> {
}
}
-pub trait Component<V: View>: 'static {
- fn render(&self, view: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;
-}
-
-pub struct ComponentHost<V: View, C: Component<V>> {
- component: C,
- view_type: PhantomData<V>,
-}
-
-impl<V: View, C: Component<V>> ComponentHost<V, C> {
- pub fn new(c: C) -> Self {
- Self {
- component: c,
- view_type: PhantomData,
- }
- }
-}
-
-impl<V: View, C: Component<V>> Deref for ComponentHost<V, C> {
- type Target = C;
-
- fn deref(&self) -> &Self::Target {
- &self.component
- }
-}
-
-impl<V: View, C: Component<V>> DerefMut for ComponentHost<V, C> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.component
- }
-}
-
-impl<V: View, C: Component<V>> Element<V> for ComponentHost<V, C> {
- type LayoutState = AnyElement<V>;
- type PaintState = ();
-
- fn layout(
- &mut self,
- constraint: SizeConstraint,
- view: &mut V,
- cx: &mut LayoutContext<V>,
- ) -> (Vector2F, AnyElement<V>) {
- let mut element = self.component.render(view, cx);
- let size = element.layout(constraint, view, cx);
- (size, element)
- }
-
- fn paint(
- &mut self,
- scene: &mut SceneBuilder,
- bounds: RectF,
- visible_bounds: RectF,
- element: &mut AnyElement<V>,
- view: &mut V,
- cx: &mut ViewContext<V>,
- ) {
- element.paint(scene, bounds.origin(), visible_bounds, view, cx);
- }
-
- fn rect_for_text_range(
- &self,
- range_utf16: Range<usize>,
- _: RectF,
- _: RectF,
- element: &AnyElement<V>,
- _: &(),
- view: &V,
- cx: &ViewContext<V>,
- ) -> Option<RectF> {
- element.rect_for_text_range(range_utf16, view, cx)
- }
-
- fn debug(
- &self,
- _: RectF,
- element: &AnyElement<V>,
- _: &(),
- view: &V,
- cx: &ViewContext<V>,
- ) -> serde_json::Value {
- element.debug(view, cx)
- }
-}
-
pub trait AnyRootElement {
fn layout(
&mut self,
@@ -26,7 +26,7 @@ pub mod color;
pub mod json;
pub mod keymap_matcher;
pub mod platform;
-pub use gpui_macros::test;
+pub use gpui_macros::{test, Element};
pub use window::{Axis, SizeConstraint, Vector2FExt, WindowContext};
pub use anyhow;
@@ -3,8 +3,8 @@ use proc_macro2::Ident;
use quote::{format_ident, quote};
use std::mem;
use syn::{
- parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, FnArg, ItemFn, Lit, Meta,
- NestedMeta, Type,
+ parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, DeriveInput, FnArg,
+ ItemFn, Lit, Meta, NestedMeta, Type,
};
#[proc_macro_attribute]
@@ -275,3 +275,68 @@ fn parse_bool(literal: &Lit) -> Result<bool, TokenStream> {
result.map_err(|err| TokenStream::from(err.into_compile_error()))
}
+
+#[proc_macro_derive(Element)]
+pub fn element_derive(input: TokenStream) -> TokenStream {
+ // Parse the input tokens into a syntax tree
+ let input = parse_macro_input!(input as DeriveInput);
+
+ // The name of the struct/enum
+ let name = input.ident;
+
+ let expanded = quote! {
+ impl<V: gpui::View> gpui::elements::Element<V> for #name {
+ type LayoutState = gpui::elements::AnyElement<V>;
+ type PaintState = ();
+
+ fn layout(
+ &mut self,
+ constraint: gpui::SizeConstraint,
+ view: &mut V,
+ cx: &mut gpui::LayoutContext<V>,
+ ) -> (gpui::geometry::vector::Vector2F, gpui::elements::AnyElement<V>) {
+ let mut element = self.render(view, cx);
+ let size = element.layout(constraint, view, cx);
+ (size, element)
+ }
+
+ fn paint(
+ &mut self,
+ scene: &mut gpui::SceneBuilder,
+ bounds: gpui::geometry::rect::RectF,
+ visible_bounds: gpui::geometry::rect::RectF,
+ element: &mut gpui::elements::AnyElement<V>,
+ view: &mut V,
+ cx: &mut gpui::ViewContext<V>,
+ ) {
+ element.paint(scene, bounds.origin(), visible_bounds, view, cx);
+ }
+
+ fn rect_for_text_range(
+ &self,
+ range_utf16: std::ops::Range<usize>,
+ _: gpui::geometry::rect::RectF,
+ _: gpui::geometry::rect::RectF,
+ element: &gpui::elements::AnyElement<V>,
+ _: &(),
+ view: &V,
+ cx: &gpui::ViewContext<V>,
+ ) -> Option<gpui::geometry::rect::RectF> {
+ element.rect_for_text_range(range_utf16, view, cx)
+ }
+
+ fn debug(
+ &self,
+ _: gpui::geometry::rect::RectF,
+ element: &gpui::elements::AnyElement<V>,
+ _: &(),
+ view: &V,
+ cx: &gpui::ViewContext<V>,
+ ) -> serde_json::Value {
+ element.debug(view, cx)
+ }
+ }
+ };
+ // Return generated code
+ TokenStream::from(expanded)
+}
@@ -209,8 +209,9 @@ impl Motion {
map: &DisplaySnapshot,
point: DisplayPoint,
goal: SelectionGoal,
- times: usize,
+ maybe_times: Option<usize>,
) -> Option<(DisplayPoint, SelectionGoal)> {
+ let times = maybe_times.unwrap_or(1);
use Motion::*;
let infallible = self.infallible();
let (new_point, goal) = match self {
@@ -236,7 +237,10 @@ impl Motion {
EndOfLine => (end_of_line(map, point), SelectionGoal::None),
CurrentLine => (end_of_line(map, point), SelectionGoal::None),
StartOfDocument => (start_of_document(map, point, times), SelectionGoal::None),
- EndOfDocument => (end_of_document(map, point, times), SelectionGoal::None),
+ EndOfDocument => (
+ end_of_document(map, point, maybe_times),
+ SelectionGoal::None,
+ ),
Matching => (matching(map, point), SelectionGoal::None),
FindForward { before, text } => (
find_forward(map, point, *before, text.clone(), times),
@@ -257,7 +261,7 @@ impl Motion {
&self,
map: &DisplaySnapshot,
selection: &mut Selection<DisplayPoint>,
- times: usize,
+ times: Option<usize>,
expand_to_surrounding_newline: bool,
) -> bool {
if let Some((new_head, goal)) =
@@ -473,14 +477,19 @@ fn start_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) ->
map.clip_point(new_point, Bias::Left)
}
-fn end_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint {
- let mut new_point = if line == 1 {
- map.max_point()
+fn end_of_document(
+ map: &DisplaySnapshot,
+ point: DisplayPoint,
+ line: Option<usize>,
+) -> DisplayPoint {
+ let new_row = if let Some(line) = line {
+ (line - 1) as u32
} else {
- Point::new((line - 1) as u32, 0).to_display_point(map)
+ map.max_buffer_row()
};
- *new_point.column_mut() = point.column();
- map.clip_point(new_point, Bias::Left)
+
+ let new_point = Point::new(new_row, point.column());
+ map.clip_point(new_point.to_display_point(map), Bias::Left)
}
fn matching(map: &DisplaySnapshot, display_point: DisplayPoint) -> DisplayPoint {
@@ -1,5 +1,6 @@
mod change;
mod delete;
+mod substitute;
mod yank;
use std::{borrow::Cow, cmp::Ordering, sync::Arc};
@@ -25,6 +26,7 @@ use workspace::Workspace;
use self::{
change::{change_motion, change_object},
delete::{delete_motion, delete_object},
+ substitute::substitute,
yank::{yank_motion, yank_object},
};
@@ -45,6 +47,7 @@ actions!(
DeleteToEndOfLine,
Paste,
Yank,
+ Substitute,
]
);
@@ -56,6 +59,12 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(insert_end_of_line);
cx.add_action(insert_line_above);
cx.add_action(insert_line_below);
+ cx.add_action(|_: &mut Workspace, _: &Substitute, cx| {
+ Vim::update(cx, |vim, cx| {
+ let times = vim.pop_number_operator(cx);
+ substitute(vim, times, cx);
+ })
+ });
cx.add_action(|_: &mut Workspace, _: &DeleteLeft, cx| {
Vim::update(cx, |vim, cx| {
let times = vim.pop_number_operator(cx);
@@ -93,7 +102,7 @@ pub fn init(cx: &mut AppContext) {
pub fn normal_motion(
motion: Motion,
operator: Option<Operator>,
- times: usize,
+ times: Option<usize>,
cx: &mut WindowContext,
) {
Vim::update(cx, |vim, cx| {
@@ -129,7 +138,7 @@ pub fn normal_object(object: Object, cx: &mut WindowContext) {
})
}
-fn move_cursor(vim: &mut Vim, motion: Motion, times: usize, cx: &mut WindowContext) {
+fn move_cursor(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, cursor, goal| {
@@ -147,7 +156,7 @@ fn insert_after(_: &mut Workspace, _: &InsertAfter, cx: &mut ViewContext<Workspa
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.maybe_move_cursors_with(|map, cursor, goal| {
- Motion::Right.move_point(map, cursor, goal, 1)
+ Motion::Right.move_point(map, cursor, goal, None)
});
});
});
@@ -164,7 +173,7 @@ fn insert_first_non_whitespace(
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.maybe_move_cursors_with(|map, cursor, goal| {
- Motion::FirstNonWhitespace.move_point(map, cursor, goal, 1)
+ Motion::FirstNonWhitespace.move_point(map, cursor, goal, None)
});
});
});
@@ -177,7 +186,7 @@ fn insert_end_of_line(_: &mut Workspace, _: &InsertEndOfLine, cx: &mut ViewConte
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.maybe_move_cursors_with(|map, cursor, goal| {
- Motion::EndOfLine.move_point(map, cursor, goal, 1)
+ Motion::EndOfLine.move_point(map, cursor, goal, None)
});
});
});
@@ -237,7 +246,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
});
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.maybe_move_cursors_with(|map, cursor, goal| {
- Motion::EndOfLine.move_point(map, cursor, goal, 1)
+ Motion::EndOfLine.move_point(map, cursor, goal, None)
});
});
editor.edit_with_autoindent(edits, cx);
@@ -6,7 +6,7 @@ use editor::{
use gpui::WindowContext;
use language::Selection;
-pub fn change_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut WindowContext) {
+pub fn change_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
// Some motions ignore failure when switching to normal mode
let mut motion_succeeded = matches!(
motion,
@@ -78,10 +78,10 @@ pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut Windo
fn expand_changed_word_selection(
map: &DisplaySnapshot,
selection: &mut Selection<DisplayPoint>,
- times: usize,
+ times: Option<usize>,
ignore_punctuation: bool,
) -> bool {
- if times == 1 {
+ if times.is_none() || times.unwrap() == 1 {
let in_word = map
.chars_at(selection.head())
.next()
@@ -97,7 +97,8 @@ fn expand_changed_word_selection(
});
true
} else {
- Motion::NextWordStart { ignore_punctuation }.expand_selection(map, selection, 1, false)
+ Motion::NextWordStart { ignore_punctuation }
+ .expand_selection(map, selection, None, false)
}
} else {
Motion::NextWordStart { ignore_punctuation }.expand_selection(map, selection, times, false)
@@ -3,7 +3,7 @@ use collections::{HashMap, HashSet};
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
use gpui::WindowContext;
-pub fn delete_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut WindowContext) {
+pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);
@@ -0,0 +1,69 @@
+use gpui::WindowContext;
+use language::Point;
+
+use crate::{motion::Motion, Mode, Vim};
+
+pub fn substitute(vim: &mut Vim, count: Option<usize>, cx: &mut WindowContext) {
+ vim.update_active_editor(cx, |editor, cx| {
+ editor.set_clip_at_line_ends(false, cx);
+ editor.change_selections(None, cx, |s| {
+ s.move_with(|map, selection| {
+ if selection.start == selection.end {
+ Motion::Right.expand_selection(map, selection, count, true);
+ }
+ })
+ });
+ editor.transact(cx, |editor, cx| {
+ let selections = editor.selections.all::<Point>(cx);
+ for selection in selections.into_iter().rev() {
+ editor.buffer().update(cx, |buffer, cx| {
+ buffer.edit([(selection.start..selection.end, "")], None, cx)
+ })
+ }
+ });
+ editor.set_clip_at_line_ends(true, cx);
+ });
+ vim.switch_mode(Mode::Insert, true, cx)
+}
+
+#[cfg(test)]
+mod test {
+ use crate::{state::Mode, test::VimTestContext};
+ use indoc::indoc;
+
+ #[gpui::test]
+ async fn test_substitute(cx: &mut gpui::TestAppContext) {
+ let mut cx = VimTestContext::new(cx, true).await;
+
+ // supports a single cursor
+ cx.set_state(indoc! {"ˇabc\n"}, Mode::Normal);
+ cx.simulate_keystrokes(["s", "x"]);
+ cx.assert_editor_state("xˇbc\n");
+
+ // supports a selection
+ cx.set_state(indoc! {"a«bcˇ»\n"}, Mode::Visual { line: false });
+ cx.assert_editor_state("a«bcˇ»\n");
+ cx.simulate_keystrokes(["s", "x"]);
+ cx.assert_editor_state("axˇ\n");
+
+ // supports counts
+ cx.set_state(indoc! {"ˇabc\n"}, Mode::Normal);
+ cx.simulate_keystrokes(["2", "s", "x"]);
+ cx.assert_editor_state("xˇc\n");
+
+ // supports multiple cursors
+ cx.set_state(indoc! {"a«bcˇ»deˇffg\n"}, Mode::Normal);
+ cx.simulate_keystrokes(["2", "s", "x"]);
+ cx.assert_editor_state("axˇdexˇg\n");
+
+ // does not read beyond end of line
+ cx.set_state(indoc! {"ˇabc\n"}, Mode::Normal);
+ cx.simulate_keystrokes(["5", "s", "x"]);
+ cx.assert_editor_state("xˇ\n");
+
+ // it handles multibyte characters
+ cx.set_state(indoc! {"ˇcàfé\n"}, Mode::Normal);
+ cx.simulate_keystrokes(["4", "s", "x"]);
+ cx.assert_editor_state("xˇ\n");
+ }
+}
@@ -2,7 +2,7 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}
use collections::HashMap;
use gpui::WindowContext;
-pub fn yank_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut WindowContext) {
+pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);
@@ -109,3 +109,17 @@ async fn test_count_down(cx: &mut gpui::TestAppContext) {
cx.simulate_keystrokes(["9", "down"]);
cx.assert_editor_state("aa\nbb\ncc\ndd\neˇe");
}
+
+#[gpui::test]
+async fn test_end_of_document_710(cx: &mut gpui::TestAppContext) {
+ let mut cx = VimTestContext::new(cx, true).await;
+
+ // goes to end by default
+ cx.set_state(indoc! {"aˇa\nbb\ncc"}, Mode::Normal);
+ cx.simulate_keystrokes(["shift-g"]);
+ cx.assert_editor_state("aa\nbb\ncˇc");
+
+ // can go to line 1 (https://github.com/zed-industries/community/issues/710)
+ cx.simulate_keystrokes(["1", "shift-g"]);
+ cx.assert_editor_state("aˇa\nbb\ncc");
+}
@@ -238,13 +238,12 @@ impl Vim {
popped_operator
}
- fn pop_number_operator(&mut self, cx: &mut WindowContext) -> usize {
- let mut times = 1;
+ fn pop_number_operator(&mut self, cx: &mut WindowContext) -> Option<usize> {
if let Some(Operator::Number(number)) = self.active_operator() {
- times = number;
self.pop_operator(cx);
+ return Some(number);
}
- times
+ None
}
fn clear_operator(&mut self, cx: &mut WindowContext) {
@@ -25,7 +25,7 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(paste);
}
-pub fn visual_motion(motion: Motion, times: usize, cx: &mut WindowContext) {
+pub fn visual_motion(motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {