From 272099bedccdba64b66477f9e62c4b09ce719e73 Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Fri, 11 Jun 2021 23:40:14 +0200 Subject: [PATCH 1/5] add alternate file inspired by vim ctrl-6/kak ga commands. the alternate file is kept per view --- helix-term/src/commands.rs | 12 ++++++++++++ helix-view/src/editor.rs | 7 +++++++ helix-view/src/view.rs | 2 ++ 3 files changed, 21 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3cb1288ec94f..f93ed2b02345 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1311,6 +1311,13 @@ fn push_jump(editor: &mut Editor) { view.jumps.push(jump); } +fn switch_to_alternate_file(cx: &mut Context) { + let alternate_file = cx.view().alternate_file; + if let Some(alt) = alternate_file { + cx.editor.switch(alt, Action::Replace); + } +} + pub fn goto_mode(cx: &mut Context) { if let Some(count) = cx._count { push_jump(cx.editor); @@ -1332,6 +1339,7 @@ pub fn goto_mode(cx: &mut Context) { match (cx.doc().mode, ch) { (_, 'g') => move_file_start(cx), (_, 'e') => move_file_end(cx), + (_, 'a') => switch_to_alternate_file(cx), (Mode::Normal, 'h') => move_line_start(cx), (Mode::Normal, 'l') => move_line_end(cx), (Mode::Select, 'h') => extend_line_start(cx), @@ -2442,6 +2450,10 @@ pub fn jump_backward(cx: &mut Context) { let (view, doc) = cx.current(); if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { + // manually set the alternate_file as we cannot use the Editor::switch function here. + if view.doc != *id { + view.alternate_file = Some(view.doc) + } view.doc = *id; let selection = selection.clone(); let (view, doc) = cx.current(); // refetch doc diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index bd53c53bb624..997149db0af7 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -88,6 +88,12 @@ impl Editor { pub fn switch(&mut self, id: DocumentId, action: Action) { use crate::tree::Layout; use helix_core::Selection; + + if !self.documents.contains_key(id) { + log::warn!("cannot switch to document that does not exist (anymore)"); + return; + } + match action { Action::Replace => { let view = self.view(); @@ -98,6 +104,7 @@ impl Editor { let view = self.view_mut(); view.jumps.push(jump); + view.alternate_file = Some(view.doc); view.doc = id; view.first_line = 0; diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 5d2d27fdb1be..fb176eab68ea 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -67,6 +67,7 @@ pub struct View { pub first_col: usize, pub area: Rect, pub jumps: JumpList, + pub alternate_file: Option, } impl View { @@ -78,6 +79,7 @@ impl View { first_col: 0, area: Rect::default(), // will get calculated upon inserting into tree jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel + alternate_file: None, } } From 25d38cfc056a85da791c11973985d9be55b21120 Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Sat, 12 Jun 2021 10:45:11 +0200 Subject: [PATCH 2/5] apply feedback from #223 --- helix-term/src/commands.rs | 4 ++-- helix-view/src/editor.rs | 4 ++-- helix-view/src/view.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f93ed2b02345..b70f0f178e8c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1312,7 +1312,7 @@ fn push_jump(editor: &mut Editor) { } fn switch_to_alternate_file(cx: &mut Context) { - let alternate_file = cx.view().alternate_file; + let alternate_file = cx.view().alternate_doc; if let Some(alt) = alternate_file { cx.editor.switch(alt, Action::Replace); } @@ -2452,7 +2452,7 @@ pub fn jump_backward(cx: &mut Context) { if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { // manually set the alternate_file as we cannot use the Editor::switch function here. if view.doc != *id { - view.alternate_file = Some(view.doc) + view.alternate_doc = Some(view.doc) } view.doc = *id; let selection = selection.clone(); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 997149db0af7..dcdd72a4ac44 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -90,7 +90,7 @@ impl Editor { use helix_core::Selection; if !self.documents.contains_key(id) { - log::warn!("cannot switch to document that does not exist (anymore)"); + log::error!("cannot switch to document that does not exist (anymore)"); return; } @@ -104,7 +104,7 @@ impl Editor { let view = self.view_mut(); view.jumps.push(jump); - view.alternate_file = Some(view.doc); + view.alternate_doc = Some(view.doc); view.doc = id; view.first_line = 0; diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index fb176eab68ea..69daa8a1f09d 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -67,7 +67,7 @@ pub struct View { pub first_col: usize, pub area: Rect, pub jumps: JumpList, - pub alternate_file: Option, + pub alternate_doc: Option, } impl View { @@ -79,7 +79,7 @@ impl View { first_col: 0, area: Rect::default(), // will get calculated upon inserting into tree jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel - alternate_file: None, + alternate_doc: None, } } From de66b8555840bd0c7708a5b3c3347fa142810913 Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Sat, 12 Jun 2021 12:32:34 +0200 Subject: [PATCH 3/5] rename to last_accessed --- helix-term/src/commands.rs | 8 ++++---- helix-view/src/editor.rs | 2 +- helix-view/src/view.rs | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b70f0f178e8c..d7a1aa7411ac 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1311,8 +1311,8 @@ fn push_jump(editor: &mut Editor) { view.jumps.push(jump); } -fn switch_to_alternate_file(cx: &mut Context) { - let alternate_file = cx.view().alternate_doc; +fn switch_to_last_accessed_file(cx: &mut Context) { + let alternate_file = cx.view().last_accessed_doc; if let Some(alt) = alternate_file { cx.editor.switch(alt, Action::Replace); } @@ -1339,7 +1339,7 @@ pub fn goto_mode(cx: &mut Context) { match (cx.doc().mode, ch) { (_, 'g') => move_file_start(cx), (_, 'e') => move_file_end(cx), - (_, 'a') => switch_to_alternate_file(cx), + (_, 'a') => switch_to_last_accessed_file(cx), (Mode::Normal, 'h') => move_line_start(cx), (Mode::Normal, 'l') => move_line_end(cx), (Mode::Select, 'h') => extend_line_start(cx), @@ -2452,7 +2452,7 @@ pub fn jump_backward(cx: &mut Context) { if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { // manually set the alternate_file as we cannot use the Editor::switch function here. if view.doc != *id { - view.alternate_doc = Some(view.doc) + view.last_accessed_doc = Some(view.doc) } view.doc = *id; let selection = selection.clone(); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index dcdd72a4ac44..ef0d8213f400 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -104,7 +104,7 @@ impl Editor { let view = self.view_mut(); view.jumps.push(jump); - view.alternate_doc = Some(view.doc); + view.last_accessed_doc = Some(view.doc); view.doc = id; view.first_line = 0; diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 69daa8a1f09d..c0a72c78a206 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -67,7 +67,8 @@ pub struct View { pub first_col: usize, pub area: Rect, pub jumps: JumpList, - pub alternate_doc: Option, + /// the last accessed file before the current one + pub last_accessed_doc: Option, } impl View { @@ -79,7 +80,7 @@ impl View { first_col: 0, area: Rect::default(), // will get calculated upon inserting into tree jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel - alternate_doc: None, + last_accessed_doc: None, } } From 30fd2d0f2bc9693dd1ab8c83f6ffad49803563ce Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Sat, 12 Jun 2021 12:32:54 +0200 Subject: [PATCH 4/5] add ga doc --- book/src/keymap.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/keymap.md b/book/src/keymap.md index 65d01b86a31f..cba89896f849 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -141,6 +141,7 @@ Jumps to various locations. | y | Go to type definition | | r | Go to references | | i | Go to implementation | +| a | Go to the last accessed/alternate file | ## Object mode From 618129d66744f5f67206521c42a5fe49c3a662c5 Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Sat, 12 Jun 2021 14:12:18 +0200 Subject: [PATCH 5/5] add fail message for ga --- helix-term/src/commands.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d7a1aa7411ac..d6734d9f5efb 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1315,6 +1315,8 @@ fn switch_to_last_accessed_file(cx: &mut Context) { let alternate_file = cx.view().last_accessed_doc; if let Some(alt) = alternate_file { cx.editor.switch(alt, Action::Replace); + } else { + cx.editor.set_error("no last buffer".to_owned()) } }