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
This commit is contained in:
Robin 2021-06-12 14:21:06 +02:00 committed by GitHub
parent 1953588873
commit 44cc0d8eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 0 deletions

View File

@ -141,6 +141,7 @@ ## Goto mode
| y | Go to type definition | | y | Go to type definition |
| r | Go to references | | r | Go to references |
| i | Go to implementation | | i | Go to implementation |
| a | Go to the last accessed/alternate file |
## Object mode ## Object mode

View File

@ -1317,6 +1317,15 @@ fn push_jump(editor: &mut Editor) {
view.jumps.push(jump); view.jumps.push(jump);
} }
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())
}
}
pub fn goto_mode(cx: &mut Context) { pub fn goto_mode(cx: &mut Context) {
if let Some(count) = cx._count { if let Some(count) = cx._count {
push_jump(cx.editor); push_jump(cx.editor);
@ -1338,6 +1347,7 @@ pub fn goto_mode(cx: &mut Context) {
match (cx.doc().mode, ch) { match (cx.doc().mode, ch) {
(_, 'g') => move_file_start(cx), (_, 'g') => move_file_start(cx),
(_, 'e') => move_file_end(cx), (_, 'e') => move_file_end(cx),
(_, 'a') => switch_to_last_accessed_file(cx),
(Mode::Normal, 'h') => move_line_start(cx), (Mode::Normal, 'h') => move_line_start(cx),
(Mode::Normal, 'l') => move_line_end(cx), (Mode::Normal, 'l') => move_line_end(cx),
(Mode::Select, 'h') => extend_line_start(cx), (Mode::Select, 'h') => extend_line_start(cx),
@ -2449,6 +2459,10 @@ pub fn jump_backward(cx: &mut Context) {
let (view, doc) = cx.current(); let (view, doc) = cx.current();
if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { 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.last_accessed_doc = Some(view.doc)
}
view.doc = *id; view.doc = *id;
let selection = selection.clone(); let selection = selection.clone();
let (view, doc) = cx.current(); // refetch doc let (view, doc) = cx.current(); // refetch doc

View File

@ -88,6 +88,12 @@ fn _refresh(&mut self) {
pub fn switch(&mut self, id: DocumentId, action: Action) { pub fn switch(&mut self, id: DocumentId, action: Action) {
use crate::tree::Layout; use crate::tree::Layout;
use helix_core::Selection; use helix_core::Selection;
if !self.documents.contains_key(id) {
log::error!("cannot switch to document that does not exist (anymore)");
return;
}
match action { match action {
Action::Replace => { Action::Replace => {
let view = self.view(); let view = self.view();
@ -98,6 +104,7 @@ pub fn switch(&mut self, id: DocumentId, action: Action) {
let view = self.view_mut(); let view = self.view_mut();
view.jumps.push(jump); view.jumps.push(jump);
view.last_accessed_doc = Some(view.doc);
view.doc = id; view.doc = id;
view.first_line = 0; view.first_line = 0;

View File

@ -67,6 +67,8 @@ pub struct View {
pub first_col: usize, pub first_col: usize,
pub area: Rect, pub area: Rect,
pub jumps: JumpList, pub jumps: JumpList,
/// the last accessed file before the current one
pub last_accessed_doc: Option<DocumentId>,
} }
impl View { impl View {
@ -78,6 +80,7 @@ pub fn new(doc: DocumentId) -> Self {
first_col: 0, first_col: 0,
area: Rect::default(), // will get calculated upon inserting into tree area: Rect::default(), // will get calculated upon inserting into tree
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
last_accessed_doc: None,
} }
} }