mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 20:57:52 +02:00
Fixing captures (#723)
* WIP fixing captures * small fix * WIP * Rewrite to proof-of-concept better parse_def * Add missing file * Finish capture refactor * Fix tests * Add more tests
This commit is contained in:
9
crates/nu-protocol/src/engine/capture_block.rs
Normal file
9
crates/nu-protocol/src/engine/capture_block.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{BlockId, Value, VarId};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CaptureBlock {
|
||||
pub block_id: BlockId,
|
||||
pub captures: HashMap<VarId, Value>,
|
||||
}
|
@ -154,6 +154,7 @@ pub const SCOPE_VARIABLE_ID: usize = 1;
|
||||
pub const IN_VARIABLE_ID: usize = 2;
|
||||
pub const CONFIG_VARIABLE_ID: usize = 3;
|
||||
pub const ENV_VARIABLE_ID: usize = 4;
|
||||
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
|
||||
|
||||
impl EngineState {
|
||||
pub fn new() -> Self {
|
||||
|
@ -1,9 +1,11 @@
|
||||
mod call_info;
|
||||
mod capture_block;
|
||||
mod command;
|
||||
mod engine_state;
|
||||
mod stack;
|
||||
|
||||
pub use call_info::*;
|
||||
pub use capture_block::*;
|
||||
pub use command::*;
|
||||
pub use engine_state::*;
|
||||
pub use stack::*;
|
||||
|
@ -62,7 +62,10 @@ impl Stack {
|
||||
return Ok(v.clone());
|
||||
}
|
||||
|
||||
Err(ShellError::NushellFailed("variable not found".into()))
|
||||
Err(ShellError::NushellFailed(format!(
|
||||
"variable (var_id: {}) not found",
|
||||
var_id
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn add_var(&mut self, var_id: VarId, value: Value) {
|
||||
@ -80,7 +83,24 @@ impl Stack {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collect_captures(&self, captures: &[VarId]) -> Stack {
|
||||
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
|
||||
let mut output = Stack::new();
|
||||
|
||||
output.vars = captures.clone();
|
||||
|
||||
// FIXME: this is probably slow
|
||||
output.env_vars = self.env_vars.clone();
|
||||
output.env_vars.push(HashMap::new());
|
||||
|
||||
let config = self
|
||||
.get_var(CONFIG_VARIABLE_ID)
|
||||
.expect("internal error: config is missing");
|
||||
output.vars.insert(CONFIG_VARIABLE_ID, config);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn gather_captures(&self, captures: &[VarId]) -> Stack {
|
||||
let mut output = Stack::new();
|
||||
|
||||
for capture in captures {
|
||||
|
Reference in New Issue
Block a user