on master help sign in
add alternate file (#223)

* add alternate file inspired by vim ctrl-6/kak ga commands. the alternate file is kept per view * apply feedback from #223 * rename to last_accessed * add ga doc * add fail message for ga

commit44cc0d8eb0743101724fca8787217a9d0aa01bd4 authorRobin <robin@robinjint.nl> dateSat 12 Jun 2021, 14:21 (5 years ago) parent1953588
4 files changed+25
book/src/keymap.md+1 helix-term/src/commands.rs+14 helix-view/src/editor.rs+7 helix-view/src/view.rs+3
book/src/keymap.md+1
@@ -141,6 +141,7 @@ Jumps to various locations.
141141| y | Go to type definition |
142142| r | Go to references |
143143| i | Go to implementation |
144+| a | Go to the last accessed/alternate file |
144145
145146## Object mode
146147
helix-term/src/commands.rs+14
@@ -1317,6 +1317,15 @@ fn push_jump(editor: &mut Editor) {
13171317 view.jumps.push(jump);
13181318}
13191319
1320+fn switch_to_last_accessed_file(cx: &mut Context) {
1321+ let alternate_file = cx.view().last_accessed_doc;
1322+ if let Some(alt) = alternate_file {
1323+ cx.editor.switch(alt, Action::Replace);
1324+ } else {
1325+ cx.editor.set_error("no last buffer".to_owned())
1326+ }
1327+}
1328+
13201329pub fn goto_mode(cx: &mut Context) {
13211330 if let Some(count) = cx._count {
13221331 push_jump(cx.editor);
@@ -1338,6 +1347,7 @@ pub fn goto_mode(cx: &mut Context) {
13381347 match (cx.doc().mode, ch) {
13391348 (_, 'g') => move_file_start(cx),
13401349 (_, 'e') => move_file_end(cx),
1350+ (_, 'a') => switch_to_last_accessed_file(cx),
13411351 (Mode::Normal, 'h') => move_line_start(cx),
13421352 (Mode::Normal, 'l') => move_line_end(cx),
13431353 (Mode::Select, 'h') => extend_line_start(cx),
@@ -2449,6 +2459,10 @@ pub fn jump_backward(cx: &mut Context) {
24492459 let (view, doc) = cx.current();
24502460
24512461 if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
2462+ // manually set the alternate_file as we cannot use the Editor::switch function here.
2463+ if view.doc != *id {
2464+ view.last_accessed_doc = Some(view.doc)
2465+ }
24522466 view.doc = *id;
24532467 let selection = selection.clone();
24542468 let (view, doc) = cx.current(); // refetch doc
helix-view/src/editor.rs+7
@@ -88,6 +88,12 @@ impl Editor {
8888 pub fn switch(&mut self, id: DocumentId, action: Action) {
8989 use crate::tree::Layout;
9090 use helix_core::Selection;
91+
92+ if !self.documents.contains_key(id) {
93+ log::error!("cannot switch to document that does not exist (anymore)");
94+ return;
95+ }
96+
9197 match action {
9298 Action::Replace => {
9399 let view = self.view();
@@ -98,6 +104,7 @@ impl Editor {
98104
99105 let view = self.view_mut();
100106 view.jumps.push(jump);
107+ view.last_accessed_doc = Some(view.doc);
101108 view.doc = id;
102109 view.first_line = 0;
103110
helix-view/src/view.rs+3
@@ -67,6 +67,8 @@ pub struct View {
6767 pub first_col: usize,
6868 pub area: Rect,
6969 pub jumps: JumpList,
70+ /// the last accessed file before the current one
71+ pub last_accessed_doc: Option<DocumentId>,
7072}
7173
7274impl View {
@@ -78,6 +80,7 @@ impl View {
7880 first_col: 0,
7981 area: Rect::default(), // will get calculated upon inserting into tree
8082 jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
83+ last_accessed_doc: None,
8184 }
8285 }
8386