Skip to content

Commit b175420

Browse files
committed
Add last modified file (gm)
1 parent b824e09 commit b175420

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

book/src/keymap.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ Jumps to various locations.
156156
| `l` | Go to the end of the line | `goto_line_end` |
157157
| `s` | Go to first non-whitespace character of the line | `goto_first_nonwhitespace` |
158158
| `t` | Go to the top of the screen | `goto_window_top` |
159-
| `m` | Go to the middle of the screen | `goto_window_middle` |
159+
| `c` | Go to the middle of the screen | `goto_window_center` |
160160
| `b` | Go to the bottom of the screen | `goto_window_bottom` |
161161
| `d` | Go to definition (**LSP**) | `goto_definition` |
162162
| `y` | Go to type definition (**LSP**) | `goto_type_definition` |
163163
| `r` | Go to references (**LSP**) | `goto_reference` |
164164
| `i` | Go to implementation (**LSP**) | `goto_implementation` |
165165
| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
166+
| `m` | Go to the last modified/alternate file | `goto_last_modified_file` |
166167
| `n` | Go to next buffer | `goto_next_buffer` |
167168
| `p` | Go to previous buffer | `goto_previous_buffer` |
168169

helix-term/src/commands.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ impl Command {
254254
goto_file_end, "Goto file end",
255255
goto_reference, "Goto references",
256256
goto_window_top, "Goto window top",
257-
goto_window_middle, "Goto window middle",
257+
goto_window_center, "Goto window center",
258258
goto_window_bottom, "Goto window bottom",
259259
goto_last_accessed_file, "Goto last accessed file",
260+
goto_last_modified_file, "Goto last modified file",
260261
goto_line, "Goto line",
261262
goto_last_line, "Goto last line",
262263
goto_first_diag, "Goto first diagnostic",
@@ -610,7 +611,7 @@ fn goto_window_top(cx: &mut Context) {
610611
goto_window(cx, Align::Top)
611612
}
612613

613-
fn goto_window_middle(cx: &mut Context) {
614+
fn goto_window_center(cx: &mut Context) {
614615
goto_window(cx, Align::Center)
615616
}
616617

@@ -3195,6 +3196,20 @@ fn goto_last_accessed_file(cx: &mut Context) {
31953196
}
31963197
}
31973198

3199+
fn goto_last_modified_file(cx: &mut Context) {
3200+
let view = view!(cx.editor);
3201+
let alternate_file = view
3202+
.last_modified_docs
3203+
.into_iter()
3204+
.flatten()
3205+
.find(|&id| id != view.doc);
3206+
if let Some(alt) = alternate_file {
3207+
cx.editor.switch(alt, Action::Replace);
3208+
} else {
3209+
cx.editor.set_error("no last modified buffer".to_owned())
3210+
}
3211+
}
3212+
31983213
fn select_mode(cx: &mut Context) {
31993214
let (view, doc) = current!(cx.editor);
32003215
let text = doc.text().slice(..);

helix-term/src/keymap.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,10 @@ impl Default for Keymaps {
520520
"r" => goto_reference,
521521
"i" => goto_implementation,
522522
"t" => goto_window_top,
523-
"m" => goto_window_middle,
523+
"c" => goto_window_center,
524524
"b" => goto_window_bottom,
525525
"a" => goto_last_accessed_file,
526+
"m" => goto_last_modified_file,
526527
"n" => goto_next_buffer,
527528
"p" => goto_previous_buffer,
528529
},

helix-view/src/document.rs

+6
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ impl Editor {
257257
.tree
258258
.traverse()
259259
.any(|(_, v)| v.doc == doc.id && v.id != view.id);
260-
let view = view_mut!(self);
260+
261+
let (view, doc) = current!(self);
261262
if remove_empty_scratch {
262263
// Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
263264
// borrow, invalidating direct access to `doc.id`.
@@ -266,7 +267,16 @@ impl Editor {
266267
} else {
267268
let jump = (view.doc, doc.selection(view.id).clone());
268269
view.jumps.push(jump);
269-
view.last_accessed_doc = Some(view.doc);
270+
// Set last accessed doc if it is a different document
271+
if doc.id != id {
272+
view.last_accessed_doc = Some(view.doc);
273+
// Set last modified doc if modified and last modified doc is different
274+
if std::mem::take(&mut doc.modified_since_accessed)
275+
&& view.last_modified_docs[0] != Some(id)
276+
{
277+
view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]];
278+
}
279+
}
270280
}
271281
view.doc = id;
272282
view.offset = Position::default();

helix-view/src/view.rs

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ pub struct View {
6565
pub jumps: JumpList,
6666
/// the last accessed file before the current one
6767
pub last_accessed_doc: Option<DocumentId>,
68+
/// the last modified files before the current one
69+
/// ordered from most frequent to least frequent
70+
// uses two docs because we want to be able to swap between the
71+
// two last modified docs which we need to manually keep track of
72+
pub last_modified_docs: [Option<DocumentId>; 2],
6873
}
6974

7075
impl View {
@@ -76,6 +81,7 @@ impl View {
7681
area: Rect::default(), // will get calculated upon inserting into tree
7782
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
7883
last_accessed_doc: None,
84+
last_modified_docs: [None, None],
7985
}
8086
}
8187

0 commit comments

Comments
 (0)