Make sort_completions helper
Used by command, custom, flag, and variable completions
This commit is contained in:
parent
1e166abf23
commit
a055e59309
|
@ -9,7 +9,7 @@ use nu_protocol::{
|
|||
};
|
||||
use reedline::Suggestion;
|
||||
|
||||
use super::SemanticSuggestion;
|
||||
use super::{completion_common::sort_completions, SemanticSuggestion};
|
||||
|
||||
pub struct CommandCompletion {
|
||||
flattened: Vec<(Span, FlatShape)>,
|
||||
|
@ -161,7 +161,7 @@ impl Completer for CommandCompletion {
|
|||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
_stack: &Stack,
|
||||
_prefix: Vec<u8>,
|
||||
prefix: Vec<u8>,
|
||||
span: Span,
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
|
@ -223,7 +223,12 @@ impl Completer for CommandCompletion {
|
|||
vec![]
|
||||
};
|
||||
|
||||
subcommands.into_iter().chain(commands).collect::<Vec<_>>()
|
||||
let all_suggestions = subcommands.into_iter().chain(commands).collect::<Vec<_>>();
|
||||
sort_completions(
|
||||
&String::from_utf8_lossy(&prefix),
|
||||
all_suggestions,
|
||||
self.get_sort_by(),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_sort_by(&self) -> SortBy {
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
use crate::completions::{matches, CompletionOptions};
|
||||
use crate::{
|
||||
completions::{matches, CompletionOptions},
|
||||
SemanticSuggestion,
|
||||
};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_engine::env_to_string;
|
||||
use nu_path::{expand_to_real_path, home_dir};
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
Span,
|
||||
levenshtein_distance, Span,
|
||||
};
|
||||
use nu_utils::get_ls_colors;
|
||||
use std::path::{
|
||||
is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR,
|
||||
};
|
||||
|
||||
use super::SortBy;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct PathBuiltFromString {
|
||||
parts: Vec<String>,
|
||||
|
@ -256,3 +261,28 @@ pub fn adjust_if_intermediate(
|
|||
readjusted,
|
||||
}
|
||||
}
|
||||
|
||||
/// # Arguments
|
||||
/// * `prefix` - What the user's typed, for sorting by Levenshtein distance
|
||||
pub fn sort_completions(
|
||||
prefix: &str,
|
||||
mut items: Vec<SemanticSuggestion>,
|
||||
sort_by: SortBy,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
// Sort items
|
||||
match sort_by {
|
||||
SortBy::LevenshteinDistance => {
|
||||
items.sort_by(|a, b| {
|
||||
let a_distance = levenshtein_distance(prefix, &a.suggestion.value);
|
||||
let b_distance = levenshtein_distance(prefix, &b.suggestion.value);
|
||||
a_distance.cmp(&b_distance)
|
||||
});
|
||||
}
|
||||
SortBy::Ascending => {
|
||||
items.sort_by(|a, b| a.suggestion.value.cmp(&b.suggestion.value));
|
||||
}
|
||||
SortBy::None => {}
|
||||
};
|
||||
|
||||
items
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ use nu_protocol::{
|
|||
use nu_utils::IgnoreCaseExt;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::completion_common::sort_completions;
|
||||
|
||||
pub struct CustomCompletion {
|
||||
stack: Stack,
|
||||
decl_id: usize,
|
||||
|
@ -122,11 +124,16 @@ impl Completer for CustomCompletion {
|
|||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
if let Some(custom_completion_options) = custom_completion_options {
|
||||
let suggestions = if let Some(custom_completion_options) = custom_completion_options {
|
||||
filter(&prefix, suggestions, &custom_completion_options)
|
||||
} else {
|
||||
filter(&prefix, suggestions, completion_options)
|
||||
}
|
||||
};
|
||||
sort_completions(
|
||||
&String::from_utf8_lossy(&prefix),
|
||||
suggestions,
|
||||
self.get_sort_by(),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_sort_by(&self) -> SortBy {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::completions::{Completer, CompletionOptions};
|
||||
use crate::completions::{completion_common::sort_completions, Completer, CompletionOptions};
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression},
|
||||
engine::{Stack, StateWorkingSet},
|
||||
|
@ -90,7 +90,11 @@ impl Completer for FlagCompletion {
|
|||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return sort_completions(
|
||||
&String::from_utf8_lossy(&prefix),
|
||||
output,
|
||||
self.get_sort_by(),
|
||||
);
|
||||
}
|
||||
|
||||
vec![]
|
||||
|
|
|
@ -9,6 +9,8 @@ use nu_protocol::{
|
|||
use reedline::Suggestion;
|
||||
use std::str;
|
||||
|
||||
use super::completion_common::sort_completions;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VariableCompletion {
|
||||
var_context: (Vec<u8>, Vec<Vec<u8>>), // tuple with $var and the sublevels (.b.c.d)
|
||||
|
@ -40,6 +42,7 @@ impl Completer for VariableCompletion {
|
|||
end: span.end - offset,
|
||||
};
|
||||
let sublevels_count = self.var_context.1.len();
|
||||
let prefix_str = String::from_utf8_lossy(&prefix);
|
||||
|
||||
// Completions for the given variable
|
||||
if !var_str.is_empty() {
|
||||
|
@ -69,7 +72,7 @@ impl Completer for VariableCompletion {
|
|||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return sort_completions(&prefix_str, output, self.get_sort_by());
|
||||
}
|
||||
} else {
|
||||
// No nesting provided, return all env vars
|
||||
|
@ -93,7 +96,7 @@ impl Completer for VariableCompletion {
|
|||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return sort_completions(&prefix_str, output, self.get_sort_by());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +120,7 @@ impl Completer for VariableCompletion {
|
|||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return sort_completions(&prefix_str, output, self.get_sort_by());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,7 +142,7 @@ impl Completer for VariableCompletion {
|
|||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return sort_completions(&prefix_str, output, self.get_sort_by());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user