Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add alternate file #223

Merged
merged 5 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this log::error!, ideally this shouldn't happen. We'll need to add something like forget_doc on the view to remove it from both alternate file and the jumplist: https://github.com/mawww/kakoune/blob/c507863a00289fe4b210ecc4fef5554de4c87bf4/src/context.cc#L197-L210

return;
}

match action {
Action::Replace => {
let view = self.view();
Expand All @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct View {
pub first_col: usize,
pub area: Rect,
pub jumps: JumpList,
pub alternate_file: Option<DocumentId>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this alternate_doc to match the doc field.

}

impl View {
Expand All @@ -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,
}
}

Expand Down