From f41c53fef117512601f5f4b91109f03aaef05cf0 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:15:16 -0600 Subject: [PATCH] allow `view source` to take `int` as a parameter (#14609) # Description This PR allows the `view source` command to view source based on an int value. I wrote this specifically to be able to see closures where the text is hidden. For example: ![image](https://github.com/user-attachments/assets/d8fe2692-0951-4366-9cb9-55f20044b68a) And then you can use those `` with the `view source` command like this. ![image](https://github.com/user-attachments/assets/f428c8ad-56a9-4e72-880e-e32fb9155531) # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/debug/view_source.rs | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/debug/view_source.rs b/crates/nu-command/src/debug/view_source.rs index 417da5ec1e..2aadd4d361 100644 --- a/crates/nu-command/src/debug/view_source.rs +++ b/crates/nu-command/src/debug/view_source.rs @@ -33,6 +33,22 @@ impl Command for ViewSource { let arg_span = arg.span(); let source = match arg { + Value::Int { val, .. } => { + let block = engine_state.get_block(nu_protocol::BlockId::new(val as usize)); + if let Some(span) = block.span { + let contents = engine_state.get_span_contents(span); + Ok(Value::string(String::from_utf8_lossy(contents), call.head) + .into_pipeline_data()) + } else { + Err(ShellError::GenericError { + error: "Cannot view int value".to_string(), + msg: "the block does not have a viewable span".to_string(), + span: Some(arg_span), + help: None, + inner: vec![], + }) + } + } Value::String { val, .. } => { if let Some(decl_id) = engine_state.find_decl(val.as_bytes(), &[]) { // arg is a command @@ -130,7 +146,7 @@ impl Command for ViewSource { Ok(Value::string(final_contents, call.head).into_pipeline_data()) } else { Err(ShellError::GenericError { - error: "Cannot view value".to_string(), + error: "Cannot view string value".to_string(), msg: "the command does not have a viewable block span".to_string(), span: Some(arg_span), help: None, @@ -139,7 +155,7 @@ impl Command for ViewSource { } } else { Err(ShellError::GenericError { - error: "Cannot view value".to_string(), + error: "Cannot view string decl value".to_string(), msg: "the command does not have a viewable block".to_string(), span: Some(arg_span), help: None, @@ -155,7 +171,7 @@ impl Command for ViewSource { .into_pipeline_data()) } else { Err(ShellError::GenericError { - error: "Cannot view value".to_string(), + error: "Cannot view string module value".to_string(), msg: "the module does not have a viewable block".to_string(), span: Some(arg_span), help: None, @@ -164,7 +180,7 @@ impl Command for ViewSource { } } else { Err(ShellError::GenericError { - error: "Cannot view value".to_string(), + error: "Cannot view string value".to_string(), msg: "this name does not correspond to a viewable value".to_string(), span: Some(arg_span), help: None,