# Description This PR renames the conversion functions on `Value` to be more consistent. It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions. The conversion functions on `Value` now come in a few forms: - `coerce_{type}` takes a `&Value` and attempts to convert the value to `type` (e.g., `i64` are converted to `f64`). This is the old behavior of some of the `as_{type}` functions -- these functions have simply been renamed to better reflect what they do. - The new `as_{type}` functions take a `&Value` and returns an `Ok` result only if the value is of `type` (no conversion is attempted). The returned value will be borrowed if `type` is non-`Copy`, otherwise an owned value is returned. - `into_{type}` exists for non-`Copy` types, but otherwise does not attempt conversion just like `as_type`. It takes an owned `Value` and always returns an owned result. - `coerce_into_{type}` has the same relationship with `coerce_{type}` as `into_{type}` does with `as_{type}`. - `to_{kind}_string`: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only. - `to_{type}`: other conversion functions. Currently, only `to_path` exists. (And `to_string` through `Display`.) This table summaries the above: | Form | Cost | Input Ownership | Output Ownership | Converts `Value` case/`type` | | ---------------------------- | ----- | --------------- | ---------------- | -------- | | `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No | | `into_{type}` | Cheap | Owned | Owned | No | | `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes | | `coerce_into_{type}` | Cheap | Owned | Owned | Yes | | `to_{kind}_string` | Expensive | Borrowed | Owned | Yes | | `to_{type}` | Expensive | Borrowed | Owned | Yes | # User-Facing Changes Breaking API change for `Value` in `nu-protocol` which is exposed as part of the plugin API.
172 lines
6.2 KiB
Rust
172 lines
6.2 KiB
Rust
use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy};
|
|
use nu_engine::eval_call;
|
|
use nu_protocol::{
|
|
ast::{Argument, Call, Expr, Expression},
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
|
PipelineData, Span, Type, Value,
|
|
};
|
|
use nu_utils::IgnoreCaseExt;
|
|
use reedline::Suggestion;
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
|
|
use super::completer::map_value_completions;
|
|
|
|
pub struct CustomCompletion {
|
|
engine_state: Arc<EngineState>,
|
|
stack: Stack,
|
|
decl_id: usize,
|
|
line: String,
|
|
sort_by: SortBy,
|
|
}
|
|
|
|
impl CustomCompletion {
|
|
pub fn new(engine_state: Arc<EngineState>, stack: Stack, decl_id: usize, line: String) -> Self {
|
|
Self {
|
|
engine_state,
|
|
stack,
|
|
decl_id,
|
|
line,
|
|
sort_by: SortBy::None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Completer for CustomCompletion {
|
|
fn fetch(
|
|
&mut self,
|
|
_: &StateWorkingSet,
|
|
prefix: Vec<u8>,
|
|
span: Span,
|
|
offset: usize,
|
|
pos: usize,
|
|
completion_options: &CompletionOptions,
|
|
) -> Vec<Suggestion> {
|
|
// Line position
|
|
let line_pos = pos - offset;
|
|
|
|
// Call custom declaration
|
|
let result = eval_call(
|
|
&self.engine_state,
|
|
&mut self.stack,
|
|
&Call {
|
|
decl_id: self.decl_id,
|
|
head: span,
|
|
arguments: vec![
|
|
Argument::Positional(Expression {
|
|
span: Span::unknown(),
|
|
ty: Type::String,
|
|
expr: Expr::String(self.line.clone()),
|
|
custom_completion: None,
|
|
}),
|
|
Argument::Positional(Expression {
|
|
span: Span::unknown(),
|
|
ty: Type::Int,
|
|
expr: Expr::Int(line_pos as i64),
|
|
custom_completion: None,
|
|
}),
|
|
],
|
|
redirect_stdout: true,
|
|
redirect_stderr: true,
|
|
parser_info: HashMap::new(),
|
|
},
|
|
PipelineData::empty(),
|
|
);
|
|
|
|
let mut custom_completion_options = None;
|
|
|
|
// Parse result
|
|
let suggestions = result
|
|
.map(|pd| {
|
|
let value = pd.into_value(span);
|
|
match &value {
|
|
Value::Record { val, .. } => {
|
|
let completions = val
|
|
.get("completions")
|
|
.and_then(|val| {
|
|
val.as_list()
|
|
.ok()
|
|
.map(|it| map_value_completions(it.iter(), span, offset))
|
|
})
|
|
.unwrap_or_default();
|
|
let options = val.get("options");
|
|
|
|
if let Some(Value::Record { val: options, .. }) = &options {
|
|
let should_sort = options
|
|
.get("sort")
|
|
.and_then(|val| val.as_bool().ok())
|
|
.unwrap_or(false);
|
|
|
|
if should_sort {
|
|
self.sort_by = SortBy::Ascending;
|
|
}
|
|
|
|
custom_completion_options = Some(CompletionOptions {
|
|
case_sensitive: options
|
|
.get("case_sensitive")
|
|
.and_then(|val| val.as_bool().ok())
|
|
.unwrap_or(true),
|
|
positional: options
|
|
.get("positional")
|
|
.and_then(|val| val.as_bool().ok())
|
|
.unwrap_or(true),
|
|
sort_by: if should_sort {
|
|
SortBy::Ascending
|
|
} else {
|
|
SortBy::None
|
|
},
|
|
match_algorithm: match options.get("completion_algorithm") {
|
|
Some(option) => option
|
|
.coerce_string()
|
|
.ok()
|
|
.and_then(|option| option.try_into().ok())
|
|
.unwrap_or(MatchAlgorithm::Prefix),
|
|
None => completion_options.match_algorithm,
|
|
},
|
|
});
|
|
}
|
|
|
|
completions
|
|
}
|
|
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
|
|
_ => vec![],
|
|
}
|
|
})
|
|
.unwrap_or_default();
|
|
|
|
if let Some(custom_completion_options) = custom_completion_options {
|
|
filter(&prefix, suggestions, &custom_completion_options)
|
|
} else {
|
|
filter(&prefix, suggestions, completion_options)
|
|
}
|
|
}
|
|
|
|
fn get_sort_by(&self) -> SortBy {
|
|
self.sort_by
|
|
}
|
|
}
|
|
|
|
fn filter(prefix: &[u8], items: Vec<Suggestion>, options: &CompletionOptions) -> Vec<Suggestion> {
|
|
items
|
|
.into_iter()
|
|
.filter(|it| match options.match_algorithm {
|
|
MatchAlgorithm::Prefix => match (options.case_sensitive, options.positional) {
|
|
(true, true) => it.value.as_bytes().starts_with(prefix),
|
|
(true, false) => it.value.contains(std::str::from_utf8(prefix).unwrap_or("")),
|
|
(false, positional) => {
|
|
let value = it.value.to_folded_case();
|
|
let prefix = std::str::from_utf8(prefix).unwrap_or("").to_folded_case();
|
|
if positional {
|
|
value.starts_with(&prefix)
|
|
} else {
|
|
value.contains(&prefix)
|
|
}
|
|
}
|
|
},
|
|
MatchAlgorithm::Fuzzy => options
|
|
.match_algorithm
|
|
.matches_u8(it.value.as_bytes(), prefix),
|
|
})
|
|
.collect()
|
|
}
|