Improve alias highlighting/completions (#3594)

* Improve alias highlighting/completions

* Add example
This commit is contained in:
JT 2021-06-10 13:13:08 +12:00 committed by GitHub
parent 2591050fbe
commit d056bf070f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 10 deletions

View File

@ -4,6 +4,7 @@ pub(crate) mod macros;
mod from_delimited_data;
mod to_delimited_data;
pub(crate) mod alias;
pub(crate) mod all;
pub(crate) mod ansi;
pub(crate) mod any;
@ -145,6 +146,7 @@ pub(crate) mod wrap;
pub(crate) use autoview::Autoview;
pub(crate) use cd::Cd;
pub(crate) use alias::Alias;
pub(crate) use ansi::Ansi;
pub(crate) use ansi::AnsiGradient;
pub(crate) use ansi::AnsiStrip;

View File

@ -0,0 +1,40 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
pub struct Alias;
impl WholeStreamCommand for Alias {
fn name(&self) -> &str {
"alias"
}
fn signature(&self) -> Signature {
Signature::build("alias")
.required("name", SyntaxShape::String, "the name of the alias")
.required("equals", SyntaxShape::String, "the equals sign")
.rest(SyntaxShape::Any, "the expansion for the alias")
}
fn usage(&self) -> &str {
"Alias a command to an expansion."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
alias(args)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Alias ll to ls -l",
example: "alias ll = ls -l",
result: None,
}]
}
}
pub fn alias(_: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::empty())
}

View File

@ -16,6 +16,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(LoadEnv),
whole_stream_command(Def),
whole_stream_command(Source),
whole_stream_command(Alias),
// System/file operations
whole_stream_command(Exec),
whole_stream_command(Pwd),

View File

@ -1826,16 +1826,6 @@ fn parse_call(
let (_, expr, err) = parse_math_expression(0, &lite_cmd.parts[..], scope, false);
error = error.or(err);
return (Some(ClassifiedCommand::Expr(Box::new(expr))), error);
} else if lite_cmd.parts[0].item == "alias" {
let error = parse_alias(&lite_cmd, scope);
if error.is_none() {
return (None, None);
} else {
return (
Some(ClassifiedCommand::Expr(Box::new(garbage(lite_cmd.span())))),
error,
);
}
} else if lite_cmd.parts.len() > 1 {
// Check if it's a sub-command
if let Some(signature) = scope.get_signature(&format!(
@ -1899,6 +1889,13 @@ fn parse_call(
)),
);
}
} else if lite_cmd.parts[0].item == "alias" {
let error = parse_alias(&lite_cmd, scope);
if error.is_none() {
return (Some(ClassifiedCommand::Internal(internal_command)), None);
} else {
return (Some(ClassifiedCommand::Internal(internal_command)), error);
}
}
error = error.or(err);