Skip to content

Add support for ~ expansion - #479

Merged
sophiajt merged 2 commits into
masterfrom
expand-tilde
Aug 27, 2019
Merged

sophiajt merged 2 commits into
masterfrom
expand-tilde

Conversation

@wycats

@wycats wycats commented Aug 27, 2019

Copy link
Copy Markdown
Contributor

This ended up being a bit of a yak shave. The basic idea in this commit is to expand ~ in paths, but only in paths.

The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for SyntaxType::Path.

As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints.

For example, the syntax where cpu > 0 expands under the hood to where { $it.cpu > 0 }. This happens because the first argument to where is defined as a SyntaxType::Block, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one.

This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells).

The syntactic expansions supported by this facility are relatively limited. For example, we don't allow $it to become a bare word simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth.

This PR adds a new SyntaxType rule: SyntaxType::Path. When a command declares a parameter as a SyntaxType::Path, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that ~ is expanded into
the home directory, but additional rules are possible in the future.

By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making ~ a generally reserved character. This will also allow us to give good tab completion for paths with ~ characters in them when a command is expecting a path.

In order to accomplish the above, this commit changes the parsing functions to take a Context instead of just a CommandRegistry. From the perspective of macro expansion, you can think of the CommandRegistry as a dictionary of in-scope macros, and the Context as the compile-time state used in expansion. This could gain additional functionality over time as we find
more uses for the expansion system.

This commit also does a number of small cleanups, which add up to a lot of cleanup. Hopefully it won't be too hard to merge.

wycats and others added 2 commits August 26, 2019 21:03
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.

The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.

As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.

For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.

This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).

The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.

This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.

By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.

In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
@salty-blue-mango

Copy link
Copy Markdown

Checking up on this issue. I'm on nu version 0.4.0 and can't seem to cd ~ still. Unless I'm doing something wrong.

elferherrera pushed a commit to elferherrera/nushell that referenced this pull request Feb 7, 2022
* Add in auto-cd if you pass just a directory

* clippy
kubouch pushed a commit that referenced this pull request Feb 7, 2022
* Add in auto-cd if you pass just a directory

* clippy
Hofer-Julian pushed a commit to Hofer-Julian/nushell that referenced this pull request Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants