forked from extern/nushell
Re-implement aliases (#8123)
# Description This PR adds an alternative alias implementation. Old aliases still work but you need to use `old-alias` instead of `alias`. Instead of replacing spans in the original code and re-parsing, which proved to be extremely error-prone and a constant source of panics, the new implementation creates a new command that references the old command. Consider the new alias defined as `alias ll = ls -l`. The parser creates a new command called `ll` and remembers that it is actually a `ls` command called with the `-l` flag. Then, when the parser sees the `ll` command, it will translate it to `ls -l` and passes to it any parameters that were passed to the call to `ll`. It works quite similar to how known externals defined with `extern` are implemented. The new alias implementation should work the same way as the old aliases, including exporting from modules, referencing both known and unknown externals. It seems to preserve custom completions and pipeline metadata. It is quite robust in most cases but there are some rough edges (see later). Fixes https://github.com/nushell/nushell/issues/7648, https://github.com/nushell/nushell/issues/8026, https://github.com/nushell/nushell/issues/7512, https://github.com/nushell/nushell/issues/5780, https://github.com/nushell/nushell/issues/7754 No effect: https://github.com/nushell/nushell/issues/8122 (we might revisit the completions code after this PR) Should use custom command instead: https://github.com/nushell/nushell/issues/6048 # User-Facing Changes Since aliases are now basically commands, it has some new implications: 1. `alias spam = "spam"` (requires command call) * **workaround**: use `alias spam = echo "spam"` 2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than once) * **workaround**: use different name (commands also have this limitation) 4. `alias ls = (ls | sort-by type name -i)` * **workaround**: Use custom command. _The common issue with this is that it is currently not easy to pass flags through custom commands and command referencing itself will lead to stack overflow. Both of these issues are meant to be addressed._ 5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc. * Should we treat the aliases as commands or should they be separated from regular commands? 6. Needs better error message and syntax highlight for recursed alias (`alias f = f`) 7. Can't create alias with the same name as existing command (`alias ls = ls -a`) * Might be possible to add support for it (not 100% sure) 8. Standalone `alias` doesn't list aliases anymore 9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay use` won't work) * TODO: Needs a better error message when attempting to do so # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
c6e2607868
commit
a3f817d71b
@ -52,17 +52,10 @@ impl Command for Alias {
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Alias ll to ls -l",
|
||||
example: "alias ll = ls -l",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Make an alias that makes a list of all custom commands",
|
||||
example: "alias customs = ($nu.scope.commands | where is_custom | get command)",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
},
|
||||
]
|
||||
vec![Example {
|
||||
description: "Alias ll to ls -l",
|
||||
example: "alias ll = ls -l",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExportAlias;
|
||||
@ -11,7 +13,7 @@ impl Command for ExportAlias {
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Define an alias and export it from a module"
|
||||
"Alias a command (with optional flags) to a new name and export it from a module"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
@ -35,6 +37,10 @@ impl Command for ExportAlias {
|
||||
true
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["abbr", "aka", "fn", "func", "function"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
@ -47,13 +53,9 @@ impl Command for ExportAlias {
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "export an alias of ll to ls -l, from a module",
|
||||
example: "export alias ll = ls -l",
|
||||
result: None,
|
||||
description: "Alias ll to ls -l and export it from a module",
|
||||
example: "module spam { export alias ll = ls -l }",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
}]
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["aka", "abbr", "module"]
|
||||
}
|
||||
}
|
||||
|
@ -450,6 +450,8 @@ pub fn create_default_context() -> EngineState {
|
||||
StrIntDeprecated,
|
||||
StrFindReplaceDeprecated,
|
||||
MathEvalDeprecated,
|
||||
OldAlias,
|
||||
ExportOldAlias,
|
||||
};
|
||||
|
||||
working_set.render()
|
||||
|
59
crates/nu-command/src/deprecated/export_old_alias.rs
Normal file
59
crates/nu-command/src/deprecated/export_old_alias.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExportOldAlias;
|
||||
|
||||
impl Command for ExportOldAlias {
|
||||
fn name(&self) -> &str {
|
||||
"export old-alias"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Define an alias and export it from a module"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("export old-alias")
|
||||
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
||||
.required("name", SyntaxShape::String, "name of the alias")
|
||||
.required(
|
||||
"initial_value",
|
||||
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
|
||||
"equals sign followed by value",
|
||||
)
|
||||
.category(Category::Core)
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
r#"This command is a parser keyword. For details, check:
|
||||
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||
}
|
||||
|
||||
fn is_parser_keyword(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(PipelineData::empty())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "export an alias of ll to ls -l, from a module",
|
||||
example: "export old-alias ll = ls -l",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["aka", "abbr", "module"]
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
mod deprecated_commands;
|
||||
mod export_old_alias;
|
||||
mod hash_base64;
|
||||
mod lpad;
|
||||
mod math_eval;
|
||||
mod old_alias;
|
||||
mod rpad;
|
||||
mod source;
|
||||
mod str_datetime;
|
||||
@ -10,9 +12,11 @@ mod str_find_replace;
|
||||
mod str_int;
|
||||
|
||||
pub use deprecated_commands::*;
|
||||
pub use export_old_alias::ExportOldAlias;
|
||||
pub use hash_base64::HashBase64;
|
||||
pub use lpad::LPadDeprecated;
|
||||
pub use math_eval::SubCommand as MathEvalDeprecated;
|
||||
pub use old_alias::OldAlias;
|
||||
pub use rpad::RPadDeprecated;
|
||||
pub use source::Source;
|
||||
pub use str_datetime::StrDatetimeDeprecated;
|
||||
|
68
crates/nu-command/src/deprecated/old_alias.rs
Normal file
68
crates/nu-command/src/deprecated/old_alias.rs
Normal file
@ -0,0 +1,68 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OldAlias;
|
||||
|
||||
impl Command for OldAlias {
|
||||
fn name(&self) -> &str {
|
||||
"old-alias"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Alias a command (with optional flags) to a new name"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("old-alias")
|
||||
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
||||
.required("name", SyntaxShape::String, "name of the alias")
|
||||
.required(
|
||||
"initial_value",
|
||||
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
|
||||
"equals sign followed by value",
|
||||
)
|
||||
.category(Category::Core)
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
r#"This command is a parser keyword. For details, check:
|
||||
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||
}
|
||||
|
||||
fn is_parser_keyword(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["abbr", "aka", "fn", "func", "function"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(PipelineData::empty())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Alias ll to ls -l",
|
||||
example: "old-alias ll = ls -l",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Make an alias that makes a list of all custom commands",
|
||||
example: "old-alias customs = ($nu.scope.commands | where is_custom | get command)",
|
||||
result: Some(Value::nothing(Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
||||
#[test]
|
||||
fn alias_simple() {
|
||||
let actual = nu!(
|
||||
@ -14,6 +15,7 @@ fn alias_simple() {
|
||||
assert_eq!(actual.out, "hello");
|
||||
}
|
||||
|
||||
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
||||
#[test]
|
||||
fn alias_hiding_1() {
|
||||
let actual = nu!(
|
||||
@ -27,6 +29,7 @@ fn alias_hiding_1() {
|
||||
assert_eq!(actual.out, "1");
|
||||
}
|
||||
|
||||
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
||||
#[test]
|
||||
fn alias_hiding_2() {
|
||||
let actual = nu!(
|
||||
@ -43,7 +46,7 @@ fn alias_hiding_2() {
|
||||
|
||||
#[test]
|
||||
fn alias_fails_with_invalid_name() {
|
||||
let err_msg = "alias name can't be a number, a filesize, or contain a hash # or caret ^";
|
||||
let err_msg = "name can't be a number, a filesize, or contain a hash # or caret ^";
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
@ -76,14 +79,3 @@ fn alias_fails_with_invalid_name() {
|
||||
));
|
||||
assert!(actual.err.contains(err_msg));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_alone_lists_aliases() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
alias a = 3; alias
|
||||
"#
|
||||
));
|
||||
assert!(actual.out.contains("name") && actual.out.contains("expansion"));
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ fn help_shows_signature() {
|
||||
assert!(!actual.out.contains("Signatures"));
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_aliases() {
|
||||
let code = &[
|
||||
@ -40,6 +41,7 @@ fn help_aliases() {
|
||||
assert_eq!(actual.out, "1");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_alias_usage_1() {
|
||||
Playground::setup("help_alias_usage_1", |dirs, sandbox| {
|
||||
@ -61,6 +63,7 @@ fn help_alias_usage_1() {
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_alias_usage_2() {
|
||||
let code = &[
|
||||
@ -72,6 +75,7 @@ fn help_alias_usage_2() {
|
||||
assert_eq!(actual.out, "line2");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_alias_usage_3() {
|
||||
Playground::setup("help_alias_usage_3", |dirs, sandbox| {
|
||||
@ -94,6 +98,7 @@ fn help_alias_usage_3() {
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_alias_name() {
|
||||
Playground::setup("help_alias_name", |dirs, sandbox| {
|
||||
@ -115,6 +120,7 @@ fn help_alias_name() {
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_alias_name_f() {
|
||||
Playground::setup("help_alias_name_f", |dirs, sandbox| {
|
||||
@ -134,6 +140,7 @@ fn help_alias_name_f() {
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_export_alias_name_single_word() {
|
||||
Playground::setup("help_export_alias_name_single_word", |dirs, sandbox| {
|
||||
@ -155,6 +162,7 @@ fn help_export_alias_name_single_word() {
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_export_alias_name_multi_word() {
|
||||
Playground::setup("help_export_alias_name_multi_word", |dirs, sandbox| {
|
||||
@ -265,8 +273,8 @@ fn help_module_sorted_aliases() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_usage_extra_usage() {
|
||||
Playground::setup("help_usage_extra_usage", |dirs, sandbox| {
|
||||
fn help_usage_extra_usage_command() {
|
||||
Playground::setup("help_usage_extra_usage_command", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"spam.nu",
|
||||
r#"
|
||||
@ -278,11 +286,6 @@ fn help_usage_extra_usage() {
|
||||
#
|
||||
# def_line2
|
||||
export def foo [] {}
|
||||
|
||||
# alias_line1
|
||||
#
|
||||
# alias_line2
|
||||
export alias bar = 'bar'
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -303,6 +306,35 @@ fn help_usage_extra_usage() {
|
||||
pipeline("use spam.nu *; help commands | where name == foo | get 0.usage"));
|
||||
assert!(actual.out.contains("def_line1"));
|
||||
assert!(!actual.out.contains("def_line2"));
|
||||
})
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Need to decide how to do help messages of new aliases"]
|
||||
#[test]
|
||||
fn help_usage_extra_usage_alias() {
|
||||
Playground::setup("help_usage_extra_usage_alias", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"spam.nu",
|
||||
r#"
|
||||
# module_line1
|
||||
#
|
||||
# module_line2
|
||||
|
||||
# alias_line1
|
||||
#
|
||||
# alias_line2
|
||||
export alias bar = echo 'bar'
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline("use spam.nu *; help modules spam"));
|
||||
assert!(actual.out.contains("module_line1"));
|
||||
assert!(actual.out.contains("module_line2"));
|
||||
|
||||
let actual = nu!(cwd: dirs.test(),
|
||||
pipeline("use spam.nu *; help modules | where name == spam | get 0.usage"));
|
||||
assert!(actual.out.contains("module_line1"));
|
||||
assert!(!actual.out.contains("module_line2"));
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline("use spam.nu *; help aliases bar"));
|
||||
assert!(actual.out.contains("alias_line1"));
|
||||
@ -343,10 +375,11 @@ fn help_modules_main_2() {
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Can't have alias with the same name as command"]
|
||||
#[test]
|
||||
fn help_alias_before_command() {
|
||||
let code = &[
|
||||
"alias SPAM = print 'spam'",
|
||||
"alias SPAM = echo 'spam'",
|
||||
"def SPAM [] { 'spam' }",
|
||||
"help SPAM",
|
||||
];
|
||||
|
@ -10,6 +10,7 @@ fn which_ls() {
|
||||
assert_eq!(actual.out, "Nushell built-in command");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Can't have alias recursion"]
|
||||
#[test]
|
||||
fn which_alias_ls() {
|
||||
let actual = nu!(
|
||||
@ -30,6 +31,7 @@ fn which_def_ls() {
|
||||
assert_eq!(actual.out, "Nushell custom command");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Can't have alias with the same name as command"]
|
||||
#[test]
|
||||
fn correct_precedence_alias_def_custom() {
|
||||
let actual = nu!(
|
||||
@ -40,6 +42,7 @@ fn correct_precedence_alias_def_custom() {
|
||||
assert_eq!(actual.out, "Nushell alias: echo alias");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Can't have alias with the same name as command"]
|
||||
#[test]
|
||||
fn multiple_reports_for_alias_def_custom() {
|
||||
let actual = nu!(
|
||||
|
@ -6,7 +6,7 @@ use nu_protocol::{
|
||||
ImportPatternMember, PathMember, Pipeline, PipelineElement,
|
||||
},
|
||||
engine::{StateWorkingSet, DEFAULT_OVERLAY_NAME},
|
||||
span, BlockId, Exportable, Module, PositionalArg, Span, Spanned, SyntaxShape, Type,
|
||||
span, Alias, BlockId, Exportable, Module, PositionalArg, Span, Spanned, SyntaxShape, Type,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
@ -21,7 +21,7 @@ use crate::{
|
||||
lex,
|
||||
lite_parser::{lite_parse, LiteCommand, LiteElement},
|
||||
parser::{
|
||||
check_call, check_name, garbage, garbage_pipeline, parse, parse_import_pattern,
|
||||
check_call, check_name, garbage, garbage_pipeline, parse, parse_call, parse_import_pattern,
|
||||
parse_internal_call, parse_multispan_value, parse_signature, parse_string, parse_value,
|
||||
parse_var_with_opt_type, trim_quotes, ParsedInternalCall,
|
||||
},
|
||||
@ -103,6 +103,34 @@ pub fn parse_def_predecl(
|
||||
signature,
|
||||
};
|
||||
|
||||
if working_set.add_predecl(Box::new(decl)).is_some() {
|
||||
return Some(ParseError::DuplicateCommandDef(spans[1]));
|
||||
}
|
||||
}
|
||||
} else if name == b"alias" && spans.len() >= 4 {
|
||||
let (name_expr, ..) = parse_string(working_set, spans[1], expand_aliases_denylist);
|
||||
let name = name_expr.as_string();
|
||||
|
||||
if let Some(name) = name {
|
||||
if name.contains('#')
|
||||
|| name.contains('^')
|
||||
|| name.parse::<bytesize::ByteSize>().is_ok()
|
||||
|| name.parse::<f64>().is_ok()
|
||||
{
|
||||
return Some(ParseError::CommandDefNotValid(spans[1]));
|
||||
}
|
||||
|
||||
// The signature will get replaced by the replacement signature
|
||||
// let mut signature = Signature::new(name.clone());
|
||||
// signature.name = name;
|
||||
|
||||
// The fields get replaced during parsing
|
||||
let decl = Alias {
|
||||
name,
|
||||
command: None,
|
||||
wrapped_call: Expression::garbage(name_expr.span),
|
||||
};
|
||||
|
||||
if working_set.add_predecl(Box::new(decl)).is_some() {
|
||||
return Some(ParseError::DuplicateCommandDef(spans[1]));
|
||||
}
|
||||
@ -603,6 +631,201 @@ pub fn parse_alias(
|
||||
) -> (Pipeline, Option<ParseError>) {
|
||||
let spans = &lite_command.parts;
|
||||
|
||||
let (name_span, split_id) =
|
||||
if spans.len() > 1 && working_set.get_span_contents(spans[0]) == b"export" {
|
||||
(spans[1], 2)
|
||||
} else {
|
||||
(spans[0], 1)
|
||||
};
|
||||
|
||||
let name = working_set.get_span_contents(name_span);
|
||||
|
||||
if name != b"alias" {
|
||||
return (
|
||||
garbage_pipeline(spans),
|
||||
Some(ParseError::InternalError(
|
||||
"Alias statement unparsable".into(),
|
||||
span(spans),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some((span, err)) = check_name(working_set, spans) {
|
||||
return (Pipeline::from_vec(vec![garbage(*span)]), Some(err));
|
||||
}
|
||||
|
||||
if let Some(decl_id) = working_set.find_decl(b"alias", &Type::Any) {
|
||||
let (command_spans, rest_spans) = spans.split_at(split_id);
|
||||
|
||||
let ParsedInternalCall { call, output, .. } = parse_internal_call(
|
||||
working_set,
|
||||
span(command_spans),
|
||||
rest_spans,
|
||||
decl_id,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
|
||||
if call.has_flag("help") {
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: output,
|
||||
custom_completion: None,
|
||||
}]),
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
if spans.len() >= split_id + 3 {
|
||||
let alias_name = working_set.get_span_contents(spans[split_id]);
|
||||
|
||||
let alias_name = if alias_name.starts_with(b"\"")
|
||||
&& alias_name.ends_with(b"\"")
|
||||
&& alias_name.len() > 1
|
||||
{
|
||||
alias_name[1..(alias_name.len() - 1)].to_vec()
|
||||
} else {
|
||||
alias_name.to_vec()
|
||||
};
|
||||
|
||||
if let Some(mod_name) = module_name {
|
||||
if alias_name == mod_name {
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: output,
|
||||
custom_completion: None,
|
||||
}]),
|
||||
Some(ParseError::NamedAsModule(
|
||||
"alias".to_string(),
|
||||
String::from_utf8_lossy(&alias_name).to_string(),
|
||||
spans[split_id],
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
if &alias_name == b"main" {
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: output,
|
||||
custom_completion: None,
|
||||
}]),
|
||||
Some(ParseError::ExportMainAliasNotAllowed(spans[split_id])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let _equals = working_set.get_span_contents(spans[split_id + 1]);
|
||||
|
||||
let replacement_spans = &spans[(split_id + 2)..];
|
||||
|
||||
let (expr, err) = parse_call(
|
||||
working_set,
|
||||
replacement_spans,
|
||||
replacement_spans[0],
|
||||
expand_aliases_denylist,
|
||||
false, // TODO: Should this be set properly???
|
||||
);
|
||||
|
||||
if let Some(e) = err {
|
||||
if let ParseError::MissingPositional(..) = e {
|
||||
// ignore missing required positional
|
||||
} else {
|
||||
return (garbage_pipeline(replacement_spans), Some(e));
|
||||
}
|
||||
}
|
||||
|
||||
let (command, wrapped_call) = match expr {
|
||||
Expression {
|
||||
expr: Expr::Call(ref call),
|
||||
..
|
||||
} => (Some(working_set.get_decl(call.decl_id).clone_box()), expr),
|
||||
Expression {
|
||||
expr: Expr::ExternalCall(..),
|
||||
..
|
||||
} => (None, expr),
|
||||
_ => {
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: output,
|
||||
custom_completion: None,
|
||||
}]),
|
||||
Some(ParseError::InternalError(
|
||||
"Parsed call not a call".into(),
|
||||
expr.span,
|
||||
)),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(decl_id) = working_set.find_predecl(&alias_name) {
|
||||
let alias_decl = working_set.get_decl_mut(decl_id);
|
||||
|
||||
let alias = Alias {
|
||||
name: String::from_utf8_lossy(&alias_name).to_string(),
|
||||
command,
|
||||
wrapped_call,
|
||||
};
|
||||
|
||||
*alias_decl = Box::new(alias);
|
||||
} else {
|
||||
return (
|
||||
garbage_pipeline(spans),
|
||||
Some(ParseError::InternalError(
|
||||
"Predeclaration failed to add declaration".into(),
|
||||
spans[split_id],
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
// It's OK if it returns None: The decl was already merged in previous parse pass.
|
||||
working_set.merge_predecl(&alias_name);
|
||||
}
|
||||
|
||||
let err = if spans.len() < 4 {
|
||||
Some(ParseError::IncorrectValue(
|
||||
"Incomplete alias".into(),
|
||||
span(&spans[..split_id]),
|
||||
"incomplete alias".into(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression {
|
||||
expr: Expr::Call(call),
|
||||
span: span(spans),
|
||||
ty: Type::Any,
|
||||
custom_completion: None,
|
||||
}]),
|
||||
err,
|
||||
);
|
||||
}
|
||||
|
||||
(
|
||||
garbage_pipeline(spans),
|
||||
Some(ParseError::InternalError(
|
||||
"Alias statement unparsable".into(),
|
||||
span(spans),
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_old_alias(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
module_name: Option<&[u8]>,
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Pipeline, Option<ParseError>) {
|
||||
let spans = &lite_command.parts;
|
||||
|
||||
// if the call is "alias", turn it into "print $nu.scope.aliases"
|
||||
if spans.len() == 1 {
|
||||
let head = Expression {
|
||||
@ -658,7 +881,7 @@ pub fn parse_alias(
|
||||
|
||||
let name = working_set.get_span_contents(name_span);
|
||||
|
||||
if name == b"alias" {
|
||||
if name == b"old-alias" {
|
||||
if let Some((span, err)) = check_name(working_set, spans) {
|
||||
return (Pipeline::from_vec(vec![garbage(*span)]), Some(err));
|
||||
}
|
||||
@ -789,7 +1012,9 @@ pub fn parse_export_in_block(
|
||||
let full_name = if lite_command.parts.len() > 1 {
|
||||
let sub = working_set.get_span_contents(lite_command.parts[1]);
|
||||
match sub {
|
||||
b"alias" | b"def" | b"def-env" | b"extern" | b"use" => [b"export ", sub].concat(),
|
||||
b"old-alias" | b"alias" | b"def" | b"def-env" | b"extern" | b"use" => {
|
||||
[b"export ", sub].concat()
|
||||
}
|
||||
_ => b"export".to_vec(),
|
||||
}
|
||||
} else {
|
||||
@ -857,6 +1082,9 @@ pub fn parse_export_in_block(
|
||||
}
|
||||
|
||||
match full_name.as_slice() {
|
||||
b"export old-alias" => {
|
||||
parse_old_alias(working_set, lite_command, None, expand_aliases_denylist)
|
||||
}
|
||||
b"export alias" => parse_alias(working_set, lite_command, None, expand_aliases_denylist),
|
||||
b"export def" | b"export def-env" => {
|
||||
parse_def(working_set, lite_command, None, expand_aliases_denylist)
|
||||
@ -1154,6 +1382,79 @@ pub fn parse_export_in_module(
|
||||
|
||||
result
|
||||
}
|
||||
b"old-alias" => {
|
||||
let lite_command = LiteCommand {
|
||||
comments: lite_command.comments.clone(),
|
||||
parts: spans[1..].to_vec(),
|
||||
};
|
||||
let (pipeline, err) = parse_old_alias(
|
||||
working_set,
|
||||
&lite_command,
|
||||
Some(module_name),
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
error = error.or(err);
|
||||
|
||||
let export_alias_decl_id =
|
||||
if let Some(id) = working_set.find_decl(b"export old-alias", &Type::Any) {
|
||||
id
|
||||
} else {
|
||||
return (
|
||||
garbage_pipeline(spans),
|
||||
vec![],
|
||||
Some(ParseError::InternalError(
|
||||
"missing 'export old-alias' command".into(),
|
||||
export_span,
|
||||
)),
|
||||
);
|
||||
};
|
||||
|
||||
// Trying to warp the 'old-alias' call into the 'export old-alias' in a very clumsy way
|
||||
if let Some(PipelineElement::Expression(
|
||||
_,
|
||||
Expression {
|
||||
expr: Expr::Call(ref alias_call),
|
||||
..
|
||||
},
|
||||
)) = pipeline.elements.get(0)
|
||||
{
|
||||
call = alias_call.clone();
|
||||
|
||||
call.head = span(&spans[0..=1]);
|
||||
call.decl_id = export_alias_decl_id;
|
||||
} else {
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::InternalError(
|
||||
"unexpected output from parsing a definition".into(),
|
||||
span(&spans[1..]),
|
||||
))
|
||||
});
|
||||
};
|
||||
|
||||
let mut result = vec![];
|
||||
|
||||
let alias_name = match spans.get(2) {
|
||||
Some(span) => working_set.get_span_contents(*span),
|
||||
None => &[],
|
||||
};
|
||||
let alias_name = trim_quotes(alias_name);
|
||||
|
||||
if let Some(alias_id) = working_set.find_alias(alias_name) {
|
||||
result.push(Exportable::Alias {
|
||||
name: alias_name.to_vec(),
|
||||
id: alias_id,
|
||||
});
|
||||
} else {
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::InternalError(
|
||||
"failed to find added alias".into(),
|
||||
span(&spans[1..]),
|
||||
))
|
||||
});
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
b"alias" => {
|
||||
let lite_command = LiteCommand {
|
||||
comments: lite_command.comments.clone(),
|
||||
@ -1211,8 +1512,8 @@ pub fn parse_export_in_module(
|
||||
};
|
||||
let alias_name = trim_quotes(alias_name);
|
||||
|
||||
if let Some(alias_id) = working_set.find_alias(alias_name) {
|
||||
result.push(Exportable::Alias {
|
||||
if let Some(alias_id) = working_set.find_decl(alias_name, &Type::Any) {
|
||||
result.push(Exportable::Decl {
|
||||
name: alias_name.to_vec(),
|
||||
id: alias_id,
|
||||
});
|
||||
@ -1507,6 +1808,16 @@ pub fn parse_module_block(
|
||||
|
||||
(pipeline, err)
|
||||
}
|
||||
b"old-alias" => {
|
||||
let (pipeline, err) = parse_old_alias(
|
||||
working_set,
|
||||
command,
|
||||
None, // using aliases named as the module locally is OK
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
|
||||
(pipeline, err)
|
||||
}
|
||||
b"alias" => {
|
||||
let (pipeline, err) = parse_alias(
|
||||
working_set,
|
||||
|
@ -20,8 +20,8 @@ use nu_protocol::{
|
||||
|
||||
use crate::parse_keywords::{
|
||||
parse_alias, parse_def, parse_def_predecl, parse_export_in_block, parse_extern, parse_for,
|
||||
parse_hide, parse_let_or_const, parse_module, parse_overlay, parse_source, parse_use,
|
||||
parse_where, parse_where_expr,
|
||||
parse_hide, parse_let_or_const, parse_module, parse_old_alias, parse_overlay, parse_source,
|
||||
parse_use, parse_where, parse_where_expr,
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
@ -253,6 +253,50 @@ pub fn check_name<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_external_arg(
|
||||
working_set: &mut StateWorkingSet,
|
||||
span: Span,
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Expression, Option<ParseError>) {
|
||||
let contents = working_set.get_span_contents(span);
|
||||
|
||||
let mut error = None;
|
||||
|
||||
if contents.starts_with(b"$") || contents.starts_with(b"(") {
|
||||
let (arg, err) = parse_dollar_expr(working_set, span, expand_aliases_denylist);
|
||||
error = error.or(err);
|
||||
(arg, error)
|
||||
} else if contents.starts_with(b"[") {
|
||||
let (arg, err) = parse_list_expression(
|
||||
working_set,
|
||||
span,
|
||||
&SyntaxShape::Any,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
error = error.or(err);
|
||||
(arg, error)
|
||||
} else {
|
||||
// Eval stage trims the quotes, so we don't have to do the same thing when parsing.
|
||||
let contents = if contents.starts_with(b"\"") {
|
||||
let (contents, err) = unescape_string(contents, span);
|
||||
error = error.or(err);
|
||||
String::from_utf8_lossy(&contents).to_string()
|
||||
} else {
|
||||
String::from_utf8_lossy(contents).to_string()
|
||||
};
|
||||
|
||||
(
|
||||
Expression {
|
||||
expr: Expr::String(contents),
|
||||
span,
|
||||
ty: Type::String,
|
||||
custom_completion: None,
|
||||
},
|
||||
error,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_external_call(
|
||||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
@ -293,38 +337,9 @@ pub fn parse_external_call(
|
||||
};
|
||||
|
||||
for span in &spans[1..] {
|
||||
let contents = working_set.get_span_contents(*span);
|
||||
|
||||
if contents.starts_with(b"$") || contents.starts_with(b"(") {
|
||||
let (arg, err) = parse_dollar_expr(working_set, *span, expand_aliases_denylist);
|
||||
error = error.or(err);
|
||||
args.push(arg);
|
||||
} else if contents.starts_with(b"[") {
|
||||
let (arg, err) = parse_list_expression(
|
||||
working_set,
|
||||
*span,
|
||||
&SyntaxShape::Any,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
error = error.or(err);
|
||||
args.push(arg);
|
||||
} else {
|
||||
// Eval stage trims the quotes, so we don't have to do the same thing when parsing.
|
||||
let contents = if contents.starts_with(b"\"") {
|
||||
let (contents, err) = unescape_string(contents, *span);
|
||||
error = error.or(err);
|
||||
String::from_utf8_lossy(&contents).to_string()
|
||||
} else {
|
||||
String::from_utf8_lossy(contents).to_string()
|
||||
};
|
||||
|
||||
args.push(Expression {
|
||||
expr: Expr::String(contents),
|
||||
span: *span,
|
||||
ty: Type::String,
|
||||
custom_completion: None,
|
||||
});
|
||||
}
|
||||
let (arg, err) = parse_external_arg(working_set, *span, expand_aliases_denylist);
|
||||
error = error.or(err);
|
||||
args.push(arg);
|
||||
}
|
||||
(
|
||||
Expression {
|
||||
@ -784,12 +799,6 @@ pub fn parse_internal_call(
|
||||
let signature = decl.signature();
|
||||
let output = signature.output_type.clone();
|
||||
|
||||
working_set.type_scope.add_type(output.clone());
|
||||
|
||||
if signature.creates_scope {
|
||||
working_set.enter_scope();
|
||||
}
|
||||
|
||||
// The index into the positional parameter in the definition
|
||||
let mut positional_idx = 0;
|
||||
|
||||
@ -797,6 +806,35 @@ pub fn parse_internal_call(
|
||||
// Starting at the first argument
|
||||
let mut spans_idx = 0;
|
||||
|
||||
if let Some(alias) = decl.as_alias() {
|
||||
if let Expression {
|
||||
expr: Expr::Call(wrapped_call),
|
||||
..
|
||||
} = &alias.wrapped_call
|
||||
{
|
||||
// Replace this command's call with the aliased call, but keep the alias name
|
||||
call = *wrapped_call.clone();
|
||||
call.head = command_span;
|
||||
// Skip positionals passed to aliased call
|
||||
positional_idx = call.positional_len();
|
||||
} else {
|
||||
return ParsedInternalCall {
|
||||
call: Box::new(call),
|
||||
output: Type::Any,
|
||||
error: Some(ParseError::UnknownState(
|
||||
"Alias does not point to internal call.".to_string(),
|
||||
command_span,
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
working_set.type_scope.add_type(output.clone());
|
||||
|
||||
if signature.creates_scope {
|
||||
working_set.enter_scope();
|
||||
}
|
||||
|
||||
while spans_idx < spans.len() {
|
||||
let arg_span = spans[spans_idx];
|
||||
|
||||
@ -1163,16 +1201,61 @@ pub fn parse_call(
|
||||
}
|
||||
}
|
||||
|
||||
trace!("parsing: internal call");
|
||||
// TODO: Try to remove the clone
|
||||
let decl = working_set.get_decl(decl_id).clone();
|
||||
|
||||
// parse internal command
|
||||
let parsed_call = parse_internal_call(
|
||||
working_set,
|
||||
span(&spans[cmd_start..pos]),
|
||||
&spans[pos..],
|
||||
decl_id,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
let parsed_call = if let Some(alias) = decl.as_alias() {
|
||||
if let Expression {
|
||||
expr: Expr::ExternalCall(head, args, is_subexpression),
|
||||
span: _,
|
||||
ty,
|
||||
custom_completion,
|
||||
} = &alias.wrapped_call
|
||||
{
|
||||
trace!("parsing: alias of external call");
|
||||
|
||||
let mut error = None;
|
||||
let mut final_args = args.clone();
|
||||
|
||||
for arg_span in spans.iter().skip(1) {
|
||||
let (arg, err) =
|
||||
parse_external_arg(working_set, *arg_span, expand_aliases_denylist);
|
||||
error = error.or(err);
|
||||
final_args.push(arg);
|
||||
}
|
||||
|
||||
let mut head = head.clone();
|
||||
head.span = spans[0]; // replacing the spans preserves syntax highlighting
|
||||
|
||||
return (
|
||||
Expression {
|
||||
expr: Expr::ExternalCall(head, final_args, *is_subexpression),
|
||||
span: span(spans),
|
||||
ty: ty.clone(),
|
||||
custom_completion: *custom_completion,
|
||||
},
|
||||
error,
|
||||
);
|
||||
} else {
|
||||
trace!("parsing: alias of internal call");
|
||||
parse_internal_call(
|
||||
working_set,
|
||||
span(&spans[cmd_start..pos]),
|
||||
&spans[pos..],
|
||||
decl_id,
|
||||
expand_aliases_denylist,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
trace!("parsing: internal call");
|
||||
parse_internal_call(
|
||||
working_set,
|
||||
span(&spans[cmd_start..pos]),
|
||||
&spans[pos..],
|
||||
decl_id,
|
||||
expand_aliases_denylist,
|
||||
)
|
||||
};
|
||||
|
||||
(
|
||||
Expression {
|
||||
@ -5038,8 +5121,8 @@ pub fn parse_expression(
|
||||
|
||||
// For now, check for special parses of certain keywords
|
||||
match bytes.as_slice() {
|
||||
b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"alias" | b"export"
|
||||
| b"hide" => (
|
||||
b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"old-alias"
|
||||
| b"alias" | b"export" | b"hide" => (
|
||||
parse_call(
|
||||
working_set,
|
||||
&spans[pos..],
|
||||
@ -5232,6 +5315,7 @@ pub fn parse_builtin_commands(
|
||||
let (expr, err) = parse_for(working_set, &lite_command.parts, expand_aliases_denylist);
|
||||
(Pipeline::from_vec(vec![expr]), err)
|
||||
}
|
||||
b"old-alias" => parse_old_alias(working_set, lite_command, None, expand_aliases_denylist),
|
||||
b"alias" => parse_alias(working_set, lite_command, None, expand_aliases_denylist),
|
||||
b"module" => parse_module(working_set, lite_command, expand_aliases_denylist),
|
||||
b"use" => {
|
||||
|
141
crates/nu-protocol/src/alias.rs
Normal file
141
crates/nu-protocol/src/alias.rs
Normal file
@ -0,0 +1,141 @@
|
||||
use crate::engine::{EngineState, Stack};
|
||||
use crate::PipelineData;
|
||||
use crate::{
|
||||
ast::{Call, Expression},
|
||||
engine::Command,
|
||||
BlockId, Example, ShellError, Signature,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Alias {
|
||||
pub name: String,
|
||||
pub command: Option<Box<dyn Command>>, // None if external call
|
||||
pub wrapped_call: Expression,
|
||||
}
|
||||
|
||||
impl Command for Alias {
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.signature()
|
||||
} else {
|
||||
Signature::new(&self.name).allows_unknown_args()
|
||||
}
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.usage()
|
||||
} else {
|
||||
"This alias wraps an unknown external command."
|
||||
}
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.extra_usage()
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Err(ShellError::NushellFailedSpanned(
|
||||
"Can't run alias directly. Unwrap it first".to_string(),
|
||||
"originates from here".to_string(),
|
||||
call.head,
|
||||
))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.examples()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
fn is_builtin(&self) -> bool {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_builtin()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_known_external(&self) -> bool {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_known_external()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_alias(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn as_alias(&self) -> Option<&Alias> {
|
||||
Some(self)
|
||||
}
|
||||
|
||||
fn is_custom_command(&self) -> bool {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_custom_command()
|
||||
} else if self.get_block_id().is_some() {
|
||||
true
|
||||
} else {
|
||||
self.is_known_external()
|
||||
}
|
||||
}
|
||||
|
||||
fn is_sub(&self) -> bool {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_sub()
|
||||
} else {
|
||||
self.name().contains(' ')
|
||||
}
|
||||
}
|
||||
|
||||
fn is_parser_keyword(&self) -> bool {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_parser_keyword()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_plugin(&self) -> Option<(&PathBuf, &Option<PathBuf>)> {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.is_plugin()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_block_id(&self) -> Option<BlockId> {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.get_block_id()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
if let Some(cmd) = &self.command {
|
||||
cmd.search_terms()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{ast::Call, BlockId, Example, PipelineData, ShellError, Signature};
|
||||
use crate::{ast::Call, Alias, BlockId, Example, PipelineData, ShellError, Signature};
|
||||
|
||||
use super::{EngineState, Stack};
|
||||
|
||||
@ -10,6 +10,7 @@ pub enum CommandType {
|
||||
Custom,
|
||||
Keyword,
|
||||
External,
|
||||
Alias,
|
||||
Plugin,
|
||||
Other,
|
||||
}
|
||||
@ -47,6 +48,16 @@ pub trait Command: Send + Sync + CommandClone {
|
||||
false
|
||||
}
|
||||
|
||||
// This is an alias of another command
|
||||
fn is_alias(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
// Return reference to the command as Alias
|
||||
fn as_alias(&self) -> Option<&Alias> {
|
||||
None
|
||||
}
|
||||
|
||||
// This is an enhanced method to determine if a command is custom command or not
|
||||
// since extern "foo" [] and def "foo" [] behaves differently
|
||||
fn is_custom_command(&self) -> bool {
|
||||
@ -88,13 +99,15 @@ pub trait Command: Send + Sync + CommandClone {
|
||||
self.is_custom_command(),
|
||||
self.is_parser_keyword(),
|
||||
self.is_known_external(),
|
||||
self.is_alias(),
|
||||
self.is_plugin().is_some(),
|
||||
) {
|
||||
(true, false, false, false, false) => CommandType::Builtin,
|
||||
(true, true, false, false, false) => CommandType::Custom,
|
||||
(true, false, true, false, false) => CommandType::Keyword,
|
||||
(false, true, false, true, false) => CommandType::External,
|
||||
(true, false, false, false, true) => CommandType::Plugin,
|
||||
(true, false, false, false, false, false) => CommandType::Builtin,
|
||||
(true, true, false, false, false, false) => CommandType::Custom,
|
||||
(true, false, true, false, false, false) => CommandType::Keyword,
|
||||
(false, true, false, true, false, false) => CommandType::External,
|
||||
(_, _, _, _, true, _) => CommandType::Alias,
|
||||
(true, false, false, false, false, true) => CommandType::Plugin,
|
||||
_ => CommandType::Other,
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
mod alias;
|
||||
pub mod ast;
|
||||
mod cli_error;
|
||||
pub mod config;
|
||||
@ -19,6 +20,7 @@ pub mod util;
|
||||
mod value;
|
||||
mod variable;
|
||||
|
||||
pub use alias::*;
|
||||
pub use cli_error::*;
|
||||
pub use config::*;
|
||||
pub use engine::{ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID};
|
||||
|
@ -29,6 +29,7 @@ fn hides_def_then_redefines() -> TestResult {
|
||||
)
|
||||
}
|
||||
|
||||
#[ignore = "TODO: We'd need to make predecls work with hiding as well"]
|
||||
#[test]
|
||||
fn hides_alias_then_redefines() -> TestResult {
|
||||
run_test(
|
||||
@ -180,30 +181,6 @@ fn hides_def_runs_env() -> TestResult {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_alias_runs_def_1() -> TestResult {
|
||||
run_test(
|
||||
r#"def foo [] { "bar" }; alias foo = echo "foo"; hide foo; foo"#,
|
||||
"bar",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_alias_runs_def_2() -> TestResult {
|
||||
run_test(
|
||||
r#"alias foo = echo "foo"; def foo [] { "bar" }; hide foo; foo"#,
|
||||
"bar",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_alias_and_def() -> TestResult {
|
||||
fail_test(
|
||||
r#"alias foo = echo "foo"; def foo [] { "bar" }; hide foo; hide foo; foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_def_import_1() -> TestResult {
|
||||
fail_test(
|
||||
@ -263,7 +240,7 @@ fn hides_def_import_then_reimports() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_1() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam; hide spam foo; spam foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam; hide spam foo; spam foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -271,7 +248,7 @@ fn hides_alias_import_1() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_2() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam; hide spam; spam foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam; hide spam; spam foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -279,7 +256,7 @@ fn hides_alias_import_2() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_3() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam; hide spam [foo]; spam foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam; hide spam [foo]; spam foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -287,7 +264,7 @@ fn hides_alias_import_3() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_4() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam foo; hide foo; foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam foo; hide foo; foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -295,7 +272,7 @@ fn hides_alias_import_4() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_5() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam *; hide foo; foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam *; hide foo; foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -303,7 +280,7 @@ fn hides_alias_import_5() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_6() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam *; hide spam *; foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam *; hide spam *; foo"#,
|
||||
"external_command",
|
||||
)
|
||||
}
|
||||
@ -311,7 +288,7 @@ fn hides_alias_import_6() -> TestResult {
|
||||
#[test]
|
||||
fn hides_alias_import_then_reimports() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export alias foo = "foo" }; use spam foo; hide foo; use spam foo; foo"#,
|
||||
r#"module spam { export alias foo = echo "foo" }; use spam foo; hide foo; use spam foo; foo"#,
|
||||
"foo",
|
||||
)
|
||||
}
|
||||
|
@ -59,9 +59,10 @@ fn alias_2_multi_word() -> TestResult {
|
||||
)
|
||||
}
|
||||
|
||||
#[ignore = "TODO: Allow alias to alias existing command with the same name"]
|
||||
#[test]
|
||||
fn alias_recursion() -> TestResult {
|
||||
run_test_contains(r#"alias ls = (ls | sort-by type name -i); ls"#, " ")
|
||||
run_test_contains(r#"alias ls = ls -a; ls"#, " ")
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -152,7 +152,7 @@ fn env_change_define_env_var() {
|
||||
#[test]
|
||||
fn env_change_define_alias() {
|
||||
let inp = &[
|
||||
&env_change_hook_code("FOO", r#"'alias spam = "spam"'"#),
|
||||
&env_change_hook_code("FOO", r#"'alias spam = echo "spam"'"#),
|
||||
"let-env FOO = 1",
|
||||
"spam",
|
||||
];
|
||||
|
@ -45,7 +45,7 @@ fn module_private_import_alias() {
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export alias foo-helper = "foo"
|
||||
export alias foo-helper = echo "foo"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -122,7 +122,7 @@ fn module_public_import_alias() {
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export alias foo = "foo"
|
||||
export alias foo = echo "foo"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -160,7 +160,7 @@ fn module_nested_imports() {
|
||||
"spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
export alias bar = echo "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -204,7 +204,7 @@ fn module_nested_imports_in_dirs() {
|
||||
"spam/spam3/spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
export alias bar = echo "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -275,7 +275,7 @@ fn module_nested_imports_in_dirs_prefixed() {
|
||||
"spam/spam3/spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
export alias bar = echo "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
@ -495,7 +495,7 @@ fn module_invalid_def_name() {
|
||||
|
||||
#[test]
|
||||
fn module_valid_alias_name_1() {
|
||||
let inp = &[r#"module spam { alias spam = "spam" }"#];
|
||||
let inp = &[r#"module spam { alias spam = echo "spam" }"#];
|
||||
|
||||
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
|
||||
|
||||
@ -504,7 +504,7 @@ fn module_valid_alias_name_1() {
|
||||
|
||||
#[test]
|
||||
fn module_valid_alias_name_2() {
|
||||
let inp = &[r#"module spam { alias main = "spam" }"#];
|
||||
let inp = &[r#"module spam { alias main = echo "spam" }"#];
|
||||
|
||||
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
|
||||
|
||||
@ -513,7 +513,7 @@ fn module_valid_alias_name_2() {
|
||||
|
||||
#[test]
|
||||
fn module_invalid_alias_name() {
|
||||
let inp = &[r#"module spam { export alias spam = "spam" }"#];
|
||||
let inp = &[r#"module spam { export alias spam = echo "spam" }"#];
|
||||
|
||||
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
|
||||
|
||||
@ -522,7 +522,7 @@ fn module_invalid_alias_name() {
|
||||
|
||||
#[test]
|
||||
fn module_main_alias_not_allowed() {
|
||||
let inp = &[r#"module spam { export alias main = 'spam' }"#];
|
||||
let inp = &[r#"module spam { export alias main = echo 'spam' }"#];
|
||||
|
||||
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
|
||||
|
||||
|
@ -475,7 +475,7 @@ fn remove_overlay_discard_decl() {
|
||||
fn remove_overlay_discard_alias() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"alias bagr = "bagr""#,
|
||||
r#"alias bagr = echo "bagr""#,
|
||||
r#"overlay hide spam"#,
|
||||
r#"bagr"#,
|
||||
];
|
||||
@ -526,7 +526,7 @@ fn remove_overlay_keep_decl() {
|
||||
fn remove_overlay_keep_alias() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"alias bagr = 'bagr'"#,
|
||||
r#"alias bagr = echo 'bagr'"#,
|
||||
r#"overlay hide --keep-custom spam"#,
|
||||
r#"bagr"#,
|
||||
];
|
||||
@ -577,7 +577,7 @@ fn remove_overlay_dont_keep_overwritten_decl() {
|
||||
fn remove_overlay_dont_keep_overwritten_alias() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"alias bar = `baz`"#,
|
||||
r#"alias bar = echo `baz`"#,
|
||||
r#"overlay hide --keep-custom spam"#,
|
||||
r#"bar"#,
|
||||
];
|
||||
@ -630,7 +630,7 @@ fn remove_overlay_keep_decl_in_latest_overlay() {
|
||||
fn remove_overlay_keep_alias_in_latest_overlay() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"alias bagr = 'bagr'"#,
|
||||
r#"alias bagr = echo 'bagr'"#,
|
||||
r#"module eggs { }"#,
|
||||
r#"overlay use eggs"#,
|
||||
r#"overlay hide --keep-custom spam"#,
|
||||
@ -1046,13 +1046,14 @@ fn overlay_preserve_hidden_decl() {
|
||||
assert_eq!(actual_repl.out, "foo");
|
||||
}
|
||||
|
||||
#[ignore = "TODO: For this to work, we'd need to make predecls respect overlays"]
|
||||
#[test]
|
||||
fn overlay_preserve_hidden_alias() {
|
||||
let inp = &[
|
||||
r#"overlay new spam"#,
|
||||
r#"alias foo = 'foo'"#,
|
||||
r#"alias foo = echo 'foo'"#,
|
||||
r#"overlay new eggs"#,
|
||||
r#"alias foo = 'bar'"#,
|
||||
r#"alias foo = echo 'bar'"#,
|
||||
r#"hide foo"#,
|
||||
r#"overlay use eggs"#,
|
||||
r#"foo"#,
|
||||
@ -1156,14 +1157,14 @@ fn overlay_use_and_reload() {
|
||||
let inp = &[
|
||||
r#"module spam {
|
||||
export def foo [] { 'foo' };
|
||||
export alias fooalias = 'foo';
|
||||
export alias fooalias = echo 'foo';
|
||||
export-env {
|
||||
let-env FOO = 'foo'
|
||||
}
|
||||
}"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"def foo [] { 'newfoo' }"#,
|
||||
r#"alias fooalias = 'newfoo'"#,
|
||||
r#"alias fooalias = echo 'newfoo'"#,
|
||||
r#"let-env FOO = 'newfoo'"#,
|
||||
r#"overlay use --reload spam"#,
|
||||
r#"$'(foo)(fooalias)($env.FOO)'"#,
|
||||
@ -1181,7 +1182,7 @@ fn overlay_use_and_reolad_keep_custom() {
|
||||
let inp = &[
|
||||
r#"overlay new spam"#,
|
||||
r#"def foo [] { 'newfoo' }"#,
|
||||
r#"alias fooalias = 'newfoo'"#,
|
||||
r#"alias fooalias = echo 'newfoo'"#,
|
||||
r#"let-env FOO = 'newfoo'"#,
|
||||
r#"overlay use --reload spam"#,
|
||||
r#"$'(foo)(fooalias)($env.FOO)'"#,
|
||||
|
@ -1,5 +1,5 @@
|
||||
export def foo [] { "foo" }
|
||||
|
||||
export alias bar = "bar"
|
||||
export alias bar = echo "bar"
|
||||
|
||||
export-env { let-env BAZ = "baz" }
|
||||
|
@ -1,5 +1,6 @@
|
||||
use nu_test_support::nu;
|
||||
|
||||
#[ignore = "TODO: This shows old-style aliases. New aliases are under commands"]
|
||||
#[test]
|
||||
fn scope_shows_alias() {
|
||||
let actual = nu!(
|
||||
|
Loading…
Reference in New Issue
Block a user