Skip to content

Commit

Permalink
Refactor push_jump so we're not needlessly fetching doc twice
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jun 6, 2022
1 parent 3d99239 commit 26dbdb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
21 changes: 10 additions & 11 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,26 +965,26 @@ fn goto_file_start(cx: &mut Context) {
if cx.count.is_some() {
goto_line(cx);
} else {
push_jump(cx.editor);
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, 0, doc.mode == Mode::Select));
push_jump(view, doc);
doc.set_selection(view.id, selection);
}
}

fn goto_file_end(cx: &mut Context) {
push_jump(cx.editor);
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let pos = doc.text().len_chars();
let selection = doc
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));
push_jump(view, doc);
doc.set_selection(view.id, selection);
}

Expand Down Expand Up @@ -2485,8 +2485,7 @@ fn try_restore_indent(doc: &mut Document, view_id: ViewId) {
}

// Store a jump on the jumplist.
fn push_jump(editor: &mut Editor) {
let (view, doc) = current!(editor);
fn push_jump(view: &mut View, doc: &Document) {
let jump = (doc.id(), doc.selection(view.id).clone());
view.jumps.push(jump);
}
Expand All @@ -2497,8 +2496,6 @@ fn goto_line(cx: &mut Context) {

fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
if let Some(count) = count {
push_jump(editor);

let (view, doc) = current!(editor);
let max_line = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
Expand All @@ -2513,13 +2510,13 @@ fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));

push_jump(view, doc);
doc.set_selection(view.id, selection);
}
}

fn goto_last_line(cx: &mut Context) {
push_jump(cx.editor);

let (view, doc) = current!(cx.editor);
let line_idx = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
Expand All @@ -2533,6 +2530,8 @@ fn goto_last_line(cx: &mut Context) {
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));

push_jump(view, doc);
doc.set_selection(view.id, selection);
}

Expand Down Expand Up @@ -2601,10 +2600,9 @@ fn exit_select_mode(cx: &mut Context) {
}

fn goto_pos(editor: &mut Editor, pos: usize) {
push_jump(editor);

let (view, doc) = current!(editor);

push_jump(view, doc);
doc.set_selection(view.id, Selection::point(pos));
align_view(doc, view, Align::Center);
}
Expand Down Expand Up @@ -3886,7 +3884,8 @@ fn jump_backward(cx: &mut Context) {
}

fn save_selection(cx: &mut Context) {
push_jump(cx.editor);
let (view, doc) = current!(cx.editor);
push_jump(view, doc);
cx.editor.set_status("Selection saved to jumplist");
}

Expand Down
12 changes: 7 additions & 5 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ fn location_to_file_location(location: &lsp::Location) -> FileLocation {
}

// TODO: share with symbol picker(symbol.location)
// TODO: need to use push_jump() before?
fn jump_to_location(
editor: &mut Editor,
location: &lsp::Location,
offset_encoding: OffsetEncoding,
action: Action,
) {
let (view, doc) = current!(editor);
push_jump(view, doc);

let path = match location.uri.to_file_path() {
Ok(path) => path,
Err(_) => {
Expand Down Expand Up @@ -93,9 +95,10 @@ fn sym_picker(
}
},
move |cx, symbol, action| {
if current_path2.as_ref() == Some(&symbol.location.uri) {
push_jump(cx.editor);
} else {
let (view, doc) = current!(cx.editor);
push_jump(view, doc);

if current_path2.as_ref() != Some(&symbol.location.uri) {
let uri = &symbol.location.uri;
let path = match uri.to_file_path() {
Ok(path) => path,
Expand Down Expand Up @@ -518,7 +521,6 @@ fn goto_impl(
format!("{}:{}", file, line).into()
},
move |cx, location, action| {
push_jump(cx.editor);
jump_to_location(cx.editor, location, offset_encoding, action)
},
move |_editor, location| Some(location_to_file_location(location)),
Expand Down

0 comments on commit 26dbdb7

Please sign in to comment.