2022-01-04 23:30:34 +01:00
|
|
|
use super::{Command, Stack};
|
2021-11-17 05:23:55 +01:00
|
|
|
use crate::{
|
2022-02-12 10:50:37 +01:00
|
|
|
ast::Block, AliasId, BlockId, DeclId, Example, Overlay, OverlayId, ShellError, Signature, Span,
|
|
|
|
Type, VarId,
|
2021-11-17 05:23:55 +01:00
|
|
|
};
|
2021-07-23 23:19:30 +02:00
|
|
|
use core::panic;
|
2021-10-28 06:13:10 +02:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::{atomic::AtomicBool, Arc},
|
|
|
|
};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
use crate::Value;
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-11-19 03:51:42 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
use std::path::PathBuf;
|
2021-12-03 15:29:55 +01:00
|
|
|
|
2021-10-10 12:10:37 +02:00
|
|
|
// Tells whether a decl etc. is visible or not
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Visibility {
|
2021-11-16 00:16:06 +01:00
|
|
|
decl_ids: HashMap<DeclId, bool>,
|
2022-02-12 10:50:37 +01:00
|
|
|
alias_ids: HashMap<AliasId, bool>,
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visibility {
|
|
|
|
fn new() -> Self {
|
|
|
|
Visibility {
|
2021-11-16 00:16:06 +01:00
|
|
|
decl_ids: HashMap::new(),
|
2022-02-12 10:50:37 +01:00
|
|
|
alias_ids: HashMap::new(),
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
fn is_decl_id_visible(&self, decl_id: &DeclId) -> bool {
|
|
|
|
*self.decl_ids.get(decl_id).unwrap_or(&true) // by default it's visible
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
fn is_alias_id_visible(&self, alias_id: &AliasId) -> bool {
|
|
|
|
*self.alias_ids.get(alias_id).unwrap_or(&true) // by default it's visible
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
fn hide_decl_id(&mut self, decl_id: &DeclId) {
|
|
|
|
self.decl_ids.insert(*decl_id, false);
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
fn hide_alias_id(&mut self, alias_id: &AliasId) {
|
|
|
|
self.alias_ids.insert(*alias_id, false);
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
fn use_decl_id(&mut self, decl_id: &DeclId) {
|
|
|
|
self.decl_ids.insert(*decl_id, true);
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
fn use_alias_id(&mut self, alias_id: &AliasId) {
|
|
|
|
self.alias_ids.insert(*alias_id, true);
|
|
|
|
}
|
|
|
|
|
2021-10-10 12:10:37 +02:00
|
|
|
fn merge_with(&mut self, other: Visibility) {
|
|
|
|
// overwrite own values with the other
|
2021-11-16 00:16:06 +01:00
|
|
|
self.decl_ids.extend(other.decl_ids);
|
2022-02-12 10:50:37 +01:00
|
|
|
self.alias_ids.extend(other.alias_ids);
|
2021-11-16 00:16:06 +01:00
|
|
|
// self.env_var_ids.extend(other.env_var_ids);
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn append(&mut self, other: &Visibility) {
|
2022-02-12 10:50:37 +01:00
|
|
|
// take new values from the other but keep own values
|
2021-11-16 00:16:06 +01:00
|
|
|
for (decl_id, visible) in other.decl_ids.iter() {
|
|
|
|
if !self.decl_ids.contains_key(decl_id) {
|
|
|
|
self.decl_ids.insert(*decl_id, *visible);
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-12 10:50:37 +01:00
|
|
|
|
|
|
|
for (alias_id, visible) in other.alias_ids.iter() {
|
|
|
|
if !self.alias_ids.contains_key(alias_id) {
|
|
|
|
self.alias_ids.insert(*alias_id, *visible);
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 12:10:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-09-06 04:20:02 +02:00
|
|
|
pub struct ScopeFrame {
|
2021-10-05 04:46:24 +02:00
|
|
|
pub vars: HashMap<Vec<u8>, VarId>,
|
2021-10-10 13:31:13 +02:00
|
|
|
predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
|
2021-11-02 04:08:05 +01:00
|
|
|
pub decls: HashMap<Vec<u8>, DeclId>,
|
2022-02-12 10:50:37 +01:00
|
|
|
pub aliases: HashMap<Vec<u8>, AliasId>,
|
2021-11-16 00:16:06 +01:00
|
|
|
pub env_vars: HashMap<Vec<u8>, BlockId>,
|
2021-11-17 05:23:55 +01:00
|
|
|
pub overlays: HashMap<Vec<u8>, OverlayId>,
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility: Visibility,
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ScopeFrame {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
vars: HashMap::new(),
|
2021-10-10 13:31:13 +02:00
|
|
|
predecls: HashMap::new(),
|
2021-07-01 08:09:55 +02:00
|
|
|
decls: HashMap::new(),
|
2021-08-09 02:19:07 +02:00
|
|
|
aliases: HashMap::new(),
|
2021-11-16 00:16:06 +01:00
|
|
|
env_vars: HashMap::new(),
|
2021-11-17 05:23:55 +01:00
|
|
|
overlays: HashMap::new(),
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility: Visibility::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-11-02 20:53:48 +01:00
|
|
|
/// The core global engine state. This includes all global definitions as well as any global state that
|
|
|
|
/// will persist for the whole session.
|
|
|
|
///
|
|
|
|
/// Declarations, variables, blocks, and other forms of data are held in the global state and referenced
|
|
|
|
/// elsewhere using their IDs. These IDs are simply their index into the global state. This allows us to
|
|
|
|
/// more easily handle creating blocks, binding variables and callsites, and more, because each of these
|
|
|
|
/// will refer to the corresponding IDs rather than their definitions directly. At runtime, this means
|
|
|
|
/// less copying and smaller structures.
|
|
|
|
///
|
|
|
|
/// Note that the runtime stack is not part of this global state. Runtime stacks are handled differently,
|
|
|
|
/// but they also rely on using IDs rather than full definitions.
|
|
|
|
///
|
|
|
|
/// A note on implementation:
|
|
|
|
///
|
|
|
|
/// Much of the global definitions are built on the Bodil's 'im' crate. This gives us a way of working with
|
|
|
|
/// lists of definitions in a way that is very cheap to access, while also allowing us to update them at
|
|
|
|
/// key points in time (often, the transition between parsing and evaluation).
|
|
|
|
///
|
|
|
|
/// Over the last two years we tried a few different approaches to global state like this. I'll list them
|
|
|
|
/// here for posterity, so we can more easily know how we got here:
|
|
|
|
///
|
|
|
|
/// * `Rc` - Rc is cheap, but not thread-safe. The moment we wanted to work with external processes, we
|
|
|
|
/// needed a way send to stdin/stdout. In Rust, the current practice is to spawn a thread to handle both.
|
|
|
|
/// These threads would need access to the global state, as they'll need to process data as it streams out
|
|
|
|
/// of the data pipeline. Because Rc isn't thread-safe, this breaks.
|
|
|
|
///
|
|
|
|
/// * `Arc` - Arc is the thread-safe version of the above. Often Arc is used in combination with a Mutex or
|
|
|
|
/// RwLock, but you can use Arc by itself. We did this a few places in the original Nushell. This *can* work
|
|
|
|
/// but because of Arc's nature of not allowing mutation if there's a second copy of the Arc around, this
|
|
|
|
/// ultimately becomes limiting.
|
|
|
|
///
|
|
|
|
/// * `Arc` + `Mutex/RwLock` - the standard practice for thread-safe containers. Unfortunately, this would
|
|
|
|
/// have meant we would incur a lock penalty every time we needed to access any declaration or block. As we
|
|
|
|
/// would be reading far more often than writing, it made sense to explore solutions that favor large amounts
|
|
|
|
/// of reads.
|
|
|
|
///
|
|
|
|
/// * `im` - the `im` crate was ultimately chosen because it has some very nice properties: it gives the
|
|
|
|
/// ability to cheaply clone these structures, which is nice as EngineState may need to be cloned a fair bit
|
|
|
|
/// to follow ownership rules for closures and iterators. It also is cheap to access. Favoring reads here fits
|
|
|
|
/// more closely to what we need with Nushell. And, of course, it's still thread-safe, so we get the same
|
|
|
|
/// benefits as above.
|
|
|
|
///
|
2021-11-02 04:08:05 +01:00
|
|
|
#[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>>,
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: im::Vector<Vec<Span>>,
|
2021-11-02 04:08:05 +01:00
|
|
|
blocks: im::Vector<Block>,
|
2021-11-17 05:23:55 +01:00
|
|
|
overlays: im::Vector<Overlay>,
|
2021-11-02 04:08:05 +01:00
|
|
|
pub scope: im::Vector<ScopeFrame>,
|
|
|
|
pub ctrlc: Option<Arc<AtomicBool>>,
|
2022-01-04 23:30:34 +01:00
|
|
|
pub env_vars: im::HashMap<String, Value>,
|
2021-11-19 03:51:42 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
pub plugin_signatures: Option<PathBuf>,
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 04:08:05 +01:00
|
|
|
pub const NU_VARIABLE_ID: usize = 0;
|
|
|
|
pub const SCOPE_VARIABLE_ID: usize = 1;
|
2021-11-08 07:21:24 +01:00
|
|
|
pub const IN_VARIABLE_ID: usize = 2;
|
2021-11-14 20:25:57 +01:00
|
|
|
pub const CONFIG_VARIABLE_ID: usize = 3;
|
2022-01-04 22:34:42 +01:00
|
|
|
pub const ENV_VARIABLE_ID: usize = 4;
|
2022-01-12 05:06:56 +01:00
|
|
|
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
|
2021-11-02 04:08:05 +01:00
|
|
|
|
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 {
|
2021-10-25 06:01:02 +02:00
|
|
|
files: im::vector![],
|
|
|
|
file_contents: im::vector![],
|
2022-01-04 22:34:42 +01:00
|
|
|
vars: im::vector![
|
|
|
|
Type::Unknown,
|
|
|
|
Type::Unknown,
|
|
|
|
Type::Unknown,
|
|
|
|
Type::Unknown,
|
|
|
|
Type::Unknown
|
|
|
|
],
|
2021-10-25 06:01:02 +02:00
|
|
|
decls: im::vector![],
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: im::vector![],
|
2021-10-25 06:01:02 +02:00
|
|
|
blocks: im::vector![],
|
2021-11-17 05:23:55 +01:00
|
|
|
overlays: im::vector![],
|
2021-10-25 06:01:02 +02:00
|
|
|
scope: im::vector![ScopeFrame::new()],
|
2021-10-28 06:13:10 +02:00
|
|
|
ctrlc: None,
|
2022-01-04 23:30:34 +01:00
|
|
|
env_vars: im::HashMap::new(),
|
2021-11-19 03:51:42 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
plugin_signatures: None,
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
/// Merges a `StateDelta` onto the current state. These deltas come from a system, like the parser, that
|
|
|
|
/// creates a new set of definitions and visible symbols in the current scope. We make this transactional
|
|
|
|
/// as there are times when we want to run the parser and immediately throw away the results (namely:
|
|
|
|
/// syntax highlighting and completions).
|
|
|
|
///
|
|
|
|
/// When we want to preserve what the parser has created, we can take its output (the `StateDelta`) and
|
|
|
|
/// use this function to merge it into the global state.
|
2022-01-04 23:30:34 +01:00
|
|
|
pub fn merge_delta(
|
|
|
|
&mut self,
|
|
|
|
mut delta: StateDelta,
|
|
|
|
stack: Option<&mut Stack>,
|
|
|
|
cwd: impl AsRef<Path>,
|
|
|
|
) -> Result<(), ShellError> {
|
2021-06-30 03:42:56 +02:00
|
|
|
// Take the mutable reference and extend the permanent state from the working set
|
2021-11-02 20:53:48 +01:00
|
|
|
self.files.extend(delta.files);
|
|
|
|
self.file_contents.extend(delta.file_contents);
|
|
|
|
self.decls.extend(delta.decls);
|
2022-02-12 10:50:37 +01:00
|
|
|
self.aliases.extend(delta.aliases);
|
2021-11-02 20:53:48 +01:00
|
|
|
self.vars.extend(delta.vars);
|
|
|
|
self.blocks.extend(delta.blocks);
|
2021-11-17 05:23:55 +01:00
|
|
|
self.overlays.extend(delta.overlays);
|
2021-07-22 09:33:38 +02:00
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
if let Some(last) = self.scope.back_mut() {
|
2021-07-22 09:33:38 +02:00
|
|
|
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-11-17 05:23:55 +01:00
|
|
|
for item in first.overlays.into_iter() {
|
|
|
|
last.overlays.insert(item.0, item.1);
|
2021-09-26 12:25:52 +02:00
|
|
|
}
|
2021-10-10 12:10:37 +02:00
|
|
|
last.visibility.merge_with(first.visibility);
|
2021-11-19 03:51:42 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "plugin")]
|
2021-12-03 15:29:55 +01:00
|
|
|
if delta.plugins_changed {
|
|
|
|
let result = self.update_plugin_file();
|
|
|
|
|
|
|
|
if result.is_ok() {
|
|
|
|
delta.plugins_changed = false;
|
2021-11-19 03:51:42 +01:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:29:55 +01:00
|
|
|
return result;
|
2021-11-19 03:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
if let Some(stack) = stack {
|
|
|
|
for mut env_scope in stack.env_vars.drain(..) {
|
|
|
|
for (k, v) in env_scope.drain() {
|
|
|
|
self.env_vars.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:26:01 +01:00
|
|
|
// FIXME: permanent state changes like this hopefully in time can be removed
|
|
|
|
// and be replaced by just passing the cwd in where needed
|
2022-01-04 23:30:34 +01:00
|
|
|
std::env::set_current_dir(cwd)?;
|
|
|
|
|
2021-11-19 03:51:42 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
pub fn update_plugin_file(&self) -> Result<(), ShellError> {
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
// Updating the signatures plugin file with the added signatures
|
2021-12-05 04:11:19 +01:00
|
|
|
self.plugin_signatures
|
|
|
|
.as_ref()
|
|
|
|
.ok_or_else(|| ShellError::PluginFailedToLoad("Plugin file not found".into()))
|
|
|
|
.and_then(|plugin_path| {
|
|
|
|
// Always create the file, which will erase previous signatures
|
|
|
|
std::fs::File::create(plugin_path.as_path())
|
|
|
|
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
|
|
|
|
})
|
|
|
|
.and_then(|mut plugin_file| {
|
2021-12-03 00:11:25 +01:00
|
|
|
// Plugin definitions with parsed signature
|
2021-12-05 04:11:19 +01:00
|
|
|
self.plugin_decls().try_for_each(|decl| {
|
2021-12-03 00:11:25 +01:00
|
|
|
// A successful plugin registration already includes the plugin filename
|
|
|
|
// No need to check the None option
|
2021-12-18 19:13:56 +01:00
|
|
|
let (path, encoding, shell) =
|
|
|
|
decl.is_plugin().expect("plugin should have file name");
|
|
|
|
let file_name = path
|
|
|
|
.to_str()
|
|
|
|
.expect("path was checked during registration as a str");
|
2021-12-03 00:11:25 +01:00
|
|
|
|
2021-12-04 13:38:21 +01:00
|
|
|
serde_json::to_string_pretty(&decl.signature())
|
2021-12-12 12:50:35 +01:00
|
|
|
.map(|signature| {
|
2021-12-18 19:13:56 +01:00
|
|
|
// Extracting the possible path to the shell used to load the plugin
|
|
|
|
let shell_str = match shell {
|
|
|
|
Some(path) => format!(
|
|
|
|
"-s {}",
|
|
|
|
path.to_str().expect(
|
|
|
|
"shell path was checked during registration as a str"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
None => "".into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Each signature is stored in the plugin file with the required
|
|
|
|
// encoding, shell and signature
|
|
|
|
// This information will be used when loading the plugin
|
|
|
|
// information when nushell starts
|
|
|
|
format!(
|
|
|
|
"register {} -e {} {} {}\n\n",
|
|
|
|
file_name, encoding, shell_str, signature
|
|
|
|
)
|
2021-12-12 12:50:35 +01:00
|
|
|
})
|
2021-12-04 13:38:21 +01:00
|
|
|
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
|
|
|
|
.and_then(|line| {
|
|
|
|
plugin_file
|
|
|
|
.write_all(line.as_bytes())
|
|
|
|
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
|
2021-12-05 04:11:19 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
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()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn num_aliases(&self) -> usize {
|
|
|
|
self.aliases.len()
|
|
|
|
}
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
pub fn num_blocks(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn num_overlays(&self) -> usize {
|
|
|
|
self.overlays.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) {
|
2021-10-25 06:01:02 +02:00
|
|
|
for (contents, _, _) in self.file_contents.iter() {
|
2021-10-25 18:58:58 +02:00
|
|
|
let string = String::from_utf8_lossy(contents);
|
2021-10-25 06:01:02 +02:00
|
|
|
println!("{}", string);
|
|
|
|
}
|
2021-09-25 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn find_aliases(&self, name: &str) -> Vec<&[Span]> {
|
2022-01-20 19:02:53 +01:00
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for frame in &self.scope {
|
2022-02-12 10:50:37 +01:00
|
|
|
if let Some(alias_id) = frame.aliases.get(name.as_bytes()) {
|
|
|
|
let alias = self.get_alias(*alias_id);
|
|
|
|
output.push(alias.as_ref());
|
2022-01-20 19:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_custom_commands(&self, name: &str) -> Vec<Block> {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for frame in &self.scope {
|
|
|
|
if let Some(decl_id) = frame.decls.get(name.as_bytes()) {
|
|
|
|
let decl = self.get_decl(*decl_id);
|
|
|
|
|
|
|
|
if let Some(block_id) = decl.get_block_id() {
|
|
|
|
output.push(self.get_block(block_id).clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:46:55 +02:00
|
|
|
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
|
2021-10-10 12:10:37 +02:00
|
|
|
let mut visibility: Visibility = Visibility::new();
|
2021-09-28 22:18:48 +02:00
|
|
|
|
2021-07-23 23:46:55 +02:00
|
|
|
for scope in self.scope.iter().rev() {
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility.append(&scope.visibility);
|
2021-09-28 22:18:48 +02:00
|
|
|
|
2021-07-23 23:46:55 +02:00
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
2021-11-16 00:16:06 +01:00
|
|
|
if visibility.is_decl_id_visible(decl_id) {
|
2021-09-28 22:18:48 +02:00
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
2021-07-23 23:46:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:29:55 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
2021-11-19 03:51:42 +01:00
|
|
|
pub fn plugin_decls(&self) -> impl Iterator<Item = &Box<dyn Command + 'static>> {
|
2021-12-03 15:29:55 +01:00
|
|
|
let mut unique_plugin_decls = HashMap::new();
|
|
|
|
|
|
|
|
// Make sure there are no duplicate decls: Newer one overwrites the older one
|
|
|
|
for decl in self.decls.iter().filter(|d| d.is_plugin().is_some()) {
|
|
|
|
unique_plugin_decls.insert(decl.name(), decl);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut plugin_decls: Vec<(&str, &Box<dyn Command>)> =
|
|
|
|
unique_plugin_decls.into_iter().collect();
|
|
|
|
|
|
|
|
// Sort the plugins by name so we don't end up with a random plugin file each time
|
|
|
|
plugin_decls.sort_by(|a, b| a.0.cmp(b.0));
|
|
|
|
plugin_decls.into_iter().map(|(_, decl)| decl)
|
2021-11-19 03:51:42 +01:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
|
2021-11-16 00:16:06 +01:00
|
|
|
for scope in self.scope.iter().rev() {
|
2021-11-17 05:23:55 +01:00
|
|
|
if let Some(overlay_id) = scope.overlays.get(name) {
|
|
|
|
return Some(*overlay_id);
|
2021-11-16 00:16:06 +01: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-10-25 06:01:02 +02:00
|
|
|
for (contents, start, finish) in &self.file_contents {
|
2021-10-25 18:58:58 +02:00
|
|
|
if span.start >= *start && span.end <= *finish {
|
2021-10-25 06:01:02 +02:00
|
|
|
return &contents[(span.start - start)..(span.end - start)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!("internal error: span missing in file contents cache")
|
2021-09-10 00:09:40 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn get_alias(&self, alias_id: AliasId) -> &[Span] {
|
|
|
|
self.aliases
|
|
|
|
.get(alias_id)
|
|
|
|
.expect("internal error: missing alias")
|
|
|
|
.as_ref()
|
|
|
|
}
|
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
/// Get all IDs of all commands within scope, sorted by the commads' names
|
|
|
|
pub fn get_decl_ids_sorted(&self, include_hidden: bool) -> impl Iterator<Item = DeclId> {
|
|
|
|
let mut decls_map = HashMap::new();
|
2021-10-01 23:53:13 +02:00
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
for frame in &self.scope {
|
|
|
|
let frame_decls = if include_hidden {
|
|
|
|
frame.decls.clone()
|
|
|
|
} else {
|
|
|
|
frame
|
|
|
|
.decls
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.filter(|(_, id)| frame.visibility.is_decl_id_visible(id))
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
decls_map.extend(frame_decls);
|
2021-10-01 23:53:13 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
let mut decls: Vec<(Vec<u8>, DeclId)> = decls_map.into_iter().collect();
|
|
|
|
|
|
|
|
decls.sort_by(|a, b| a.0.cmp(&b.0));
|
|
|
|
decls.into_iter().map(|(_, id)| id)
|
2021-10-01 23:53:13 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
/// Get signatures of all commands within scope.
|
|
|
|
pub fn get_signatures(&self, include_hidden: bool) -> Vec<Signature> {
|
|
|
|
self.get_decl_ids_sorted(include_hidden)
|
|
|
|
.map(|id| {
|
|
|
|
let decl = self.get_decl(id);
|
|
|
|
|
2021-10-09 03:02:01 +02:00
|
|
|
let mut signature = (*decl).signature();
|
|
|
|
signature.usage = decl.usage().to_string();
|
|
|
|
signature.extra_usage = decl.extra_usage().to_string();
|
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
signature
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-10-09 03:02:01 +02:00
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
/// Get signatures of all commands within scope.
|
|
|
|
///
|
|
|
|
/// In addition to signatures, it returns whether each command is:
|
|
|
|
/// a) a plugin
|
|
|
|
/// b) custom
|
|
|
|
pub fn get_signatures_with_examples(
|
|
|
|
&self,
|
|
|
|
include_hidden: bool,
|
|
|
|
) -> Vec<(Signature, Vec<Example>, bool, bool)> {
|
|
|
|
self.get_decl_ids_sorted(include_hidden)
|
|
|
|
.map(|id| {
|
|
|
|
let decl = self.get_decl(id);
|
2021-12-03 15:29:55 +01:00
|
|
|
|
2021-12-03 19:45:29 +01:00
|
|
|
let mut signature = (*decl).signature();
|
|
|
|
signature.usage = decl.usage().to_string();
|
|
|
|
signature.extra_usage = decl.extra_usage().to_string();
|
|
|
|
|
|
|
|
(
|
|
|
|
signature,
|
|
|
|
decl.examples(),
|
|
|
|
decl.is_plugin().is_some(),
|
|
|
|
decl.get_block_id().is_some(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
2021-10-09 03:02:01 +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
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn get_overlay(&self, overlay_id: OverlayId) -> &Overlay {
|
|
|
|
self.overlays
|
|
|
|
.get(overlay_id)
|
|
|
|
.expect("internal error: missing overlay")
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
pub fn next_span_start(&self) -> usize {
|
2021-10-25 18:58:58 +02:00
|
|
|
if let Some((_, _, last)) = self.file_contents.last() {
|
|
|
|
*last
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
2021-07-03 05:35:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
pub fn files(&self) -> impl Iterator<Item = &(String, usize, usize)> {
|
2021-08-10 07:08:10 +02:00
|
|
|
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 {
|
2021-10-25 06:01:02 +02:00
|
|
|
let contents = self.get_span_contents(&Span {
|
|
|
|
start: file.1 .1,
|
|
|
|
end: file.1 .2,
|
|
|
|
});
|
|
|
|
let output = String::from_utf8_lossy(contents).to_string();
|
2021-08-10 07:08:10 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"<unknown>".into()
|
|
|
|
}
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
pub fn add_file(&mut self, filename: String, contents: Vec<u8>) -> usize {
|
2021-07-03 05:35:15 +02:00
|
|
|
let next_span_start = self.next_span_start();
|
2021-10-25 06:01:02 +02:00
|
|
|
let next_span_end = next_span_start + contents.len();
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
self.file_contents
|
|
|
|
.push_back((contents, next_span_start, next_span_end));
|
2021-07-03 05:35:15 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
self.files
|
|
|
|
.push_back((filename, next_span_start, next_span_end));
|
2021-07-03 05:11:24 +02:00
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
self.num_files() - 1
|
2021-07-03 05:11:24 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 04:08:05 +01:00
|
|
|
impl Default for EngineState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
/// A temporary extension to the global state. This handles bridging between the global state and the
|
|
|
|
/// additional declarations and scope changes that are not yet part of the global scope.
|
|
|
|
///
|
|
|
|
/// This working set is created by the parser as a way of handling declarations and scope changes that
|
|
|
|
/// may later be merged or dropped (and not merged) depending on the needs of the code calling the parser.
|
2021-09-02 10:25:22 +02:00
|
|
|
pub struct StateWorkingSet<'a> {
|
|
|
|
pub permanent_state: &'a EngineState,
|
|
|
|
pub delta: StateDelta,
|
2021-07-22 09:33:38 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
/// A delta (or change set) between the current global state and a possible future global state. Deltas
|
|
|
|
/// can be applied to the global state to update it to contain both previous state and the state held
|
|
|
|
/// within the delta.
|
2021-09-02 10:25:22 +02:00
|
|
|
pub struct StateDelta {
|
2021-07-03 05:35:15 +02:00
|
|
|
files: Vec<(String, usize, usize)>,
|
2021-10-25 06:01:02 +02:00
|
|
|
pub(crate) file_contents: Vec<(Vec<u8>, usize, usize)>,
|
2021-10-10 13:31:13 +02:00
|
|
|
vars: Vec<Type>, // indexed by VarId
|
|
|
|
decls: Vec<Box<dyn Command>>, // indexed by DeclId
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: Vec<Vec<Span>>, // indexed by AliasId
|
2022-02-11 13:37:10 +01:00
|
|
|
pub blocks: Vec<Block>, // indexed by BlockId
|
2021-11-17 05:23:55 +01:00
|
|
|
overlays: Vec<Overlay>, // indexed by OverlayId
|
2021-09-06 04:20:02 +02:00
|
|
|
pub scope: Vec<ScopeFrame>,
|
2021-12-02 06:42:56 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
2021-12-03 15:29:55 +01:00
|
|
|
plugins_changed: bool, // marks whether plugin file should be updated
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 22:06:32 +01:00
|
|
|
impl Default for StateDelta {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:25:22 +02:00
|
|
|
impl StateDelta {
|
2022-01-14 22:06:32 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
StateDelta {
|
|
|
|
files: vec![],
|
|
|
|
file_contents: vec![],
|
|
|
|
vars: vec![],
|
|
|
|
decls: vec![],
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: vec![],
|
2022-01-14 22:06:32 +01:00
|
|
|
blocks: vec![],
|
|
|
|
overlays: vec![],
|
|
|
|
scope: vec![ScopeFrame::new()],
|
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
plugins_changed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
pub fn num_files(&self) -> usize {
|
|
|
|
self.files.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_decls(&self) -> usize {
|
|
|
|
self.decls.len()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn num_aliases(&self) -> usize {
|
|
|
|
self.aliases.len()
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
pub fn num_blocks(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn num_overlays(&self) -> usize {
|
|
|
|
self.overlays.len()
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
pub fn enter_scope(&mut self) {
|
|
|
|
self.scope.push(ScopeFrame::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_scope(&mut self) {
|
|
|
|
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 {
|
2022-01-14 22:06:32 +01:00
|
|
|
delta: StateDelta::new(),
|
2021-06-30 03:42:56 +02:00
|
|
|
permanent_state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_files(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
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 {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.num_decls() + self.permanent_state.num_decls()
|
2021-07-16 08:24:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn num_aliases(&self) -> usize {
|
|
|
|
self.delta.num_aliases() + self.permanent_state.num_aliases()
|
|
|
|
}
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
pub fn num_blocks(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.num_blocks() + self.permanent_state.num_blocks()
|
2021-07-16 08:24:46 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn num_overlays(&self) -> usize {
|
|
|
|
self.delta.num_overlays() + self.permanent_state.num_overlays()
|
|
|
|
}
|
|
|
|
|
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();
|
2021-07-16 08:24:46 +02:00
|
|
|
|
2021-07-22 09:33:38 +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
|
2021-07-22 09:33:38 +02:00
|
|
|
.delta
|
2021-07-02 00:40:08 +02:00
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
2021-09-28 22:18:48 +02:00
|
|
|
|
2021-07-02 00:40:08 +02:00
|
|
|
scope_frame.decls.insert(name, decl_id);
|
2021-11-16 00:16:06 +01:00
|
|
|
scope_frame.visibility.use_decl_id(&decl_id);
|
2021-07-02 00:40:08 +02:00
|
|
|
|
|
|
|
decl_id
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:25:51 +01:00
|
|
|
pub fn use_decls(&mut self, decls: Vec<(Vec<u8>, DeclId)>) {
|
2021-11-16 00:16:06 +01:00
|
|
|
let scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
|
|
|
for (name, decl_id) in decls {
|
|
|
|
scope_frame.decls.insert(name, decl_id);
|
|
|
|
scope_frame.visibility.use_decl_id(&decl_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:16:27 +02:00
|
|
|
pub fn add_predecl(&mut self, decl: Box<dyn Command>) -> Option<DeclId> {
|
2021-10-01 21:29:24 +02:00
|
|
|
let name = decl.name().as_bytes().to_vec();
|
|
|
|
|
|
|
|
self.delta.decls.push(decl);
|
|
|
|
let decl_id = self.num_decls() - 1;
|
|
|
|
|
2021-10-10 13:31:13 +02:00
|
|
|
let scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
|
|
|
scope_frame.predecls.insert(name, decl_id)
|
2021-10-01 21:29:24 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 03:51:42 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
2021-12-03 15:29:55 +01:00
|
|
|
pub fn mark_plugins_file_dirty(&mut self) {
|
|
|
|
self.delta.plugins_changed = true;
|
2021-11-19 03:51:42 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
pub fn merge_predecl(&mut self, name: &[u8]) -> Option<DeclId> {
|
2021-10-10 13:31:13 +02:00
|
|
|
let scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
2021-10-01 21:29:24 +02:00
|
|
|
|
2021-10-10 13:31:13 +02:00
|
|
|
if let Some(decl_id) = scope_frame.predecls.remove(name) {
|
2021-10-01 21:29:24 +02:00
|
|
|
scope_frame.decls.insert(name.into(), decl_id);
|
2021-11-16 00:16:06 +01:00
|
|
|
scope_frame.visibility.use_decl_id(&decl_id);
|
2021-10-01 21:29:24 +02:00
|
|
|
|
|
|
|
return Some(decl_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hide_decl(&mut self, name: &[u8]) -> Option<DeclId> {
|
2021-10-10 12:10:37 +02:00
|
|
|
let mut visibility: Visibility = Visibility::new();
|
2021-10-01 23:12:30 +02:00
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
// Since we can mutate scope frames in delta, remove the id directly
|
|
|
|
for scope in self.delta.scope.iter_mut().rev() {
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility.append(&scope.visibility);
|
2021-10-01 23:12:30 +02:00
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
if let Some(decl_id) = scope.decls.remove(name) {
|
|
|
|
return Some(decl_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot mutate the permanent state => store the information in the current scope frame
|
|
|
|
let last_scope_frame = self
|
2021-09-28 22:18:48 +02:00
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility.append(&scope.visibility);
|
2021-10-01 23:12:30 +02:00
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
2021-11-16 00:16:06 +01:00
|
|
|
if visibility.is_decl_id_visible(decl_id) {
|
2021-10-04 19:33:27 +02:00
|
|
|
// Hide decl only if it's not already hidden
|
2021-11-16 00:16:06 +01:00
|
|
|
last_scope_frame.visibility.hide_decl_id(decl_id);
|
2021-10-01 23:12:30 +02:00
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
2021-10-01 21:29:24 +02:00
|
|
|
}
|
2021-09-28 22:18:48 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 21:29:24 +02:00
|
|
|
None
|
2021-09-28 22:18:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn hide_alias(&mut self, name: &[u8]) -> Option<AliasId> {
|
|
|
|
let mut visibility: Visibility = Visibility::new();
|
|
|
|
|
|
|
|
// Since we can mutate scope frames in delta, remove the id directly
|
|
|
|
for scope in self.delta.scope.iter_mut().rev() {
|
|
|
|
visibility.append(&scope.visibility);
|
|
|
|
|
|
|
|
if let Some(alias_id) = scope.aliases.remove(name) {
|
|
|
|
return Some(alias_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot mutate the permanent state => store the information in the current scope frame
|
|
|
|
let last_scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
|
|
|
visibility.append(&scope.visibility);
|
|
|
|
|
|
|
|
if let Some(alias_id) = scope.aliases.get(name) {
|
|
|
|
if visibility.is_alias_id_visible(alias_id) {
|
|
|
|
// Hide alias only if it's not already hidden
|
|
|
|
last_scope_frame.visibility.hide_alias_id(alias_id);
|
|
|
|
return Some(*alias_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hide_decls(&mut self, decls: &[Vec<u8>]) {
|
2021-11-16 00:16:06 +01:00
|
|
|
for decl in decls.iter() {
|
2022-02-12 10:50:37 +01:00
|
|
|
self.hide_decl(decl); // let's assume no errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hide_aliases(&mut self, aliases: &[Vec<u8>]) {
|
|
|
|
for alias in aliases.iter() {
|
|
|
|
self.hide_alias(alias); // let's assume no errors
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:26:40 +02:00
|
|
|
pub fn add_block(&mut self, block: Block) -> BlockId {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.blocks.push(block);
|
2021-07-16 08:24:46 +02:00
|
|
|
|
|
|
|
self.num_blocks() - 1
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
pub fn add_env_var(&mut self, name_span: Span, block: Block) -> BlockId {
|
2021-09-26 14:00:03 +02:00
|
|
|
self.delta.blocks.push(block);
|
|
|
|
let block_id = self.num_blocks() - 1;
|
2021-11-16 00:16:06 +01:00
|
|
|
let name = self.get_span_contents(name_span).to_vec();
|
2021-09-26 14:00:03 +02:00
|
|
|
|
|
|
|
let scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
scope_frame.env_vars.insert(name, block_id);
|
2021-09-26 14:00:03 +02:00
|
|
|
|
|
|
|
block_id
|
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn add_overlay(&mut self, name: &str, overlay: Overlay) -> OverlayId {
|
2021-11-16 00:16:06 +01:00
|
|
|
let name = name.as_bytes().to_vec();
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
self.delta.overlays.push(overlay);
|
|
|
|
let overlay_id = self.num_overlays() - 1;
|
2021-11-16 00:16:06 +01:00
|
|
|
|
2021-09-26 12:53:52 +02:00
|
|
|
let scope_frame = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
scope_frame.overlays.insert(name, overlay_id);
|
2021-11-16 00:16:06 +01:00
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
overlay_id
|
2021-09-26 12:53:52 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
pub fn next_span_start(&self) -> usize {
|
2021-10-25 18:58:58 +02:00
|
|
|
let permanent_span_start = self.permanent_state.next_span_start();
|
|
|
|
|
|
|
|
if let Some((_, _, last)) = self.delta.file_contents.last() {
|
2021-10-31 16:22:10 +01:00
|
|
|
*last
|
2021-10-25 18:58:58 +02:00
|
|
|
} else {
|
|
|
|
permanent_span_start
|
|
|
|
}
|
2021-07-03 05:35:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 22:45:23 +02:00
|
|
|
pub fn global_span_offset(&self) -> usize {
|
|
|
|
self.permanent_state.next_span_start()
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:08:10 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
pub fn add_file(&mut self, filename: String, contents: &[u8]) -> usize {
|
2021-07-03 05:35:15 +02:00
|
|
|
let next_span_start = self.next_span_start();
|
2021-10-25 06:01:02 +02:00
|
|
|
let next_span_end = next_span_start + contents.len();
|
2021-07-03 05:35:15 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
self.delta
|
|
|
|
.file_contents
|
|
|
|
.push((contents.to_vec(), next_span_start, next_span_end));
|
2021-07-03 05:35:15 +02:00
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
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] {
|
2021-07-22 09:33:38 +02:00
|
|
|
let permanent_end = self.permanent_state.next_span_start();
|
|
|
|
if permanent_end <= span.start {
|
2021-10-25 06:01:02 +02:00
|
|
|
for (contents, start, finish) in &self.delta.file_contents {
|
2021-10-25 18:58:58 +02:00
|
|
|
if (span.start >= *start) && (span.end <= *finish) {
|
2021-10-31 16:22:10 +01:00
|
|
|
return &contents[(span.start - start)..(span.end - start)];
|
2021-10-25 06:01:02 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 09:15:30 +02:00
|
|
|
} else {
|
2021-10-25 06:01:02 +02:00
|
|
|
return self.permanent_state.get_span_contents(&span);
|
2021-07-02 09:15:30 +02:00
|
|
|
}
|
2021-10-25 06:01:02 +02:00
|
|
|
|
|
|
|
panic!("internal error: missing span contents in file cache")
|
2021-07-02 09:15:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 03:42:56 +02:00
|
|
|
pub fn enter_scope(&mut self) {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.enter_scope();
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_scope(&mut self) {
|
2021-07-22 09:33:38 +02:00
|
|
|
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> {
|
2021-10-10 12:10:37 +02:00
|
|
|
let mut visibility: Visibility = Visibility::new();
|
2021-10-01 21:29:24 +02:00
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.delta.scope.iter().rev() {
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility.append(&scope.visibility);
|
2021-09-28 22:18:48 +02:00
|
|
|
|
2021-10-10 13:31:13 +02:00
|
|
|
if let Some(decl_id) = scope.predecls.get(name) {
|
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
|
|
|
|
2021-07-08 00:55:46 +02:00
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
2021-10-01 21:29:24 +02:00
|
|
|
return Some(*decl_id);
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
2021-10-10 12:10:37 +02:00
|
|
|
visibility.append(&scope.visibility);
|
2021-09-28 22:18:48 +02:00
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
2021-11-16 00:16:06 +01:00
|
|
|
if visibility.is_decl_id_visible(decl_id) {
|
2021-09-28 22:18:48 +02:00
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
2021-07-17 08:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:09:55 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn find_alias(&self, name: &[u8]) -> Option<AliasId> {
|
|
|
|
let mut visibility: Visibility = Visibility::new();
|
|
|
|
|
|
|
|
for scope in self.delta.scope.iter().rev() {
|
|
|
|
visibility.append(&scope.visibility);
|
|
|
|
|
|
|
|
if let Some(alias_id) = scope.aliases.get(name) {
|
|
|
|
return Some(*alias_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
|
|
|
visibility.append(&scope.visibility);
|
|
|
|
|
|
|
|
if let Some(alias_id) = scope.aliases.get(name) {
|
|
|
|
if visibility.is_alias_id_visible(alias_id) {
|
|
|
|
return Some(*alias_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-11-17 05:23:55 +01:00
|
|
|
pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
|
2021-09-26 12:25:52 +02:00
|
|
|
for scope in self.delta.scope.iter().rev() {
|
2021-11-17 05:23:55 +01:00
|
|
|
if let Some(overlay_id) = scope.overlays.get(name) {
|
|
|
|
return Some(*overlay_id);
|
2021-09-26 12:25:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
2021-11-17 05:23:55 +01:00
|
|
|
if let Some(overlay_id) = scope.overlays.get(name) {
|
|
|
|
return Some(*overlay_id);
|
2021-09-26 12:25:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-07-22 09:33:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
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 {
|
2021-07-22 09:33:38 +02:00
|
|
|
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> {
|
2021-07-22 09:33:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +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-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
|
2021-07-22 09:33:38 +02:00
|
|
|
.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>) {
|
2022-02-12 10:50:37 +01:00
|
|
|
self.delta.aliases.push(replacement);
|
|
|
|
let alias_id = self.num_aliases() - 1;
|
|
|
|
|
2021-08-09 02:19:07 +02:00
|
|
|
let last = self
|
|
|
|
.delta
|
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing stack frame");
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
last.aliases.insert(name, alias_id);
|
|
|
|
last.visibility.use_alias_id(&alias_id);
|
2021-08-09 02:19:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:26:01 +01:00
|
|
|
pub fn get_cwd(&self) -> String {
|
|
|
|
let pwd = self
|
|
|
|
.permanent_state
|
|
|
|
.env_vars
|
|
|
|
.get("PWD")
|
|
|
|
.expect("internal error: can't find PWD");
|
|
|
|
pwd.as_string().expect("internal error: PWD not a string")
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-07-22 09:33:38 +02:00
|
|
|
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> {
|
2021-07-22 09:33:38 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn get_alias(&self, alias_id: AliasId) -> &[Span] {
|
|
|
|
let num_permanent_aliases = self.permanent_state.num_aliases();
|
|
|
|
if alias_id < num_permanent_aliases {
|
|
|
|
self.permanent_state.get_alias(alias_id)
|
|
|
|
} else {
|
|
|
|
self.delta
|
|
|
|
.aliases
|
|
|
|
.get(alias_id - num_permanent_aliases)
|
|
|
|
.expect("internal error: missing alias")
|
|
|
|
.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-17 05:23:55 +01:00
|
|
|
pub fn get_overlay(&self, overlay_id: OverlayId) -> &Overlay {
|
|
|
|
let num_permanent_overlays = self.permanent_state.num_overlays();
|
|
|
|
if overlay_id < num_permanent_overlays {
|
|
|
|
self.permanent_state.get_overlay(overlay_id)
|
|
|
|
} else {
|
|
|
|
self.delta
|
|
|
|
.overlays
|
|
|
|
.get(overlay_id - num_permanent_overlays)
|
|
|
|
.expect("internal error: missing overlay")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:21:24 +01:00
|
|
|
pub fn get_block_mut(&mut self, block_id: BlockId) -> &mut Block {
|
|
|
|
let num_permanent_blocks = self.permanent_state.num_blocks();
|
|
|
|
if block_id < num_permanent_blocks {
|
|
|
|
panic!("Attempt to mutate a block that is in the permanent (immutable) state")
|
|
|
|
} else {
|
|
|
|
self.delta
|
|
|
|
.blocks
|
|
|
|
.get_mut(block_id - num_permanent_blocks)
|
|
|
|
.expect("internal error: missing block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-09-20 23:37:26 +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> {
|
2021-09-22 02:54:20 +02:00
|
|
|
let debugging = std::env::var("MIETTE_DEBUG").is_ok();
|
|
|
|
if debugging {
|
|
|
|
let finding_span = "Finding span in StateWorkingSet";
|
|
|
|
dbg!(finding_span, span);
|
|
|
|
}
|
2021-09-20 23:37:26 +02:00
|
|
|
for (filename, start, end) in self.files() {
|
2021-09-22 02:54:20 +02:00
|
|
|
if debugging {
|
|
|
|
dbg!(&filename, start, end);
|
|
|
|
}
|
2021-09-20 23:37:26 +02:00
|
|
|
if span.offset() >= *start && span.offset() + span.len() <= *end {
|
2021-09-22 02:54:20 +02:00
|
|
|
if debugging {
|
|
|
|
let found_file = "Found matching file";
|
|
|
|
dbg!(found_file);
|
|
|
|
}
|
2021-09-20 23:37:26 +02:00
|
|
|
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();
|
2021-09-22 02:54:20 +02:00
|
|
|
if debugging {
|
|
|
|
dbg!(&local_span);
|
|
|
|
}
|
2021-09-20 23:37:26 +02:00
|
|
|
let span_contents = self.get_span_contents(our_span);
|
2021-09-22 02:54:20 +02:00
|
|
|
if debugging {
|
|
|
|
dbg!(String::from_utf8_lossy(span_contents));
|
|
|
|
}
|
2021-09-20 23:37:26 +02:00
|
|
|
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();
|
2021-09-22 02:54:20 +02:00
|
|
|
if debugging {
|
|
|
|
dbg!(&retranslated);
|
|
|
|
}
|
2021-09-21 06:03:06 +02:00
|
|
|
|
2021-09-22 02:54:20 +02:00
|
|
|
let data = span_contents.data();
|
2021-09-21 06:03:06 +02:00
|
|
|
if filename == "<cli>" {
|
2021-09-22 02:54:20 +02:00
|
|
|
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(
|
2021-09-22 02:54:20 +02:00
|
|
|
data,
|
2021-09-21 06:03:06 +02:00
|
|
|
retranslated,
|
|
|
|
span_contents.line(),
|
|
|
|
span_contents.column(),
|
|
|
|
span_contents.line_count(),
|
|
|
|
)));
|
|
|
|
} else {
|
2021-09-22 02:54:20 +02:00
|
|
|
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(),
|
2021-09-22 02:54:20 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-09-20 23:37:26 +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);
|
2021-07-22 08:04:50 +02:00
|
|
|
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]
|
2021-11-19 06:00:29 +01:00
|
|
|
fn merge_states() -> Result<(), ShellError> {
|
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
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
let delta = {
|
2021-09-02 10:25:22 +02:00
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-07-22 09:33:38 +02:00
|
|
|
working_set.add_file("child.nu".into(), &[]);
|
2021-07-22 09:48:45 +02:00
|
|
|
working_set.render()
|
2021-07-22 09:33:38 +02:00
|
|
|
};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
engine_state.merge_delta(delta, None, &cwd)?;
|
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-11-19 06:00:29 +01:00
|
|
|
|
|
|
|
Ok(())
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
}
|