Skip to content

Commit

Permalink
Improve empty pipelines (#7383)
Browse files Browse the repository at this point in the history
# Description

This fix changes pipelines to allow them to actually be empty. Mapping
over empty pipelines gives empty pipelines. Empty pipelines immediately
return `None` when iterated.

This removes a some of where `Span::new(0, 0)` was coming from, though
there are other cases where we still use it.

# User-Facing Changes

None

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
  • Loading branch information
sophiajt committed Dec 7, 2022
1 parent d185873 commit eaec480
Show file tree
Hide file tree
Showing 80 changed files with 195 additions and 191 deletions.
2 changes: 1 addition & 1 deletion crates/nu-cli/src/completions/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl NuCompleter {
&self.engine_state,
&mut callee_stack,
block,
PipelineData::new(span),
PipelineData::empty(),
true,
true,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/completions/custom_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Completer for CustomCompletion {
redirect_stdout: true,
redirect_stderr: true,
},
PipelineData::new(span),
PipelineData::empty(),
);

let mut custom_completion_options = None;
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-cli/src/config_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
#[cfg(feature = "plugin")]
use nu_protocol::Spanned;
use nu_protocol::{HistoryFileFormat, PipelineData, Span};
use nu_protocol::{HistoryFileFormat, PipelineData};
use std::path::PathBuf;

#[cfg(feature = "plugin")]
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn read_plugin_file(
stack,
&contents,
&plugin_filename,
PipelineData::new(Span::new(0, 0)),
PipelineData::empty(),
);
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn eval_config_contents(
stack,
&contents,
&config_filename,
PipelineData::new(Span::new(0, 0)),
PipelineData::empty(),
);

// Merge the environment in case env vars changed in the config
Expand Down
8 changes: 1 addition & 7 deletions crates/nu-cli/src/eval_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ pub fn evaluate_file(
if working_set.find_decl(b"main", &Type::Any).is_some() {
let args = format!("main {}", args.join(" "));

if !eval_source(
engine_state,
stack,
&file,
&path,
PipelineData::new(Span::new(0, 0)),
) {
if !eval_source(engine_state, stack, &file, &path, PipelineData::empty()) {
std::process::exit(1);
}
if !eval_source(engine_state, stack, args.as_bytes(), "<commandline>", input) {
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ Since this command has no output, there is no point in piping it with other comm
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
let no_newline = call.has_flag("no-newline");
let to_stderr = call.has_flag("stderr");
let head = call.head;

for arg in args {
arg.into_pipeline_data()
.print(engine_state, stack, no_newline, to_stderr)?;
}

Ok(PipelineData::new(head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
17 changes: 4 additions & 13 deletions crates/nu-cli/src/prompt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::info;
use nu_engine::eval_subexpression;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Config, PipelineData, Span, Value,
Config, PipelineData, Value,
};
use reedline::Prompt;

Expand Down Expand Up @@ -37,12 +37,8 @@ fn get_prompt_string(
let block = engine_state.get_block(block_id);
let mut stack = stack.captures_to_stack(&captures);
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
let ret_val = eval_subexpression(
engine_state,
&mut stack,
block,
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
);
let ret_val =
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
info!(
"get_prompt_string (block) {}:{}:{}",
file!(),
Expand All @@ -62,12 +58,7 @@ fn get_prompt_string(
Value::Block { val: block_id, .. } => {
let block = engine_state.get_block(block_id);
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
let ret_val = eval_subexpression(
engine_state,
stack,
block,
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
);
let ret_val = eval_subexpression(engine_state, stack, block, PipelineData::empty());
info!(
"get_prompt_string (block) {}:{}:{}",
file!(),
Expand Down
12 changes: 6 additions & 6 deletions crates/nu-cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn evaluate_repl(
stack,
s.item.as_bytes(),
&format!("entry #{}", entry_num),
PipelineData::new(Span::new(0, 0)),
PipelineData::empty(),
);
engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?;
}
Expand Down Expand Up @@ -431,7 +431,7 @@ pub fn evaluate_repl(
stack,
s.as_bytes(),
&format!("entry #{}", entry_num),
PipelineData::new(Span::new(0, 0)),
PipelineData::empty(),
);
}
let cmd_duration = start_time.elapsed();
Expand Down Expand Up @@ -637,7 +637,7 @@ pub fn eval_string_with_input(

let input_as_pipeline_data = match input {
Some(input) => PipelineData::Value(input, None),
None => PipelineData::new(Span::test_data()),
None => PipelineData::empty(),
};

eval_block(
Expand Down Expand Up @@ -722,7 +722,7 @@ pub fn eval_hook(
val: "condition".to_string(),
span: value_span,
};
let mut output = PipelineData::new(Span::new(0, 0));
let mut output = PipelineData::empty();

let code_path = PathMember::String {
val: "code".to_string(),
Expand Down Expand Up @@ -823,7 +823,7 @@ pub fn eval_hook(
};

engine_state.merge_delta(delta)?;
let input = PipelineData::new(value_span);
let input = PipelineData::empty();

let var_ids: Vec<VarId> = vars
.into_iter()
Expand Down Expand Up @@ -943,7 +943,7 @@ pub fn run_hook_block(
) -> Result<Value, ShellError> {
let block = engine_state.get_block(block_id);

let input = optional_input.unwrap_or_else(|| PipelineData::new(span));
let input = optional_input.unwrap_or_else(PipelineData::empty);

let mut callee_stack = stack.gather_captures(&block.captures);

Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl Command for Alias {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-command/src/core_commands/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ impl Command for Ast {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let pipeline: Spanned<String> = call.req(engine_state, stack, 0)?;
let mut working_set = StateWorkingSet::new(engine_state);

let (output, err) = parse(&mut working_set, None, pipeline.item.as_bytes(), false, &[]);
eprintln!("output: {:#?}\nerror: {:#?}", output, err);

Ok(PipelineData::new(head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ impl Command for Def {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/def_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def-env cd_with_fallback [arg = ""] {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/do_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ impl Command for Do {
trim_end_newline,
}),
Ok(PipelineData::Value(Value::Error { .. }, ..)) if ignore_shell_errors => {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}
Err(_) if ignore_shell_errors => Ok(PipelineData::new(call.head)),
Err(_) if ignore_shell_errors => Ok(PipelineData::empty()),
r => r,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/export_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl Command for ExportAlias {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/export_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ impl Command for ExportDef {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/export_def_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export def-env cd_with_fallback [arg = ""] {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/export_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl Command for ExportExtern {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/export_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ impl Command for ExportUse {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/extern_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl Command for Extern {
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-command/src/core_commands/for_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Command for For {
&engine_state,
stack,
&block,
PipelineData::new(head),
PipelineData::empty(),
redirect_stdout,
redirect_stderr,
) {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Command for For {
&engine_state,
stack,
&block,
PipelineData::new(head),
PipelineData::empty(),
redirect_stdout,
redirect_stderr,
) {
Expand All @@ -181,14 +181,14 @@ impl Command for For {
&engine_state,
stack,
&block,
PipelineData::new(head),
PipelineData::empty(),
redirect_stdout,
redirect_stderr,
)?
.into_value(head);
}
}
Ok(PipelineData::new(head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This command is a parser keyword. For details, check:

stack.remove_env_var(engine_state, &env_var_name.item);

Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/hide_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Command for HideEnv {
}
}

Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/if_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Command for If {
.map(|res| res.0)
}
} else {
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}
}
x => Err(ShellError::CantConvert(
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Command for Ignore {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
input.into_value(call.head);
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/let_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Command for Let {
//println!("Adding: {:?} to {}", rhs, var_id);

stack.add_var(var_id, rhs.into_value(call.head));
Ok(PipelineData::new(call.head))
Ok(PipelineData::empty())
}

fn examples(&self) -> Vec<Example> {
Expand Down
Loading

0 comments on commit eaec480

Please sign in to comment.