Skip to content

Commit

Permalink
use view as old_id; make align_view method of Action
Browse files Browse the repository at this point in the history
  • Loading branch information
woojiq committed Jul 31, 2023
1 parent 47e5e98 commit 2bced89
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
12 changes: 5 additions & 7 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,9 +2187,8 @@ fn global_search(cx: &mut Context) {
all_matches,
current_path,
move |cx, FileResult { path, line_num }, action| {
let old_doc_id = doc!(cx.editor).id();
let doc_id = match cx.editor.open(path, action) {
Ok(id) => id,
let doc = match cx.editor.open(path, action) {
Ok(id) => doc_mut!(cx.editor, &id),
Err(e) => {
cx.editor.set_error(format!(
"Failed to open file '{}': {}",
Expand All @@ -2200,7 +2199,7 @@ fn global_search(cx: &mut Context) {
}
};
let line_num = *line_num;
let (view, doc) = (view_mut!(cx.editor), doc_mut!(cx.editor, &doc_id));
let view = view_mut!(cx.editor);
let text = doc.text();
if line_num >= text.len_lines() {
cx.editor.set_error("The line you jumped to does not exist anymore because the file has changed.");
Expand All @@ -2210,7 +2209,7 @@ fn global_search(cx: &mut Context) {
let end = text.line_to_char((line_num + 1).min(text.len_lines()));

doc.set_selection(view.id, Selection::single(start, end));
if View::change_align_view(action, old_doc_id, doc_id) {
if action.align_view(view, doc.id()){
align_view(doc, view, Align::Center);
}
}).with_preview(|_editor, FileResult { path, line_num }| {
Expand Down Expand Up @@ -2723,12 +2722,11 @@ fn jumplist_picker(cx: &mut Context) {
.collect(),
(),
|cx, meta, action| {
let old_doc_id = doc!(cx.editor).id();
cx.editor.switch(meta.id, action);
let config = cx.editor.config();
let (view, doc) = (view_mut!(cx.editor), doc_mut!(cx.editor, &meta.id));
doc.set_selection(view.id, meta.selection.clone());
if View::change_align_view(action, old_doc_id, doc.id()) {
if action.align_view(view, doc.id()) {
view.ensure_cursor_in_view_center(doc, config.scrolloff);
}
},
Expand Down
3 changes: 1 addition & 2 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ fn jump_to_location(
}
};

let old_doc_id = doc!(editor).id();
let doc = match editor.open(&path, action) {
Ok(id) => doc_mut!(editor, &id),
Err(err) => {
Expand All @@ -234,7 +233,7 @@ fn jump_to_location(
// we flip the range so that the cursor sits on the start of the symbol
// (for example start of the function).
doc.set_selection(view.id, Selection::single(new_range.head, new_range.anchor));
if View::change_align_view(action, old_doc_id, doc.id()) {
if action.align_view(view, doc.id()) {
align_view(doc, view, Align::Center);
}
}
Expand Down
3 changes: 2 additions & 1 deletion helix-term/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[cfg(feature = "integration")]
// XXX remove before PR
// #[cfg(feature = "integration")]
mod test {
mod helpers;

Expand Down
7 changes: 7 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,13 @@ pub enum Action {
VerticalSplit,
}

impl Action {
/// Whether to align the view to the cursor after executing this action
pub fn align_view(&self, view: &View, new_doc: DocumentId) -> bool {
!matches!((self, view.doc == new_doc), (Action::Load, false))
}
}

/// Error thrown on failed document closed
pub enum CloseError {
/// Document doesn't exist
Expand Down
21 changes: 1 addition & 20 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
align_view,
document::DocumentInlayHints,
editor::{Action, GutterConfig, GutterType},
editor::{GutterConfig, GutterType},
graphics::Rect,
Align, Document, DocumentId, Theme, ViewId,
};
Expand Down Expand Up @@ -314,13 +314,6 @@ impl View {
self.offset_coords_to_in_view(doc, scrolloff).is_none()
}

/// Whether to change a view alignment of current file. You probably don't want to
/// change the alignment of current file when open new files in the background (Load).
/// Except when the new file is the old file
pub fn change_align_view(action: Action, old_id: DocumentId, new_id: DocumentId) -> bool {
!matches!((action, old_id == new_id), (Action::Load, false))
}

/// Estimates the last visible document line on screen.
/// This estimate is an upper bound obtained by calculating the first
/// visible line and adding the viewport height.
Expand Down Expand Up @@ -1023,16 +1016,4 @@ mod tests {
Some(7)
);
}

#[test]
fn test_change_align_view() {
use std::num::NonZeroUsize;
let docs = [
DocumentId(NonZeroUsize::new(1).unwrap()),
DocumentId(NonZeroUsize::new(2).unwrap()),
];
assert!(View::change_align_view(Action::Replace, docs[0], docs[1]));
assert!(View::change_align_view(Action::Load, docs[0], docs[0]));
assert!(!View::change_align_view(Action::Load, docs[0], docs[1]));
}
}

0 comments on commit 2bced89

Please sign in to comment.