Keep cursor at top when diff view is first opened (#25682)
Previously, we had the cursor at the bottom while the scroll stayed at the top. Now, if you run `git: diff`, the cursor will also be at the top. The cursor moving to the end was possibly a side-effect of using `Bias::Right` for selections. --- Release Notes: - N/A Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
4e60ebab5e
commit
cc3b5c729e
1 changed files with 23 additions and 16 deletions
|
@ -106,7 +106,7 @@ impl ProjectDiff {
|
||||||
};
|
};
|
||||||
if let Some(entry) = entry {
|
if let Some(entry) = entry {
|
||||||
project_diff.update(cx, |project_diff, cx| {
|
project_diff.update(cx, |project_diff, cx| {
|
||||||
project_diff.scroll_to(entry, window, cx);
|
project_diff.move_to_entry(entry, window, cx);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ impl ProjectDiff {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_to(
|
pub fn move_to_entry(
|
||||||
&mut self,
|
&mut self,
|
||||||
entry: GitStatusEntry,
|
entry: GitStatusEntry,
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
|
@ -189,16 +189,16 @@ impl ProjectDiff {
|
||||||
|
|
||||||
let path_key = PathKey::namespaced(namespace, entry.repo_path.0.clone());
|
let path_key = PathKey::namespaced(namespace, entry.repo_path.0.clone());
|
||||||
|
|
||||||
self.scroll_to_path(path_key, window, cx)
|
self.move_to_path(path_key, window, cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scroll_to_path(&mut self, path_key: PathKey, window: &mut Window, cx: &mut Context<Self>) {
|
fn move_to_path(&mut self, path_key: PathKey, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
if let Some(position) = self.multibuffer.read(cx).location_for_path(&path_key, cx) {
|
if let Some(position) = self.multibuffer.read(cx).location_for_path(&path_key, cx) {
|
||||||
self.editor.update(cx, |editor, cx| {
|
self.editor.update(cx, |editor, cx| {
|
||||||
editor.change_selections(Some(Autoscroll::focused()), window, cx, |s| {
|
editor.change_selections(Some(Autoscroll::focused()), window, cx, |s| {
|
||||||
s.select_ranges([position..position]);
|
s.select_ranges([position..position]);
|
||||||
})
|
})
|
||||||
})
|
});
|
||||||
} else {
|
} else {
|
||||||
self.pending_scroll = Some(path_key);
|
self.pending_scroll = Some(path_key);
|
||||||
}
|
}
|
||||||
|
@ -385,21 +385,28 @@ impl ProjectDiff {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_excerpt_newly_added = self.multibuffer.update(cx, |multibuffer, cx| {
|
let (was_empty, is_excerpt_newly_added) = self.multibuffer.update(cx, |multibuffer, cx| {
|
||||||
multibuffer.set_excerpts_for_path(
|
let was_empty = multibuffer.is_empty();
|
||||||
|
let is_newly_added = multibuffer.set_excerpts_for_path(
|
||||||
path_key.clone(),
|
path_key.clone(),
|
||||||
buffer,
|
buffer,
|
||||||
diff_hunk_ranges,
|
diff_hunk_ranges,
|
||||||
editor::DEFAULT_MULTIBUFFER_CONTEXT,
|
editor::DEFAULT_MULTIBUFFER_CONTEXT,
|
||||||
cx,
|
cx,
|
||||||
)
|
);
|
||||||
|
(was_empty, is_newly_added)
|
||||||
});
|
});
|
||||||
|
|
||||||
if is_excerpt_newly_added && diff_buffer.file_status.is_deleted() {
|
|
||||||
self.editor.update(cx, |editor, cx| {
|
self.editor.update(cx, |editor, cx| {
|
||||||
editor.fold_buffer(snapshot.text.remote_id(), cx)
|
if was_empty {
|
||||||
|
editor.change_selections(None, window, cx, |selections| {
|
||||||
|
selections.select_ranges([0..0])
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if is_excerpt_newly_added && diff_buffer.file_status.is_deleted() {
|
||||||
|
editor.fold_buffer(snapshot.text.remote_id(), cx)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if self.multibuffer.read(cx).is_empty()
|
if self.multibuffer.read(cx).is_empty()
|
||||||
&& self
|
&& self
|
||||||
|
@ -415,7 +422,7 @@ impl ProjectDiff {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if self.pending_scroll.as_ref() == Some(&path_key) {
|
if self.pending_scroll.as_ref() == Some(&path_key) {
|
||||||
self.scroll_to_path(path_key, window, cx);
|
self.move_to_path(path_key, window, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -995,13 +1002,13 @@ mod tests {
|
||||||
cx,
|
cx,
|
||||||
&"
|
&"
|
||||||
- foo
|
- foo
|
||||||
+ FOO
|
+ ˇFOO
|
||||||
ˇ"
|
"
|
||||||
.unindent(),
|
.unindent(),
|
||||||
);
|
);
|
||||||
|
|
||||||
editor.update_in(cx, |editor, window, cx| {
|
editor.update_in(cx, |editor, window, cx| {
|
||||||
editor.restore_file(&Default::default(), window, cx);
|
editor.git_restore(&Default::default(), window, cx);
|
||||||
});
|
});
|
||||||
fs.with_git_state(path!("/project/.git").as_ref(), true, |state| {
|
fs.with_git_state(path!("/project/.git").as_ref(), true, |state| {
|
||||||
state.statuses = HashMap::default();
|
state.statuses = HashMap::default();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue