on master help sign in
Add last modified file (gm) (#1093)
commite2b428cc2d23965ab7a91809c5705ede0298107d authorIvan Tham <pickfire@riseup.net> dateThu 2 Dec 2021, 12:46 (5 years ago) parent418b833
6 files changed+45−6
book/src/keymap.md+2−1 helix-term/src/commands.rs+17−2 helix-term/src/keymap.rs+2−1 helix-view/src/document.rs+6 helix-view/src/editor.rs+12−2 helix-view/src/view.rs+6
book/src/keymap.md+2−1
@@ -165,13 +165,14 @@ Jumps to various locations.
165165| `l` | Go to the end of the line | `goto_line_end` |
166166| `s` | Go to first non-whitespace character of the line | `goto_first_nonwhitespace` |
167167| `t` | Go to the top of the screen | `goto_window_top` |
168−| `m` | Go to the middle of the screen | `goto_window_middle` |
168+| `c` | Go to the middle of the screen | `goto_window_center` |
169169| `b` | Go to the bottom of the screen | `goto_window_bottom` |
170170| `d` | Go to definition (**LSP**) | `goto_definition` |
171171| `y` | Go to type definition (**LSP**) | `goto_type_definition` |
172172| `r` | Go to references (**LSP**) | `goto_reference` |
173173| `i` | Go to implementation (**LSP**) | `goto_implementation` |
174174| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
175+| `m` | Go to the last modified/alternate file | `goto_last_modified_file` |
175176| `n` | Go to next buffer | `goto_next_buffer` |
176177| `p` | Go to previous buffer | `goto_previous_buffer` |
177178| `.` | Go to last modification in current file | `goto_last_modification` |
helix-term/src/commands.rs+17−2
@@ -264,9 +264,10 @@ impl Command {
264264 goto_file_vsplit, "Goto files in the selection in vertical splits",
265265 goto_reference, "Goto references",
266266 goto_window_top, "Goto window top",
267− goto_window_middle, "Goto window middle",
267+ goto_window_center, "Goto window center",
268268 goto_window_bottom, "Goto window bottom",
269269 goto_last_accessed_file, "Goto last accessed file",
270+ goto_last_modified_file, "Goto last modified file",
270271 goto_last_modification, "Goto last modification",
271272 goto_line, "Goto line",
272273 goto_last_line, "Goto last line",
@@ -763,7 +764,7 @@ fn goto_window_top(cx: &mut Context) {
763764 goto_window(cx, Align::Top)
764765}
765766
766−fn goto_window_middle(cx: &mut Context) {
767+fn goto_window_center(cx: &mut Context) {
767768 goto_window(cx, Align::Center)
768769}
769770
@@ -3610,6 +3611,20 @@ fn goto_last_modification(cx: &mut Context) {
36103611 }
36113612}
36123613
3614+fn goto_last_modified_file(cx: &mut Context) {
3615+ let view = view!(cx.editor);
3616+ let alternate_file = view
3617+ .last_modified_docs
3618+ .into_iter()
3619+ .flatten()
3620+ .find(|&id| id != view.doc);
3621+ if let Some(alt) = alternate_file {
3622+ cx.editor.switch(alt, Action::Replace);
3623+ } else {
3624+ cx.editor.set_error("no last modified buffer".to_owned())
3625+ }
3626+}
3627+
36133628fn select_mode(cx: &mut Context) {
36143629 let (view, doc) = current!(cx.editor);
36153630 let text = doc.text().slice(..);
helix-term/src/keymap.rs+2−1
@@ -521,9 +521,10 @@ impl Default for Keymaps {
521521 "r" => goto_reference,
522522 "i" => goto_implementation,
523523 "t" => goto_window_top,
524− "m" => goto_window_middle,
524+ "c" => goto_window_center,
525525 "b" => goto_window_bottom,
526526 "a" => goto_last_accessed_file,
527+ "m" => goto_last_modified_file,
527528 "n" => goto_next_buffer,
528529 "p" => goto_previous_buffer,
529530 "." => goto_last_modification,
helix-view/src/document.rs+6
@@ -104,6 +104,7 @@ pub struct Document {
104104
105105 last_saved_revision: usize,
106106 version: i32, // should be usize?
107+ pub(crate) modified_since_accessed: bool,
107108
108109 diagnostics: Vec<Diagnostic>,
109110 language_server: Option<Arc<helix_lsp::Client>>,
@@ -127,6 +128,7 @@ impl fmt::Debug for Document {
127128 // .field("history", &self.history)
128129 .field("last_saved_revision", &self.last_saved_revision)
129130 .field("version", &self.version)
131+ .field("modified_since_accessed", &self.modified_since_accessed)
130132 .field("diagnostics", &self.diagnostics)
131133 // .field("language_server", &self.language_server)
132134 .finish()
@@ -344,6 +346,7 @@ impl Document {
344346 history: Cell::new(History::default()),
345347 savepoint: None,
346348 last_saved_revision: 0,
349+ modified_since_accessed: false,
347350 language_server: None,
348351 }
349352 }
@@ -639,6 +642,9 @@ impl Document {
639642 selection.clone().ensure_invariants(self.text.slice(..)),
640643 );
641644 }
645+
646+ // set modified since accessed
647+ self.modified_since_accessed = true;
642648 }
643649
644650 if !transaction.changes().is_empty() {
helix-view/src/editor.rs+12−2
@@ -369,7 +369,8 @@ impl Editor {
369369 .tree
370370 .traverse()
371371 .any(|(_, v)| v.doc == doc.id && v.id != view.id);
372− let view = view_mut!(self);
372+
373+ let (view, doc) = current!(self);
373374 if remove_empty_scratch {
374375 // Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
375376 // borrow, invalidating direct access to `doc.id`.
@@ -378,7 +379,16 @@ impl Editor {
378379 } else {
379380 let jump = (view.doc, doc.selection(view.id).clone());
380381 view.jumps.push(jump);
381− view.last_accessed_doc = Some(view.doc);
382+ // Set last accessed doc if it is a different document
383+ if doc.id != id {
384+ view.last_accessed_doc = Some(view.doc);
385+ // Set last modified doc if modified and last modified doc is different
386+ if std::mem::take(&mut doc.modified_since_accessed)
387+ && view.last_modified_docs[0] != Some(id)
388+ {
389+ view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]];
390+ }
391+ }
382392 }
383393
384394 let view_id = view.id;
helix-view/src/view.rs+6
@@ -75,6 +75,11 @@ pub struct View {
7575 pub jumps: JumpList,
7676 /// the last accessed file before the current one
7777 pub last_accessed_doc: Option<DocumentId>,
78+ /// the last modified files before the current one
79+ /// ordered from most frequent to least frequent
80+ // uses two docs because we want to be able to swap between the
81+ // two last modified docs which we need to manually keep track of
82+ pub last_modified_docs: [Option<DocumentId>; 2],
7883}
7984
8085impl View {
@@ -86,6 +91,7 @@ impl View {
8691 area: Rect::default(), // will get calculated upon inserting into tree
8792 jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
8893 last_accessed_doc: None,
94+ last_modified_docs: [None, None],
8995 }
9096 }
9197