nushell/crates/nu-protocol/src
Jakub Žádník a3f817d71b
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.
2023-02-27 20:44:05 +13:00
..
ast explain command (#7957) 2023-02-09 13:59:38 -06:00
engine Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
value allow date grouping in group-by (#8084) 2023-02-15 19:10:55 +00:00
alias.rs Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
cli_error.rs Clippy fix for Rust 1.63 (#6299) 2022-08-11 11:54:54 -05:00
config.rs Display empty records and lists (#7925) 2023-02-22 16:18:33 +00:00
example.rs Make plugin commands support examples. (#7984) 2023-02-08 16:14:18 -06:00
exportable.rs Removes export env command (#6468) 2022-09-25 19:52:43 +03:00
id.rs Overlays (#5375) 2022-05-08 07:39:22 +12:00
lev_distance.rs Reduce again the number of match calls (#7815) 2023-01-24 12:23:42 +01:00
lib.rs Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
module.rs Allow main command to define top-level module command (#7764) 2023-01-22 21:34:15 +02:00
pipeline_data.rs allow Range to expand to array-like when converting to json (#8047) 2023-02-24 15:31:33 -06:00
plugin_signature.rs Make plugin commands support examples. (#7984) 2023-02-08 16:14:18 -06:00
shell_error.rs remove links to ShellError and ParseError docs - #8167 (#8193) 2023-02-24 09:26:31 -08:00
signature.rs view span & view files commands (#7989) 2023-02-09 11:35:23 -06:00
span.rs Use variable names directly in the format strings (#7906) 2023-01-29 19:37:54 -06:00
syntax_shape.rs Use variable names directly in the format strings (#7906) 2023-01-29 19:37:54 -06:00
ty.rs Display empty records and lists (#7925) 2023-02-22 16:18:33 +00:00
util.rs move BufferedReader out of nu-command (#7697) 2023-01-06 15:22:17 -08:00
variable.rs Limited mutable variables (#7089) 2022-11-11 19:51:08 +13:00