Add a simple scope variable

This commit is contained in:
JT
2021-11-02 16:08:05 +13:00
parent 3c1b3473ae
commit d856cebebd
8 changed files with 204 additions and 47 deletions

View File

@ -95,16 +95,6 @@ pub fn create_default_context() -> EngineState {
let sig = Signature::build("exit");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("vars");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("decls");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("blocks");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("stack");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("contents");
working_set.add_decl(sig.predeclare());
working_set.render()
};

View File

@ -371,12 +371,12 @@ pub fn eval_block(
}
pub fn eval_variable(
_engine_state: &EngineState,
engine_state: &EngineState,
stack: &Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
if var_id == 0 {
if var_id == nu_protocol::NU_VARIABLE_ID {
// $nu
let mut output_cols = vec![];
let mut output_vals = vec![];
@ -425,6 +425,71 @@ pub fn eval_variable(
output_vals.push(Value::String { val: cwd, span })
}
Ok(Value::Record {
cols: output_cols,
vals: output_vals,
span,
})
} else if var_id == nu_protocol::SCOPE_VARIABLE_ID {
let mut output_cols = vec![];
let mut output_vals = vec![];
let mut vars = vec![];
let mut commands = vec![];
let mut aliases = vec![];
let mut modules = vec![];
for frame in &engine_state.scope {
for var in &frame.vars {
vars.push(Value::String {
val: String::from_utf8_lossy(var.0).to_string(),
span,
});
}
for command in &frame.decls {
commands.push(Value::String {
val: String::from_utf8_lossy(command.0).to_string(),
span,
});
}
for alias in &frame.aliases {
aliases.push(Value::String {
val: String::from_utf8_lossy(alias.0).to_string(),
span,
});
}
for module in &frame.modules {
modules.push(Value::String {
val: String::from_utf8_lossy(module.0).to_string(),
span,
});
}
}
output_cols.push("vars".to_string());
output_vals.push(Value::List { vals: vars, span });
output_cols.push("commands".to_string());
output_vals.push(Value::List {
vals: commands,
span,
});
output_cols.push("aliases".to_string());
output_vals.push(Value::List {
vals: aliases,
span,
});
output_cols.push("modules".to_string());
output_vals.push(Value::List {
vals: modules,
span,
});
Ok(Value::Record {
cols: output_cols,
vals: output_vals,

View File

@ -1198,7 +1198,17 @@ pub fn parse_variable_expr(
} else if contents == b"$nu" {
return (
Expression {
expr: Expr::Var(0),
expr: Expr::Var(nu_protocol::NU_VARIABLE_ID),
span,
ty: Type::Unknown,
custom_completion: None,
},
None,
);
} else if contents == b"$scope" {
return (
Expression {
expr: Expr::Var(nu_protocol::SCOPE_VARIABLE_ID),
span,
ty: Type::Unknown,
custom_completion: None,

View File

@ -6,17 +6,6 @@ use std::{
sync::{atomic::AtomicBool, Arc},
};
#[derive(Clone)]
pub struct EngineState {
files: im::Vector<(String, usize, usize)>,
file_contents: im::Vector<(Vec<u8>, usize, usize)>,
vars: im::Vector<Type>,
decls: im::Vector<Box<dyn Command + 'static>>,
blocks: im::Vector<Block>,
pub scope: im::Vector<ScopeFrame>,
pub ctrlc: Option<Arc<AtomicBool>>,
}
// Tells whether a decl etc. is visible or not
// TODO: When adding new exportables (env vars, aliases, etc.), parametrize the ID type with generics
#[derive(Debug, Clone)]
@ -62,9 +51,9 @@ impl Visibility {
pub struct ScopeFrame {
pub vars: HashMap<Vec<u8>, VarId>,
predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
decls: HashMap<Vec<u8>, DeclId>,
aliases: HashMap<Vec<u8>, Vec<Span>>,
modules: HashMap<Vec<u8>, BlockId>,
pub decls: HashMap<Vec<u8>, DeclId>,
pub aliases: HashMap<Vec<u8>, Vec<Span>>,
pub modules: HashMap<Vec<u8>, BlockId>,
visibility: Visibility,
}
@ -91,18 +80,26 @@ impl Default for ScopeFrame {
}
}
impl Default for EngineState {
fn default() -> Self {
Self::new()
}
#[derive(Clone)]
pub struct EngineState {
files: im::Vector<(String, usize, usize)>,
file_contents: im::Vector<(Vec<u8>, usize, usize)>,
vars: im::Vector<Type>,
decls: im::Vector<Box<dyn Command + 'static>>,
blocks: im::Vector<Block>,
pub scope: im::Vector<ScopeFrame>,
pub ctrlc: Option<Arc<AtomicBool>>,
}
pub const NU_VARIABLE_ID: usize = 0;
pub const SCOPE_VARIABLE_ID: usize = 1;
impl EngineState {
pub fn new() -> Self {
Self {
files: im::vector![],
file_contents: im::vector![],
vars: im::vector![Type::Unknown],
vars: im::vector![Type::Unknown, Type::Unknown],
decls: im::vector![],
blocks: im::vector![],
scope: im::vector![ScopeFrame::new()],
@ -319,6 +316,12 @@ impl EngineState {
}
}
impl Default for EngineState {
fn default() -> Self {
Self::new()
}
}
pub struct StateWorkingSet<'a> {
pub permanent_state: &'a EngineState,
pub delta: StateDelta,

View File

@ -11,6 +11,7 @@ mod ty;
mod value;
pub use value::Value;
pub use engine::{NU_VARIABLE_ID, SCOPE_VARIABLE_ID};
pub use example::*;
pub use id::*;
pub use pipeline_data::*;