on master help sign in
Trivial jumplist implementation.
8657c57Blaž Hrastnik
helix-term/src/commands.rs+22
@@ -1642,3 +1642,25 @@ pub fn match_brackets(cx: &mut Context) {
1642 };
1642 };
1643 }
1643 }
1644}
1644}
1645+
1646+//
1647+
1648+pub fn jump_forward(cx: &mut Context) {
1649+ let count = cx.count;
1650+ let view = cx.view();
1651+
1652+ if let Some((id, selection)) = view.jumps.forward(count) {
1653+ view.first_line = 0;
1654+ view.doc = *id;
1655+ };
1656+}
1657+
1658+pub fn jump_backward(cx: &mut Context) {
1659+ let count = cx.count;
1660+ let view = cx.view();
1661+
1662+ if let Some((id, selection)) = view.jumps.backward(count) {
1663+ view.first_line = 0;
1664+ view.doc = *id;
1665+ };
1666+}
helix-term/src/keymap.rs+3
@@ -253,6 +253,9 @@ pub fn default() -> Keymaps {
253 shift!('K') => commands::hover,
253 shift!('K') => commands::hover,
254
254
255 // z family for save/restore/combine from/to sels from register
255 // z family for save/restore/combine from/to sels from register
256+
257+ ctrl!('i') => commands::jump_forward, // TODO: ctrl-i conflicts tab
258+ ctrl!('o') => commands::jump_backward,
256 );
259 );
257 // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
260 // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
258 // we keep this separate select mode. More keys can fit into normal mode then, but it's weird
261 // we keep this separate select mode. More keys can fit into normal mode then, but it's weird
helix-view/src/editor.rs+4
@@ -90,7 +90,11 @@ impl Editor {
90 use crate::tree::Layout;
90 use crate::tree::Layout;
91 match action {
91 match action {
92 Action::Replace => {
92 Action::Replace => {
93+ let view = self.view();
94+ let jump = (view.doc, self.documents[view.doc].selection().clone());
95+
93 let view = self.view_mut();
96 let view = self.view_mut();
97+ view.jumps.push(jump);
94 view.doc = id;
98 view.doc = id;
95 view.first_line = 0;
99 view.first_line = 0;
96 return Ok(id);
100 return Ok(id);
helix-view/src/view.rs+41−1
@@ -5,17 +5,56 @@ use std::borrow::Cow;
5use crate::{Document, DocumentId, ViewId};
5use crate::{Document, DocumentId, ViewId};
6use helix_core::{
6use helix_core::{
7 graphemes::{grapheme_width, RopeGraphemes},
7 graphemes::{grapheme_width, RopeGraphemes},
8− Position, RopeSlice,
8+ Position, RopeSlice, Selection,
9};
9};
10use tui::layout::Rect;
10use tui::layout::Rect;
11
11
12pub const PADDING: usize = 5;
12pub const PADDING: usize = 5;
13
13
14+type Jump = (DocumentId, Selection);
15+
16+pub struct JumpList {
17+ jumps: Vec<Jump>,
18+ current: usize,
19+}
20+
21+impl JumpList {
22+ pub fn new(initial: Jump) -> Self {
23+ Self {
24+ jumps: vec![initial],
25+ current: 0,
26+ }
27+ }
28+
29+ pub fn push(&mut self, jump: Jump) {
30+ self.jumps.truncate(self.current + 1);
31+ self.jumps.push(jump);
32+ self.current += 1;
33+ }
34+
35+ pub fn forward(&mut self, count: usize) -> Option<&Jump> {
36+ if self.current + count < self.jumps.len() {
37+ self.current += count;
38+ return self.jumps.get(self.current);
39+ }
40+ None
41+ }
42+
43+ pub fn backward(&mut self, count: usize) -> Option<&Jump> {
44+ if self.current.checked_sub(count).is_some() {
45+ self.current -= count;
46+ return self.jumps.get(self.current);
47+ }
48+ None
49+ }
50+}
51+
14pub struct View {
52pub struct View {
15 pub id: ViewId,
53 pub id: ViewId,
16 pub doc: DocumentId,
54 pub doc: DocumentId,
17 pub first_line: usize,
55 pub first_line: usize,
18 pub area: Rect,
56 pub area: Rect,
57+ pub jumps: JumpList,
19}
58}
20
59
21impl View {
60impl View {
@@ -25,6 +64,7 @@ impl View {
25 doc,
64 doc,
26 first_line: 0,
65 first_line: 0,
27 area: Rect::default(), // will get calculated upon inserting into tree
66 area: Rect::default(), // will get calculated upon inserting into tree
67+ jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
28 };
68 };
29
69
30 Ok(view)
70 Ok(view)