Allowed for view-source to include entire custom command definition (#5435)

* allowed for view-source to include entire custom command definition

* fmt

* clippy
This commit is contained in:
pwygab 2022-05-04 19:35:09 +08:00 committed by GitHub
parent 1bcb87c48d
commit 48cf103439
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,12 +50,27 @@ impl Command for ViewSource {
if let Some(decl_id) = engine_state.find_decl(val.as_bytes()) {
// arg is a command
let decl = engine_state.get_decl(decl_id);
let sig = decl.signature();
let vec_of_required = &sig.required_positional;
// gets vector of positionals.
if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
if let Some(block_span) = block.span {
let contents = engine_state.get_span_contents(&block_span);
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
.into_pipeline_data())
let mut final_contents = String::from("def ");
final_contents.push_str(&val);
// The name of the function...
final_contents.push_str(" [ ");
for n in vec_of_required {
final_contents.push_str(&n.name);
// name of positional arg
final_contents.push(':');
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
}
final_contents.push_str("] ");
final_contents.push_str(&String::from_utf8_lossy(contents));
Ok(Value::string(final_contents, call.head).into_pipeline_data())
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),