Merge pull request #864 from zed-industries/polish-project-panel

Sort directories before files in a case-insensitive manner
This commit is contained in:
Max Brunsfeld 2022-04-21 11:48:04 -07:00 committed by GitHub
commit 066e572767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 103 deletions

2
Cargo.lock generated
View File

@ -3545,11 +3545,11 @@ name = "project_panel"
version = "0.1.0"
dependencies = [
"gpui",
"postage",
"project",
"serde_json",
"settings",
"theme",
"unicase",
"util",
"workspace",
]

View File

@ -14,7 +14,7 @@ settings = { path = "../settings" }
theme = { path = "../theme" }
util = { path = "../util" }
workspace = { path = "../workspace" }
postage = { version = "0.4.1", features = ["futures-traits"] }
unicase = "2.6"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }

View File

@ -9,13 +9,15 @@ use gpui::{
AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, View, ViewContext,
ViewHandle, WeakViewHandle,
};
use project::{Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId};
use project::{Entry, Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId};
use settings::Settings;
use std::{
cmp::Ordering,
collections::{hash_map, HashMap},
ffi::OsStr,
ops::Range,
};
use unicase::UniCase;
use workspace::{
menu::{SelectNext, SelectPrev},
Workspace,
@ -24,7 +26,7 @@ use workspace::{
pub struct ProjectPanel {
project: ModelHandle<Project>,
list: UniformListState,
visible_entries: Vec<(WorktreeId, Vec<usize>)>,
visible_entries: Vec<(WorktreeId, Vec<Entry>)>,
expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
selection: Option<Selection>,
handle: WeakViewHandle<Self>,
@ -34,7 +36,6 @@ pub struct ProjectPanel {
struct Selection {
worktree_id: WorktreeId,
entry_id: ProjectEntryId,
index: usize,
}
#[derive(Debug, PartialEq, Eq)]
@ -204,12 +205,23 @@ impl ProjectPanel {
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
if let Some(selection) = self.selection {
let prev_ix = selection.index.saturating_sub(1);
let (worktree, entry) = self.visible_entry_for_index(prev_ix, cx).unwrap();
let (mut worktree_ix, mut entry_ix, _) =
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 {
worktree_id: worktree.id(),
entry_id: entry.id,
index: prev_ix,
worktree_id: *worktree_id,
entry_id: worktree_entries[entry_ix].id,
});
self.autoscroll();
cx.notify();
@ -224,15 +236,26 @@ impl ProjectPanel {
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
if let Some(selection) = self.selection {
let next_ix = selection.index + 1;
if let Some((worktree, entry)) = self.visible_entry_for_index(next_ix, cx) {
self.selection = Some(Selection {
worktree_id: worktree.id(),
entry_id: entry.id,
index: next_ix,
});
self.autoscroll();
cx.notify();
let (mut worktree_ix, mut entry_ix, _) =
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) = worktree_entries.get(entry_ix) {
self.selection = Some(Selection {
worktree_id: *worktree_id,
entry_id: entry.id,
});
self.autoscroll();
cx.notify();
}
}
} else {
self.select_first(cx);
@ -251,7 +274,6 @@ impl ProjectPanel {
self.selection = Some(Selection {
worktree_id,
entry_id: root_entry.id,
index: 0,
});
self.autoscroll();
cx.notify();
@ -260,35 +282,32 @@ impl ProjectPanel {
}
fn autoscroll(&mut self) {
if let Some(selection) = self.selection {
self.list.scroll_to(ScrollTarget::Show(selection.index));
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
self.list.scroll_to(ScrollTarget::Show(index));
}
}
fn visible_entry_for_index<'a>(
&self,
target_ix: usize,
cx: &'a AppContext,
) -> Option<(&'a Worktree, &'a project::Entry)> {
let project = self.project.read(cx);
let mut offset = None;
let mut ix = 0;
for (worktree_id, visible_entries) in &self.visible_entries {
if target_ix < ix + visible_entries.len() {
offset = project
.worktree_for_id(*worktree_id, cx)
.map(|w| (w.read(cx), visible_entries[target_ix - ix]));
fn index_for_selection(&self, selection: Selection) -> Option<(usize, usize, usize)> {
let mut worktree_index = 0;
let mut entry_index = 0;
let mut visible_entries_index = 0;
for (worktree_id, worktree_entries) in &self.visible_entries {
if *worktree_id == selection.worktree_id {
for entry in worktree_entries {
if entry.id == selection.entry_id {
return Some((worktree_index, entry_index, visible_entries_index));
} else {
visible_entries_index += 1;
entry_index += 1;
}
}
break;
} else {
ix += visible_entries.len();
visible_entries_index += worktree_entries.len();
}
worktree_index += 1;
}
offset.and_then(|(worktree, offset)| {
let mut entries = worktree.entries(false);
entries.advance_to_offset(offset);
Some((worktree, entries.entry()?))
})
None
}
fn selected_entry<'a>(&self, cx: &'a AppContext) -> Option<(&'a Worktree, &'a project::Entry)> {
@ -310,7 +329,6 @@ impl ProjectPanel {
.filter(|worktree| worktree.read(cx).is_visible());
self.visible_entries.clear();
let mut entry_ix = 0;
for worktree in worktrees {
let snapshot = worktree.read(cx).snapshot();
let worktree_id = snapshot.id();
@ -331,26 +349,7 @@ impl ProjectPanel {
let mut visible_worktree_entries = Vec::new();
let mut entry_iter = snapshot.entries(false);
while let Some(item) = entry_iter.entry() {
visible_worktree_entries.push(entry_iter.offset());
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;
visible_worktree_entries.push(item.clone());
if expanded_dir_ids.binary_search(&item.id).is_err() {
if entry_iter.advance_to_sibling() {
continue;
@ -358,9 +357,41 @@ impl ProjectPanel {
}
entry_iter.advance();
}
visible_worktree_entries.sort_by(|entry_a, entry_b| {
let mut components_a = entry_a.path.components().peekable();
let mut components_b = entry_b.path.components().peekable();
loop {
match (components_a.next(), components_b.next()) {
(Some(component_a), Some(component_b)) => {
let a_is_file = components_a.peek().is_none() && entry_a.is_file();
let b_is_file = components_b.peek().is_none() && entry_b.is_file();
let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
let name_a =
UniCase::new(component_a.as_os_str().to_string_lossy());
let name_b =
UniCase::new(component_b.as_os_str().to_string_lossy());
name_a.cmp(&name_b)
});
if !ordering.is_eq() {
return ordering;
}
}
(Some(_), None) => break Ordering::Greater,
(None, Some(_)) => break Ordering::Less,
(None, None) => break Ordering::Equal,
}
}
});
self.visible_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(
@ -419,26 +450,19 @@ impl ProjectPanel {
.map(Vec::as_slice)
.unwrap_or(&[]);
let root_name = OsStr::new(snapshot.root_name());
let mut cursor = snapshot.entries(false);
for ix in visible_worktree_entries[range.start.saturating_sub(ix)..end_ix - ix]
.iter()
.copied()
for entry in &visible_worktree_entries[range.start.saturating_sub(ix)..end_ix - ix]
{
cursor.advance_to_offset(ix);
if let Some(entry) = cursor.entry() {
let filename = entry.path.file_name().unwrap_or(root_name);
let details = EntryDetails {
filename: filename.to_string_lossy().to_string(),
depth: entry.path.components().count(),
is_dir: entry.is_dir(),
is_expanded: expanded_entry_ids.binary_search(&entry.id).is_ok(),
is_selected: self.selection.map_or(false, |e| {
e.worktree_id == snapshot.id() && e.entry_id == entry.id
}),
};
callback(entry.id, details, cx);
}
let filename = entry.path.file_name().unwrap_or(root_name);
let details = EntryDetails {
filename: filename.to_string_lossy().to_string(),
depth: entry.path.components().count(),
is_dir: entry.is_dir(),
is_expanded: expanded_entry_ids.binary_search(&entry.id).is_ok(),
is_selected: self.selection.map_or(false, |e| {
e.worktree_id == snapshot.id() && e.entry_id == entry.id
}),
};
callback(entry.id, details, cx);
}
}
ix = end_ix;
@ -586,7 +610,7 @@ mod tests {
"3": { "Q": "" },
"4": { "R": "", "S": "", "T": "", "U": "" },
},
"c": {
"C": {
"5": {},
"6": { "V": "", "W": "" },
"7": { "X": "" },
@ -646,13 +670,6 @@ mod tests {
is_expanded: true,
is_selected: false,
},
EntryDetails {
filename: ".dockerignore".to_string(),
depth: 1,
is_dir: false,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: "a".to_string(),
depth: 1,
@ -668,12 +685,19 @@ mod tests {
is_selected: false,
},
EntryDetails {
filename: "c".to_string(),
filename: "C".to_string(),
depth: 1,
is_dir: true,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: ".dockerignore".to_string(),
depth: 1,
is_dir: false,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: "root2".to_string(),
depth: 0,
@ -709,13 +733,6 @@ mod tests {
is_expanded: true,
is_selected: false,
},
EntryDetails {
filename: ".dockerignore".to_string(),
depth: 1,
is_dir: false,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: "a".to_string(),
depth: 1,
@ -745,12 +762,19 @@ mod tests {
is_selected: false,
},
EntryDetails {
filename: "c".to_string(),
filename: "C".to_string(),
depth: 1,
is_dir: true,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: ".dockerignore".to_string(),
depth: 1,
is_dir: false,
is_expanded: false,
is_selected: false,
},
EntryDetails {
filename: "root2".to_string(),
depth: 0,
@ -779,16 +803,16 @@ mod tests {
visible_entry_details(&panel, 5..8, cx),
[
EntryDetails {
filename: "4".to_string(),
depth: 2,
filename: "C".to_string(),
depth: 1,
is_dir: true,
is_expanded: false,
is_selected: false
},
EntryDetails {
filename: "c".to_string(),
filename: ".dockerignore".to_string(),
depth: 1,
is_dir: true,
is_dir: false,
is_expanded: false,
is_selected: false
},