diff --git a/crates/nu-cli/src/completions/directory_completions.rs b/crates/nu-cli/src/completions/directory_completions.rs index 36e043b53c..2a539a0d3d 100644 --- a/crates/nu-cli/src/completions/directory_completions.rs +++ b/crates/nu-cli/src/completions/directory_completions.rs @@ -8,7 +8,7 @@ use std::fs; use std::path::Path; use std::sync::Arc; -use super::{partial_from, prepend_base_dir}; +use super::{partial_from, prepend_base_dir, SortBy}; const SEP: char = std::path::MAIN_SEPARATOR; @@ -60,12 +60,20 @@ impl Completer for DirectoryCompletion { // Sort items let mut sorted_items = items; - sorted_items.sort_by(|a, b| a.value.cmp(&b.value)); - sorted_items.sort_by(|a, b| { - let a_distance = levenshtein_distance(&prefix_str, &a.value); - let b_distance = levenshtein_distance(&prefix_str, &b.value); - a_distance.cmp(&b_distance) - }); + + match self.get_sort_by() { + SortBy::Ascending => { + sorted_items.sort_by(|a, b| a.value.cmp(&b.value)); + } + SortBy::LevenshteinDistance => { + sorted_items.sort_by(|a, b| { + let a_distance = levenshtein_distance(&prefix_str, &a.value); + let b_distance = levenshtein_distance(&prefix_str, &b.value); + a_distance.cmp(&b_distance) + }); + } + _ => (), + } // Separate the results between hidden and non hidden let mut hidden: Vec = vec![]; diff --git a/crates/nu-cli/src/completions/file_completions.rs b/crates/nu-cli/src/completions/file_completions.rs index 94069ae772..d3d8858bc9 100644 --- a/crates/nu-cli/src/completions/file_completions.rs +++ b/crates/nu-cli/src/completions/file_completions.rs @@ -7,6 +7,8 @@ use reedline::Suggestion; use std::path::{is_separator, Path}; use std::sync::Arc; +use super::SortBy; + const SEP: char = std::path::MAIN_SEPARATOR; #[derive(Clone)] @@ -55,12 +57,20 @@ impl Completer for FileCompletion { // Sort items let mut sorted_items = items; - sorted_items.sort_by(|a, b| a.value.cmp(&b.value)); - sorted_items.sort_by(|a, b| { - let a_distance = levenshtein_distance(&prefix_str, &a.value); - let b_distance = levenshtein_distance(&prefix_str, &b.value); - a_distance.cmp(&b_distance) - }); + + match self.get_sort_by() { + SortBy::Ascending => { + sorted_items.sort_by(|a, b| a.value.cmp(&b.value)); + } + SortBy::LevenshteinDistance => { + sorted_items.sort_by(|a, b| { + let a_distance = levenshtein_distance(&prefix_str, &a.value); + let b_distance = levenshtein_distance(&prefix_str, &b.value); + a_distance.cmp(&b_distance) + }); + } + _ => (), + } // Separate the results between hidden and non hidden let mut hidden: Vec = vec![]; diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index 7ba6350344..bfe7fb5d1f 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -178,11 +178,11 @@ fn file_completions() { // Create the expected values let expected_paths: Vec = vec![ + folder(dir.join("another")), + file(dir.join("custom_completion.nu")), file(dir.join("nushell")), folder(dir.join("test_a")), folder(dir.join("test_b")), - folder(dir.join("another")), - file(dir.join("custom_completion.nu")), file(dir.join(".hidden_file")), folder(dir.join(".hidden_folder")), ]; @@ -212,21 +212,21 @@ fn command_ls_with_filecompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -244,21 +244,21 @@ fn command_open_with_filecompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -277,21 +277,21 @@ fn command_rm_with_globcompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -310,21 +310,21 @@ fn command_cp_with_globcompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -343,21 +343,21 @@ fn command_save_with_filecompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -376,21 +376,21 @@ fn command_touch_with_filecompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -409,21 +409,21 @@ fn command_watch_with_filecompletion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -499,9 +499,9 @@ fn folder_with_directorycompletions() { // Create the expected values let expected_paths: Vec = vec![ + folder(dir.join("another")), folder(dir.join("test_a")), folder(dir.join("test_b")), - folder(dir.join("another")), folder(dir.join(".hidden_folder")), ]; @@ -695,21 +695,21 @@ fn unknown_command_completion() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ]; @@ -755,21 +755,21 @@ fn filecompletions_triggers_after_cursor() { #[cfg(windows)] let expected_paths: Vec = vec![ + "another\\".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a\\".to_string(), "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder\\".to_string(), ]; #[cfg(not(windows))] let expected_paths: Vec = vec![ + "another/".to_string(), + "custom_completion.nu".to_string(), "nushell".to_string(), "test_a/".to_string(), "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), ".hidden_file".to_string(), ".hidden_folder/".to_string(), ];