mirror of
https://github.com/nushell/nushell
synced 2025-01-14 14:14:13 +00:00
Signature improves, sorted completions (#545)
This commit is contained in:
parent
3ad5d4af66
commit
266fac910a
3 changed files with 74 additions and 33 deletions
|
@ -18,10 +18,8 @@ impl NuCompleter {
|
||||||
pub fn new(engine_state: EngineState) -> Self {
|
pub fn new(engine_state: EngineState) -> Self {
|
||||||
Self { engine_state }
|
Self { engine_state }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Completer for NuCompleter {
|
fn completion_helper(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
|
||||||
fn complete(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
|
|
||||||
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||||
let offset = working_set.next_span_start();
|
let offset = working_set.next_span_start();
|
||||||
let pos = offset + pos;
|
let pos = offset + pos;
|
||||||
|
@ -201,6 +199,16 @@ impl Completer for NuCompleter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Completer for NuCompleter {
|
||||||
|
fn complete(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
|
||||||
|
let mut output = self.completion_helper(line, pos);
|
||||||
|
|
||||||
|
output.sort_by(|a, b| a.1.cmp(&b.1));
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn file_path_completion(
|
fn file_path_completion(
|
||||||
span: nu_protocol::Span,
|
span: nu_protocol::Span,
|
||||||
partial: &str,
|
partial: &str,
|
||||||
|
|
|
@ -623,7 +623,7 @@ pub fn eval_variable(
|
||||||
Value::string(&signature.name, span),
|
Value::string(&signature.name, span),
|
||||||
Value::string(req.name, span),
|
Value::string(req.name, span),
|
||||||
Value::string("positional", span),
|
Value::string("positional", span),
|
||||||
Value::string(req.shape.to_type().to_string(), span),
|
Value::string(req.shape.to_string(), span),
|
||||||
Value::boolean(false, span),
|
Value::boolean(false, span),
|
||||||
Value::nothing(span),
|
Value::nothing(span),
|
||||||
Value::string(req.desc, span),
|
Value::string(req.desc, span),
|
||||||
|
@ -642,7 +642,7 @@ pub fn eval_variable(
|
||||||
Value::string(&signature.name, span),
|
Value::string(&signature.name, span),
|
||||||
Value::string(opt.name, span),
|
Value::string(opt.name, span),
|
||||||
Value::string("positional", span),
|
Value::string("positional", span),
|
||||||
Value::string(opt.shape.to_type().to_string(), span),
|
Value::string(opt.shape.to_string(), span),
|
||||||
Value::boolean(true, span),
|
Value::boolean(true, span),
|
||||||
Value::nothing(span),
|
Value::nothing(span),
|
||||||
Value::string(opt.desc, span),
|
Value::string(opt.desc, span),
|
||||||
|
@ -657,42 +657,39 @@ pub fn eval_variable(
|
||||||
|
|
||||||
{
|
{
|
||||||
// rest_positional
|
// rest_positional
|
||||||
let (name, shape, desc) = if let Some(rest) = signature.rest_positional {
|
if let Some(rest) = signature.rest_positional {
|
||||||
(
|
let sig_vals = vec![
|
||||||
|
Value::string(&signature.name, span),
|
||||||
Value::string(rest.name, span),
|
Value::string(rest.name, span),
|
||||||
Value::string(rest.shape.to_type().to_string(), span),
|
Value::string("rest", span),
|
||||||
|
Value::string(rest.shape.to_string(), span),
|
||||||
|
Value::boolean(true, span),
|
||||||
|
Value::nothing(span),
|
||||||
Value::string(rest.desc, span),
|
Value::string(rest.desc, span),
|
||||||
)
|
];
|
||||||
} else {
|
|
||||||
(
|
|
||||||
Value::nothing(span),
|
|
||||||
Value::nothing(span),
|
|
||||||
Value::nothing(span),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let sig_vals = vec![
|
sig_records.push(Value::Record {
|
||||||
Value::string(&signature.name, span),
|
cols: sig_cols.clone(),
|
||||||
name,
|
vals: sig_vals,
|
||||||
Value::string("rest", span),
|
span,
|
||||||
shape,
|
});
|
||||||
Value::boolean(false, span),
|
}
|
||||||
Value::nothing(span),
|
|
||||||
desc,
|
|
||||||
];
|
|
||||||
|
|
||||||
sig_records.push(Value::Record {
|
|
||||||
cols: sig_cols.clone(),
|
|
||||||
vals: sig_vals,
|
|
||||||
span,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// named flags
|
// named flags
|
||||||
for named in signature.named {
|
for named in signature.named {
|
||||||
|
let flag_type;
|
||||||
|
|
||||||
|
// Skip the help flag
|
||||||
|
if named.long == "help" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let shape = if let Some(arg) = named.arg {
|
let shape = if let Some(arg) = named.arg {
|
||||||
Value::string(arg.to_type().to_string(), span)
|
flag_type = Value::string("named", span);
|
||||||
|
Value::string(arg.to_string(), span)
|
||||||
} else {
|
} else {
|
||||||
|
flag_type = Value::string("switch", span);
|
||||||
Value::nothing(span)
|
Value::nothing(span)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -705,7 +702,7 @@ pub fn eval_variable(
|
||||||
let sig_vals = vec![
|
let sig_vals = vec![
|
||||||
Value::string(&signature.name, span),
|
Value::string(&signature.name, span),
|
||||||
Value::string(named.long, span),
|
Value::string(named.long, span),
|
||||||
Value::string("named", span),
|
flag_type,
|
||||||
shape,
|
shape,
|
||||||
Value::boolean(!named.required, span),
|
Value::boolean(!named.required, span),
|
||||||
short_flag,
|
short_flag,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::Type;
|
use crate::Type;
|
||||||
|
@ -116,3 +118,37 @@ impl SyntaxShape {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for SyntaxShape {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
SyntaxShape::Keyword(kw, shape) => {
|
||||||
|
write!(f, "\"{}\" {}", String::from_utf8_lossy(kw), shape)
|
||||||
|
}
|
||||||
|
SyntaxShape::Any => write!(f, "any"),
|
||||||
|
SyntaxShape::String => write!(f, "string"),
|
||||||
|
SyntaxShape::CellPath => write!(f, "cellpath"),
|
||||||
|
SyntaxShape::FullCellPath => write!(f, "cellpath"),
|
||||||
|
SyntaxShape::Number => write!(f, "number"),
|
||||||
|
SyntaxShape::Range => write!(f, "range"),
|
||||||
|
SyntaxShape::Int => write!(f, "int"),
|
||||||
|
SyntaxShape::Filepath => write!(f, "path"),
|
||||||
|
SyntaxShape::GlobPattern => write!(f, "glob"),
|
||||||
|
SyntaxShape::ImportPattern => write!(f, "import"),
|
||||||
|
SyntaxShape::Block(_) => write!(f, "block"),
|
||||||
|
SyntaxShape::Table => write!(f, "table"),
|
||||||
|
SyntaxShape::List(x) => write!(f, "list<{}>", x),
|
||||||
|
SyntaxShape::Filesize => write!(f, "filesize"),
|
||||||
|
SyntaxShape::Duration => write!(f, "duration"),
|
||||||
|
SyntaxShape::Operator => write!(f, "operator"),
|
||||||
|
SyntaxShape::RowCondition => write!(f, "condition"),
|
||||||
|
SyntaxShape::MathExpression => write!(f, "variable"),
|
||||||
|
SyntaxShape::Variable => write!(f, "var"),
|
||||||
|
SyntaxShape::VarWithOptType => write!(f, "vardecl"),
|
||||||
|
SyntaxShape::Signature => write!(f, "signature"),
|
||||||
|
SyntaxShape::Expression => write!(f, "expression"),
|
||||||
|
SyntaxShape::Boolean => write!(f, "bool"),
|
||||||
|
SyntaxShape::Custom(x, _) => write!(f, "custom<{}>", x),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue