nushell/src/commands/enter.rs

153 lines
6.8 KiB
Rust
Raw Normal View History

2019-08-07 19:49:11 +02:00
use crate::commands::command::CommandAction;
2019-08-15 07:02:02 +02:00
use crate::commands::PerItemCommand;
2019-08-31 02:59:21 +02:00
use crate::commands::UnevaluatedCallInfo;
2019-08-07 19:49:11 +02:00
use crate::errors::ShellError;
2019-08-14 19:02:39 +02:00
use crate::parser::registry;
2019-08-07 19:49:11 +02:00
use crate::prelude::*;
2019-08-31 02:59:21 +02:00
use std::path::PathBuf;
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
}
2019-08-14 19:02:39 +02:00
fn signature(&self) -> registry::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
}
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,
2019-08-31 02:59:21 +02:00
registry: &registry::CommandRegistry,
raw_args: &RawCommandArgs,
_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)? {
Value {
value: UntaggedValue::Primitive(Primitive::Path(location)),
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();
let tag_clone = tag.clone();
if location.starts_with("help") {
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 spec = location_string.split(":").collect::<Vec<&str>>();
let (_, command) = (spec[0], spec[1]);
if registry.has(command) {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterHelpShell(
UntaggedValue::string(command).into_value(Tag::unknown()),
)))]
.into())
} else {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterHelpShell(
UntaggedValue::nothing().into_value(Tag::unknown()),
)))]
.into())
}
} else if PathBuf::from(location).is_dir() {
2019-08-31 02:59:21 +02:00
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterShell(
location_clone,
)))]
.into())
} else {
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();
let full_path = std::path::PathBuf::from(cwd);
let (file_extension, contents, contents_tag) =
2019-08-31 02:59:21 +02:00
crate::commands::open::fetch(
&full_path,
&location_clone,
tag_clone.span,
).await?;
2019-08-31 02:59:21 +02:00
match contents {
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) =
registry.get_command(&command_name)
{
let new_args = RawCommandArgs {
host: raw_args.host,
ctrl_c: raw_args.ctrl_c,
2019-08-31 02:59:21 +02:00
shell_manager: raw_args.shell_manager,
call_info: UnevaluatedCallInfo {
args: crate::parser::hir::Call {
head: raw_args.call_info.args.head,
positional: None,
named: None,
span: Span::unknown()
2019-08-31 02:59:21 +02:00
},
source: raw_args.call_info.source,
name_tag: raw_args.call_info.name_tag,
2019-08-31 02:59:21 +02:00
},
};
let mut result = converter.run(
new_args.with_input(vec![tagged_contents]),
&registry,
);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await;
for res in result_vec {
match res {
Ok(ReturnSuccess::Value(Value {
value,
2019-08-31 02:59:21 +02:00
..
})) => {
yield Ok(ReturnSuccess::Action(CommandAction::EnterValueShell(
Value {
value,
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)));
}
}
_ => {
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
}