Skip to content

Commit

Permalink
Add last modified file (gm)
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed Nov 13, 2021
1 parent b824e09 commit 8133c00
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ Jumps to various locations.
| `l` | Go to the end of the line | `goto_line_end` |
| `s` | Go to first non-whitespace character of the line | `goto_first_nonwhitespace` |
| `t` | Go to the top of the screen | `goto_window_top` |
| `m` | Go to the middle of the screen | `goto_window_middle` |
| `c` | Go to the middle of the screen | `goto_window_middle` |
| `b` | Go to the bottom of the screen | `goto_window_bottom` |
| `d` | Go to definition (**LSP**) | `goto_definition` |
| `y` | Go to type definition (**LSP**) | `goto_type_definition` |
| `r` | Go to references (**LSP**) | `goto_reference` |
| `i` | Go to implementation (**LSP**) | `goto_implementation` |
| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
| `m` | Go to the last modified/alternate file | `goto_last_modified_file` |
| `n` | Go to next buffer | `goto_next_buffer` |
| `p` | Go to previous buffer | `goto_previous_buffer` |

Expand Down
14 changes: 14 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl Command {
goto_window_middle, "Goto window middle",
goto_window_bottom, "Goto window bottom",
goto_last_accessed_file, "Goto last accessed file",
goto_last_modified_file, "Goto last modified file",
goto_line, "Goto line",
goto_last_line, "Goto last line",
goto_first_diag, "Goto first diagnostic",
Expand Down Expand Up @@ -3195,6 +3196,19 @@ fn goto_last_accessed_file(cx: &mut Context) {
}
}

fn goto_last_modified_file(cx: &mut Context) {
let view = view!(cx.editor);
let alternate_file = view
.last_modified_doc1
.filter(|&id| id != view.doc)
.or(view.last_modified_doc2);
if let Some(alt) = alternate_file {
cx.editor.switch(alt, Action::Replace);
} else {
cx.editor.set_error("no last modified buffer".to_owned())
}
}

fn select_mode(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,10 @@ impl Default for Keymaps {
"r" => goto_reference,
"i" => goto_implementation,
"t" => goto_window_top,
"m" => goto_window_middle,
"c" => goto_window_middle,
"b" => goto_window_bottom,
"a" => goto_last_accessed_file,
"m" => goto_last_modified_file,
"n" => goto_next_buffer,
"p" => goto_previous_buffer,
},
Expand Down
9 changes: 9 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct Document {

last_saved_revision: usize,
version: i32, // should be usize?
pub(crate) modified_since_accessed: Cell<bool>,

diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>,
Expand All @@ -127,6 +128,10 @@ impl fmt::Debug for Document {
// .field("history", &self.history)
.field("last_saved_revision", &self.last_saved_revision)
.field("version", &self.version)
.field(
"modified_since_accessed",
&self.modified_since_accessed.get(),
)
.field("diagnostics", &self.diagnostics)
// .field("language_server", &self.language_server)
.finish()
Expand Down Expand Up @@ -344,6 +349,7 @@ impl Document {
history: Cell::new(History::default()),
savepoint: None,
last_saved_revision: 0,
modified_since_accessed: Cell::new(false),
language_server: None,
}
}
Expand Down Expand Up @@ -639,6 +645,9 @@ impl Document {
selection.clone().ensure_invariants(self.text.slice(..)),
);
}

// set modified since accessed
self.modified_since_accessed.set(true);
}

if !transaction.changes().is_empty() {
Expand Down
12 changes: 11 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,17 @@ impl Editor {
} else {
let jump = (view.doc, doc.selection(view.id).clone());
view.jumps.push(jump);
view.last_accessed_doc = Some(view.doc);
// Set last accessed doc if it is a different document
if doc.id != id {
view.last_accessed_doc = Some(view.doc);
// Set last modified doc if modified and last modified doc is different
if doc.modified_since_accessed.take()
&& view.last_modified_doc1.map_or(true, |doc_id| doc_id != id)
{
view.last_modified_doc2 = view.last_modified_doc1;
view.last_modified_doc1 = Some(view.doc);
}
}
}
view.doc = id;
view.offset = Position::default();
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub struct View {
pub jumps: JumpList,
/// the last accessed file before the current one
pub last_accessed_doc: Option<DocumentId>,
/// the last modified file before the current one
pub last_modified_doc1: Option<DocumentId>,
pub last_modified_doc2: Option<DocumentId>,
}

impl View {
Expand All @@ -76,6 +79,8 @@ impl View {
area: Rect::default(), // will get calculated upon inserting into tree
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
last_accessed_doc: None,
last_modified_doc1: None,
last_modified_doc2: None,
}
}

Expand Down

0 comments on commit 8133c00

Please sign in to comment.