Skip to content

Commit 028db87

Browse files
committed
Add missing ViewId related updates
* swap contents of views except id and area * update selection information stored in Document on swap
1 parent 90f3242 commit 028db87

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

helix-view/src/document.rs

+20
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,26 @@ impl Document {
636636
.insert(view_id, selection.ensure_invariants(self.text().slice(..)));
637637
}
638638

639+
pub fn swap_selection_in_views(&mut self, view_a: ViewId, view_b: ViewId) {
640+
let selection_a = self.selections.remove(&view_a);
641+
let selection_b = self.selections.remove(&view_b);
642+
643+
match (selection_a, selection_b) {
644+
(Some(selection_a), Some(selection_b)) => {
645+
// swapped same document with different views
646+
self.selections.insert(view_a, selection_b);
647+
self.selections.insert(view_b, selection_a);
648+
}
649+
(Some(selection_a), _) => {
650+
self.selections.insert(view_b, selection_a);
651+
}
652+
(_, Some(selection_b)) => {
653+
self.selections.insert(view_a, selection_b);
654+
}
655+
_ => unreachable!(),
656+
}
657+
}
658+
639659
/// Apply a [`Transaction`] to the [`Document`] to change its text.
640660
fn apply_impl(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
641661
let old_doc = self.text().clone();

helix-view/src/editor.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -885,20 +885,31 @@ impl Editor {
885885
self.tree.focus_direction(tree::Direction::Down);
886886
}
887887

888+
fn swap_in_direction(&mut self, direction: tree::Direction) {
889+
if let Some(update) = self.tree.swap_split_in_direction(direction) {
890+
for doc_id in &update.documents {
891+
if let Some(doc) = self.document_mut(*doc_id) {
892+
let [view_a, view_b] = update.views;
893+
doc.swap_selection_in_views(view_a, view_b);
894+
}
895+
}
896+
}
897+
}
898+
888899
pub fn swap_right(&mut self) {
889-
self.tree.swap_split_in_direction(tree::Direction::Right);
900+
self.swap_in_direction(tree::Direction::Right);
890901
}
891902

892903
pub fn swap_left(&mut self) {
893-
self.tree.swap_split_in_direction(tree::Direction::Left);
904+
self.swap_in_direction(tree::Direction::Left);
894905
}
895906

896907
pub fn swap_up(&mut self) {
897-
self.tree.swap_split_in_direction(tree::Direction::Up);
908+
self.swap_in_direction(tree::Direction::Up);
898909
}
899910

900911
pub fn swap_down(&mut self) {
901-
self.tree.swap_split_in_direction(tree::Direction::Down);
912+
self.swap_in_direction(tree::Direction::Down);
902913
}
903914

904915
pub fn should_close(&self) -> bool {

helix-view/src/tree.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{graphics::Rect, View, ViewId};
1+
use crate::{graphics::Rect, DocumentId, View, ViewId};
22
use slotmap::HopSlotMap;
33

44
// the dimensions are recomputed on window resize/tree change.
@@ -29,6 +29,12 @@ pub enum Content {
2929
Container(Box<Container>),
3030
}
3131

32+
#[derive(Debug)]
33+
pub struct ViewSwap {
34+
pub documents: [DocumentId; 2],
35+
pub views: [ViewId; 2],
36+
}
37+
3238
impl Node {
3339
pub fn container(layout: Layout) -> Self {
3440
Self {
@@ -526,14 +532,18 @@ impl Tree {
526532
}
527533
}
528534

529-
pub fn swap_split_in_direction(&mut self, direction: Direction) {
535+
pub fn swap_split_in_direction(&mut self, direction: Direction) -> Option<ViewSwap> {
530536
if let Some(id) = self.find_split_in_direction(self.focus, direction) {
531537
if let Some([focused, target]) = self.nodes.get_disjoint_mut([self.focus, id]) {
532538
match (&mut focused.content, &mut target.content) {
533539
(Content::View(focused), Content::View(target)) => {
534-
std::mem::swap(&mut focused.doc, &mut target.doc);
535-
std::mem::swap(&mut focused.id, &mut target.id);
540+
View::swap_content(focused, target);
536541
self.focus = id;
542+
543+
return Some(ViewSwap {
544+
documents: [focused.doc, target.doc],
545+
views: [focused.id, target.id],
546+
});
537547
}
538548
// self.focus always points to a view which has a content of Content::View
539549
// and find_split_in_direction() only returns a view which has content of
@@ -542,6 +552,7 @@ impl Tree {
542552
}
543553
}
544554
}
555+
None
545556
}
546557

547558
pub fn area(&self) -> Rect {

helix-view/src/view.rs

+15
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ impl View {
117117
}
118118
}
119119

120+
pub fn swap_content(view_a: &mut View, view_b: &mut View) {
121+
std::mem::swap(&mut view_a.doc, &mut view_b.doc);
122+
std::mem::swap(&mut view_a.offset, &mut view_b.offset);
123+
std::mem::swap(&mut view_a.jumps, &mut view_b.jumps);
124+
std::mem::swap(
125+
&mut view_a.docs_access_history,
126+
&mut view_b.docs_access_history,
127+
);
128+
std::mem::swap(
129+
&mut view_a.last_modified_docs,
130+
&mut view_b.last_modified_docs,
131+
);
132+
std::mem::swap(&mut view_a.gutters, &mut view_b.gutters);
133+
}
134+
120135
pub fn add_to_history(&mut self, id: DocumentId) {
121136
if let Some(pos) = self.docs_access_history.iter().position(|&doc| doc == id) {
122137
self.docs_access_history.remove(pos);

0 commit comments

Comments
 (0)