mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +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 {
|
||||
Self { engine_state }
|
||||
}
|
||||
}
|
||||
|
||||
impl Completer for NuCompleter {
|
||||
fn complete(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
|
||||
fn completion_helper(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
|
||||
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||
let offset = working_set.next_span_start();
|
||||
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(
|
||||
span: nu_protocol::Span,
|
||||
partial: &str,
|
||||
|
|
|
@ -623,7 +623,7 @@ pub fn eval_variable(
|
|||
Value::string(&signature.name, span),
|
||||
Value::string(req.name, 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::nothing(span),
|
||||
Value::string(req.desc, span),
|
||||
|
@ -642,7 +642,7 @@ pub fn eval_variable(
|
|||
Value::string(&signature.name, span),
|
||||
Value::string(opt.name, 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::nothing(span),
|
||||
Value::string(opt.desc, span),
|
||||
|
@ -657,28 +657,15 @@ pub fn eval_variable(
|
|||
|
||||
{
|
||||
// rest_positional
|
||||
let (name, shape, desc) = if let Some(rest) = signature.rest_positional {
|
||||
(
|
||||
Value::string(rest.name, span),
|
||||
Value::string(rest.shape.to_type().to_string(), span),
|
||||
Value::string(rest.desc, span),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
Value::nothing(span),
|
||||
Value::nothing(span),
|
||||
Value::nothing(span),
|
||||
)
|
||||
};
|
||||
|
||||
if let Some(rest) = signature.rest_positional {
|
||||
let sig_vals = vec![
|
||||
Value::string(&signature.name, span),
|
||||
name,
|
||||
Value::string(rest.name, span),
|
||||
Value::string("rest", span),
|
||||
shape,
|
||||
Value::boolean(false, span),
|
||||
Value::string(rest.shape.to_string(), span),
|
||||
Value::boolean(true, span),
|
||||
Value::nothing(span),
|
||||
desc,
|
||||
Value::string(rest.desc, span),
|
||||
];
|
||||
|
||||
sig_records.push(Value::Record {
|
||||
|
@ -687,12 +674,22 @@ pub fn eval_variable(
|
|||
span,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// named flags
|
||||
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 {
|
||||
Value::string(arg.to_type().to_string(), span)
|
||||
flag_type = Value::string("named", span);
|
||||
Value::string(arg.to_string(), span)
|
||||
} else {
|
||||
flag_type = Value::string("switch", span);
|
||||
Value::nothing(span)
|
||||
};
|
||||
|
||||
|
@ -705,7 +702,7 @@ pub fn eval_variable(
|
|||
let sig_vals = vec![
|
||||
Value::string(&signature.name, span),
|
||||
Value::string(named.long, span),
|
||||
Value::string("named", span),
|
||||
flag_type,
|
||||
shape,
|
||||
Value::boolean(!named.required, span),
|
||||
short_flag,
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
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