diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index 87ec535ed0..67198959ba 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -34,7 +34,9 @@ impl Highlighter for NuHighlighter { .to_string(); output.push((Style::new(), gap)); } - + // println!("line: \n{}", line); + // println!("shape: \n{:#?}\n", shape); + // println!("global_span_offset: \n{:#?}\n", global_span_offset); let next_token = line [(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)] .to_string(); diff --git a/crates/nu-command/src/core_commands/source.rs b/crates/nu-command/src/core_commands/source.rs index 76bcc461bb..c154566c22 100644 --- a/crates/nu-command/src/core_commands/source.rs +++ b/crates/nu-command/src/core_commands/source.rs @@ -1,5 +1,7 @@ +use std::borrow::Borrow; use std::path::Path; +use nu_engine::{eval_block, eval_expression}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EvaluationContext}; use nu_protocol::{ShellError, Signature, SyntaxShape, Value}; @@ -30,8 +32,9 @@ impl Command for Source { call: &Call, input: Value, ) -> Result { - Ok(Value::Nothing { span: call.head }) - // source(_context, call, input) + let block = &call.positional[0]; + + Ok(eval_expression(_context, block)?) } } diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index ffce2acc40..44dd96ae26 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -549,8 +549,9 @@ pub fn parse_source( if name == b"source" { if let Some(decl_id) = working_set.find_decl(b"source") { - let (call, call_span, _) = + let (call, call_span, err) = parse_internal_call(working_set, spans[0], &spans[1..], decl_id); + // println!("\nSpans: {:#?}", spans); // Command and one file name if spans.len() >= 2 { @@ -570,19 +571,44 @@ pub fn parse_source( &contents, false, ); + if let Some(_) = err { // Unsuccessful parse of file - // return ( - // Statement::Pipeline(Pipeline::from_vec(vec![Expression { - // expr: Expr::Call(call), - // span: call_span, - // ty: Type::Unknown, - // }])), - // None, - // ); + return ( + Statement::Pipeline(Pipeline::from_vec(vec![Expression { + expr: Expr::Call(call), + span: span(&spans[1..]), + ty: Type::Unknown, + custom_completion: None, + }])), + // Return the file parse error + err, + ); + } else { + // Save the block into the working set + let block_id = working_set.add_block(block); + // println!("CALL:{:?}", call); + + let mut call_with_block = call.clone(); + // println!("CALL_WITH_BLOCK: {:?}", call_with_block); + + call_with_block.positional.push(Expression { + expr: Expr::Block(block_id), + span: span(&spans[1..]), + ty: Type::Unknown, + custom_completion: None, + }); + + return ( + Statement::Pipeline(Pipeline::from_vec(vec![Expression { + expr: Expr::Call(call_with_block), + span: call_span, + ty: Type::Unknown, + custom_completion: None, + }])), + None, + ); } - } else { - // Source file couldn't be parsed correctly } } } diff --git a/example.nu b/example.nu index 6a1392318a..a4f40bac8a 100644 --- a/example.nu +++ b/example.nu @@ -1,5 +1,5 @@ -def greet [name] { - echo "hello" $name +def inc [x] { + $x + 1 } -greet "world" \ No newline at end of file +inc 5 \ No newline at end of file