Allow open to work with 'from ...' block commands (#5049)

* Remove panic from BlockCommands run function

Instead of panicing, the run method now returns an error to prevent
nushell from unexpected termination.

* Add ability to open command to run with blocks

The open command tries to parse the content of the file
if there is a command called 'from (file ending)'.  This works
fine if the command was 'built in' because the run method doesn't
fail in this case.  It did fail on a BlockCommand, though.

This change will first probe if the command contains a block and
evaluate it, if this is the case.  If there is no block, it will run
the command the same way as before.

* Add test open files with BlockCommands

* Update open.rs

* Adjust file type on open with BlockCommand parser

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
Simon Goller
2022-04-01 20:52:32 +02:00
committed by GitHub
parent 2cb815b7b4
commit 19c3570cf9
4 changed files with 33 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use nu_engine::{get_full_help, CallExt};
use nu_engine::{eval_block, get_full_help, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -141,12 +141,15 @@ impl Command for Open {
if let Some(ext) = ext {
match engine_state.find_decl(format!("from {}", ext).as_bytes()) {
Some(converter_id) => engine_state.get_decl(converter_id).run(
engine_state,
stack,
&Call::new(arg_span),
output,
),
Some(converter_id) => {
let decl = engine_state.get_decl(converter_id);
if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
eval_block(engine_state, stack, block, output, false, false)
} else {
decl.run(engine_state, stack, &Call::new(arg_span), output)
}
}
None => Ok(output),
}
} else {