mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
Adapt tk's work for a source command
This commit is contained in:
parent
d481d5ca96
commit
7eb022b58c
4 changed files with 18 additions and 101 deletions
|
@ -34,9 +34,6 @@ impl Highlighter for NuHighlighter {
|
||||||
.to_string();
|
.to_string();
|
||||||
output.push((Style::new(), gap));
|
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
|
let next_token = line
|
||||||
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use std::borrow::Borrow;
|
use nu_engine::{eval_block, CallExt};
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use nu_engine::{eval_block, eval_expression};
|
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EvaluationContext};
|
use nu_protocol::engine::{Command, EvaluationContext};
|
||||||
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
|
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
|
||||||
|
@ -28,92 +25,19 @@ impl Command for Source {
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
&self,
|
&self,
|
||||||
_context: &EvaluationContext,
|
context: &EvaluationContext,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: Value,
|
input: Value,
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
let block = &call.positional[0];
|
// Note: this hidden positional is the block_id that corresponded to the 0th position
|
||||||
|
// it is put here by the parser
|
||||||
|
let block_id: i64 = call.req(context, 1)?;
|
||||||
|
|
||||||
Ok(eval_expression(_context, block)?)
|
let block = context
|
||||||
|
.engine_state
|
||||||
|
.borrow()
|
||||||
|
.get_block(block_id as usize)
|
||||||
|
.clone();
|
||||||
|
eval_block(context, &block, input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn source(ctx: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
|
|
||||||
// let filename = call.positional[0]
|
|
||||||
// .as_string()
|
|
||||||
// .expect("internal error: missing file name");
|
|
||||||
|
|
||||||
// let source_file = Path::new(&filename);
|
|
||||||
|
|
||||||
// // This code is in the current Nushell version
|
|
||||||
// // ...Not entirely sure what it's doing or if there's an equivalent in engine-q
|
|
||||||
|
|
||||||
// // Note: this is a special case for setting the context from a command
|
|
||||||
// // In this case, if we don't set it now, we'll lose the scope that this
|
|
||||||
// // variable should be set into.
|
|
||||||
|
|
||||||
// // let lib_dirs = &ctx
|
|
||||||
// // .configs()
|
|
||||||
// // .lock()
|
|
||||||
// // .global_config
|
|
||||||
// // .as_ref()
|
|
||||||
// // .map(|configuration| match configuration.var("lib_dirs") {
|
|
||||||
// // Some(paths) => paths
|
|
||||||
// // .table_entries()
|
|
||||||
// // .cloned()
|
|
||||||
// // .map(|path| path.as_string())
|
|
||||||
// // .collect(),
|
|
||||||
// // None => vec![],
|
|
||||||
// // });
|
|
||||||
|
|
||||||
// // if let Some(dir) = lib_dirs {
|
|
||||||
// // for lib_path in dir {
|
|
||||||
// // match lib_path {
|
|
||||||
// // Ok(name) => {
|
|
||||||
// // let path = PathBuf::from(name).join(source_file);
|
|
||||||
|
|
||||||
// // if let Ok(contents) =
|
|
||||||
// // std::fs::read_to_string(&expand_path(Cow::Borrowed(path.as_path())))
|
|
||||||
// // {
|
|
||||||
// // let result = script::run_script_standalone(contents, true, ctx, false);
|
|
||||||
|
|
||||||
// // if let Err(err) = result {
|
|
||||||
// // ctx.error(err);
|
|
||||||
// // }
|
|
||||||
// // return Ok(OutputStream::empty());
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // Err(reason) => {
|
|
||||||
// // ctx.error(reason.clone());
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// // This is to stay consistent w/ the code taken from nushell
|
|
||||||
// let path = source_file;
|
|
||||||
|
|
||||||
// let contents = std::fs::read(path);
|
|
||||||
|
|
||||||
// match contents {
|
|
||||||
// Ok(contents) => {
|
|
||||||
// let engine_state = EngineState::new();
|
|
||||||
// let mut working_set = StateWorkingSet::new(&engine_state);
|
|
||||||
|
|
||||||
// let (block, err) = parse(&mut working_set, None, &contents, true);
|
|
||||||
// if let Some(e) = err {
|
|
||||||
// // Be more specific here: need to convert parse error to string
|
|
||||||
// Err(e.into())
|
|
||||||
// } else {
|
|
||||||
// let result = eval_block(ctx, &block, input);
|
|
||||||
// match result {
|
|
||||||
// Err(e) => Err(e),
|
|
||||||
// _ => Ok(Value::nothing()),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// Err(_) => Err(ShellError::InternalError(
|
|
||||||
// "Can't load file to source".to_string(),
|
|
||||||
// )),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use nu_parser::parse;
|
|
||||||
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
||||||
use nu_protocol::engine::EvaluationContext;
|
use nu_protocol::engine::EvaluationContext;
|
||||||
use nu_protocol::{Range, ShellError, Span, Type, Unit, Value};
|
use nu_protocol::{Range, ShellError, Span, Type, Unit, Value};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::{Block, Call, Expr, Expression, ImportPatternMember, Pipeline, Statement},
|
ast::{Block, Call, Expr, Expression, ImportPatternMember, Pipeline, Statement},
|
||||||
engine::StateWorkingSet,
|
engine::StateWorkingSet,
|
||||||
span, DeclId, ShellError, Span, SyntaxShape, Type,
|
span, DeclId, Span, SyntaxShape, Type,
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
@ -779,7 +779,6 @@ pub fn parse_source(
|
||||||
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
|
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
|
||||||
let (call, call_span, err) =
|
let (call, call_span, err) =
|
||||||
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
||||||
// println!("\nSpans: {:#?}", spans);
|
|
||||||
|
|
||||||
// Command and one file name
|
// Command and one file name
|
||||||
if spans.len() >= 2 {
|
if spans.len() >= 2 {
|
||||||
|
@ -800,7 +799,7 @@ pub fn parse_source(
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(_) = err {
|
if err.is_some() {
|
||||||
// Unsuccessful parse of file
|
// Unsuccessful parse of file
|
||||||
return (
|
return (
|
||||||
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
||||||
|
@ -815,16 +814,14 @@ pub fn parse_source(
|
||||||
} else {
|
} else {
|
||||||
// Save the block into the working set
|
// Save the block into the working set
|
||||||
let block_id = working_set.add_block(block);
|
let block_id = working_set.add_block(block);
|
||||||
// println!("CALL:{:?}", call);
|
|
||||||
|
|
||||||
let mut call_with_block = call.clone();
|
let mut call_with_block = call;
|
||||||
// println!("CALL_WITH_BLOCK: {:?}", call_with_block);
|
|
||||||
|
|
||||||
// Adding this expression to the positional creates a syntax highlighting error
|
// Adding this expression to the positional creates a syntax highlighting error
|
||||||
// after writing `source example.nu`
|
// after writing `source example.nu`
|
||||||
call_with_block.positional.push(Expression {
|
call_with_block.positional.push(Expression {
|
||||||
expr: Expr::Block(block_id),
|
expr: Expr::Int(block_id as i64),
|
||||||
span: span(&spans[1..]),
|
span: spans[1],
|
||||||
ty: Type::Unknown,
|
ty: Type::Unknown,
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
});
|
});
|
||||||
|
@ -849,14 +846,14 @@ pub fn parse_source(
|
||||||
ty: Type::Unknown,
|
ty: Type::Unknown,
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
}])),
|
}])),
|
||||||
None,
|
err,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
garbage_statement(spans),
|
garbage_statement(spans),
|
||||||
Some(ParseError::UnknownState(
|
Some(ParseError::UnknownState(
|
||||||
"internal error: let statement unparseable".into(),
|
"internal error: source statement unparseable".into(),
|
||||||
span(spans),
|
span(spans),
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue