Removed From impl for clarity

pull/8339/head
ItsEthra 9 months ago
parent 9b055e03f0
commit 56d822f4b8

@ -62,7 +62,9 @@ use crate::{
filter_picker_entry, filter_picker_entry,
job::Callback, job::Callback,
keymap::ReverseKeymap, keymap::ReverseKeymap,
ui::{self, menu::Item, overlay::overlaid, Picker, Popup, Prompt, PromptEvent}, ui::{
self, menu::Item, overlay::overlaid, CompletionResult, Picker, Popup, Prompt, PromptEvent,
},
}; };
use crate::job::{self, Jobs}; use crate::job::{self, Jobs};
@ -2094,12 +2096,14 @@ fn searcher(cx: &mut Context, direction: Direction) {
"search:".into(), "search:".into(),
Some(reg), Some(reg),
move |_editor: &Editor, input: &str| { move |_editor: &Editor, input: &str| {
completions CompletionResult::new(
.iter() completions
.filter(|comp| comp.starts_with(input)) .iter()
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) .filter(|comp| comp.starts_with(input))
.collect::<Vec<_>>() .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.into() .collect::<Vec<_>>(),
false,
)
}, },
move |cx, regex, event| { move |cx, regex, event| {
if event == PromptEvent::Validate { if event == PromptEvent::Validate {
@ -2287,12 +2291,14 @@ fn global_search(cx: &mut Context) {
"global-search:".into(), "global-search:".into(),
Some(reg), Some(reg),
move |_editor: &Editor, input: &str| { move |_editor: &Editor, input: &str| {
completions CompletionResult::new(
.iter() completions
.filter(|comp| comp.starts_with(input)) .iter()
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) .filter(|comp| comp.starts_with(input))
.collect::<Vec<_>>() .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.into() .collect::<Vec<_>>(),
false,
)
}, },
move |cx, _, input, event| { move |cx, _, input, event| {
if event != PromptEvent::Validate { if event != PromptEvent::Validate {

@ -3134,18 +3134,17 @@ pub(super) fn command_mode(cx: &mut Context) {
let words = shellwords.words(); let words = shellwords.words();
if words.is_empty() || (words.len() == 1 && !shellwords.ends_with_whitespace()) { if words.is_empty() || (words.len() == 1 && !shellwords.ends_with_whitespace()) {
let mut result: CompletionResult = fuzzy_match( CompletionResult::new(
input, fuzzy_match(
TYPABLE_COMMAND_LIST.iter().map(|command| command.name), input,
false, TYPABLE_COMMAND_LIST.iter().map(|command| command.name),
false,
)
.into_iter()
.map(|(name, _)| (0.., name.into()))
.collect(),
matches!(editor.config().command_hints, CommandHints::Always),
) )
.into_iter()
.map(|(name, _)| (0.., name.into()))
.collect::<Vec<_>>()
.into();
result.show_popup = matches!(editor.config().command_hints, CommandHints::Always);
result
} else { } else {
// Otherwise, use the command's completer and the last shellword // Otherwise, use the command's completer and the last shellword
// as completion input. // as completion input.
@ -3161,19 +3160,21 @@ pub(super) fn command_mode(cx: &mut Context) {
.get(&words[0] as &str) .get(&words[0] as &str)
.map(|tc| tc.completer_for_argument_number(argument_number)) .map(|tc| tc.completer_for_argument_number(argument_number))
{ {
completer(editor, word) CompletionResult::new(
.completions completer(editor, word)
.into_iter() .completions
.map(|(range, file)| { .into_iter()
let file = shellwords::escape(file); .map(|(range, file)| {
let file = shellwords::escape(file);
// offset ranges to input
let offset = input.len() - word_len; // offset ranges to input
let range = (range.start + offset)..; let offset = input.len() - word_len;
(range, file) let range = (range.start + offset)..;
}) (range, file)
.collect::<Vec<_>>() })
.into() .collect::<Vec<_>>(),
false,
)
} else { } else {
CompletionResult::default() CompletionResult::default()
}; };

@ -278,11 +278,13 @@ pub mod completers {
.unwrap_or_else(|| Cow::from(SCRATCH_BUFFER_NAME)) .unwrap_or_else(|| Cow::from(SCRATCH_BUFFER_NAME))
}); });
fuzzy_match(input, names, true) CompletionResult::new(
.into_iter() fuzzy_match(input, names, true)
.map(|(name, _)| ((0..), name)) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| ((0..), name))
.into() .collect::<Vec<Completion>>(),
false,
)
} }
pub fn theme(_editor: &Editor, input: &str) -> CompletionResult { pub fn theme(_editor: &Editor, input: &str) -> CompletionResult {
@ -295,11 +297,13 @@ pub mod completers {
names.sort(); names.sort();
names.dedup(); names.dedup();
fuzzy_match(input, names, false) CompletionResult::new(
.into_iter() fuzzy_match(input, names, false)
.map(|(name, _)| ((0..), name.into())) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| ((0..), name.into()))
.into() .collect::<Vec<Completion>>(),
false,
)
} }
/// Recursive function to get all keys from this value and add them to vec /// Recursive function to get all keys from this value and add them to vec
@ -326,11 +330,13 @@ pub mod completers {
keys keys
}); });
fuzzy_match(input, &*KEYS, false) CompletionResult::new(
.into_iter() fuzzy_match(input, &*KEYS, false)
.map(|(name, _)| ((0..), name.into())) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| ((0..), name.into()))
.into() .collect::<Vec<Completion>>(),
false,
)
} }
pub fn filename(editor: &Editor, input: &str) -> CompletionResult { pub fn filename(editor: &Editor, input: &str) -> CompletionResult {
@ -363,11 +369,13 @@ pub mod completers {
.map(|config| &config.language_id) .map(|config| &config.language_id)
.chain(std::iter::once(&text)); .chain(std::iter::once(&text));
fuzzy_match(input, language_ids, false) CompletionResult::new(
.into_iter() fuzzy_match(input, language_ids, false)
.map(|(name, _)| ((0..), name.to_owned().into())) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| ((0..), name.to_owned().into()))
.into() .collect::<Vec<Completion>>(),
false,
)
} }
pub fn lsp_workspace_command(editor: &Editor, input: &str) -> CompletionResult { pub fn lsp_workspace_command(editor: &Editor, input: &str) -> CompletionResult {
@ -378,11 +386,13 @@ pub mod completers {
return CompletionResult::default(); return CompletionResult::default();
}; };
fuzzy_match(input, &options.commands, false) CompletionResult::new(
.into_iter() fuzzy_match(input, &options.commands, false)
.map(|(name, _)| ((0..), name.to_owned().into())) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| ((0..), name.to_owned().into()))
.into() .collect::<Vec<Completion>>(),
false,
)
} }
pub fn directory(editor: &Editor, input: &str) -> CompletionResult { pub fn directory(editor: &Editor, input: &str) -> CompletionResult {
@ -504,17 +514,19 @@ pub mod completers {
// if empty, return a list of dirs and files in current dir // if empty, return a list of dirs and files in current dir
if let Some(file_name) = file_name { if let Some(file_name) = file_name {
let range = (input.len().saturating_sub(file_name.len()))..; let range = (input.len().saturating_sub(file_name.len()))..;
fuzzy_match(&file_name, files, true) CompletionResult::new(
.into_iter() fuzzy_match(&file_name, files, true)
.map(|(name, _)| (range.clone(), name)) .into_iter()
.collect::<Vec<Completion>>() .map(|(name, _)| (range.clone(), name))
.into() .collect::<Vec<Completion>>(),
false,
)
// TODO: complete to longest common match // TODO: complete to longest common match
} else { } else {
let mut files: Vec<_> = files.map(|file| (end.clone(), file)).collect(); let mut files: Vec<_> = files.map(|file| (end.clone(), file)).collect();
files.sort_unstable_by(|(_, path1), (_, path2)| path1.cmp(path2)); files.sort_unstable_by(|(_, path1), (_, path2)| path1.cmp(path2));
files.into() CompletionResult::new(files, false)
} }
} }
@ -526,10 +538,10 @@ pub mod completers {
.filter(|(ch, _)| !matches!(ch, '%' | '#' | '.')) .filter(|(ch, _)| !matches!(ch, '%' | '#' | '.'))
.map(|(ch, _)| ch.to_string()); .map(|(ch, _)| ch.to_string());
fuzzy_match(input, iter, false) let results = fuzzy_match(input, iter, false)
.into_iter() .into_iter()
.map(|(name, _)| ((0..), name.into())) .map(|(name, _)| ((0..), name.into()))
.collect::<Vec<Completion>>() .collect::<Vec<Completion>>();
.into() CompletionResult::new(results, false)
} }
} }

@ -29,11 +29,11 @@ pub struct CompletionResult {
pub show_popup: bool, pub show_popup: bool,
} }
impl From<Vec<Completion>> for CompletionResult { impl CompletionResult {
fn from(completions: Vec<Completion>) -> Self { pub fn new(completions: Vec<Completion>, show_popup: bool) -> Self {
Self { Self {
show_popup: true,
completions, completions,
show_popup,
} }
} }
} }

Loading…
Cancel
Save