Merge branch 'tree_explore' of github.com:pinelang/helix-tree-explorer into tree_explore

pull/9/head
wongjiahau 1 year ago
commit c0073edebf

@ -49,13 +49,18 @@ impl FileInfo {
} }
fn get_text(&self) -> Cow<'static, str> { fn get_text(&self) -> Cow<'static, str> {
match self.file_type { let text = match self.file_type {
FileType::Root => format!("{}", self.path.display()).into(), FileType::Root => format!("{}", self.path.display()),
FileType::File | FileType::Folder => self FileType::File | FileType::Folder => self
.path .path
.file_name() .file_name()
.map_or("/".into(), |p| p.to_string_lossy().into_owned().into()), .map_or("/".into(), |p| p.to_string_lossy().into_owned()),
} };
#[cfg(test)]
let text = text.replace(std::path::MAIN_SEPARATOR, "/");
text.into()
} }
} }
@ -197,8 +202,7 @@ impl Explorer {
fn new_tree_view(root: PathBuf) -> Result<TreeView<FileInfo>> { fn new_tree_view(root: PathBuf) -> Result<TreeView<FileInfo>> {
let root = FileInfo::root(root); let root = FileInfo::root(root);
let children = root.get_children()?; Ok(TreeView::build_tree(root)?.with_enter_fn(Self::toggle_current))
Ok(TreeView::build_tree(root, children).with_enter_fn(Self::toggle_current))
} }
fn push_history(&mut self, tree_view: TreeView<FileInfo>) { fn push_history(&mut self, tree_view: TreeView<FileInfo>) {
@ -220,31 +224,38 @@ impl Explorer {
fn reveal_file(&mut self, path: PathBuf) -> Result<()> { fn reveal_file(&mut self, path: PathBuf) -> Result<()> {
let current_root = &self.state.current_root; let current_root = &self.state.current_root;
let current_path = path.as_path().to_string_lossy().to_string(); let current_path = &path;
let current_root = current_root.as_path().to_string_lossy().to_string() + "/"; let current_root = format!(
"{}{}",
current_root.as_path().to_string_lossy(),
std::path::MAIN_SEPARATOR
);
let segments = { let segments = {
let stripped = match current_path.strip_prefix(current_root.as_str()) { let stripped = match current_path.strip_prefix(current_root.as_str()) {
Some(stripped) => Ok(stripped), Ok(stripped) => Ok(stripped),
None => { Err(_) => {
let parent = path let parent = path.parent().ok_or_else(|| {
.parent() anyhow::anyhow!("Failed get parent of '{}'", current_path.to_string_lossy())
.ok_or_else(|| anyhow::anyhow!("Failed get parent of '{current_path}'"))?; })?;
self.change_root(parent.into())?; self.change_root(parent.into())?;
current_path current_path
.strip_prefix((parent.to_string_lossy().to_string() + "/").as_str()) .strip_prefix(
.ok_or_else(|| { format!("{}{}", parent.to_string_lossy(), std::path::MAIN_SEPARATOR)
.as_str(),
)
.map_err(|_| {
anyhow::anyhow!( anyhow::anyhow!(
"Failed to strip prefix (parent) '{}' from '{}'", "Failed to strip prefix (parent) '{}' from '{}'",
parent.to_string_lossy(), parent.to_string_lossy(),
current_path current_path.to_string_lossy()
) )
}) })
} }
}?; }?;
stripped stripped
.split(std::path::MAIN_SEPARATOR) .components()
.map(|s| s.to_string()) .map(|c| c.as_os_str().to_string_lossy().to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
self.tree.reveal_item(segments, &self.state.filter)?; self.tree.reveal_item(segments, &self.state.filter)?;
@ -922,7 +933,7 @@ mod test_explorer {
} }
".gitignore" => file!("") ".gitignore" => file!("")
}); });
let path: PathBuf = format!("test-explorer/{}", name).into(); let path: PathBuf = format!("test-explorer{}{}", std::path::MAIN_SEPARATOR, name).into();
if path.exists() { if path.exists() {
fs::remove_dir_all(path.clone()).unwrap(); fs::remove_dir_all(path.clone()).unwrap();
} }
@ -1022,7 +1033,7 @@ mod test_explorer {
// 1. Rename the current file to a name that is lexicographically greater than "index.html" // 1. Rename the current file to a name that is lexicographically greater than "index.html"
explorer explorer
.rename_current(&path.join("who.is").to_string_lossy().into()) .rename_current(&path.join("who.is").display().to_string())
.unwrap(); .unwrap();
// 1a. Expect the file is renamed, and is focused // 1a. Expect the file is renamed, and is focused
@ -1042,7 +1053,7 @@ mod test_explorer {
// 2. Rename the current file into an existing folder // 2. Rename the current file into an existing folder
explorer explorer
.rename_current(&path.join("styles/lol").to_string_lossy().into()) .rename_current(&path.join("styles/lol").display().to_string())
.unwrap(); .unwrap();
// 2a. Expect the file is moved to the folder, and is focused // 2a. Expect the file is moved to the folder, and is focused
@ -1064,7 +1075,7 @@ mod test_explorer {
// 3. Rename the current file into a non-existent folder // 3. Rename the current file into a non-existent folder
explorer explorer
.rename_current(&path.join("new_folder/sponge/bob").to_string_lossy().into()) .rename_current(&path.join("new_folder/sponge/bob").display().to_string())
.unwrap(); .unwrap();
// 3a. Expect the non-existent folder to be created, // 3a. Expect the non-existent folder to be created,
@ -1105,7 +1116,7 @@ mod test_explorer {
// 5. Move cursor to "bob", and move it outside of the current root // 5. Move cursor to "bob", and move it outside of the current root
explorer.tree.move_down(1); explorer.tree.move_down(1);
explorer explorer
.rename_current(&path.join("scripts/bob").to_string_lossy().into()) .rename_current(&path.join("scripts/bob").display().to_string())
.unwrap(); .unwrap();
// 5a. Expect the current root to be "scripts" // 5a. Expect the current root to be "scripts"

@ -339,8 +339,9 @@ impl<T: TreeViewItem> TreeView<T> {
} }
} }
pub fn build_tree(root: T, items: Vec<T>) -> Self { pub fn build_tree(root: T) -> Result<Self> {
Self::new(root, vec_to_tree(items)) let children = root.get_children()?;
Ok(Self::new(root, vec_to_tree(children)))
} }
pub fn with_enter_fn<F>(mut self, f: F) -> Self pub fn with_enter_fn<F>(mut self, f: F) -> Self

Loading…
Cancel
Save