mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 05:38:19 +02:00
Span ID Refactor - Step 1 (#12960)
# Description First part of SpanID refactoring series. This PR adds a `SpanId` type and a corresponding `span_id` field to `Expression`. Parser creating expressions will now add them to an array in `StateWorkingSet`, generates a corresponding ID and saves the ID to the Expression. The IDs are not used anywhere yet. For the rough overall plan, see https://github.com/nushell/nushell/issues/12963. # User-Facing Changes Hopefully none. This is only a refactor of Nushell's internals that shouldn't have visible side effects. # Tests + Formatting # After Submitting
This commit is contained in:
@ -338,9 +338,13 @@ impl Call {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::engine::EngineState;
|
||||
|
||||
#[test]
|
||||
fn argument_span_named() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
let named = Spanned {
|
||||
item: "named".to_string(),
|
||||
span: Span::new(2, 3),
|
||||
@ -349,7 +353,7 @@ mod test {
|
||||
item: "short".to_string(),
|
||||
span: Span::new(5, 7),
|
||||
};
|
||||
let expr = Expression::garbage(Span::new(11, 13));
|
||||
let expr = Expression::garbage(&mut working_set, Span::new(11, 13));
|
||||
|
||||
let arg = Argument::Named((named.clone(), None, None));
|
||||
|
||||
@ -370,8 +374,11 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn argument_span_positional() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
let span = Span::new(2, 3);
|
||||
let expr = Expression::garbage(span);
|
||||
let expr = Expression::garbage(&mut working_set, span);
|
||||
let arg = Argument::Positional(expr);
|
||||
|
||||
assert_eq!(span, arg.span());
|
||||
@ -379,8 +386,11 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn argument_span_unknown() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
let span = Span::new(2, 3);
|
||||
let expr = Expression::garbage(span);
|
||||
let expr = Expression::garbage(&mut working_set, span);
|
||||
let arg = Argument::Unknown(expr);
|
||||
|
||||
assert_eq!(span, arg.span());
|
||||
@ -388,9 +398,12 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn call_arguments_span() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
let mut call = Call::new(Span::new(0, 1));
|
||||
call.add_positional(Expression::garbage(Span::new(2, 3)));
|
||||
call.add_positional(Expression::garbage(Span::new(5, 7)));
|
||||
call.add_positional(Expression::garbage(&mut working_set, Span::new(2, 3)));
|
||||
call.add_positional(Expression::garbage(&mut working_set, Span::new(5, 7)));
|
||||
|
||||
assert_eq!(Span::new(2, 7), call.arguments_span());
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
ast::{Argument, Block, Expr, ExternalArgument, ImportPattern, MatchPattern, RecordItem},
|
||||
engine::StateWorkingSet,
|
||||
BlockId, DeclId, Signature, Span, Type, VarId, IN_VARIABLE_ID,
|
||||
engine::{EngineState, StateWorkingSet},
|
||||
BlockId, DeclId, Signature, Span, SpanId, Type, VarId, IN_VARIABLE_ID,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
@ -10,15 +10,18 @@ use std::sync::Arc;
|
||||
pub struct Expression {
|
||||
pub expr: Expr,
|
||||
pub span: Span,
|
||||
pub span_id: SpanId,
|
||||
pub ty: Type,
|
||||
pub custom_completion: Option<DeclId>,
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn garbage(span: Span) -> Expression {
|
||||
pub fn garbage(working_set: &mut StateWorkingSet, span: Span) -> Expression {
|
||||
let span_id = working_set.add_span(span);
|
||||
Expression {
|
||||
expr: Expr::Garbage,
|
||||
span,
|
||||
span_id,
|
||||
ty: Type::Any,
|
||||
custom_completion: None,
|
||||
}
|
||||
@ -471,4 +474,49 @@ impl Expression {
|
||||
Expr::VarDecl(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(working_set: &mut StateWorkingSet, expr: Expr, span: Span, ty: Type) -> Expression {
|
||||
let span_id = working_set.add_span(span);
|
||||
Expression {
|
||||
expr,
|
||||
span,
|
||||
span_id,
|
||||
ty,
|
||||
custom_completion: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_existing(expr: Expr, span: Span, span_id: SpanId, ty: Type) -> Expression {
|
||||
Expression {
|
||||
expr,
|
||||
span,
|
||||
span_id,
|
||||
ty,
|
||||
custom_completion: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_unknown(expr: Expr, span: Span, ty: Type) -> Expression {
|
||||
Expression {
|
||||
expr,
|
||||
span,
|
||||
span_id: SpanId(0),
|
||||
ty,
|
||||
custom_completion: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_span_id(self, span_id: SpanId) -> Expression {
|
||||
Expression {
|
||||
expr: self.expr,
|
||||
span: self.span,
|
||||
span_id,
|
||||
ty: self.ty,
|
||||
custom_completion: self.custom_completion,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn span(&self, engine_state: &EngineState) -> Span {
|
||||
engine_state.get_span(self.span_id)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user