Signature improves, sorted completions (#545)

This commit is contained in:
JT 2021-12-22 07:50:18 +11:00 committed by GitHub
parent 3ad5d4af66
commit 266fac910a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 33 deletions

View file

@ -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,

View file

@ -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,42 +657,39 @@ pub fn eval_variable(
{
// 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.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),
)
} else {
(
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
)
};
];
let sig_vals = vec![
Value::string(&signature.name, span),
name,
Value::string("rest", span),
shape,
Value::boolean(false, span),
Value::nothing(span),
desc,
];
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
span,
});
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
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,

View file

@ -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),
}
}
}