diff --git a/crates/nu-command/src/core_commands/mod.rs b/crates/nu-command/src/core_commands/mod.rs index 520a873b9..7999652aa 100644 --- a/crates/nu-command/src/core_commands/mod.rs +++ b/crates/nu-command/src/core_commands/mod.rs @@ -1,5 +1,4 @@ mod alias; -mod ast; mod break_; mod commandline; mod const_; @@ -39,7 +38,6 @@ mod version; mod while_; pub use alias::Alias; -pub use ast::Ast; pub use break_::Break; pub use commandline::Commandline; pub use const_::Const; diff --git a/crates/nu-command/src/core_commands/ast.rs b/crates/nu-command/src/debug/ast.rs similarity index 61% rename from crates/nu-command/src/core_commands/ast.rs rename to crates/nu-command/src/debug/ast.rs index 854ac1151..ac7d062c3 100644 --- a/crates/nu-command/src/core_commands/ast.rs +++ b/crates/nu-command/src/debug/ast.rs @@ -3,8 +3,8 @@ use nu_parser::parse; use nu_protocol::{ ast::Call, engine::{Command, EngineState, Stack, StateWorkingSet}, - Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, - Value, + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape, + Type, Value, }; #[derive(Clone)] @@ -21,13 +21,14 @@ impl Command for Ast { fn signature(&self) -> Signature { Signature::build("ast") - .input_output_types(vec![(Type::Nothing, Type::Nothing)]) + .input_output_types(vec![(Type::String, Type::Record(vec![]))]) .required( "pipeline", SyntaxShape::String, "the pipeline to print the ast for", ) - .category(Category::Core) + .allow_variants_without_examples(true) + .category(Category::Debug) } fn run( @@ -40,10 +41,22 @@ impl Command for Ast { let pipeline: Spanned = call.req(engine_state, stack, 0)?; let mut working_set = StateWorkingSet::new(engine_state); - let (output, err) = parse(&mut working_set, None, pipeline.item.as_bytes(), false, &[]); - eprintln!("output: {output:#?}\nerror: {err:#?}"); - - Ok(PipelineData::empty()) + let (block_output, error_output) = + parse(&mut working_set, None, pipeline.item.as_bytes(), false, &[]); + let block_value = Value::String { + val: format!("{block_output:#?}"), + span: pipeline.span, + }; + let error_value = Value::String { + val: format!("{error_output:#?}"), + span: pipeline.span, + }; + let output_record = Value::Record { + cols: vec!["block".to_string(), "error".to_string()], + vals: vec![block_value, error_value], + span: pipeline.span, + }; + Ok(output_record.into_pipeline_data()) } fn examples(&self) -> Vec { @@ -51,17 +64,17 @@ impl Command for Ast { Example { description: "Print the ast of a string", example: "ast 'hello'", - result: Some(Value::nothing(Span::test_data())), + result: None, }, Example { description: "Print the ast of a pipeline", example: "ast 'ls | where name =~ README'", - result: Some(Value::nothing(Span::test_data())), + result: None, }, Example { description: "Print the ast of a pipeline with an error", example: "ast 'for x in 1..10 { echo $x '", - result: Some(Value::nothing(Span::test_data())), + result: None, }, ] } diff --git a/crates/nu-command/src/debug/mod.rs b/crates/nu-command/src/debug/mod.rs index d97cfe248..c92786170 100644 --- a/crates/nu-command/src/debug/mod.rs +++ b/crates/nu-command/src/debug/mod.rs @@ -1,3 +1,4 @@ +mod ast; mod debug_; mod explain; mod inspect; @@ -10,6 +11,7 @@ mod view_files; mod view_source; mod view_span; +pub use ast::Ast; pub use debug_::Debug; pub use explain::Explain; pub use inspect::Inspect; diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 545d5552d..99583e05f 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -29,7 +29,6 @@ pub fn create_default_context() -> EngineState { // Core bind_command! { Alias, - Ast, Break, Commandline, Const, @@ -178,6 +177,7 @@ pub fn create_default_context() -> EngineState { // Debug bind_command! { + Ast, Debug, Explain, Inspect,