WIP line mode operations
This commit is contained in:
parent
8044586296
commit
d7d17b2148
15 changed files with 166 additions and 30 deletions
|
@ -72,6 +72,10 @@
|
||||||
"v": [
|
"v": [
|
||||||
"vim::SwitchMode",
|
"vim::SwitchMode",
|
||||||
"Visual"
|
"Visual"
|
||||||
|
],
|
||||||
|
"V": [
|
||||||
|
"vim::SwitchMode",
|
||||||
|
"VisualLine"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -112,6 +116,14 @@
|
||||||
"x": "vim::VisualDelete"
|
"x": "vim::VisualDelete"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"context": "Editor && vim_mode == visual_line",
|
||||||
|
"bindings": {
|
||||||
|
"c": "vim::VisualLineChange",
|
||||||
|
"d": "vim::VisualLineDelete",
|
||||||
|
"x": "vim::VisualLineDelete"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"context": "Editor && vim_mode == insert",
|
"context": "Editor && vim_mode == insert",
|
||||||
"bindings": {
|
"bindings": {
|
||||||
|
|
|
@ -1319,7 +1319,11 @@ impl Editor {
|
||||||
) {
|
) {
|
||||||
if self.focused && self.leader_replica_id.is_none() {
|
if self.focused && self.leader_replica_id.is_none() {
|
||||||
self.buffer.update(cx, |buffer, cx| {
|
self.buffer.update(cx, |buffer, cx| {
|
||||||
buffer.set_active_selections(&self.selections.disjoint_anchors(), cx)
|
buffer.set_active_selections(
|
||||||
|
&self.selections.disjoint_anchors(),
|
||||||
|
self.selections.line_mode,
|
||||||
|
cx,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5599,7 +5603,11 @@ impl View for Editor {
|
||||||
self.buffer.update(cx, |buffer, cx| {
|
self.buffer.update(cx, |buffer, cx| {
|
||||||
buffer.finalize_last_transaction(cx);
|
buffer.finalize_last_transaction(cx);
|
||||||
if self.leader_replica_id.is_none() {
|
if self.leader_replica_id.is_none() {
|
||||||
buffer.set_active_selections(&self.selections.disjoint_anchors(), cx);
|
buffer.set_active_selections(
|
||||||
|
&self.selections.disjoint_anchors(),
|
||||||
|
self.selections.line_mode,
|
||||||
|
cx,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -345,12 +345,13 @@ impl EditorElement {
|
||||||
scroll_top,
|
scroll_top,
|
||||||
scroll_left,
|
scroll_left,
|
||||||
bounds,
|
bounds,
|
||||||
|
false,
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut cursors = SmallVec::<[Cursor; 32]>::new();
|
let mut cursors = SmallVec::<[Cursor; 32]>::new();
|
||||||
for (replica_id, selections) in &layout.selections {
|
for ((replica_id, line_mode), selections) in &layout.selections {
|
||||||
let selection_style = style.replica_selection_style(*replica_id);
|
let selection_style = style.replica_selection_style(*replica_id);
|
||||||
let corner_radius = 0.15 * layout.line_height;
|
let corner_radius = 0.15 * layout.line_height;
|
||||||
|
|
||||||
|
@ -367,6 +368,7 @@ impl EditorElement {
|
||||||
scroll_top,
|
scroll_top,
|
||||||
scroll_left,
|
scroll_left,
|
||||||
bounds,
|
bounds,
|
||||||
|
*line_mode,
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -483,6 +485,7 @@ impl EditorElement {
|
||||||
scroll_top: f32,
|
scroll_top: f32,
|
||||||
scroll_left: f32,
|
scroll_left: f32,
|
||||||
bounds: RectF,
|
bounds: RectF,
|
||||||
|
line_mode: bool,
|
||||||
cx: &mut PaintContext,
|
cx: &mut PaintContext,
|
||||||
) {
|
) {
|
||||||
if range.start != range.end {
|
if range.start != range.end {
|
||||||
|
@ -503,14 +506,14 @@ impl EditorElement {
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
let line_layout = &layout.line_layouts[(row - start_row) as usize];
|
let line_layout = &layout.line_layouts[(row - start_row) as usize];
|
||||||
HighlightedRangeLine {
|
HighlightedRangeLine {
|
||||||
start_x: if row == range.start.row() {
|
start_x: if row == range.start.row() && !line_mode {
|
||||||
content_origin.x()
|
content_origin.x()
|
||||||
+ line_layout.x_for_index(range.start.column() as usize)
|
+ line_layout.x_for_index(range.start.column() as usize)
|
||||||
- scroll_left
|
- scroll_left
|
||||||
} else {
|
} else {
|
||||||
content_origin.x() - scroll_left
|
content_origin.x() - scroll_left
|
||||||
},
|
},
|
||||||
end_x: if row == range.end.row() {
|
end_x: if row == range.end.row() && !line_mode {
|
||||||
content_origin.x()
|
content_origin.x()
|
||||||
+ line_layout.x_for_index(range.end.column() as usize)
|
+ line_layout.x_for_index(range.end.column() as usize)
|
||||||
- scroll_left
|
- scroll_left
|
||||||
|
@ -934,7 +937,7 @@ impl Element for EditorElement {
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut remote_selections = HashMap::default();
|
let mut remote_selections = HashMap::default();
|
||||||
for (replica_id, selection) in display_map
|
for (replica_id, line_mode, selection) in display_map
|
||||||
.buffer_snapshot
|
.buffer_snapshot
|
||||||
.remote_selections_in_range(&(start_anchor.clone()..end_anchor.clone()))
|
.remote_selections_in_range(&(start_anchor.clone()..end_anchor.clone()))
|
||||||
{
|
{
|
||||||
|
@ -944,7 +947,7 @@ impl Element for EditorElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
remote_selections
|
remote_selections
|
||||||
.entry(replica_id)
|
.entry((replica_id, line_mode))
|
||||||
.or_insert(Vec::new())
|
.or_insert(Vec::new())
|
||||||
.push(crate::Selection {
|
.push(crate::Selection {
|
||||||
id: selection.id,
|
id: selection.id,
|
||||||
|
@ -978,7 +981,7 @@ impl Element for EditorElement {
|
||||||
let local_replica_id = view.leader_replica_id.unwrap_or(view.replica_id(cx));
|
let local_replica_id = view.leader_replica_id.unwrap_or(view.replica_id(cx));
|
||||||
|
|
||||||
selections.push((
|
selections.push((
|
||||||
local_replica_id,
|
(local_replica_id, view.selections.line_mode),
|
||||||
local_selections
|
local_selections
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|selection| crate::Selection {
|
.map(|selection| crate::Selection {
|
||||||
|
@ -1237,7 +1240,7 @@ pub struct LayoutState {
|
||||||
em_width: f32,
|
em_width: f32,
|
||||||
em_advance: f32,
|
em_advance: f32,
|
||||||
highlighted_ranges: Vec<(Range<DisplayPoint>, Color)>,
|
highlighted_ranges: Vec<(Range<DisplayPoint>, Color)>,
|
||||||
selections: Vec<(ReplicaId, Vec<text::Selection<DisplayPoint>>)>,
|
selections: Vec<((ReplicaId, bool), Vec<text::Selection<DisplayPoint>>)>,
|
||||||
context_menu: Option<(DisplayPoint, ElementBox)>,
|
context_menu: Option<(DisplayPoint, ElementBox)>,
|
||||||
code_actions_indicator: Option<(u32, ElementBox)>,
|
code_actions_indicator: Option<(u32, ElementBox)>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,11 @@ impl FollowableItem for Editor {
|
||||||
} else {
|
} else {
|
||||||
self.buffer.update(cx, |buffer, cx| {
|
self.buffer.update(cx, |buffer, cx| {
|
||||||
if self.focused {
|
if self.focused {
|
||||||
buffer.set_active_selections(&self.selections.disjoint_anchors(), cx);
|
buffer.set_active_selections(
|
||||||
|
&self.selections.disjoint_anchors(),
|
||||||
|
self.selections.line_mode,
|
||||||
|
cx,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -509,6 +509,7 @@ impl MultiBuffer {
|
||||||
pub fn set_active_selections(
|
pub fn set_active_selections(
|
||||||
&mut self,
|
&mut self,
|
||||||
selections: &[Selection<Anchor>],
|
selections: &[Selection<Anchor>],
|
||||||
|
line_mode: bool,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
let mut selections_by_buffer: HashMap<usize, Vec<Selection<text::Anchor>>> =
|
let mut selections_by_buffer: HashMap<usize, Vec<Selection<text::Anchor>>> =
|
||||||
|
@ -573,7 +574,7 @@ impl MultiBuffer {
|
||||||
}
|
}
|
||||||
Some(selection)
|
Some(selection)
|
||||||
}));
|
}));
|
||||||
buffer.set_active_selections(merged_selections, cx);
|
buffer.set_active_selections(merged_selections, line_mode, cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2397,7 +2398,7 @@ impl MultiBufferSnapshot {
|
||||||
pub fn remote_selections_in_range<'a>(
|
pub fn remote_selections_in_range<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
range: &'a Range<Anchor>,
|
range: &'a Range<Anchor>,
|
||||||
) -> impl 'a + Iterator<Item = (ReplicaId, Selection<Anchor>)> {
|
) -> impl 'a + Iterator<Item = (ReplicaId, bool, Selection<Anchor>)> {
|
||||||
let mut cursor = self.excerpts.cursor::<Option<&ExcerptId>>();
|
let mut cursor = self.excerpts.cursor::<Option<&ExcerptId>>();
|
||||||
cursor.seek(&Some(&range.start.excerpt_id), Bias::Left, &());
|
cursor.seek(&Some(&range.start.excerpt_id), Bias::Left, &());
|
||||||
cursor
|
cursor
|
||||||
|
@ -2414,7 +2415,7 @@ impl MultiBufferSnapshot {
|
||||||
excerpt
|
excerpt
|
||||||
.buffer
|
.buffer
|
||||||
.remote_selections_in_range(query_range)
|
.remote_selections_in_range(query_range)
|
||||||
.flat_map(move |(replica_id, selections)| {
|
.flat_map(move |(replica_id, line_mode, selections)| {
|
||||||
selections.map(move |selection| {
|
selections.map(move |selection| {
|
||||||
let mut start = Anchor {
|
let mut start = Anchor {
|
||||||
buffer_id: Some(excerpt.buffer_id),
|
buffer_id: Some(excerpt.buffer_id),
|
||||||
|
@ -2435,6 +2436,7 @@ impl MultiBufferSnapshot {
|
||||||
|
|
||||||
(
|
(
|
||||||
replica_id,
|
replica_id,
|
||||||
|
line_mode,
|
||||||
Selection {
|
Selection {
|
||||||
id: selection.id,
|
id: selection.id,
|
||||||
start,
|
start,
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub struct SelectionsCollection {
|
||||||
display_map: ModelHandle<DisplayMap>,
|
display_map: ModelHandle<DisplayMap>,
|
||||||
buffer: ModelHandle<MultiBuffer>,
|
buffer: ModelHandle<MultiBuffer>,
|
||||||
pub next_selection_id: usize,
|
pub next_selection_id: usize,
|
||||||
|
pub line_mode: bool,
|
||||||
disjoint: Arc<[Selection<Anchor>]>,
|
disjoint: Arc<[Selection<Anchor>]>,
|
||||||
pending: Option<PendingSelection>,
|
pending: Option<PendingSelection>,
|
||||||
}
|
}
|
||||||
|
@ -37,6 +38,7 @@ impl SelectionsCollection {
|
||||||
display_map,
|
display_map,
|
||||||
buffer,
|
buffer,
|
||||||
next_selection_id: 1,
|
next_selection_id: 1,
|
||||||
|
line_mode: true,
|
||||||
disjoint: Arc::from([]),
|
disjoint: Arc::from([]),
|
||||||
pending: Some(PendingSelection {
|
pending: Some(PendingSelection {
|
||||||
selection: Selection {
|
selection: Selection {
|
||||||
|
|
|
@ -83,6 +83,7 @@ pub struct BufferSnapshot {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct SelectionSet {
|
struct SelectionSet {
|
||||||
|
line_mode: bool,
|
||||||
selections: Arc<[Selection<Anchor>]>,
|
selections: Arc<[Selection<Anchor>]>,
|
||||||
lamport_timestamp: clock::Lamport,
|
lamport_timestamp: clock::Lamport,
|
||||||
}
|
}
|
||||||
|
@ -129,6 +130,7 @@ pub enum Operation {
|
||||||
UpdateSelections {
|
UpdateSelections {
|
||||||
selections: Arc<[Selection<Anchor>]>,
|
selections: Arc<[Selection<Anchor>]>,
|
||||||
lamport_timestamp: clock::Lamport,
|
lamport_timestamp: clock::Lamport,
|
||||||
|
line_mode: bool,
|
||||||
},
|
},
|
||||||
UpdateCompletionTriggers {
|
UpdateCompletionTriggers {
|
||||||
triggers: Vec<String>,
|
triggers: Vec<String>,
|
||||||
|
@ -343,6 +345,7 @@ impl Buffer {
|
||||||
this.remote_selections.insert(
|
this.remote_selections.insert(
|
||||||
selection_set.replica_id as ReplicaId,
|
selection_set.replica_id as ReplicaId,
|
||||||
SelectionSet {
|
SelectionSet {
|
||||||
|
line_mode: selection_set.line_mode,
|
||||||
selections: proto::deserialize_selections(selection_set.selections),
|
selections: proto::deserialize_selections(selection_set.selections),
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
},
|
},
|
||||||
|
@ -385,6 +388,7 @@ impl Buffer {
|
||||||
replica_id: *replica_id as u32,
|
replica_id: *replica_id as u32,
|
||||||
selections: proto::serialize_selections(&set.selections),
|
selections: proto::serialize_selections(&set.selections),
|
||||||
lamport_timestamp: set.lamport_timestamp.value,
|
lamport_timestamp: set.lamport_timestamp.value,
|
||||||
|
line_mode: set.line_mode,
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
|
diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
|
||||||
|
@ -1030,6 +1034,7 @@ impl Buffer {
|
||||||
pub fn set_active_selections(
|
pub fn set_active_selections(
|
||||||
&mut self,
|
&mut self,
|
||||||
selections: Arc<[Selection<Anchor>]>,
|
selections: Arc<[Selection<Anchor>]>,
|
||||||
|
line_mode: bool,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
let lamport_timestamp = self.text.lamport_clock.tick();
|
let lamport_timestamp = self.text.lamport_clock.tick();
|
||||||
|
@ -1038,11 +1043,13 @@ impl Buffer {
|
||||||
SelectionSet {
|
SelectionSet {
|
||||||
selections: selections.clone(),
|
selections: selections.clone(),
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
|
line_mode,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
self.send_operation(
|
self.send_operation(
|
||||||
Operation::UpdateSelections {
|
Operation::UpdateSelections {
|
||||||
selections,
|
selections,
|
||||||
|
line_mode,
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
},
|
},
|
||||||
cx,
|
cx,
|
||||||
|
@ -1050,7 +1057,7 @@ impl Buffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
|
pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
|
||||||
self.set_active_selections(Arc::from([]), cx);
|
self.set_active_selections(Arc::from([]), false, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
|
pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
|
||||||
|
@ -1287,6 +1294,7 @@ impl Buffer {
|
||||||
Operation::UpdateSelections {
|
Operation::UpdateSelections {
|
||||||
selections,
|
selections,
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
|
line_mode,
|
||||||
} => {
|
} => {
|
||||||
if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
|
if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
|
||||||
if set.lamport_timestamp > lamport_timestamp {
|
if set.lamport_timestamp > lamport_timestamp {
|
||||||
|
@ -1299,6 +1307,7 @@ impl Buffer {
|
||||||
SelectionSet {
|
SelectionSet {
|
||||||
selections,
|
selections,
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
|
line_mode,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
self.text.lamport_clock.observe(lamport_timestamp);
|
self.text.lamport_clock.observe(lamport_timestamp);
|
||||||
|
@ -1890,8 +1899,14 @@ impl BufferSnapshot {
|
||||||
pub fn remote_selections_in_range<'a>(
|
pub fn remote_selections_in_range<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
range: Range<Anchor>,
|
range: Range<Anchor>,
|
||||||
) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
|
) -> impl 'a
|
||||||
{
|
+ Iterator<
|
||||||
|
Item = (
|
||||||
|
ReplicaId,
|
||||||
|
bool,
|
||||||
|
impl 'a + Iterator<Item = &'a Selection<Anchor>>,
|
||||||
|
),
|
||||||
|
> {
|
||||||
self.remote_selections
|
self.remote_selections
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(replica_id, set)| {
|
.filter(|(replica_id, set)| {
|
||||||
|
@ -1909,7 +1924,11 @@ impl BufferSnapshot {
|
||||||
Ok(ix) | Err(ix) => ix,
|
Ok(ix) | Err(ix) => ix,
|
||||||
};
|
};
|
||||||
|
|
||||||
(*replica_id, set.selections[start_ix..end_ix].iter())
|
(
|
||||||
|
*replica_id,
|
||||||
|
set.line_mode,
|
||||||
|
set.selections[start_ix..end_ix].iter(),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,11 +43,13 @@ pub fn serialize_operation(operation: &Operation) -> proto::Operation {
|
||||||
}),
|
}),
|
||||||
Operation::UpdateSelections {
|
Operation::UpdateSelections {
|
||||||
selections,
|
selections,
|
||||||
|
line_mode,
|
||||||
lamport_timestamp,
|
lamport_timestamp,
|
||||||
} => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
|
} => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
|
||||||
replica_id: lamport_timestamp.replica_id as u32,
|
replica_id: lamport_timestamp.replica_id as u32,
|
||||||
lamport_timestamp: lamport_timestamp.value,
|
lamport_timestamp: lamport_timestamp.value,
|
||||||
selections: serialize_selections(selections),
|
selections: serialize_selections(selections),
|
||||||
|
line_mode: *line_mode,
|
||||||
}),
|
}),
|
||||||
Operation::UpdateDiagnostics {
|
Operation::UpdateDiagnostics {
|
||||||
diagnostics,
|
diagnostics,
|
||||||
|
@ -217,6 +219,7 @@ pub fn deserialize_operation(message: proto::Operation) -> Result<Operation> {
|
||||||
value: message.lamport_timestamp,
|
value: message.lamport_timestamp,
|
||||||
},
|
},
|
||||||
selections: Arc::from(selections),
|
selections: Arc::from(selections),
|
||||||
|
line_mode: message.line_mode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
proto::operation::Variant::UpdateDiagnostics(message) => Operation::UpdateDiagnostics {
|
proto::operation::Variant::UpdateDiagnostics(message) => Operation::UpdateDiagnostics {
|
||||||
|
|
|
@ -828,7 +828,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
|
||||||
selections
|
selections
|
||||||
);
|
);
|
||||||
active_selections.insert(replica_id, selections.clone());
|
active_selections.insert(replica_id, selections.clone());
|
||||||
buffer.set_active_selections(selections, cx);
|
buffer.set_active_selections(selections, false, cx);
|
||||||
});
|
});
|
||||||
mutation_count -= 1;
|
mutation_count -= 1;
|
||||||
}
|
}
|
||||||
|
@ -984,7 +984,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
|
||||||
let buffer = buffer.read(cx).snapshot();
|
let buffer = buffer.read(cx).snapshot();
|
||||||
let actual_remote_selections = buffer
|
let actual_remote_selections = buffer
|
||||||
.remote_selections_in_range(Anchor::MIN..Anchor::MAX)
|
.remote_selections_in_range(Anchor::MIN..Anchor::MAX)
|
||||||
.map(|(replica_id, selections)| (replica_id, selections.collect::<Vec<_>>()))
|
.map(|(replica_id, _, selections)| (replica_id, selections.collect::<Vec<_>>()))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let expected_remote_selections = active_selections
|
let expected_remote_selections = active_selections
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -779,6 +779,7 @@ message SelectionSet {
|
||||||
uint32 replica_id = 1;
|
uint32 replica_id = 1;
|
||||||
repeated Selection selections = 2;
|
repeated Selection selections = 2;
|
||||||
uint32 lamport_timestamp = 3;
|
uint32 lamport_timestamp = 3;
|
||||||
|
bool line_mode = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Selection {
|
message Selection {
|
||||||
|
@ -854,6 +855,7 @@ message Operation {
|
||||||
uint32 replica_id = 1;
|
uint32 replica_id = 1;
|
||||||
uint32 lamport_timestamp = 2;
|
uint32 lamport_timestamp = 2;
|
||||||
repeated Selection selections = 3;
|
repeated Selection selections = 3;
|
||||||
|
bool line_mode = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message UpdateCompletionTriggers {
|
message UpdateCompletionTriggers {
|
||||||
|
|
|
@ -18,22 +18,29 @@ fn editor_created(EditorCreated(editor): &EditorCreated, cx: &mut MutableAppCont
|
||||||
}
|
}
|
||||||
|
|
||||||
fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
|
fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
|
||||||
Vim::update(cx, |state, cx| {
|
Vim::update(cx, |vim, cx| {
|
||||||
state.active_editor = Some(editor.downgrade());
|
vim.active_editor = Some(editor.downgrade());
|
||||||
|
vim.selection_subscription = Some(cx.subscribe(editor, |editor, event, cx| {
|
||||||
|
if let editor::Event::SelectionsChanged { local: true } = event {
|
||||||
|
let newest_empty = !editor.read(cx).selections.newest::<usize>(cx).is_empty();
|
||||||
|
editor_local_selections_changed(newest_empty, cx);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
if editor.read(cx).mode() != EditorMode::Full {
|
if editor.read(cx).mode() != EditorMode::Full {
|
||||||
state.switch_mode(Mode::Insert, cx);
|
vim.switch_mode(Mode::Insert, cx);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn editor_blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
|
fn editor_blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
|
||||||
Vim::update(cx, |state, cx| {
|
Vim::update(cx, |vim, cx| {
|
||||||
if let Some(previous_editor) = state.active_editor.clone() {
|
if let Some(previous_editor) = vim.active_editor.clone() {
|
||||||
if previous_editor == editor.clone() {
|
if previous_editor == editor.clone() {
|
||||||
state.active_editor = None;
|
vim.active_editor = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state.sync_editor_options(cx);
|
vim.sync_editor_options(cx);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,3 +54,11 @@ fn editor_released(EditorReleased(editor): &EditorReleased, cx: &mut MutableAppC
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn editor_local_selections_changed(newest_empty: bool, cx: &mut MutableAppContext) {
|
||||||
|
Vim::update(cx, |vim, cx| {
|
||||||
|
if vim.state.mode == Mode::Normal && !newest_empty {
|
||||||
|
vim.switch_mode(Mode::Visual, cx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -112,6 +112,7 @@ fn motion(motion: Motion, cx: &mut MutableAppContext) {
|
||||||
match Vim::read(cx).state.mode {
|
match Vim::read(cx).state.mode {
|
||||||
Mode::Normal => normal_motion(motion, cx),
|
Mode::Normal => normal_motion(motion, cx),
|
||||||
Mode::Visual => visual_motion(motion, cx),
|
Mode::Visual => visual_motion(motion, cx),
|
||||||
|
Mode::VisualLine => visual_motion(motion, cx),
|
||||||
Mode::Insert => {
|
Mode::Insert => {
|
||||||
// Shouldn't execute a motion in insert mode. Ignoring
|
// Shouldn't execute a motion in insert mode. Ignoring
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ pub enum Mode {
|
||||||
Normal,
|
Normal,
|
||||||
Insert,
|
Insert,
|
||||||
Visual,
|
Visual,
|
||||||
|
VisualLine,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Mode {
|
impl Default for Mode {
|
||||||
|
@ -36,8 +37,7 @@ pub struct VimState {
|
||||||
impl VimState {
|
impl VimState {
|
||||||
pub fn cursor_shape(&self) -> CursorShape {
|
pub fn cursor_shape(&self) -> CursorShape {
|
||||||
match self.mode {
|
match self.mode {
|
||||||
Mode::Normal => CursorShape::Block,
|
Mode::Normal | Mode::Visual | Mode::VisualLine => CursorShape::Block,
|
||||||
Mode::Visual => CursorShape::Block,
|
|
||||||
Mode::Insert => CursorShape::Bar,
|
Mode::Insert => CursorShape::Bar,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ impl VimState {
|
||||||
match self.mode {
|
match self.mode {
|
||||||
Mode::Normal => "normal",
|
Mode::Normal => "normal",
|
||||||
Mode::Visual => "visual",
|
Mode::Visual => "visual",
|
||||||
|
Mode::VisualLine => "visual_line",
|
||||||
Mode::Insert => "insert",
|
Mode::Insert => "insert",
|
||||||
}
|
}
|
||||||
.to_string(),
|
.to_string(),
|
||||||
|
|
|
@ -10,7 +10,7 @@ mod visual;
|
||||||
|
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use editor::{CursorShape, Editor};
|
use editor::{CursorShape, Editor};
|
||||||
use gpui::{impl_actions, MutableAppContext, ViewContext, WeakViewHandle};
|
use gpui::{impl_actions, MutableAppContext, Subscription, ViewContext, WeakViewHandle};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
|
@ -51,6 +51,7 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||||
pub struct Vim {
|
pub struct Vim {
|
||||||
editors: HashMap<usize, WeakViewHandle<Editor>>,
|
editors: HashMap<usize, WeakViewHandle<Editor>>,
|
||||||
active_editor: Option<WeakViewHandle<Editor>>,
|
active_editor: Option<WeakViewHandle<Editor>>,
|
||||||
|
selection_subscription: Option<Subscription>,
|
||||||
|
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
state: VimState,
|
state: VimState,
|
||||||
|
|
|
@ -4,11 +4,21 @@ use workspace::Workspace;
|
||||||
|
|
||||||
use crate::{motion::Motion, state::Mode, Vim};
|
use crate::{motion::Motion, state::Mode, Vim};
|
||||||
|
|
||||||
actions!(vim, [VisualDelete, VisualChange]);
|
actions!(
|
||||||
|
vim,
|
||||||
|
[
|
||||||
|
VisualDelete,
|
||||||
|
VisualChange,
|
||||||
|
VisualLineDelete,
|
||||||
|
VisualLineChange
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
pub fn init(cx: &mut MutableAppContext) {
|
pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_action(change);
|
cx.add_action(change);
|
||||||
|
cx.add_action(change_line);
|
||||||
cx.add_action(delete);
|
cx.add_action(delete);
|
||||||
|
cx.add_action(delete_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn visual_motion(motion: Motion, cx: &mut MutableAppContext) {
|
pub fn visual_motion(motion: Motion, cx: &mut MutableAppContext) {
|
||||||
|
@ -58,6 +68,22 @@ pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspac
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn change_line(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspace>) {
|
||||||
|
Vim::update(cx, |vim, cx| {
|
||||||
|
vim.update_active_editor(cx, |editor, cx| {
|
||||||
|
editor.set_clip_at_line_ends(false, cx);
|
||||||
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||||
|
s.move_with(|map, selection| {
|
||||||
|
selection.start = map.prev_line_boundary(selection.start.to_point(map)).1;
|
||||||
|
selection.end = map.next_line_boundary(selection.end.to_point(map)).1;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
editor.insert("", cx);
|
||||||
|
});
|
||||||
|
vim.switch_mode(Mode::Insert, cx);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspace>) {
|
pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspace>) {
|
||||||
Vim::update(cx, |vim, cx| {
|
Vim::update(cx, |vim, cx| {
|
||||||
vim.switch_mode(Mode::Normal, cx);
|
vim.switch_mode(Mode::Normal, cx);
|
||||||
|
@ -88,6 +114,43 @@ pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspac
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete_line(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspace>) {
|
||||||
|
Vim::update(cx, |vim, cx| {
|
||||||
|
vim.switch_mode(Mode::Normal, cx);
|
||||||
|
vim.update_active_editor(cx, |editor, cx| {
|
||||||
|
editor.set_clip_at_line_ends(false, cx);
|
||||||
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||||
|
s.move_with(|map, selection| {
|
||||||
|
selection.start = map.prev_line_boundary(selection.start.to_point(map)).1;
|
||||||
|
|
||||||
|
if selection.end.row() < map.max_point().row() {
|
||||||
|
*selection.end.row_mut() += 1;
|
||||||
|
*selection.end.column_mut() = 0;
|
||||||
|
// Don't reset the end here
|
||||||
|
return;
|
||||||
|
} else if selection.start.row() > 0 {
|
||||||
|
*selection.start.row_mut() -= 1;
|
||||||
|
*selection.start.column_mut() = map.line_len(selection.start.row());
|
||||||
|
}
|
||||||
|
|
||||||
|
selection.end = map.next_line_boundary(selection.end.to_point(map)).1;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
editor.insert("", cx);
|
||||||
|
|
||||||
|
// Fixup cursor position after the deletion
|
||||||
|
editor.set_clip_at_line_ends(true, cx);
|
||||||
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||||
|
s.move_with(|map, selection| {
|
||||||
|
let mut cursor = selection.head();
|
||||||
|
cursor = map.clip_point(cursor, Bias::Left);
|
||||||
|
selection.collapse_to(cursor, selection.goal)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue