Add completions.sort option (#13311)

This commit is contained in:
Yash Thakur
2024-08-05 20:30:10 -04:00
committed by GitHub
parent 2f44801414
commit 6d36941e55
15 changed files with 139 additions and 85 deletions

View File

@ -35,6 +35,35 @@ impl ReconstructVal for CompletionAlgorithm {
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq)]
pub enum CompletionSort {
#[default]
Smart,
Alphabetical,
}
impl FromStr for CompletionSort {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"smart" => Ok(Self::Smart),
"alphabetical" => Ok(Self::Alphabetical),
_ => Err("expected either 'smart' or 'alphabetical'"),
}
}
}
impl ReconstructVal for CompletionSort {
fn reconstruct_value(&self, span: Span) -> Value {
let str = match self {
Self::Smart => "smart",
Self::Alphabetical => "alphabetical",
};
Value::string(str, span)
}
}
pub(super) fn reconstruct_external_completer(config: &Config, span: Span) -> Value {
if let Some(closure) = config.external_completer.as_ref() {
Value::closure(closure.clone(), span)

View File

@ -11,7 +11,7 @@ use crate::{record, ShellError, Span, Value};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub use self::completer::CompletionAlgorithm;
pub use self::completer::{CompletionAlgorithm, CompletionSort};
pub use self::helper::extract_value;
pub use self::hooks::Hooks;
pub use self::output::ErrorStyle;
@ -69,6 +69,7 @@ pub struct Config {
pub quick_completions: bool,
pub partial_completions: bool,
pub completion_algorithm: CompletionAlgorithm,
pub completion_sort: CompletionSort,
pub edit_mode: EditBindings,
pub history: HistoryConfig,
pub keybindings: Vec<ParsedKeybinding>,
@ -141,6 +142,7 @@ impl Default for Config {
quick_completions: true,
partial_completions: true,
completion_algorithm: CompletionAlgorithm::default(),
completion_sort: CompletionSort::default(),
enable_external_completion: true,
max_external_completion_results: 100,
recursion_limit: 50,
@ -341,6 +343,13 @@ impl Value {
"case_sensitive" => {
process_bool_config(value, &mut errors, &mut config.case_sensitive_completions);
}
"sort" => {
process_string_enum(
&mut config.completion_sort,
&[key, key2],
value,
&mut errors);
}
"external" => {
if let Value::Record { val, .. } = value {
val.to_mut().retain_mut(|key3, value|
@ -401,6 +410,7 @@ impl Value {
"partial" => Value::bool(config.partial_completions, span),
"algorithm" => config.completion_algorithm.reconstruct_value(span),
"case_sensitive" => Value::bool(config.case_sensitive_completions, span),
"sort" => config.completion_sort.reconstruct_value(span),
"external" => reconstruct_external(&config, span),
"use_ls_colors" => Value::bool(config.use_ls_colors_completions, span),
},