allow view-source to read rest arguments (#9247)

# Description
Fixes #8896. Also went back and cleaned up the code slightly.

# User-Facing Changes
`view-source` now is more comprehensive when viewing definitions. 

# 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 -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` 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>
This commit is contained in:
pwygab 2023-05-20 21:56:21 +08:00 committed by GitHub
parent 4954a762b7
commit 6564ed710d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value, Value,
}; };
use std::fmt::Write;
#[derive(Clone)] #[derive(Clone)]
pub struct ViewSource; pub struct ViewSource;
@ -54,47 +55,43 @@ impl Command for ViewSource {
let sig = decl.signature(); let sig = decl.signature();
let vec_of_required = &sig.required_positional; let vec_of_required = &sig.required_positional;
let vec_of_optional = &sig.optional_positional; let vec_of_optional = &sig.optional_positional;
let rest = &sig.rest_positional;
let vec_of_flags = &sig.named; let vec_of_flags = &sig.named;
// gets vector of positionals. // gets vector of positionals.
if let Some(block_id) = decl.get_block_id() { if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id); let block = engine_state.get_block(block_id);
if let Some(block_span) = block.span { if let Some(block_span) = block.span {
let contents = engine_state.get_span_contents(&block_span); let contents = engine_state.get_span_contents(&block_span);
let mut final_contents = String::from("def "); // name of function
final_contents.push_str(&val); let mut final_contents = format!("def {val} [ ");
// The name of the function...
final_contents.push_str(" [ ");
for n in vec_of_required { for n in vec_of_required {
final_contents.push_str(&n.name); let _ = write!(&mut final_contents, "{}: {} ", n.name, n.shape);
// name of positional arg // positional argu,emts
final_contents.push(':');
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
} }
for n in vec_of_optional { for n in vec_of_optional {
final_contents.push_str(&n.name); let _ = write!(&mut final_contents, "{}?: {} ", n.name, n.shape);
// name of positional arg
final_contents.push_str("?:");
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
} }
for n in vec_of_flags { for n in vec_of_flags {
final_contents.push_str("--"); // skip adding the help flag
final_contents.push_str(&n.long); if n.long == "help" {
final_contents.push(' '); continue;
if n.short.is_some() {
final_contents.push_str("(-");
final_contents.push(n.short.expect("this cannot trigger."));
final_contents.push(')');
} }
if n.arg.is_some() { let _ = write!(&mut final_contents, "--{}", n.long);
final_contents.push_str(": "); if let Some(short) = n.short {
final_contents.push_str( let _ = write!(&mut final_contents, "(-{})", short);
&n.arg.as_ref().expect("this cannot trigger.").to_string(), }
if let Some(arg) = &n.arg {
let _ = write!(&mut final_contents, ": {}", arg);
}
final_contents.push(' ');
}
if let Some(rest_arg) = rest {
let _ = write!(
&mut final_contents,
"...{}:{}",
rest_arg.name, rest_arg.shape
); );
} }
final_contents.push(' ');
}
final_contents.push_str("] "); final_contents.push_str("] ");
final_contents.push_str(&String::from_utf8_lossy(contents)); final_contents.push_str(&String::from_utf8_lossy(contents));
Ok(Value::string(final_contents, call.head).into_pipeline_data()) Ok(Value::string(final_contents, call.head).into_pipeline_data())
@ -157,17 +154,22 @@ impl Command for ViewSource {
Example { Example {
description: "View the source of a code block", description: "View the source of a code block",
example: r#"let abc = {|| echo 'hi' }; view source $abc"#, example: r#"let abc = {|| echo 'hi' }; view source $abc"#,
result: Some(Value::test_string("{ echo 'hi' }")), result: Some(Value::test_string("{|| echo 'hi' }")),
}, },
Example { Example {
description: "View the source of a custom command", description: "View the source of a custom command",
example: r#"def hi [] { echo 'Hi!' }; view source hi"#, example: r#"def hi [] { echo 'Hi!' }; view source hi"#,
result: Some(Value::test_string("{ echo 'Hi!' }")), result: Some(Value::test_string("def hi [] { echo 'Hi!' }")),
}, },
Example { Example {
description: "View the source of a custom command, which participates in the caller environment", description: "View the source of a custom command, which participates in the caller environment",
example: r#"def-env foo [] { let-env BAR = 'BAZ' }; view source foo"#, example: r#"def-env foo [] { let-env BAR = 'BAZ' }; view source foo"#,
result: Some(Value::test_string("{ let-env BAR = 'BAZ' }")), result: Some(Value::test_string("def foo [] { let-env BAR = 'BAZ' }")),
},
Example {
description: "View the source of a custom command with flags and arguments",
example: r#"def test [a?:any --b:int ...rest:string] { echo 'test' }; view source test"#,
result: Some(Value::test_string("def test [ a?: any --b: int ...rest: string] { echo 'test' }")),
}, },
Example { Example {
description: "View the source of a module", description: "View the source of a module",