Store intra-files jumps (goto) on the jumplist.

pull/11/head
Blaž Hrastnik 4 years ago
parent 0083a6c325
commit 742b3a709f

@ -127,7 +127,7 @@ impl Range {
/// A selection consists of one or more selection ranges. /// A selection consists of one or more selection ranges.
/// invariant: A selection can never be empty (always contains at least primary range). /// invariant: A selection can never be empty (always contains at least primary range).
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selection { pub struct Selection {
ranges: SmallVec<[Range; 1]>, ranges: SmallVec<[Range; 1]>,
primary_index: usize, primary_index: usize,

@ -238,11 +238,13 @@ pub fn move_next_word_end(cx: &mut Context) {
} }
pub fn move_file_start(cx: &mut Context) { pub fn move_file_start(cx: &mut Context) {
push_jump(cx);
let doc = cx.doc(); let doc = cx.doc();
doc.set_selection(Selection::point(0)); doc.set_selection(Selection::point(0));
} }
pub fn move_file_end(cx: &mut Context) { pub fn move_file_end(cx: &mut Context) {
push_jump(cx);
let doc = cx.doc(); let doc = cx.doc();
let text = doc.text(); let text = doc.text();
let last_line = text.line_to_char(text.len_lines().saturating_sub(2)); let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
@ -1027,9 +1029,21 @@ pub fn normal_mode(cx: &mut Context) {
} }
} }
// Store a jump on the jumplist.
fn push_jump(cx: &mut Context) {
let jump = {
let doc = cx.doc();
(doc.id(), doc.selection().clone())
};
cx.view().jumps.push(jump);
}
pub fn goto_mode(cx: &mut Context) { pub fn goto_mode(cx: &mut Context) {
let count = cx.count; let count = cx.count;
if count > 1 { if count > 1 {
push_jump(cx);
// TODO: can't go to line 1 since we can't distinguish between g and 1g, g gets converted // TODO: can't go to line 1 since we can't distinguish between g and 1g, g gets converted
// to 1g // to 1g
let doc = cx.doc(); let doc = cx.doc();
@ -1069,6 +1083,8 @@ pub fn exit_select_mode(cx: &mut Context) {
fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) { fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
use helix_view::editor::Action; use helix_view::editor::Action;
push_jump(cx);
fn jump_to(editor: &mut Editor, location: &lsp::Location, action: Action) { fn jump_to(editor: &mut Editor, location: &lsp::Location, action: Action) {
let id = editor let id = editor
.open(PathBuf::from(location.uri.path()), action) .open(PathBuf::from(location.uri.path()), action)

@ -258,6 +258,7 @@ pub fn default() -> Keymaps {
ctrl!('i') => commands::jump_forward, // TODO: ctrl-i conflicts tab ctrl!('i') => commands::jump_forward, // TODO: ctrl-i conflicts tab
ctrl!('o') => commands::jump_backward, ctrl!('o') => commands::jump_backward,
// ctrl!('s') => commands::save_selection,
); );
// TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
// we keep this separate select mode. More keys can fit into normal mode then, but it's weird // we keep this separate select mode. More keys can fit into normal mode then, but it's weird

@ -49,7 +49,7 @@ pub fn regex_prompt(
doc.set_selection(snapshot.clone()); doc.set_selection(snapshot.clone());
} }
PromptEvent::Validate => { PromptEvent::Validate => {
// // TODO: push_jump to store selection just before jump
} }
PromptEvent::Update => { PromptEvent::Update => {
// skip empty input, TODO: trigger default // skip empty input, TODO: trigger default

@ -274,6 +274,11 @@ impl Document {
self.history.commit_revision(&transaction, &old_state); self.history.commit_revision(&transaction, &old_state);
} }
#[inline]
pub fn id(&self) -> DocumentId {
self.id
}
#[inline] #[inline]
pub fn mode(&self) -> Mode { pub fn mode(&self) -> Mode {
self.mode self.mode

@ -27,9 +27,12 @@ impl JumpList {
} }
pub fn push(&mut self, jump: Jump) { pub fn push(&mut self, jump: Jump) {
self.jumps.truncate(self.current + 1); self.jumps.truncate(self.current);
// don't push duplicates
if self.jumps.last() != Some(&jump) {
self.jumps.push(jump); self.jumps.push(jump);
self.current += 1; self.current = self.jumps.len();
}
} }
pub fn forward(&mut self, count: usize) -> Option<&Jump> { pub fn forward(&mut self, count: usize) -> Option<&Jump> {

Loading…
Cancel
Save