Table help rendering (#13182)

# Description

Mostly fixes #13149 with much of the credit to @fdncred.

This PR runs `table --expand` against `help` example results. This is
essentially the same fix that #13146 was for `std help`.

It also changes the shape of the result for the `table --expand`
example, as it was hardcoded wrong.

~Still needed is a fix for the `table --collapse` example.~ Note that
this is also still a bug in `std help` that I didn't noticed before.

# User-Facing Changes

Certain tables are now rendered correctly in the help examples for:

* `table`
* `zip`
* `flatten`
* And almost certainly others

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
NotTheDr01ds 2024-06-19 21:12:25 -04:00 committed by GitHub
parent 12991cd36f
commit 44aa0a2de4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 4 deletions

View File

@ -170,7 +170,10 @@ impl Command for Table {
}), }),
Value::test_record(record! { Value::test_record(record! {
"a" => Value::test_int(3), "a" => Value::test_int(3),
"b" => Value::test_int(4), "b" => Value::test_list(vec![
Value::test_int(4),
Value::test_int(4),
])
}), }),
])), ])),
}, },
@ -184,7 +187,10 @@ impl Command for Table {
}), }),
Value::test_record(record! { Value::test_record(record! {
"a" => Value::test_int(3), "a" => Value::test_int(3),
"b" => Value::test_int(4), "b" => Value::test_list(vec![
Value::test_int(4),
Value::test_int(4),
])
}), }),
])), ])),
}, },

View File

@ -3,7 +3,7 @@ use nu_protocol::{
ast::{Argument, Call, Expr, Expression, RecordItem}, ast::{Argument, Call, Expr, Expression, RecordItem},
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID}, engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID},
record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SpanId, record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SpanId, Spanned,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
use std::{collections::HashMap, fmt::Write}; use std::{collections::HashMap, fmt::Write};
@ -296,6 +296,28 @@ fn get_documentation(
} }
if let Some(result) = &example.result { if let Some(result) = &example.result {
let mut table_call = Call::new(Span::unknown());
if example.example.ends_with("--collapse") {
// collapse the result
table_call.add_named((
Spanned {
item: "collapse".to_string(),
span: Span::unknown(),
},
None,
None,
))
} else {
// expand the result
table_call.add_named((
Spanned {
item: "expand".to_string(),
span: Span::unknown(),
},
None,
None,
))
}
let table = engine_state let table = engine_state
.find_decl("table".as_bytes(), &[]) .find_decl("table".as_bytes(), &[])
.and_then(|decl_id| { .and_then(|decl_id| {
@ -304,7 +326,7 @@ fn get_documentation(
.run( .run(
engine_state, engine_state,
stack, stack,
&Call::new(Span::new(0, 0)), &table_call,
PipelineData::Value(result.clone(), None), PipelineData::Value(result.clone(), None),
) )
.ok() .ok()