Store visible entries in project panel by id rather than offset
This commit is contained in:
parent
80b4324807
commit
11828040cc
1 changed files with 67 additions and 65 deletions
|
@ -24,7 +24,7 @@ use workspace::{
|
||||||
pub struct ProjectPanel {
|
pub struct ProjectPanel {
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
list: UniformListState,
|
list: UniformListState,
|
||||||
visible_entries: Vec<(WorktreeId, Vec<usize>)>,
|
visible_entries: Vec<(WorktreeId, Vec<ProjectEntryId>)>,
|
||||||
expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
|
expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
|
||||||
selection: Option<Selection>,
|
selection: Option<Selection>,
|
||||||
handle: WeakViewHandle<Self>,
|
handle: WeakViewHandle<Self>,
|
||||||
|
@ -34,7 +34,6 @@ pub struct ProjectPanel {
|
||||||
struct Selection {
|
struct Selection {
|
||||||
worktree_id: WorktreeId,
|
worktree_id: WorktreeId,
|
||||||
entry_id: ProjectEntryId,
|
entry_id: ProjectEntryId,
|
||||||
index: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -204,12 +203,23 @@ impl ProjectPanel {
|
||||||
|
|
||||||
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
|
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
|
||||||
if let Some(selection) = self.selection {
|
if let Some(selection) = self.selection {
|
||||||
let prev_ix = selection.index.saturating_sub(1);
|
let (mut worktree_ix, mut entry_ix, _) =
|
||||||
let (worktree, entry) = self.visible_entry_for_index(prev_ix, cx).unwrap();
|
self.index_for_selection(selection).unwrap_or_default();
|
||||||
|
if entry_ix > 0 {
|
||||||
|
entry_ix -= 1;
|
||||||
|
} else {
|
||||||
|
if worktree_ix > 0 {
|
||||||
|
worktree_ix -= 1;
|
||||||
|
entry_ix = self.visible_entries[worktree_ix].1.len() - 1;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (worktree_id, worktree_entries) = &self.visible_entries[worktree_ix];
|
||||||
self.selection = Some(Selection {
|
self.selection = Some(Selection {
|
||||||
worktree_id: worktree.id(),
|
worktree_id: *worktree_id,
|
||||||
entry_id: entry.id,
|
entry_id: worktree_entries[entry_ix],
|
||||||
index: prev_ix,
|
|
||||||
});
|
});
|
||||||
self.autoscroll();
|
self.autoscroll();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
|
@ -224,16 +234,27 @@ impl ProjectPanel {
|
||||||
|
|
||||||
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
|
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
|
||||||
if let Some(selection) = self.selection {
|
if let Some(selection) = self.selection {
|
||||||
let next_ix = selection.index + 1;
|
let (mut worktree_ix, mut entry_ix, _) =
|
||||||
if let Some((worktree, entry)) = self.visible_entry_for_index(next_ix, cx) {
|
self.index_for_selection(selection).unwrap_or_default();
|
||||||
|
if let Some((_, worktree_entries)) = self.visible_entries.get(worktree_ix) {
|
||||||
|
if entry_ix + 1 < worktree_entries.len() {
|
||||||
|
entry_ix += 1;
|
||||||
|
} else {
|
||||||
|
worktree_ix += 1;
|
||||||
|
entry_ix = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((worktree_id, worktree_entries)) = self.visible_entries.get(worktree_ix) {
|
||||||
|
if let Some(entry_id) = worktree_entries.get(entry_ix) {
|
||||||
self.selection = Some(Selection {
|
self.selection = Some(Selection {
|
||||||
worktree_id: worktree.id(),
|
worktree_id: *worktree_id,
|
||||||
entry_id: entry.id,
|
entry_id: *entry_id,
|
||||||
index: next_ix,
|
|
||||||
});
|
});
|
||||||
self.autoscroll();
|
self.autoscroll();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.select_first(cx);
|
self.select_first(cx);
|
||||||
}
|
}
|
||||||
|
@ -251,7 +272,6 @@ impl ProjectPanel {
|
||||||
self.selection = Some(Selection {
|
self.selection = Some(Selection {
|
||||||
worktree_id,
|
worktree_id,
|
||||||
entry_id: root_entry.id,
|
entry_id: root_entry.id,
|
||||||
index: 0,
|
|
||||||
});
|
});
|
||||||
self.autoscroll();
|
self.autoscroll();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
|
@ -260,35 +280,32 @@ impl ProjectPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn autoscroll(&mut self) {
|
fn autoscroll(&mut self) {
|
||||||
if let Some(selection) = self.selection {
|
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
|
||||||
self.list.scroll_to(ScrollTarget::Show(selection.index));
|
self.list.scroll_to(ScrollTarget::Show(index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visible_entry_for_index<'a>(
|
fn index_for_selection(&self, selection: Selection) -> Option<(usize, usize, usize)> {
|
||||||
&self,
|
let mut worktree_index = 0;
|
||||||
target_ix: usize,
|
let mut entry_index = 0;
|
||||||
cx: &'a AppContext,
|
let mut visible_entries_index = 0;
|
||||||
) -> Option<(&'a Worktree, &'a project::Entry)> {
|
for (worktree_id, worktree_entries) in &self.visible_entries {
|
||||||
let project = self.project.read(cx);
|
if *worktree_id == selection.worktree_id {
|
||||||
let mut offset = None;
|
for entry_id in worktree_entries {
|
||||||
let mut ix = 0;
|
if *entry_id == selection.entry_id {
|
||||||
for (worktree_id, visible_entries) in &self.visible_entries {
|
return Some((worktree_index, entry_index, visible_entries_index));
|
||||||
if target_ix < ix + visible_entries.len() {
|
} else {
|
||||||
offset = project
|
visible_entries_index += 1;
|
||||||
.worktree_for_id(*worktree_id, cx)
|
entry_index += 1;
|
||||||
.map(|w| (w.read(cx), visible_entries[target_ix - ix]));
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
ix += visible_entries.len();
|
visible_entries_index += worktree_entries.len();
|
||||||
}
|
}
|
||||||
|
worktree_index += 1;
|
||||||
}
|
}
|
||||||
|
None
|
||||||
offset.and_then(|(worktree, offset)| {
|
|
||||||
let mut entries = worktree.entries(false);
|
|
||||||
entries.advance_to_offset(offset);
|
|
||||||
Some((worktree, entries.entry()?))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn selected_entry<'a>(&self, cx: &'a AppContext) -> Option<(&'a Worktree, &'a project::Entry)> {
|
fn selected_entry<'a>(&self, cx: &'a AppContext) -> Option<(&'a Worktree, &'a project::Entry)> {
|
||||||
|
@ -310,7 +327,6 @@ impl ProjectPanel {
|
||||||
.filter(|worktree| worktree.read(cx).is_visible());
|
.filter(|worktree| worktree.read(cx).is_visible());
|
||||||
self.visible_entries.clear();
|
self.visible_entries.clear();
|
||||||
|
|
||||||
let mut entry_ix = 0;
|
|
||||||
for worktree in worktrees {
|
for worktree in worktrees {
|
||||||
let snapshot = worktree.read(cx).snapshot();
|
let snapshot = worktree.read(cx).snapshot();
|
||||||
let worktree_id = snapshot.id();
|
let worktree_id = snapshot.id();
|
||||||
|
@ -331,26 +347,7 @@ impl ProjectPanel {
|
||||||
let mut visible_worktree_entries = Vec::new();
|
let mut visible_worktree_entries = Vec::new();
|
||||||
let mut entry_iter = snapshot.entries(false);
|
let mut entry_iter = snapshot.entries(false);
|
||||||
while let Some(item) = entry_iter.entry() {
|
while let Some(item) = entry_iter.entry() {
|
||||||
visible_worktree_entries.push(entry_iter.offset());
|
visible_worktree_entries.push(item.id);
|
||||||
if let Some(new_selected_entry) = new_selected_entry {
|
|
||||||
if new_selected_entry == (worktree_id, item.id) {
|
|
||||||
self.selection = Some(Selection {
|
|
||||||
worktree_id,
|
|
||||||
entry_id: item.id,
|
|
||||||
index: entry_ix,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if self.selection.map_or(false, |e| {
|
|
||||||
e.worktree_id == worktree_id && e.entry_id == item.id
|
|
||||||
}) {
|
|
||||||
self.selection = Some(Selection {
|
|
||||||
worktree_id,
|
|
||||||
entry_id: item.id,
|
|
||||||
index: entry_ix,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
entry_ix += 1;
|
|
||||||
if expanded_dir_ids.binary_search(&item.id).is_err() {
|
if expanded_dir_ids.binary_search(&item.id).is_err() {
|
||||||
if entry_iter.advance_to_sibling() {
|
if entry_iter.advance_to_sibling() {
|
||||||
continue;
|
continue;
|
||||||
|
@ -361,6 +358,13 @@ impl ProjectPanel {
|
||||||
self.visible_entries
|
self.visible_entries
|
||||||
.push((worktree_id, visible_worktree_entries));
|
.push((worktree_id, visible_worktree_entries));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some((worktree_id, entry_id)) = new_selected_entry {
|
||||||
|
self.selection = Some(Selection {
|
||||||
|
worktree_id,
|
||||||
|
entry_id,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_entry(
|
fn expand_entry(
|
||||||
|
@ -419,14 +423,12 @@ impl ProjectPanel {
|
||||||
.map(Vec::as_slice)
|
.map(Vec::as_slice)
|
||||||
.unwrap_or(&[]);
|
.unwrap_or(&[]);
|
||||||
let root_name = OsStr::new(snapshot.root_name());
|
let root_name = OsStr::new(snapshot.root_name());
|
||||||
let mut cursor = snapshot.entries(false);
|
for entry_id in visible_worktree_entries
|
||||||
|
[range.start.saturating_sub(ix)..end_ix - ix]
|
||||||
for ix in visible_worktree_entries[range.start.saturating_sub(ix)..end_ix - ix]
|
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
.copied()
|
||||||
{
|
{
|
||||||
cursor.advance_to_offset(ix);
|
if let Some(entry) = snapshot.entry_for_id(entry_id) {
|
||||||
if let Some(entry) = cursor.entry() {
|
|
||||||
let filename = entry.path.file_name().unwrap_or(root_name);
|
let filename = entry.path.file_name().unwrap_or(root_name);
|
||||||
let details = EntryDetails {
|
let details = EntryDetails {
|
||||||
filename: filename.to_string_lossy().to_string(),
|
filename: filename.to_string_lossy().to_string(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue