Refactor push_jump so we're not needlessly fetching doc twice

This commit is contained in:
Blaž Hrastnik 2022-06-07 00:19:01 +09:00
parent 3d9923969a
commit 26dbdb70fb
No known key found for this signature in database
GPG key ID: 1238B9C4AD889640
2 changed files with 17 additions and 16 deletions

View file

@ -965,19 +965,18 @@ 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();
@ -985,6 +984,7 @@ fn goto_file_end(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);
}
@ -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);
}
@ -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.
@ -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.
@ -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);
}
@ -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);
}
@ -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");
}

View file

@ -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(_) => {
@ -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,
@ -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)),