mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-08 22:07:49 +03:00
Merge pull request #864 from zed-industries/polish-project-panel
Sort directories before files in a case-insensitive manner
This commit is contained in:
commit
066e572767
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -3545,11 +3545,11 @@ name = "project_panel"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"gpui",
|
"gpui",
|
||||||
"postage",
|
|
||||||
"project",
|
"project",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"settings",
|
"settings",
|
||||||
"theme",
|
"theme",
|
||||||
|
"unicase",
|
||||||
"util",
|
"util",
|
||||||
"workspace",
|
"workspace",
|
||||||
]
|
]
|
||||||
|
@ -14,7 +14,7 @@ settings = { path = "../settings" }
|
|||||||
theme = { path = "../theme" }
|
theme = { path = "../theme" }
|
||||||
util = { path = "../util" }
|
util = { path = "../util" }
|
||||||
workspace = { path = "../workspace" }
|
workspace = { path = "../workspace" }
|
||||||
postage = { version = "0.4.1", features = ["futures-traits"] }
|
unicase = "2.6"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gpui = { path = "../gpui", features = ["test-support"] }
|
gpui = { path = "../gpui", features = ["test-support"] }
|
||||||
|
@ -9,13 +9,15 @@ use gpui::{
|
|||||||
AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, View, ViewContext,
|
AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, View, ViewContext,
|
||||||
ViewHandle, WeakViewHandle,
|
ViewHandle, WeakViewHandle,
|
||||||
};
|
};
|
||||||
use project::{Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId};
|
use project::{Entry, Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::{
|
use std::{
|
||||||
|
cmp::Ordering,
|
||||||
collections::{hash_map, HashMap},
|
collections::{hash_map, HashMap},
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
ops::Range,
|
ops::Range,
|
||||||
};
|
};
|
||||||
|
use unicase::UniCase;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
menu::{SelectNext, SelectPrev},
|
menu::{SelectNext, SelectPrev},
|
||||||
Workspace,
|
Workspace,
|
||||||
@ -24,7 +26,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<Entry>)>,
|
||||||
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 +36,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 +205,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].id,
|
||||||
index: prev_ix,
|
|
||||||
});
|
});
|
||||||
self.autoscroll();
|
self.autoscroll();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
@ -224,16 +236,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) = 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 +274,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 +282,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 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 +329,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 +349,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.clone());
|
||||||
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;
|
||||||
@ -358,9 +357,41 @@ impl ProjectPanel {
|
|||||||
}
|
}
|
||||||
entry_iter.advance();
|
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
|
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 +450,8 @@ 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 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()
|
|
||||||
.copied()
|
|
||||||
{
|
{
|
||||||
cursor.advance_to_offset(ix);
|
|
||||||
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(),
|
||||||
@ -440,7 +465,6 @@ impl ProjectPanel {
|
|||||||
callback(entry.id, details, cx);
|
callback(entry.id, details, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
ix = end_ix;
|
ix = end_ix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -586,7 +610,7 @@ mod tests {
|
|||||||
"3": { "Q": "" },
|
"3": { "Q": "" },
|
||||||
"4": { "R": "", "S": "", "T": "", "U": "" },
|
"4": { "R": "", "S": "", "T": "", "U": "" },
|
||||||
},
|
},
|
||||||
"c": {
|
"C": {
|
||||||
"5": {},
|
"5": {},
|
||||||
"6": { "V": "", "W": "" },
|
"6": { "V": "", "W": "" },
|
||||||
"7": { "X": "" },
|
"7": { "X": "" },
|
||||||
@ -646,13 +670,6 @@ mod tests {
|
|||||||
is_expanded: true,
|
is_expanded: true,
|
||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
EntryDetails {
|
|
||||||
filename: ".dockerignore".to_string(),
|
|
||||||
depth: 1,
|
|
||||||
is_dir: false,
|
|
||||||
is_expanded: false,
|
|
||||||
is_selected: false,
|
|
||||||
},
|
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "a".to_string(),
|
filename: "a".to_string(),
|
||||||
depth: 1,
|
depth: 1,
|
||||||
@ -668,12 +685,19 @@ mod tests {
|
|||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "c".to_string(),
|
filename: "C".to_string(),
|
||||||
depth: 1,
|
depth: 1,
|
||||||
is_dir: true,
|
is_dir: true,
|
||||||
is_expanded: false,
|
is_expanded: false,
|
||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
|
EntryDetails {
|
||||||
|
filename: ".dockerignore".to_string(),
|
||||||
|
depth: 1,
|
||||||
|
is_dir: false,
|
||||||
|
is_expanded: false,
|
||||||
|
is_selected: false,
|
||||||
|
},
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "root2".to_string(),
|
filename: "root2".to_string(),
|
||||||
depth: 0,
|
depth: 0,
|
||||||
@ -709,13 +733,6 @@ mod tests {
|
|||||||
is_expanded: true,
|
is_expanded: true,
|
||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
EntryDetails {
|
|
||||||
filename: ".dockerignore".to_string(),
|
|
||||||
depth: 1,
|
|
||||||
is_dir: false,
|
|
||||||
is_expanded: false,
|
|
||||||
is_selected: false,
|
|
||||||
},
|
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "a".to_string(),
|
filename: "a".to_string(),
|
||||||
depth: 1,
|
depth: 1,
|
||||||
@ -745,12 +762,19 @@ mod tests {
|
|||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "c".to_string(),
|
filename: "C".to_string(),
|
||||||
depth: 1,
|
depth: 1,
|
||||||
is_dir: true,
|
is_dir: true,
|
||||||
is_expanded: false,
|
is_expanded: false,
|
||||||
is_selected: false,
|
is_selected: false,
|
||||||
},
|
},
|
||||||
|
EntryDetails {
|
||||||
|
filename: ".dockerignore".to_string(),
|
||||||
|
depth: 1,
|
||||||
|
is_dir: false,
|
||||||
|
is_expanded: false,
|
||||||
|
is_selected: false,
|
||||||
|
},
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "root2".to_string(),
|
filename: "root2".to_string(),
|
||||||
depth: 0,
|
depth: 0,
|
||||||
@ -779,16 +803,16 @@ mod tests {
|
|||||||
visible_entry_details(&panel, 5..8, cx),
|
visible_entry_details(&panel, 5..8, cx),
|
||||||
[
|
[
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "4".to_string(),
|
filename: "C".to_string(),
|
||||||
depth: 2,
|
depth: 1,
|
||||||
is_dir: true,
|
is_dir: true,
|
||||||
is_expanded: false,
|
is_expanded: false,
|
||||||
is_selected: false
|
is_selected: false
|
||||||
},
|
},
|
||||||
EntryDetails {
|
EntryDetails {
|
||||||
filename: "c".to_string(),
|
filename: ".dockerignore".to_string(),
|
||||||
depth: 1,
|
depth: 1,
|
||||||
is_dir: true,
|
is_dir: false,
|
||||||
is_expanded: false,
|
is_expanded: false,
|
||||||
is_selected: false
|
is_selected: false
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user