From c3b8be978e5de70a74d5210afbf74838ae498626 Mon Sep 17 00:00:00 2001 From: WJH Date: Sun, 26 Feb 2023 22:55:15 +0800 Subject: [PATCH] fix(ci): clippy + failure on Windows --- helix-term/src/ui/explorer.rs | 67 ++++++++++++++++++++--------------- helix-term/src/ui/tree.rs | 31 ++++++++-------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/helix-term/src/ui/explorer.rs b/helix-term/src/ui/explorer.rs index 3b08702a..6e654f6e 100644 --- a/helix-term/src/ui/explorer.rs +++ b/helix-term/src/ui/explorer.rs @@ -49,13 +49,18 @@ impl FileInfo { } fn get_text(&self) -> Cow<'static, str> { - match self.file_type { - FileType::Root => format!("{}", self.path.display()).into(), + let text = match self.file_type { + FileType::Root => format!("{}", self.path.display()), FileType::File | FileType::Folder => self .path .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> { let root = FileInfo::root(root); - let children = root.get_children()?; - Ok(TreeView::build_tree(root, children).with_enter_fn(Self::toggle_current)) + Ok(TreeView::build_tree(root)?.with_enter_fn(Self::toggle_current)) } fn push_history(&mut self, tree_view: TreeView) { @@ -220,31 +224,38 @@ impl Explorer { fn reveal_file(&mut self, path: PathBuf) -> Result<()> { let current_root = &self.state.current_root; - let current_path = path.as_path().to_string_lossy().to_string(); - let current_root = current_root.as_path().to_string_lossy().to_string() + "/"; + let current_path = &path; + let current_root = format!( + "{}{}", + current_root.as_path().to_string_lossy(), + std::path::MAIN_SEPARATOR + ); let segments = { let stripped = match current_path.strip_prefix(current_root.as_str()) { - Some(stripped) => Ok(stripped), - None => { - let parent = path - .parent() - .ok_or_else(|| anyhow::anyhow!("Failed get parent of '{current_path}'"))?; + Ok(stripped) => Ok(stripped), + Err(_) => { + let parent = path.parent().ok_or_else(|| { + anyhow::anyhow!("Failed get parent of '{}'", current_path.to_string_lossy()) + })?; self.change_root(parent.into())?; current_path - .strip_prefix((parent.to_string_lossy().to_string() + "/").as_str()) - .ok_or_else(|| { + .strip_prefix( + format!("{}{}", parent.to_string_lossy(), std::path::MAIN_SEPARATOR) + .as_str(), + ) + .map_err(|_| { anyhow::anyhow!( "Failed to strip prefix (parent) '{}' from '{}'", parent.to_string_lossy(), - current_path + current_path.to_string_lossy() ) }) } }?; stripped - .split(std::path::MAIN_SEPARATOR) - .map(|s| s.to_string()) + .components() + .map(|c| c.as_os_str().to_string_lossy().to_string()) .collect::>() }; self.tree.reveal_item(segments, &self.state.filter)?; @@ -907,7 +918,7 @@ mod test_explorer { use pretty_assertions::assert_eq; use std::{fs, path::PathBuf}; - fn dummy_file_tree<'a>(name: &'a str) -> PathBuf { + fn dummy_file_tree(name: &str) -> PathBuf { use build_fs_tree::{dir, file, Build, MergeableFileSystemTree}; let tree = MergeableFileSystemTree::<&str, &str>::from(dir! { "index.html" => file!("") @@ -922,7 +933,7 @@ mod test_explorer { } ".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() { fs::remove_dir_all(path.clone()).unwrap(); } @@ -930,13 +941,11 @@ mod test_explorer { path } - fn render<'a>(explorer: &mut Explorer) -> String { - explorer - .tree - .render_to_string(Rect::new(0, 0, 50, 10), &"".to_string()) + fn render(explorer: &mut Explorer) -> String { + explorer.tree.render_to_string(Rect::new(0, 0, 50, 10), "") } - fn new_explorer<'a>(name: &'a str) -> (PathBuf, Explorer) { + fn new_explorer(name: &str) -> (PathBuf, Explorer) { let path = dummy_file_tree(name); (path.clone(), Explorer::from_path(path, 30).unwrap()) } @@ -1024,7 +1033,7 @@ mod test_explorer { // 1. Rename the current file to a name that is lexicographically greater than "index.html" explorer - .rename_current(&path.join("who.is").to_string_lossy().into()) + .rename_current(&path.join("who.is").display().to_string()) .unwrap(); // 1a. Expect the file is renamed, and is focused @@ -1044,7 +1053,7 @@ mod test_explorer { // 2. Rename the current file into an existing folder explorer - .rename_current(&path.join("styles/lol").to_string_lossy().into()) + .rename_current(&path.join("styles/lol").display().to_string()) .unwrap(); // 2a. Expect the file is moved to the folder, and is focused @@ -1066,7 +1075,7 @@ mod test_explorer { // 3. Rename the current file into a non-existent folder 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(); // 3a. Expect the non-existent folder to be created, @@ -1107,7 +1116,7 @@ mod test_explorer { // 5. Move cursor to "bob", and move it outside of the current root explorer.tree.move_down(1); explorer - .rename_current(&path.join("scripts/bob").to_string_lossy().into()) + .rename_current(&path.join("scripts/bob").display().to_string()) .unwrap(); // 5a. Expect the current root to be "scripts" diff --git a/helix-term/src/ui/tree.rs b/helix-term/src/ui/tree.rs index 716a819e..73542430 100644 --- a/helix-term/src/ui/tree.rs +++ b/helix-term/src/ui/tree.rs @@ -339,8 +339,9 @@ impl TreeView { } } - pub fn build_tree(root: T, items: Vec) -> Self { - Self::new(root, vec_to_tree(items)) + pub fn build_tree(root: T) -> Result { + let children = root.get_children()?; + Ok(Self::new(root, vec_to_tree(children))) } pub fn with_enter_fn(mut self, f: F) -> Self @@ -1222,7 +1223,7 @@ mod test_tree_view { name: &'a str, } - fn item<'a>(name: &'a str) -> Item<'a> { + fn item(name: &str) -> Item { Item { name } } @@ -1273,8 +1274,8 @@ mod test_tree_view { Rect::new(0, 0, 50, 5) } - fn render<'a>(view: &mut TreeView>) -> String { - view.render_to_string(dummy_area(), &"".to_string()) + fn render(view: &mut TreeView) -> String { + view.render_to_string(dummy_area(), "") } #[test] @@ -1624,8 +1625,8 @@ mod test_tree_view { fn test_move_left_right() { let mut view = dummy_tree_view(); - fn render<'a>(view: &mut TreeView>) -> String { - view.render_to_string(dummy_area().with_width(20), &"".to_string()) + fn render(view: &mut TreeView) -> String { + view.render_to_string(dummy_area().with_width(20), "") } assert_eq!( @@ -1979,13 +1980,13 @@ krabby_patty // 1. Move to the last child item on the tree view.move_to_last_line(); - view.move_to_children(&"".to_string()).unwrap(); + view.move_to_children("").unwrap(); view.move_to_last_line(); - view.move_to_children(&"".to_string()).unwrap(); + view.move_to_children("").unwrap(); view.move_to_last_line(); - view.move_to_children(&"".to_string()).unwrap(); + view.move_to_children("").unwrap(); view.move_to_last_line(); - view.move_to_children(&"".to_string()).unwrap(); + view.move_to_children("").unwrap(); // 1a. Expect the current selected item is the last child on the tree assert_eq!( @@ -2000,7 +2001,7 @@ krabby_patty ); // 2. Refreshes the tree with a filter that will remove the last child - view.refresh_with_filter(&"ar".to_string()).unwrap(); + view.refresh_with_filter("ar").unwrap(); // 3. Get the current item let item = view.current_item().unwrap(); @@ -2113,7 +2114,7 @@ krabby_patty } } - fn child<'a>(name: &'a str) -> Item<'a> { + fn child(name: &str) -> Item { Item { name, children: None, @@ -2143,8 +2144,8 @@ krabby_patty } } - fn render<'a>(view: &mut TreeView>) -> String { - view.render_to_string(dummy_area().with_height(3), &"".to_string()) + fn render(view: &mut TreeView>) -> String { + view.render_to_string(dummy_area().with_height(3), "") } let mut view = TreeView::new(