on master help sign in
Trivial jumplist implementation.
commit8657c57cf2d56b9a89e2f780675426f4c78df48b authorBlaž Hrastnik <blaz@mxxn.io> dateWed 24 Mar 2021, 18:01 (6 years ago) parent4b176ca
4 files changed+70−1
helix-term/src/commands.rs+22 helix-term/src/keymap.rs+3 helix-view/src/editor.rs+4 helix-view/src/view.rs+41−1
helix-term/src/commands.rs+22
@@ -1642,3 +1642,25 @@ pub fn match_brackets(cx: &mut Context) {
16421642 };
16431643 }
16441644}
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 {
253253 shift!('K') => commands::hover,
254254
255255 // 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,
256259 );
257260 // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
258261 // 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 {
9090 use crate::tree::Layout;
9191 match action {
9292 Action::Replace => {
93+ let view = self.view();
94+ let jump = (view.doc, self.documents[view.doc].selection().clone());
95+
9396 let view = self.view_mut();
97+ view.jumps.push(jump);
9498 view.doc = id;
9599 view.first_line = 0;
96100 return Ok(id);
helix-view/src/view.rs+41−1
@@ -5,17 +5,56 @@ use std::borrow::Cow;
55use crate::{Document, DocumentId, ViewId};
66use helix_core::{
77 graphemes::{grapheme_width, RopeGraphemes},
8− Position, RopeSlice,
8+ Position, RopeSlice, Selection,
99};
1010use tui::layout::Rect;
1111
1212pub const PADDING: usize = 5;
1313
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+
1452pub struct View {
1553 pub id: ViewId,
1654 pub doc: DocumentId,
1755 pub first_line: usize,
1856 pub area: Rect,
57+ pub jumps: JumpList,
1958}
2059
2160impl View {
@@ -25,6 +64,7 @@ impl View {
2564 doc,
2665 first_line: 0,
2766 area: Rect::default(), // will get calculated upon inserting into tree
67+ jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
2868 };
2969
3070 Ok(view)