Move Value to helpers, separate span call (#10121)

# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@@ -243,10 +243,7 @@ fn create_grid_output(
if let Some(grid_display) = grid.fit_into_width(cols as usize) {
Value::string(grid_display.to_string(), call.head)
} else {
Value::String {
val: format!("Couldn't fit grid into {cols} columns!"),
span: call.head,
}
Value::string(format!("Couldn't fit grid into {cols} columns!"), call.head)
}
.into_pipeline_data(),
)

View File

@@ -129,10 +129,7 @@ impl Command for Table {
// if list argument is present we just need to return a list of supported table themes
if list {
let val = Value::List {
vals: supported_table_modes(),
span: Span::test_data(),
};
let val = Value::list(supported_table_modes(), Span::test_data());
return Ok(val.into_pipeline_data());
}
@@ -165,8 +162,8 @@ impl Command for Table {
Example {
description: "Render data in table view",
example: r#"[[a b]; [1 2] [3 4]] | table"#,
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
@@ -177,13 +174,13 @@ impl Command for Table {
}),
],
span,
}),
)),
},
Example {
description: "Render data in table view (expanded)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --expand"#,
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
@@ -194,13 +191,13 @@ impl Command for Table {
}),
],
span,
}),
)),
},
Example {
description: "Render data in table view (collapsed)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --collapse"#,
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
@@ -211,7 +208,7 @@ impl Command for Table {
}),
],
span,
}),
)),
},
]
}
@@ -229,6 +226,7 @@ fn handle_table_command(
let ctrlc = engine_state.ctrlc.clone();
let config = get_config(engine_state, stack);
let span = input.span().unwrap_or(call.head);
match input {
PipelineData::ExternalStream { .. } => Ok(input),
PipelineData::Value(Value::Binary { val, .. }, ..) => Ok(PipelineData::ExternalStream {
@@ -270,7 +268,7 @@ fn handle_table_command(
ctrlc,
metadata,
),
PipelineData::Value(Value::Record { val, span }, ..) => {
PipelineData::Value(Value::Record { val, .. }, ..) => {
let term_width = get_width_param(term_width);
handle_record(
@@ -302,7 +300,7 @@ fn handle_table_command(
// instead of stdout.
Err(*error)
}
PipelineData::Value(Value::CustomValue { val, span }, ..) => {
PipelineData::Value(Value::CustomValue { val, .. }, ..) => {
let base_pipeline = val.to_base_value(span)?.into_pipeline_data();
Table.run(engine_state, stack, call, base_pipeline)
}
@@ -363,10 +361,7 @@ fn handle_record(
}
};
let val = Value::String {
val: result,
span: call.head,
};
let val = Value::string(result, call.head);
Ok(val.into_pipeline_data())
}
@@ -421,7 +416,7 @@ fn build_table_batch(
ExpandedTable::new(limit, flatten, sep).build_list(&vals, opts)
}
TableView::Collapsed => {
let value = Value::List { vals, span };
let value = Value::list(vals, span);
CollapsedTable::build(value, opts)
}
}
@@ -436,6 +431,7 @@ fn handle_row_stream(
ctrlc: Option<Arc<AtomicBool>>,
metadata: Option<Box<PipelineMetadata>>,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let stream = match metadata.as_deref() {
// First, `ls` sources:
Some(PipelineMetadata {
@@ -457,8 +453,9 @@ fn handle_row_stream(
while idx < record.len() {
// Only the name column gets special colors, for now
if record.cols[idx] == "name" {
if let Some(Value::String { val, span }) = record.vals.get(idx) {
let val = render_path_name(val, &config, &ls_colors, *span);
let span = record.vals.get(idx).map(|v| v.span()).unwrap_or(head);
if let Some(Value::String { val, .. }) = record.vals.get(idx) {
let val = render_path_name(val, &config, &ls_colors, span);
if let Some(val) = val {
record.vals[idx] = val;
}
@@ -490,7 +487,9 @@ fn handle_row_stream(
if record.cols[idx] != "name" {
// Simple routine to grab the hex code, convert to a style,
// then place it in a new Value::String.
if let Some(Value::String { val, span }) = record.vals.get(idx) {
let span = record.vals.get(idx).map(|v| v.span()).unwrap_or(head);
if let Some(Value::String { val, .. }) = record.vals.get(idx) {
let s = match color_from_hex(val) {
Ok(c) => match c {
// .normal() just sets the text foreground color.
@@ -499,11 +498,11 @@ fn handle_row_stream(
},
Err(_) => nu_ansi_term::Style::default(),
};
record.vals[idx] = Value::String {
record.vals[idx] = Value::string(
// Apply the style (ANSI codes) to the string
val: s.paint(val).to_string(),
span: *span,
};
s.paint(val).to_string(),
span,
);
}
}
idx += 1;
@@ -814,7 +813,7 @@ fn render_path_name(
);
let val = ansi_style.paint(full_path_link).to_string();
Some(Value::String { val, span })
Some(Value::string(val, span))
}
#[derive(Debug)]