This commit is contained in:
Antonio Scandurra 2021-09-30 15:05:05 +02:00
parent 2f508af017
commit e98731876c

View file

@ -1,6 +1,8 @@
use crate::{ use crate::{
project::{self, Project}, project::{self, Project},
theme, worktree, Settings, theme,
worktree::{self, Worktree},
Settings,
}; };
use gpui::{ use gpui::{
action, action,
@ -26,13 +28,13 @@ pub struct ProjectPanel {
list: UniformListState, list: UniformListState,
visible_entries: Vec<Vec<usize>>, visible_entries: Vec<Vec<usize>>,
expanded_dir_ids: HashMap<usize, Vec<usize>>, expanded_dir_ids: HashMap<usize, Vec<usize>>,
selected_entry: Option<SelectedEntry>, selection: Option<Selection>,
settings: watch::Receiver<Settings>, settings: watch::Receiver<Settings>,
handle: WeakViewHandle<Self>, handle: WeakViewHandle<Self>,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct SelectedEntry { struct Selection {
worktree_id: usize, worktree_id: usize,
entry_id: usize, entry_id: usize,
index: usize, index: usize,
@ -44,7 +46,7 @@ struct EntryDetails {
depth: usize, depth: usize,
is_dir: bool, is_dir: bool,
is_expanded: bool, is_expanded: bool,
is_active: bool, is_selected: bool,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@ -53,8 +55,8 @@ pub struct ProjectEntry {
pub entry_id: usize, pub entry_id: usize,
} }
action!(ExpandActiveEntry); action!(ExpandSelectedEntry);
action!(CollapseActiveEntry); action!(CollapseSelectedEntry);
action!(ToggleExpanded, ProjectEntry); action!(ToggleExpanded, ProjectEntry);
action!(Open, ProjectEntry); action!(Open, ProjectEntry);
@ -65,8 +67,8 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(ProjectPanel::select_prev); cx.add_action(ProjectPanel::select_prev);
cx.add_action(ProjectPanel::select_next); cx.add_action(ProjectPanel::select_next);
cx.add_bindings([ cx.add_bindings([
Binding::new("right", ExpandActiveEntry, None), Binding::new("right", ExpandSelectedEntry, None),
Binding::new("left", CollapseActiveEntry, None), Binding::new("left", CollapseSelectedEntry, None),
]); ]);
} }
@ -84,18 +86,17 @@ impl ProjectPanel {
}) })
.detach(); .detach();
cx.subscribe(&project, |this, _, event, cx| match event { cx.subscribe(&project, |this, _, event, cx| match event {
project::Event::ActiveEntryChanged(entry) => { project::Event::ActiveEntryChanged(Some((worktree_id, entry_id))) => {
if let Some((worktree_id, entry_id)) = *entry { this.expand_entry(*worktree_id, *entry_id, cx);
this.expand_entry(worktree_id, entry_id, cx); this.update_visible_entries(Some((*worktree_id, *entry_id)), cx);
this.update_visible_entries(Some((worktree_id, entry_id)), cx); cx.notify();
cx.notify();
}
} }
project::Event::WorktreeRemoved(id) => { project::Event::WorktreeRemoved(id) => {
this.expanded_dir_ids.remove(id); this.expanded_dir_ids.remove(id);
this.update_visible_entries(None, cx); this.update_visible_entries(None, cx);
cx.notify(); cx.notify();
} }
_ => {}
}) })
.detach(); .detach();
@ -105,68 +106,60 @@ impl ProjectPanel {
list: Default::default(), list: Default::default(),
visible_entries: Default::default(), visible_entries: Default::default(),
expanded_dir_ids: Default::default(), expanded_dir_ids: Default::default(),
selected_entry: None, selection: None,
handle: cx.handle().downgrade(), handle: cx.handle().downgrade(),
}; };
this.update_visible_entries(None, cx); this.update_visible_entries(None, cx);
this this
} }
fn expand_selected_entry(&mut self, _: &ExpandActiveEntry, cx: &mut ViewContext<Self>) { fn expand_selected_entry(&mut self, _: &ExpandSelectedEntry, cx: &mut ViewContext<Self>) {
if let Some(selected_entry) = self.selected_entry { if let Some((worktree, entry)) = self.selected_entry(cx) {
let project = self.project.read(cx); let expanded_dir_ids =
if let Some(worktree) = project.worktree_for_id(selected_entry.worktree_id) { if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree.id()) {
if let Some(entry) = worktree.read(cx).entry_for_id(selected_entry.entry_id) { expanded_dir_ids
if entry.is_dir() { } else {
if let Some(expanded_dir_ids) = return;
self.expanded_dir_ids.get_mut(&selected_entry.worktree_id) };
{
match expanded_dir_ids.binary_search(&selected_entry.entry_id) { if entry.is_dir() {
Ok(_) => self.select_next(&SelectNext, cx), match expanded_dir_ids.binary_search(&entry.id) {
Err(ix) => { Ok(_) => self.select_next(&SelectNext, cx),
expanded_dir_ids.insert(ix, selected_entry.entry_id); Err(ix) => {
self.update_visible_entries(None, cx); expanded_dir_ids.insert(ix, entry.id);
cx.notify(); self.update_visible_entries(None, cx);
} cx.notify();
}
}
} else {
} }
} }
} else {
} }
} }
} }
fn collapse_selected_entry(&mut self, _: &CollapseActiveEntry, cx: &mut ViewContext<Self>) { fn collapse_selected_entry(&mut self, _: &CollapseSelectedEntry, cx: &mut ViewContext<Self>) {
if let Some(selected_entry) = self.selected_entry { if let Some((worktree, mut entry)) = self.selected_entry(cx) {
let project = self.project.read(cx); let expanded_dir_ids =
if let Some(worktree) = project.worktree_for_id(selected_entry.worktree_id) { if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree.id()) {
let worktree = worktree.read(cx); expanded_dir_ids
if let Some(mut entry) = worktree.entry_for_id(selected_entry.entry_id) { } else {
if let Some(expanded_dir_ids) = return;
self.expanded_dir_ids.get_mut(&selected_entry.worktree_id) };
{
loop { loop {
match expanded_dir_ids.binary_search(&entry.id) { match expanded_dir_ids.binary_search(&entry.id) {
Ok(ix) => { Ok(ix) => {
expanded_dir_ids.remove(ix); expanded_dir_ids.remove(ix);
self.update_visible_entries( self.update_visible_entries(Some((worktree.id(), entry.id)), cx);
Some((selected_entry.worktree_id, entry.id)), cx.notify();
cx, break;
); }
cx.notify(); Err(_) => {
break; if let Some(parent_entry) =
} entry.path.parent().and_then(|p| worktree.entry_for_path(p))
Err(_) => { {
if let Some(parent_entry) = entry = parent_entry;
entry.path.parent().and_then(|p| worktree.entry_for_path(p)) } else {
{ break;
entry = parent_entry;
} else {
break;
}
}
}
} }
} }
} }
@ -195,33 +188,29 @@ 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(selected_entry) = self.selected_entry { if let Some(selection) = self.selection {
if selected_entry.index > 0 { let prev_ix = selection.index.saturating_sub(1);
let (worktree_id, entry) = self let (worktree, entry) = self.visible_entry_for_index(prev_ix, cx).unwrap();
.visible_entry_for_index(selected_entry.index - 1, cx) self.selection = Some(Selection {
.unwrap(); worktree_id: worktree.id(),
self.selected_entry = Some(SelectedEntry { entry_id: entry.id,
worktree_id, index: prev_ix,
entry_id: entry.id, });
index: selected_entry.index - 1, self.autoscroll();
}); cx.notify();
self.autoscroll();
cx.notify();
}
} else { } else {
self.select_first(cx); self.select_first(cx);
} }
} }
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) { fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
if let Some(selected_entry) = self.selected_entry { if let Some(selection) = self.selection {
if let Some((worktree_id, entry)) = let next_ix = selection.index + 1;
self.visible_entry_for_index(selected_entry.index + 1, cx) if let Some((worktree, entry)) = self.visible_entry_for_index(next_ix, cx) {
{ self.selection = Some(Selection {
self.selected_entry = Some(SelectedEntry { worktree_id: worktree.id(),
worktree_id,
entry_id: entry.id, entry_id: entry.id,
index: selected_entry.index + 1, index: next_ix,
}); });
self.autoscroll(); self.autoscroll();
cx.notify(); cx.notify();
@ -236,7 +225,7 @@ impl ProjectPanel {
let worktree_id = worktree.id(); let worktree_id = worktree.id();
let worktree = worktree.read(cx); let worktree = worktree.read(cx);
if let Some(root_entry) = worktree.root_entry() { if let Some(root_entry) = worktree.root_entry() {
self.selected_entry = Some(SelectedEntry { self.selection = Some(Selection {
worktree_id, worktree_id,
entry_id: root_entry.id, entry_id: root_entry.id,
index: 0, index: 0,
@ -248,8 +237,8 @@ impl ProjectPanel {
} }
fn autoscroll(&mut self) { fn autoscroll(&mut self) {
if let Some(selected_entry) = self.selected_entry { if let Some(selection) = self.selection {
self.list.scroll_to(selected_entry.index); self.list.scroll_to(selection.index);
} }
} }
@ -257,13 +246,13 @@ impl ProjectPanel {
&self, &self,
target_ix: usize, target_ix: usize,
cx: &'a AppContext, cx: &'a AppContext,
) -> Option<(usize, &'a worktree::Entry)> { ) -> Option<(&'a Worktree, &'a worktree::Entry)> {
let project = self.project.read(cx); let project = self.project.read(cx);
let mut offset = None; let mut offset = None;
let mut ix = 0; let mut ix = 0;
for (worktree_ix, visible_entries) in self.visible_entries.iter().enumerate() { for (worktree_ix, visible_entries) in self.visible_entries.iter().enumerate() {
if target_ix < ix + visible_entries.len() { if target_ix < ix + visible_entries.len() {
let worktree = &project.worktrees()[worktree_ix]; let worktree = project.worktrees()[worktree_ix].read(cx);
offset = Some((worktree, visible_entries[target_ix - ix])); offset = Some((worktree, visible_entries[target_ix - ix]));
break; break;
} else { } else {
@ -272,12 +261,22 @@ impl ProjectPanel {
} }
offset.and_then(|(worktree, offset)| { offset.and_then(|(worktree, offset)| {
let mut entries = worktree.read(cx).entries(false); let mut entries = worktree.entries(false);
entries.advance_to_offset(offset); entries.advance_to_offset(offset);
Some((worktree.id(), entries.entry()?)) Some((worktree, entries.entry()?))
}) })
} }
fn selected_entry<'a>(
&self,
cx: &'a AppContext,
) -> Option<(&'a Worktree, &'a worktree::Entry)> {
let selection = self.selection?;
let project = self.project.read(cx);
let worktree = project.worktree_for_id(selection.worktree_id)?.read(cx);
Some((worktree, worktree.entry_for_id(selection.entry_id)?))
}
fn update_visible_entries( fn update_visible_entries(
&mut self, &mut self,
new_selected_entry: Option<(usize, usize)>, new_selected_entry: Option<(usize, usize)>,
@ -310,16 +309,16 @@ impl ProjectPanel {
visible_worktree_entries.push(entry_iter.offset()); visible_worktree_entries.push(entry_iter.offset());
if let Some(new_selected_entry) = new_selected_entry { if let Some(new_selected_entry) = new_selected_entry {
if new_selected_entry == (worktree.id(), item.id) { if new_selected_entry == (worktree.id(), item.id) {
self.selected_entry = Some(SelectedEntry { self.selection = Some(Selection {
worktree_id, worktree_id,
entry_id: item.id, entry_id: item.id,
index: entry_ix, index: entry_ix,
}); });
} }
} else if self.selected_entry.map_or(false, |e| { } else if self.selection.map_or(false, |e| {
e.worktree_id == worktree_id && e.entry_id == item.id e.worktree_id == worktree_id && e.entry_id == item.id
}) { }) {
self.selected_entry = Some(SelectedEntry { self.selection = Some(Selection {
worktree_id, worktree_id,
entry_id: item.id, entry_id: item.id,
index: entry_ix, index: entry_ix,
@ -407,7 +406,7 @@ impl ProjectPanel {
depth: entry.path.components().count(), depth: entry.path.components().count(),
is_dir: entry.is_dir(), is_dir: entry.is_dir(),
is_expanded: expanded_entry_ids.binary_search(&entry.id).is_ok(), is_expanded: expanded_entry_ids.binary_search(&entry.id).is_ok(),
is_active: self.selected_entry.map_or(false, |e| { is_selected: self.selection.map_or(false, |e| {
e.worktree_id == worktree.id() && e.entry_id == entry.id e.worktree_id == worktree.id() && e.entry_id == entry.id
}), }),
}; };
@ -433,7 +432,7 @@ impl ProjectPanel {
(entry.worktree_ix, entry.entry_id), (entry.worktree_ix, entry.entry_id),
cx, cx,
|state, _| { |state, _| {
let style = if details.is_active { let style = if details.is_selected {
&theme.selected_entry &theme.selected_entry
} else if state.hovered { } else if state.hovered {
&theme.hovered_entry &theme.hovered_entry
@ -578,56 +577,56 @@ mod tests {
depth: 0, depth: 0,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: ".dockerignore".to_string(), filename: ".dockerignore".to_string(),
depth: 1, depth: 1,
is_dir: false, is_dir: false,
is_expanded: false, is_expanded: false,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "a".to_string(), filename: "a".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "b".to_string(), filename: "b".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: 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_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "root2".to_string(), filename: "root2".to_string(),
depth: 0, depth: 0,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false is_selected: false
}, },
EntryDetails { EntryDetails {
filename: "d".to_string(), filename: "d".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false is_selected: false
}, },
EntryDetails { EntryDetails {
filename: "e".to_string(), filename: "e".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false is_selected: false
} }
], ],
); );
@ -641,70 +640,70 @@ mod tests {
depth: 0, depth: 0,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: ".dockerignore".to_string(), filename: ".dockerignore".to_string(),
depth: 1, depth: 1,
is_dir: false, is_dir: false,
is_expanded: false, is_expanded: false,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "a".to_string(), filename: "a".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "b".to_string(), filename: "b".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "3".to_string(), filename: "3".to_string(),
depth: 2, depth: 2,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "4".to_string(), filename: "4".to_string(),
depth: 2, depth: 2,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: 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_active: false, is_selected: false,
}, },
EntryDetails { EntryDetails {
filename: "root2".to_string(), filename: "root2".to_string(),
depth: 0, depth: 0,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false is_selected: false
}, },
EntryDetails { EntryDetails {
filename: "d".to_string(), filename: "d".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false is_selected: false
}, },
EntryDetails { EntryDetails {
filename: "e".to_string(), filename: "e".to_string(),
depth: 1, depth: 1,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: false is_selected: false
} }
] ]
); );
@ -717,21 +716,21 @@ mod tests {
depth: 2, depth: 2,
is_dir: true, is_dir: true,
is_expanded: false, is_expanded: false,
is_active: 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_active: false is_selected: false
}, },
EntryDetails { EntryDetails {
filename: "root2".to_string(), filename: "root2".to_string(),
depth: 0, depth: 0,
is_dir: true, is_dir: true,
is_expanded: true, is_expanded: true,
is_active: false is_selected: false
} }
] ]
); );