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:
JT
2022-01-12 15:06:56 +11:00
committed by GitHub
parent 47495715a6
commit 186da4d725
30 changed files with 424 additions and 164 deletions

View File

@@ -6,6 +6,7 @@ use std::str::FromStr;
use chrono::{DateTime, FixedOffset};
// use nu_path::expand_path;
use crate::ast::{CellPath, PathMember};
use crate::engine::CaptureBlock;
use crate::ShellError;
use crate::{Range, Spanned, Value};
@@ -351,3 +352,42 @@ impl FromValue for Vec<Value> {
}
}
}
impl FromValue for CaptureBlock {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::Block { val, captures, .. } => Ok(CaptureBlock {
block_id: *val,
captures: captures.clone(),
}),
v => Err(ShellError::CantConvert(
"Block".into(),
v.get_type().to_string(),
v.span()?,
)),
}
}
}
impl FromValue for Spanned<CaptureBlock> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::Block {
val,
captures,
span,
} => Ok(Spanned {
item: CaptureBlock {
block_id: *val,
captures: captures.clone(),
},
span: *span,
}),
v => Err(ShellError::CantConvert(
"Block".into(),
v.get_type().to_string(),
v.span()?,
)),
}
}
}

View File

@@ -22,7 +22,7 @@ use std::path::PathBuf;
use std::{cmp::Ordering, fmt::Debug};
use crate::ast::{CellPath, PathMember};
use crate::{did_you_mean, span, BlockId, Config, Span, Spanned, Type};
use crate::{did_you_mean, span, BlockId, Config, Span, Spanned, Type, VarId};
use crate::ast::Operator;
pub use custom_value::CustomValue;
@@ -75,6 +75,7 @@ pub enum Value {
},
Block {
val: BlockId,
captures: HashMap<VarId, Value>,
span: Span,
},
Nothing {
@@ -141,8 +142,13 @@ impl Clone for Value {
vals: vals.clone(),
span: *span,
},
Value::Block { val, span } => Value::Block {
Value::Block {
val,
captures,
span,
} => Value::Block {
val: *val,
captures: captures.clone(),
span: *span,
},
Value::Nothing { span } => Value::Nothing { span: *span },