on master help sign in
add alternate file (#223)
44cc0d8Robin
book/src/keymap.md+1
@@ -141,6 +141,7 @@ Jumps to various locations.
141| y | Go to type definition |
141| y | Go to type definition |
142| r | Go to references |
142| r | Go to references |
143| i | Go to implementation |
143| i | Go to implementation |
144+| a | Go to the last accessed/alternate file |
144
145
145## Object mode
146## Object mode
146
147
helix-term/src/commands.rs+14
@@ -1317,6 +1317,15 @@ fn push_jump(editor: &mut Editor) {
1317 view.jumps.push(jump);
1317 view.jumps.push(jump);
1318}
1318}
1319
1319
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+
1320pub fn goto_mode(cx: &mut Context) {
1329pub fn goto_mode(cx: &mut Context) {
1321 if let Some(count) = cx._count {
1330 if let Some(count) = cx._count {
1322 push_jump(cx.editor);
1331 push_jump(cx.editor);
@@ -1338,6 +1347,7 @@ pub fn goto_mode(cx: &mut Context) {
1338 match (cx.doc().mode, ch) {
1347 match (cx.doc().mode, ch) {
1339 (_, 'g') => move_file_start(cx),
1348 (_, 'g') => move_file_start(cx),
1340 (_, 'e') => move_file_end(cx),
1349 (_, 'e') => move_file_end(cx),
1350+ (_, 'a') => switch_to_last_accessed_file(cx),
1341 (Mode::Normal, 'h') => move_line_start(cx),
1351 (Mode::Normal, 'h') => move_line_start(cx),
1342 (Mode::Normal, 'l') => move_line_end(cx),
1352 (Mode::Normal, 'l') => move_line_end(cx),
1343 (Mode::Select, 'h') => extend_line_start(cx),
1353 (Mode::Select, 'h') => extend_line_start(cx),
@@ -2449,6 +2459,10 @@ pub fn jump_backward(cx: &mut Context) {
2449 let (view, doc) = cx.current();
2459 let (view, doc) = cx.current();
2450
2460
2451 if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
2461 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+ }
2452 view.doc = *id;
2466 view.doc = *id;
2453 let selection = selection.clone();
2467 let selection = selection.clone();
2454 let (view, doc) = cx.current(); // refetch doc
2468 let (view, doc) = cx.current(); // refetch doc
helix-view/src/editor.rs+7
@@ -88,6 +88,12 @@ impl Editor {
88 pub fn switch(&mut self, id: DocumentId, action: Action) {
88 pub fn switch(&mut self, id: DocumentId, action: Action) {
89 use crate::tree::Layout;
89 use crate::tree::Layout;
90 use helix_core::Selection;
90 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+
91 match action {
97 match action {
92 Action::Replace => {
98 Action::Replace => {
93 let view = self.view();
99 let view = self.view();
@@ -98,6 +104,7 @@ impl Editor {
98
104
99 let view = self.view_mut();
105 let view = self.view_mut();
100 view.jumps.push(jump);
106 view.jumps.push(jump);
107+ view.last_accessed_doc = Some(view.doc);
101 view.doc = id;
108 view.doc = id;
102 view.first_line = 0;
109 view.first_line = 0;
103
110
helix-view/src/view.rs+3
@@ -67,6 +67,8 @@ pub struct View {
67 pub first_col: usize,
67 pub first_col: usize,
68 pub area: Rect,
68 pub area: Rect,
69 pub jumps: JumpList,
69 pub jumps: JumpList,
70+ /// the last accessed file before the current one
71+ pub last_accessed_doc: Option<DocumentId>,
70}
72}
71
73
72impl View {
74impl View {
@@ -78,6 +80,7 @@ impl View {
78 first_col: 0,
80 first_col: 0,
79 area: Rect::default(), // will get calculated upon inserting into tree
81 area: Rect::default(), // will get calculated upon inserting into tree
80 jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
82 jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
83+ last_accessed_doc: None,
81 }
84 }
82 }
85 }
83
86