nushell/crates/nu-protocol/src/engine/engine_state.rs

812 lines
24 KiB
Rust
Raw Normal View History

2021-09-02 20:21:37 +02:00
use super::Command;
use crate::{ast::Block, BlockId, DeclId, Span, Type, VarId};
2021-07-23 23:19:30 +02:00
use core::panic;
use std::{collections::HashMap, slice::Iter};
2021-06-30 03:42:56 +02:00
2021-09-02 10:25:22 +02:00
pub struct EngineState {
files: Vec<(String, usize, usize)>,
file_contents: Vec<u8>,
2021-07-01 08:09:55 +02:00
vars: Vec<Type>,
2021-09-02 10:25:22 +02:00
decls: Vec<Box<dyn Command>>,
2021-07-16 22:26:40 +02:00
blocks: Vec<Block>,
2021-08-09 09:53:06 +02:00
scope: Vec<ScopeFrame>,
2021-06-30 03:42:56 +02:00
}
2021-07-02 00:40:08 +02:00
#[derive(Debug)]
2021-09-06 04:20:02 +02:00
pub struct ScopeFrame {
2021-07-01 02:01:04 +02:00
vars: HashMap<Vec<u8>, VarId>,
2021-07-01 08:09:55 +02:00
decls: HashMap<Vec<u8>, DeclId>,
2021-08-09 02:19:07 +02:00
aliases: HashMap<Vec<u8>, Vec<Span>>,
modules: HashMap<Vec<u8>, BlockId>,
hiding: HashMap<Vec<u8>, usize>, // defines what is being hidden and its "hiding strength"
2021-06-30 03:42:56 +02:00
}
impl ScopeFrame {
pub fn new() -> Self {
Self {
vars: HashMap::new(),
2021-07-01 08:09:55 +02:00
decls: HashMap::new(),
2021-08-09 02:19:07 +02:00
aliases: HashMap::new(),
modules: HashMap::new(),
hiding: HashMap::new(),
2021-06-30 03:42:56 +02:00
}
}
2021-09-06 04:20:02 +02:00
pub fn get_var(&self, var_name: &[u8]) -> Option<&VarId> {
self.vars.get(var_name)
}
}
impl Default for ScopeFrame {
fn default() -> Self {
Self::new()
}
2021-06-30 03:42:56 +02:00
}
2021-09-02 10:25:22 +02:00
impl Default for EngineState {
2021-06-30 03:42:56 +02:00
fn default() -> Self {
Self::new()
}
}
2021-09-02 10:25:22 +02:00
impl EngineState {
2021-06-30 03:42:56 +02:00
pub fn new() -> Self {
2021-07-01 08:09:55 +02:00
Self {
files: vec![],
file_contents: vec![],
2021-07-01 08:09:55 +02:00
vars: vec![],
decls: vec![],
blocks: vec![],
2021-07-17 08:31:34 +02:00
scope: vec![ScopeFrame::new()],
2021-07-01 08:09:55 +02:00
}
2021-06-30 03:42:56 +02:00
}
2021-09-02 10:25:22 +02:00
pub fn merge_delta(this: &mut EngineState, mut delta: StateDelta) {
2021-06-30 03:42:56 +02:00
// Take the mutable reference and extend the permanent state from the working set
this.files.extend(delta.files);
this.file_contents.extend(delta.file_contents);
this.decls.extend(delta.decls);
this.vars.extend(delta.vars);
this.blocks.extend(delta.blocks);
if let Some(last) = this.scope.last_mut() {
let first = delta.scope.remove(0);
for item in first.decls.into_iter() {
last.decls.insert(item.0, item.1);
}
for item in first.vars.into_iter() {
last.vars.insert(item.0, item.1);
2021-07-17 08:31:34 +02:00
}
2021-08-09 09:53:06 +02:00
for item in first.aliases.into_iter() {
last.aliases.insert(item.0, item.1);
}
2021-09-26 12:25:52 +02:00
for item in first.modules.into_iter() {
last.modules.insert(item.0, item.1);
}
for item in first.hiding.into_iter() {
last.hiding.insert(item.0, item.1);
}
2021-06-30 03:42:56 +02:00
}
}
pub fn num_files(&self) -> usize {
self.files.len()
}
2021-07-01 08:09:55 +02:00
pub fn num_vars(&self) -> usize {
self.vars.len()
}
pub fn num_decls(&self) -> usize {
self.decls.len()
}
2021-07-16 08:24:46 +02:00
pub fn num_blocks(&self) -> usize {
self.blocks.len()
}
2021-07-23 23:19:30 +02:00
pub fn print_vars(&self) {
for var in self.vars.iter().enumerate() {
println!("var{}: {:?}", var.0, var.1);
}
}
pub fn print_decls(&self) {
for decl in self.decls.iter().enumerate() {
2021-09-02 10:25:22 +02:00
println!("decl{}: {:?}", decl.0, decl.1.signature());
2021-07-23 23:19:30 +02:00
}
}
pub fn print_blocks(&self) {
for block in self.blocks.iter().enumerate() {
println!("block{}: {:?}", block.0, block.1);
}
}
2021-09-25 18:28:15 +02:00
pub fn print_contents(&self) {
let string = String::from_utf8_lossy(&self.file_contents);
println!("{}", string);
}
2021-07-23 23:46:55 +02:00
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
let mut hiding_strength = 0;
// println!("state: starting finding {}", String::from_utf8_lossy(&name));
2021-07-23 23:46:55 +02:00
for scope in self.scope.iter().rev() {
// println!("hiding map: {:?}", scope.hiding);
// check if we're hiding the declin this scope
if let Some(strength) = scope.hiding.get(name) {
hiding_strength += strength;
}
2021-07-23 23:46:55 +02:00
if let Some(decl_id) = scope.decls.get(name) {
// if we're hiding this decl, do not return it and reduce the hiding strength
if hiding_strength > 0 {
hiding_strength -= 1;
} else {
return Some(*decl_id);
}
2021-07-23 23:46:55 +02:00
}
}
None
}
2021-09-10 00:09:40 +02:00
pub fn find_commands_by_prefix(&self, name: &[u8]) -> Vec<Vec<u8>> {
let mut output = vec![];
for scope in self.scope.iter().rev() {
for decl in &scope.decls {
if decl.0.starts_with(name) {
output.push(decl.0.clone());
}
}
}
output
}
2021-09-12 17:34:43 +02:00
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
2021-09-10 00:09:40 +02:00
&self.file_contents[span.start..span.end]
}
2021-07-23 07:14:49 +02:00
pub fn get_var(&self, var_id: VarId) -> &Type {
self.vars
.get(var_id)
.expect("internal error: missing variable")
2021-07-01 08:09:55 +02:00
}
2021-09-04 09:59:38 +02:00
#[allow(clippy::borrowed_box)]
2021-09-02 10:25:22 +02:00
pub fn get_decl(&self, decl_id: DeclId) -> &Box<dyn Command> {
2021-07-23 07:14:49 +02:00
self.decls
.get(decl_id)
.expect("internal error: missing declaration")
2021-07-01 08:09:55 +02:00
}
2021-07-23 07:14:49 +02:00
pub fn get_block(&self, block_id: BlockId) -> &Block {
self.blocks
.get(block_id)
.expect("internal error: missing block")
2021-07-22 21:50:59 +02:00
}
pub fn next_span_start(&self) -> usize {
self.file_contents.len()
}
pub fn files(&self) -> Iter<(String, usize, usize)> {
self.files.iter()
}
pub fn get_filename(&self, file_id: usize) -> String {
for file in self.files.iter().enumerate() {
if file.0 == file_id {
return file.1 .0.clone();
}
}
"<unknown>".into()
}
pub fn get_file_source(&self, file_id: usize) -> String {
for file in self.files.iter().enumerate() {
if file.0 == file_id {
let output =
String::from_utf8_lossy(&self.file_contents[file.1 .1..file.1 .2]).to_string();
return output;
}
}
"<unknown>".into()
}
2021-07-02 00:54:04 +02:00
#[allow(unused)]
2021-06-30 03:42:56 +02:00
pub(crate) fn add_file(&mut self, filename: String, contents: Vec<u8>) -> usize {
let next_span_start = self.next_span_start();
2021-06-30 03:42:56 +02:00
self.file_contents.extend(&contents);
let next_span_end = self.next_span_start();
self.files.push((filename, next_span_start, next_span_end));
self.num_files() - 1
}
2021-06-30 03:42:56 +02:00
}
2021-09-02 10:25:22 +02:00
pub struct StateWorkingSet<'a> {
pub permanent_state: &'a EngineState,
pub delta: StateDelta,
}
2021-09-02 10:25:22 +02:00
pub struct StateDelta {
files: Vec<(String, usize, usize)>,
pub(crate) file_contents: Vec<u8>,
2021-09-02 10:25:22 +02:00
vars: Vec<Type>, // indexed by VarId
decls: Vec<Box<dyn Command>>, // indexed by DeclId
blocks: Vec<Block>, // indexed by BlockId
2021-09-06 04:20:02 +02:00
pub scope: Vec<ScopeFrame>,
2021-07-01 08:09:55 +02:00
}
2021-09-02 10:25:22 +02:00
impl StateDelta {
pub fn num_files(&self) -> usize {
self.files.len()
}
pub fn num_decls(&self) -> usize {
self.decls.len()
}
pub fn num_blocks(&self) -> usize {
self.blocks.len()
}
pub fn enter_scope(&mut self) {
// println!("enter scope");
self.scope.push(ScopeFrame::new());
}
pub fn exit_scope(&mut self) {
// println!("exit scope");
self.scope.pop();
}
}
2021-09-02 10:25:22 +02:00
impl<'a> StateWorkingSet<'a> {
pub fn new(permanent_state: &'a EngineState) -> Self {
2021-06-30 03:42:56 +02:00
Self {
2021-09-02 10:25:22 +02:00
delta: StateDelta {
files: vec![],
file_contents: vec![],
vars: vec![],
decls: vec![],
blocks: vec![],
scope: vec![ScopeFrame::new()],
},
2021-06-30 03:42:56 +02:00
permanent_state,
}
}
pub fn num_files(&self) -> usize {
self.delta.num_files() + self.permanent_state.num_files()
2021-06-30 03:42:56 +02:00
}
2021-07-16 08:24:46 +02:00
pub fn num_decls(&self) -> usize {
self.delta.num_decls() + self.permanent_state.num_decls()
2021-07-16 08:24:46 +02:00
}
pub fn num_blocks(&self) -> usize {
self.delta.num_blocks() + self.permanent_state.num_blocks()
2021-07-16 08:24:46 +02:00
}
2021-09-02 10:25:22 +02:00
pub fn add_decl(&mut self, decl: Box<dyn Command>) -> DeclId {
let name = decl.name().as_bytes().to_vec();
// println!("adding {}", String::from_utf8_lossy(&name));
2021-07-16 08:24:46 +02:00
self.delta.decls.push(decl);
2021-07-16 08:24:46 +02:00
let decl_id = self.num_decls() - 1;
2021-07-02 00:40:08 +02:00
let scope_frame = self
.delta
2021-07-02 00:40:08 +02:00
.scope
.last_mut()
.expect("internal error: missing required scope frame");
// reset "hiding strength" to 0 => not hidden
if let Some(strength) = scope_frame.hiding.get_mut(&name) {
*strength = 0;
// println!(" strength: {}", strength);
}
2021-07-02 00:40:08 +02:00
scope_frame.decls.insert(name, decl_id);
decl_id
}
pub fn hide_decl(&mut self, name: Vec<u8>) {
let scope_frame = self
.delta
.scope
.last_mut()
.expect("internal error: missing required scope frame");
if let Some(strength) = scope_frame.hiding.get_mut(&name) {
*strength += 1;
// println!("hiding {}, strength: {}", String::from_utf8_lossy(&name), strength);
} else {
// println!("hiding {}, strength: 1", String::from_utf8_lossy(&name));
scope_frame.hiding.insert(name, 1);
}
// println!("hiding map: {:?}", scope_frame.hiding);
}
2021-07-16 22:26:40 +02:00
pub fn add_block(&mut self, block: Block) -> BlockId {
self.delta.blocks.push(block);
2021-07-16 08:24:46 +02:00
self.num_blocks() - 1
}
pub fn add_module(&mut self, name: &str, block: Block) -> BlockId {
let name = name.as_bytes().to_vec();
self.delta.blocks.push(block);
let block_id = self.num_blocks() - 1;
let scope_frame = self
.delta
.scope
.last_mut()
.expect("internal error: missing required scope frame");
scope_frame.modules.insert(name, block_id);
block_id
}
pub fn activate_overlay(&mut self, overlay: Vec<(Vec<u8>, DeclId)>) {
// TODO: This will overwrite all existing definitions in a scope. When we add deactivate,
// we need to re-think how make it recoverable.
let scope_frame = self
.delta
.scope
.last_mut()
.expect("internal error: missing required scope frame");
for (name, decl_id) in overlay {
scope_frame.decls.insert(name, decl_id);
}
}
pub fn next_span_start(&self) -> usize {
self.permanent_state.next_span_start() + self.delta.file_contents.len()
}
2021-07-22 22:45:23 +02:00
pub fn global_span_offset(&self) -> usize {
self.permanent_state.next_span_start()
}
pub fn files(&'a self) -> impl Iterator<Item = &(String, usize, usize)> {
self.permanent_state.files().chain(self.delta.files.iter())
}
pub fn get_filename(&self, file_id: usize) -> String {
for file in self.files().enumerate() {
if file.0 == file_id {
return file.1 .0.clone();
}
}
"<unknown>".into()
}
pub fn get_file_source(&self, file_id: usize) -> String {
for file in self.files().enumerate() {
if file.0 == file_id {
let output = String::from_utf8_lossy(self.get_span_contents(Span {
start: file.1 .1,
end: file.1 .2,
}))
.to_string();
return output;
}
}
"<unknown>".into()
}
pub fn add_file(&mut self, filename: String, contents: &[u8]) -> usize {
let next_span_start = self.next_span_start();
self.delta.file_contents.extend(contents);
let next_span_end = self.next_span_start();
self.delta
.files
.push((filename, next_span_start, next_span_end));
2021-06-30 03:42:56 +02:00
self.num_files() - 1
}
pub fn get_span_contents(&self, span: Span) -> &[u8] {
let permanent_end = self.permanent_state.next_span_start();
if permanent_end <= span.start {
&self.delta.file_contents[(span.start - permanent_end)..(span.end - permanent_end)]
2021-07-02 09:15:30 +02:00
} else {
&self.permanent_state.file_contents[span.start..span.end]
2021-07-02 09:15:30 +02:00
}
}
2021-06-30 03:42:56 +02:00
pub fn enter_scope(&mut self) {
self.delta.enter_scope();
2021-06-30 03:42:56 +02:00
}
pub fn exit_scope(&mut self) {
self.delta.exit_scope();
2021-06-30 03:42:56 +02:00
}
2021-07-01 08:09:55 +02:00
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
let mut hiding_strength = 0;
// println!("set: starting finding {}", String::from_utf8_lossy(&name));
for scope in self.delta.scope.iter().rev() {
// println!("delta frame");
// println!("hiding map: {:?}", scope.hiding);
// check if we're hiding the declin this scope
if let Some(strength) = scope.hiding.get(name) {
hiding_strength += strength;
// println!(" was hiding, strength {}", hiding_strength);
}
2021-07-08 00:55:46 +02:00
if let Some(decl_id) = scope.decls.get(name) {
// if we're hiding this decl, do not return it and reduce the hiding strength
if hiding_strength > 0 {
hiding_strength -= 1;
// println!(" decl found, strength {}", hiding_strength);
} else {
// println!(" decl found, return");
return Some(*decl_id);
}
2021-07-01 08:09:55 +02:00
}
}
for scope in self.permanent_state.scope.iter().rev() {
// println!("perma frame");
// println!("hiding map: {:?}", scope.hiding);
// check if we're hiding the declin this scope
if let Some(strength) = scope.hiding.get(name) {
hiding_strength += strength;
// println!(" was hiding, strength {}", hiding_strength);
}
if let Some(decl_id) = scope.decls.get(name) {
// if we're hiding this decl, do not return it and reduce the hiding strength
if hiding_strength > 0 {
hiding_strength -= 1;
// println!(" decl found, strength {}", hiding_strength);
} else {
// println!(" decl found, return");
return Some(*decl_id);
}
2021-07-17 08:31:34 +02:00
}
}
2021-07-01 08:09:55 +02:00
None
}
2021-09-26 12:25:52 +02:00
pub fn find_module(&self, name: &[u8]) -> Option<BlockId> {
for scope in self.delta.scope.iter().rev() {
if let Some(block_id) = scope.modules.get(name) {
return Some(*block_id);
}
}
for scope in self.permanent_state.scope.iter().rev() {
if let Some(block_id) = scope.modules.get(name) {
return Some(*block_id);
}
}
None
}
2021-09-02 10:25:22 +02:00
// pub fn update_decl(&mut self, decl_id: usize, block: Option<BlockId>) {
// let decl = self.get_decl_mut(decl_id);
// decl.body = block;
// }
2021-07-31 06:04:42 +02:00
2021-07-17 21:34:43 +02:00
pub fn contains_decl_partial_match(&self, name: &[u8]) -> bool {
for scope in self.delta.scope.iter().rev() {
2021-07-17 21:34:43 +02:00
for decl in &scope.decls {
if decl.0.starts_with(name) {
return true;
}
}
}
for scope in self.permanent_state.scope.iter().rev() {
for decl in &scope.decls {
if decl.0.starts_with(name) {
return true;
2021-07-17 21:34:43 +02:00
}
}
}
false
}
2021-07-01 08:09:55 +02:00
pub fn next_var_id(&self) -> VarId {
let num_permanent_vars = self.permanent_state.num_vars();
num_permanent_vars + self.delta.vars.len()
2021-07-01 08:09:55 +02:00
}
pub fn find_variable(&self, name: &[u8]) -> Option<VarId> {
for scope in self.delta.scope.iter().rev() {
2021-07-08 00:55:46 +02:00
if let Some(var_id) = scope.vars.get(name) {
2021-07-01 08:09:55 +02:00
return Some(*var_id);
2021-06-30 03:42:56 +02:00
}
}
for scope in self.permanent_state.scope.iter().rev() {
if let Some(var_id) = scope.vars.get(name) {
return Some(*var_id);
2021-07-17 08:31:34 +02:00
}
}
2021-06-30 03:42:56 +02:00
None
}
2021-08-09 02:19:07 +02:00
pub fn find_alias(&self, name: &[u8]) -> Option<&[Span]> {
for scope in self.delta.scope.iter().rev() {
if let Some(spans) = scope.aliases.get(name) {
return Some(spans);
}
}
for scope in self.permanent_state.scope.iter().rev() {
if let Some(spans) = scope.aliases.get(name) {
return Some(spans);
}
}
None
}
2021-07-30 00:56:51 +02:00
pub fn add_variable(&mut self, mut name: Vec<u8>, ty: Type) -> VarId {
2021-07-01 08:09:55 +02:00
let next_id = self.next_var_id();
2021-07-30 00:56:51 +02:00
// correct name if necessary
if !name.starts_with(b"$") {
name.insert(0, b'$');
}
2021-07-01 02:01:04 +02:00
let last = self
.delta
2021-07-01 02:01:04 +02:00
.scope
.last_mut()
.expect("internal error: missing stack frame");
last.vars.insert(name, next_id);
2021-07-23 23:19:30 +02:00
self.delta.vars.push(ty);
2021-07-01 02:01:04 +02:00
next_id
}
2021-07-01 08:09:55 +02:00
2021-08-09 02:19:07 +02:00
pub fn add_alias(&mut self, name: Vec<u8>, replacement: Vec<Span>) {
let last = self
.delta
.scope
.last_mut()
.expect("internal error: missing stack frame");
last.aliases.insert(name, replacement);
}
2021-07-23 23:19:30 +02:00
pub fn set_variable_type(&mut self, var_id: VarId, ty: Type) {
let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars {
panic!("Internal error: attempted to set into permanent state from working set")
} else {
self.delta.vars[var_id - num_permanent_vars] = ty;
}
}
2021-07-23 07:14:49 +02:00
pub fn get_variable(&self, var_id: VarId) -> &Type {
let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars {
2021-07-22 21:50:59 +02:00
self.permanent_state.get_var(var_id)
2021-07-01 08:09:55 +02:00
} else {
2021-07-23 07:14:49 +02:00
self.delta
.vars
.get(var_id - num_permanent_vars)
.expect("internal error: missing variable")
2021-07-01 08:09:55 +02:00
}
}
2021-09-04 09:59:38 +02:00
#[allow(clippy::borrowed_box)]
2021-09-02 10:25:22 +02:00
pub fn get_decl(&self, decl_id: DeclId) -> &Box<dyn Command> {
let num_permanent_decls = self.permanent_state.num_decls();
if decl_id < num_permanent_decls {
2021-07-22 21:50:59 +02:00
self.permanent_state.get_decl(decl_id)
} else {
2021-07-23 07:14:49 +02:00
self.delta
.decls
.get(decl_id - num_permanent_decls)
.expect("internal error: missing declaration")
2021-07-22 21:50:59 +02:00
}
}
2021-09-02 10:25:22 +02:00
pub fn get_decl_mut(&mut self, decl_id: DeclId) -> &mut Box<dyn Command> {
2021-07-31 06:04:42 +02:00
let num_permanent_decls = self.permanent_state.num_decls();
if decl_id < num_permanent_decls {
panic!("internal error: can only mutate declarations in working set")
} else {
self.delta
.decls
.get_mut(decl_id - num_permanent_decls)
.expect("internal error: missing declaration")
}
}
2021-09-10 00:09:40 +02:00
pub fn find_commands_by_prefix(&self, name: &[u8]) -> Vec<Vec<u8>> {
let mut output = vec![];
for scope in self.delta.scope.iter().rev() {
for decl in &scope.decls {
if decl.0.starts_with(name) {
output.push(decl.0.clone());
}
}
}
let mut permanent = self.permanent_state.find_commands_by_prefix(name);
output.append(&mut permanent);
output
}
2021-07-23 07:14:49 +02:00
pub fn get_block(&self, block_id: BlockId) -> &Block {
2021-07-22 21:50:59 +02:00
let num_permanent_blocks = self.permanent_state.num_blocks();
if block_id < num_permanent_blocks {
self.permanent_state.get_block(block_id)
2021-07-01 08:09:55 +02:00
} else {
2021-07-23 07:14:49 +02:00
self.delta
.blocks
.get(block_id - num_permanent_blocks)
.expect("internal error: missing block")
2021-07-01 08:09:55 +02:00
}
}
2021-07-22 09:48:45 +02:00
2021-09-02 10:25:22 +02:00
pub fn render(self) -> StateDelta {
2021-07-22 09:48:45 +02:00
self.delta
}
2021-07-01 02:01:04 +02:00
}
2021-06-30 03:42:56 +02:00
impl<'a> miette::SourceCode for &StateWorkingSet<'a> {
fn read_span<'b>(
&'b self,
span: &miette::SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn miette::SpanContents + 'b>, miette::MietteError> {
let debugging = std::env::var("MIETTE_DEBUG").is_ok();
if debugging {
let finding_span = "Finding span in StateWorkingSet";
dbg!(finding_span, span);
}
for (filename, start, end) in self.files() {
if debugging {
dbg!(&filename, start, end);
}
if span.offset() >= *start && span.offset() + span.len() <= *end {
if debugging {
let found_file = "Found matching file";
dbg!(found_file);
}
let our_span = Span {
start: *start,
end: *end,
};
// We need to move to a local span because we're only reading
// the specific file contents via self.get_span_contents.
let local_span = (span.offset() - *start, span.len()).into();
if debugging {
dbg!(&local_span);
}
let span_contents = self.get_span_contents(our_span);
if debugging {
dbg!(String::from_utf8_lossy(span_contents));
}
let span_contents = span_contents.read_span(
&local_span,
context_lines_before,
context_lines_after,
)?;
let content_span = span_contents.span();
// Back to "global" indexing
let retranslated = (content_span.offset() + start, content_span.len()).into();
if debugging {
dbg!(&retranslated);
}
2021-09-21 06:03:06 +02:00
let data = span_contents.data();
2021-09-21 06:03:06 +02:00
if filename == "<cli>" {
if debugging {
let success_cli = "Successfully read CLI span";
dbg!(success_cli, String::from_utf8_lossy(data));
}
2021-09-21 06:03:06 +02:00
return Ok(Box::new(miette::MietteSpanContents::new(
data,
2021-09-21 06:03:06 +02:00
retranslated,
span_contents.line(),
span_contents.column(),
span_contents.line_count(),
)));
} else {
if debugging {
let success_file = "Successfully read file span";
dbg!(success_file);
}
2021-09-21 06:03:06 +02:00
return Ok(Box::new(miette::MietteSpanContents::new_named(
filename.clone(),
data,
2021-09-21 06:03:06 +02:00
retranslated,
span_contents.line(),
span_contents.column(),
span_contents.line_count(),
)));
}
2021-09-02 10:25:22 +02:00
}
}
Err(miette::MietteError::OutOfBounds)
2021-09-02 10:25:22 +02:00
}
}
2021-06-30 03:42:56 +02:00
#[cfg(test)]
2021-09-02 10:25:22 +02:00
mod engine_state_tests {
2021-06-30 03:42:56 +02:00
use super::*;
#[test]
fn add_file_gives_id() {
2021-09-02 10:25:22 +02:00
let engine_state = EngineState::new();
let mut engine_state = StateWorkingSet::new(&engine_state);
let id = engine_state.add_file("test.nu".into(), &[]);
2021-06-30 03:42:56 +02:00
assert_eq!(id, 0);
}
#[test]
fn add_file_gives_id_including_parent() {
2021-09-02 10:25:22 +02:00
let mut engine_state = EngineState::new();
let parent_id = engine_state.add_file("test.nu".into(), vec![]);
2021-06-30 03:42:56 +02:00
2021-09-02 10:25:22 +02:00
let mut working_set = StateWorkingSet::new(&engine_state);
let working_set_id = working_set.add_file("child.nu".into(), &[]);
2021-06-30 03:42:56 +02:00
assert_eq!(parent_id, 0);
assert_eq!(working_set_id, 1);
}
#[test]
fn merge_states() {
2021-09-02 10:25:22 +02:00
let mut engine_state = EngineState::new();
engine_state.add_file("test.nu".into(), vec![]);
2021-06-30 03:42:56 +02:00
let delta = {
2021-09-02 10:25:22 +02:00
let mut working_set = StateWorkingSet::new(&engine_state);
working_set.add_file("child.nu".into(), &[]);
2021-07-22 09:48:45 +02:00
working_set.render()
};
2021-06-30 03:42:56 +02:00
2021-09-02 10:25:22 +02:00
EngineState::merge_delta(&mut engine_state, delta);
2021-06-30 03:42:56 +02:00
2021-09-02 10:25:22 +02:00
assert_eq!(engine_state.num_files(), 2);
assert_eq!(&engine_state.files[0].0, "test.nu");
assert_eq!(&engine_state.files[1].0, "child.nu");
2021-06-30 03:42:56 +02:00
}
}