From d056bf070f3eecbdb96dcee322b7cc6ac475308f Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 10 Jun 2021 13:13:08 +1200 Subject: [PATCH] Improve alias highlighting/completions (#3594) * Improve alias highlighting/completions * Add example --- crates/nu-command/src/commands.rs | 2 + crates/nu-command/src/commands/alias.rs | 40 +++++++++++++++++++ .../src/commands/default_context.rs | 1 + crates/nu-parser/src/parse.rs | 17 ++++---- 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 crates/nu-command/src/commands/alias.rs diff --git a/crates/nu-command/src/commands.rs b/crates/nu-command/src/commands.rs index f910d5ef8..183811762 100644 --- a/crates/nu-command/src/commands.rs +++ b/crates/nu-command/src/commands.rs @@ -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; diff --git a/crates/nu-command/src/commands/alias.rs b/crates/nu-command/src/commands/alias.rs new file mode 100644 index 000000000..859e0cfdb --- /dev/null +++ b/crates/nu-command/src/commands/alias.rs @@ -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 { + alias(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Alias ll to ls -l", + example: "alias ll = ls -l", + result: None, + }] + } +} + +pub fn alias(_: CommandArgs) -> Result { + Ok(OutputStream::empty()) +} diff --git a/crates/nu-command/src/commands/default_context.rs b/crates/nu-command/src/commands/default_context.rs index 3830e5456..4bf7489ee 100644 --- a/crates/nu-command/src/commands/default_context.rs +++ b/crates/nu-command/src/commands/default_context.rs @@ -16,6 +16,7 @@ pub fn create_default_context(interactive: bool) -> Result 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);