2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::PerItemCommand;
|
2019-08-31 02:59:21 +02:00
|
|
|
use crate::commands::UnevaluatedCallInfo;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use crate::context::CommandRegistry;
|
2019-08-07 19:49:11 +02:00
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{
|
|
|
|
CallInfo, CommandAction, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
|
|
|
};
|
2019-08-07 19:49:11 +02:00
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
pub struct Enter;
|
2019-08-09 06:51:21 +02:00
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
impl PerItemCommand for Enter {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"enter"
|
2019-08-07 19:49:11 +02:00
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 06:15:35 +01:00
|
|
|
Signature::build("enter").required(
|
|
|
|
"location",
|
|
|
|
SyntaxShape::Path,
|
|
|
|
"the location to create a new shell from",
|
|
|
|
)
|
2019-08-14 19:02:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Create a new shell and begin at this path."
|
|
|
|
}
|
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
2019-08-15 07:02:02 +02:00
|
|
|
call_info: &CallInfo,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2019-08-31 02:59:21 +02:00
|
|
|
raw_args: &RawCommandArgs,
|
2019-11-21 15:33:14 +01:00
|
|
|
_input: Value,
|
2019-08-24 21:36:19 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-31 02:59:21 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
2019-08-14 19:02:39 +02:00
|
|
|
match call_info.args.expect_nth(0)? {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Path(location)),
|
2019-10-22 14:21:34 +02:00
|
|
|
tag,
|
2019-08-14 19:02:39 +02:00
|
|
|
..
|
2019-08-31 02:59:21 +02:00
|
|
|
} => {
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
let location_string = location.display().to_string();
|
|
|
|
let location_clone = location_string.clone();
|
2019-10-22 14:21:34 +02:00
|
|
|
let tag_clone = tag.clone();
|
2019-08-30 00:52:32 +02:00
|
|
|
|
2020-01-13 05:27:00 +01:00
|
|
|
if location_string.starts_with("help") {
|
2019-12-06 16:28:26 +01:00
|
|
|
let spec = location_string.split(':').collect::<Vec<&str>>();
|
2019-09-03 11:05:52 +02:00
|
|
|
|
2020-01-13 05:27:00 +01:00
|
|
|
if spec.len() == 2 {
|
|
|
|
let (_, command) = (spec[0], spec[1]);
|
2019-09-03 11:05:52 +02:00
|
|
|
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
if registry.has(command) {
|
2020-01-13 05:27:00 +01:00
|
|
|
return Ok(vec![Ok(ReturnSuccess::Action(
|
|
|
|
CommandAction::EnterHelpShell(
|
|
|
|
UntaggedValue::string(command).into_value(Tag::unknown()),
|
|
|
|
),
|
|
|
|
))]
|
|
|
|
.into());
|
|
|
|
}
|
2019-09-03 11:05:52 +02:00
|
|
|
}
|
2020-01-13 05:27:00 +01:00
|
|
|
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterHelpShell(
|
|
|
|
UntaggedValue::nothing().into_value(Tag::unknown()),
|
|
|
|
)))]
|
|
|
|
.into())
|
|
|
|
} else if location.is_dir() {
|
2019-08-31 02:59:21 +02:00
|
|
|
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterShell(
|
|
|
|
location_clone,
|
|
|
|
)))]
|
|
|
|
.into())
|
|
|
|
} else {
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2019-08-31 02:59:21 +02:00
|
|
|
// If it's a file, attempt to open the file as a value and enter it
|
|
|
|
let cwd = raw_args.shell_manager.path();
|
|
|
|
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
let full_path = std::path::PathBuf::from(cwd);
|
2019-08-31 02:59:21 +02:00
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
let (file_extension, contents, contents_tag) =
|
2019-08-31 02:59:21 +02:00
|
|
|
crate::commands::open::fetch(
|
|
|
|
&full_path,
|
|
|
|
&location_clone,
|
2019-10-22 14:21:34 +02:00
|
|
|
tag_clone.span,
|
|
|
|
).await?;
|
2019-08-31 02:59:21 +02:00
|
|
|
|
|
|
|
match contents {
|
2019-11-21 15:33:14 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(_)) => {
|
|
|
|
let tagged_contents = contents.into_value(&contents_tag);
|
2019-08-31 02:59:21 +02:00
|
|
|
|
|
|
|
if let Some(extension) = file_extension {
|
|
|
|
let command_name = format!("from-{}", extension);
|
|
|
|
if let Some(converter) =
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
registry.get_command(&command_name)
|
2019-08-31 02:59:21 +02:00
|
|
|
{
|
|
|
|
let new_args = RawCommandArgs {
|
|
|
|
host: raw_args.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: raw_args.ctrl_c,
|
2019-08-31 02:59:21 +02:00
|
|
|
shell_manager: raw_args.shell_manager,
|
|
|
|
call_info: UnevaluatedCallInfo {
|
2020-04-13 09:59:57 +02:00
|
|
|
args: nu_protocol::hir::Call {
|
2019-08-31 02:59:21 +02:00
|
|
|
head: raw_args.call_info.args.head,
|
|
|
|
positional: None,
|
|
|
|
named: None,
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
span: Span::unknown(),
|
|
|
|
is_last: false,
|
2019-08-31 02:59:21 +02:00
|
|
|
},
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag: raw_args.call_info.name_tag,
|
2020-04-15 07:43:23 +02:00
|
|
|
scope: raw_args.call_info.scope.clone()
|
2019-08-31 02:59:21 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
let mut result = converter.run(
|
|
|
|
new_args.with_input(vec![tagged_contents]),
|
|
|
|
®istry,
|
|
|
|
);
|
|
|
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
|
|
|
|
result.drain_vec().await;
|
|
|
|
for res in result_vec {
|
|
|
|
match res {
|
2019-11-21 15:33:14 +01:00
|
|
|
Ok(ReturnSuccess::Value(Value {
|
|
|
|
value,
|
2019-08-31 02:59:21 +02:00
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
yield Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value,
|
2019-10-13 06:12:43 +02:00
|
|
|
tag: contents_tag.clone(),
|
2019-08-31 02:59:21 +02:00
|
|
|
})));
|
|
|
|
}
|
|
|
|
x => yield x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(tagged_contents)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(tagged_contents)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2019-11-21 15:33:14 +01:00
|
|
|
let tagged_contents = contents.into_value(contents_tag);
|
2019-08-31 02:59:21 +02:00
|
|
|
|
|
|
|
yield Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(tagged_contents)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 19:02:39 +02:00
|
|
|
x => Ok(
|
|
|
|
vec![Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(
|
|
|
|
x.clone(),
|
|
|
|
)))]
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 19:49:11 +02:00
|
|
|
}
|