mirror of
https://github.com/danbulant/nushell
synced 2026-05-20 12:58:32 +00:00
Restructure and streamline token expansion The purpose of this commit is to streamline the token expansion code, by removing aspects of the code that are no longer relevant, removing pointless duplication, and eliminating the need to pass the same arguments to `expand_syntax`. The first big-picture change in this commit is that instead of a handful of `expand_` functions, which take a TokensIterator and ExpandContext, a smaller number of methods on the `TokensIterator` do the same job. The second big-picture change in this commit is fully eliminating the coloring traits, making coloring a responsibility of the base expansion implementations. This also means that the coloring tracer is merged into the expansion tracer, so you can follow a single expansion and see how the expansion process produced colored tokens. One side effect of this change is that the expander itself is marginally more error-correcting. The error correction works by switching from structured expansion to `BackoffColoringMode` when an unexpected token is found, which guarantees that all spans of the source are colored, but may not be the most optimal error recovery strategy. That said, because `BackoffColoringMode` only extends as far as a closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in fairly granular correction strategy. The current code still produces an `Err` (plus a complete list of colored shapes) from the parsing process if any errors are encountered, but this could easily be addressed now that the underlying expansion is error-correcting. This commit also colors any spans that are syntax errors in red, and causes the parser to include some additional information about what tokens were expected at any given point where an error was encountered, so that completions and hinting could be more robust in the future. Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com> Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
194 lines
5.7 KiB
Rust
194 lines
5.7 KiB
Rust
use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext};
|
|
use crate::commands::cp::CopyArgs;
|
|
use crate::commands::ls::LsArgs;
|
|
use crate::commands::mkdir::MkdirArgs;
|
|
use crate::commands::mv::MoveArgs;
|
|
use crate::commands::rm::RemoveArgs;
|
|
use crate::prelude::*;
|
|
use crate::shell::filesystem_shell::FilesystemShell;
|
|
use crate::shell::shell::Shell;
|
|
use crate::stream::OutputStream;
|
|
use nu_errors::ShellError;
|
|
use nu_parser::ExpandContext;
|
|
use parking_lot::Mutex;
|
|
use std::error::Error;
|
|
use std::path::PathBuf;
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
use std::sync::Arc;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ShellManager {
|
|
pub(crate) current_shell: Arc<AtomicUsize>,
|
|
pub(crate) shells: Arc<Mutex<Vec<Box<dyn Shell + Send>>>>,
|
|
}
|
|
|
|
impl ShellManager {
|
|
pub fn basic(commands: CommandRegistry) -> Result<ShellManager, Box<dyn Error>> {
|
|
Ok(ShellManager {
|
|
current_shell: Arc::new(AtomicUsize::new(0)),
|
|
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic(
|
|
commands,
|
|
)?)])),
|
|
})
|
|
}
|
|
|
|
pub fn insert_at_current(&mut self, shell: Box<dyn Shell + Send>) {
|
|
self.shells.lock().push(shell);
|
|
self.current_shell
|
|
.store(self.shells.lock().len() - 1, Ordering::SeqCst);
|
|
self.set_path(self.path());
|
|
}
|
|
|
|
pub fn current_shell(&self) -> usize {
|
|
self.current_shell.load(Ordering::SeqCst)
|
|
}
|
|
|
|
pub fn remove_at_current(&mut self) {
|
|
{
|
|
let mut shells = self.shells.lock();
|
|
if shells.len() > 0 {
|
|
if self.current_shell() == shells.len() - 1 {
|
|
shells.pop();
|
|
let new_len = shells.len();
|
|
if new_len > 0 {
|
|
self.current_shell.store(new_len - 1, Ordering::SeqCst);
|
|
} else {
|
|
return;
|
|
}
|
|
} else {
|
|
shells.remove(self.current_shell());
|
|
}
|
|
}
|
|
}
|
|
self.set_path(self.path())
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.shells.lock().is_empty()
|
|
}
|
|
|
|
pub fn path(&self) -> String {
|
|
self.shells.lock()[self.current_shell()].path()
|
|
}
|
|
|
|
pub fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
|
|
let env = self.shells.lock();
|
|
|
|
env[self.current_shell()].pwd(args)
|
|
}
|
|
|
|
pub fn set_path(&mut self, path: String) {
|
|
self.shells.lock()[self.current_shell()].set_path(path)
|
|
}
|
|
|
|
pub fn complete(
|
|
&self,
|
|
line: &str,
|
|
pos: usize,
|
|
ctx: &rustyline::Context<'_>,
|
|
) -> Result<(usize, Vec<rustyline::completion::Pair>), rustyline::error::ReadlineError> {
|
|
self.shells.lock()[self.current_shell()].complete(line, pos, ctx)
|
|
}
|
|
|
|
pub fn hint(
|
|
&self,
|
|
line: &str,
|
|
pos: usize,
|
|
ctx: &rustyline::Context<'_>,
|
|
context: ExpandContext,
|
|
) -> Option<String> {
|
|
self.shells.lock()[self.current_shell()].hint(line, pos, ctx, context)
|
|
}
|
|
|
|
pub fn next(&mut self) {
|
|
{
|
|
let shell_len = self.shells.lock().len();
|
|
if self.current_shell() == (shell_len - 1) {
|
|
self.current_shell.store(0, Ordering::SeqCst);
|
|
} else {
|
|
self.current_shell
|
|
.store(self.current_shell() + 1, Ordering::SeqCst);
|
|
}
|
|
}
|
|
self.set_path(self.path())
|
|
}
|
|
|
|
pub fn prev(&mut self) {
|
|
{
|
|
let shell_len = self.shells.lock().len();
|
|
if self.current_shell() == 0 {
|
|
self.current_shell.store(shell_len - 1, Ordering::SeqCst);
|
|
} else {
|
|
self.current_shell
|
|
.store(self.current_shell() - 1, Ordering::SeqCst);
|
|
}
|
|
}
|
|
self.set_path(self.path())
|
|
}
|
|
|
|
pub fn homedir(&self) -> Option<PathBuf> {
|
|
let env = self.shells.lock();
|
|
|
|
env[self.current_shell()].homedir()
|
|
}
|
|
|
|
pub fn ls(
|
|
&self,
|
|
args: LsArgs,
|
|
context: &RunnablePerItemContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let env = self.shells.lock();
|
|
|
|
env[self.current_shell()].ls(args, context)
|
|
}
|
|
|
|
pub fn cd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
|
|
let env = self.shells.lock();
|
|
|
|
env[self.current_shell()].cd(args)
|
|
}
|
|
|
|
pub fn cp(
|
|
&self,
|
|
args: CopyArgs,
|
|
context: &RunnablePerItemContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let shells = self.shells.lock();
|
|
|
|
let path = shells[self.current_shell()].path();
|
|
shells[self.current_shell()].cp(args, context.name.clone(), &path)
|
|
}
|
|
|
|
pub fn rm(
|
|
&self,
|
|
args: RemoveArgs,
|
|
context: &RunnablePerItemContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let shells = self.shells.lock();
|
|
|
|
let path = shells[self.current_shell()].path();
|
|
shells[self.current_shell()].rm(args, context.name.clone(), &path)
|
|
}
|
|
|
|
pub fn mkdir(
|
|
&self,
|
|
args: MkdirArgs,
|
|
context: &RunnablePerItemContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let shells = self.shells.lock();
|
|
|
|
let path = shells[self.current_shell()].path();
|
|
shells[self.current_shell()].mkdir(args, context.name.clone(), &path)
|
|
}
|
|
|
|
pub fn mv(
|
|
&self,
|
|
args: MoveArgs,
|
|
context: &RunnablePerItemContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let shells = self.shells.lock();
|
|
|
|
let path = shells[self.current_shell()].path();
|
|
shells[self.current_shell()].mv(args, context.name.clone(), &path)
|
|
}
|
|
}
|