Repository navigation
Add support for ~ expansion - #479
Merged
Merged
Conversation
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 was referenced Aug 27, 2019
Closed
Closed
Closed
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 > 0expands under the hood towhere { $it.cpu > 0 }. This happens because the first argument towhereis defined as aSyntaxType::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
$itto 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
SyntaxTyperule:SyntaxType::Path. When a command declares a parameter as aSyntaxType::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 intothe 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
Contextinstead of just aCommandRegistry. From the perspective of macro expansion, you can think of theCommandRegistryas a dictionary of in-scope macros, and theContextas the compile-time state used in expansion. This could gain additional functionality over time as we findmore 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.