fix(tree): deleting last file causes panic

pull/9/head
wongjiahau 2 years ago
parent ef73559a8e
commit 0f8e0a51ba

@ -548,15 +548,13 @@ impl<T: TreeItem> TreeView<T> {
fn set_selected(&mut self, selected: usize) { fn set_selected(&mut self, selected: usize) {
if selected > self.selected { if selected > self.selected {
// Move down // Move down
self.winline = std::cmp::min( self.winline = selected.min(
selected,
self.winline self.winline
.saturating_add(selected.saturating_sub(self.selected)), .saturating_add(selected.saturating_sub(self.selected)),
); );
} else { } else {
// Move up // Move up
self.winline = std::cmp::min( self.winline = selected.min(
selected,
self.winline self.winline
.saturating_sub(self.selected.saturating_sub(selected)), .saturating_sub(self.selected.saturating_sub(selected)),
); );
@ -567,7 +565,7 @@ impl<T: TreeItem> TreeView<T> {
pub fn move_up(&mut self, rows: usize) { pub fn move_up(&mut self, rows: usize) {
let len = self.tree.len(); let len = self.tree.len();
if len > 0 { if len > 0 {
self.set_selected(std::cmp::max(0, self.selected.saturating_sub(rows))) self.set_selected(self.selected.saturating_sub(rows).max(0))
} }
} }
@ -647,7 +645,8 @@ impl<T: TreeItem> TreeView<T> {
} }
pub fn remove_current(&mut self) { pub fn remove_current(&mut self) {
self.tree.remove(self.selected) self.tree.remove(self.selected);
self.set_selected(self.selected.min(self.tree.len().saturating_sub(1)));
} }
pub fn replace_current(&mut self, item: T) { pub fn replace_current(&mut self, item: T) {
@ -695,7 +694,7 @@ impl<T: TreeItem + Clone> TreeView<T> {
self.max_len = 0; self.max_len = 0;
self.area_height = area.height.saturating_sub(1) as usize; self.area_height = area.height.saturating_sub(1) as usize;
self.winline = std::cmp::min(self.winline, self.area_height); self.winline = self.winline.min(self.area_height);
let style = cx.editor.theme.get(&self.tree_symbol_style); let style = cx.editor.theme.get(&self.tree_symbol_style);
let ancestor_style = cx.editor.theme.get("ui.text.focus"); let ancestor_style = cx.editor.theme.get("ui.text.focus");
let skip = self.selected.saturating_sub(self.winline); let skip = self.selected.saturating_sub(self.winline);

Loading…
Cancel
Save