nushell/crates/nu-parser/src/known_external.rs

136 lines
4.0 KiB
Rust
Raw Normal View History

Simplify known external name recovery (#5213) Prior to this change we would recover the names for known externals by looking up the span in the engine state. This would fail when using an alias for two reasons: 1. In cases where we don't have a subcommand, like this: ``` >>> extern bat [filename: string] >>> alias b = bat >>> bat some_file 'b' is not recognized as an internal or external command, operable program or batch file. ``` The problem is that after alias expansion, we replace the span of the expanded name with the original alias (this is done to alleviate non-related issues). The span contents we look up therefore contain `b`, the alias, instead of the expanded command name. 2. In cases where there's a subcommand: ``` >>> alias g = git >>> g push thread 'main' panicked at 'internal error: span missing in file contents cache', crates\nu-protocol\src\engine\engine_state.rs:474:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` In this case, the span in call starts where the expansion for the `g` alias is defined and end after `push` on the last command entered. This is not a proper span and causes a panic when we try to look it up. Note that this is the case for all expanded aliases that involve a subcommand, but we never actually try to retrieve the contents for that span in other cases. Anyway, the new way of looking up the name is arguably cleaner regardless of the issues mentioned above. But it's nice that it fixes them too. Co-authored-by: Hristo Filaretov <h.filaretov@protonmail.com>
2022-04-17 05:07:38 +02:00
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{
ast::{Argument, Call, Expr, Expression},
engine::Command,
ShellError, Signature,
};
use nu_protocol::{PipelineData, Spanned, Type};
#[derive(Clone)]
pub struct KnownExternal {
pub name: String,
pub signature: Box<Signature>,
pub usage: String,
}
impl Command for KnownExternal {
fn name(&self) -> &str {
&self.name
}
fn signature(&self) -> Signature {
*self.signature.clone()
}
fn usage(&self) -> &str {
&self.usage
}
fn is_known_external(&self) -> bool {
true
}
fn is_builtin(&self) -> bool {
false
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let call_span = call.span();
let head_span = call.head;
let decl_id = engine_state
Overlays (#5375) * WIP: Start laying overlays * Rename Overlay->Module; Start adding overlay * Revamp adding overlay * Add overlay add tests; Disable debug print * Fix overlay add; Add overlay remove * Add overlay remove tests * Add missing overlay remove file * Add overlay list command * (WIP?) Enable overlays for env vars * Move OverlayFrames to ScopeFrames * (WIP) Move everything to overlays only ScopeFrame contains nothing but overlays now * Fix predecls * Fix wrong overlay id translation and aliases * Fix broken env lookup logic * Remove TODOs * Add overlay add + remove for environment * Add a few overlay tests; Fix overlay add name * Some cleanup; Fix overlay add/remove names * Clippy * Fmt * Remove walls of comments * List overlays from stack; Add debugging flag Currently, the engine state ordering is somehow broken. * Fix (?) overlay list test * Fix tests on Windows * Fix activated overlay ordering * Check for active overlays equality in overlay list This removes the -p flag: Either both parser and engine will have the same overlays, or the command will fail. * Add merging on overlay remove * Change help message and comment * Add some remove-merge/discard tests * (WIP) Track removed overlays properly * Clippy; Fmt * Fix getting last overlay; Fix predecls in overlays * Remove merging; Fix re-add overwriting stuff Also some error message tweaks. * Fix overlay error in the engine * Update variable_completions.rs * Adds flags and optional arguments to view-source (#5446) * added flags and optional arguments to view-source * removed redundant code * removed redundant code * fmt * fix bug in shell_integration (#5450) * fix bug in shell_integration * add some comments * enable cd to work with directory abbreviations (#5452) * enable cd to work with abbreviations * add abbreviation example * fix tests * make it configurable * make cd recornize symblic link (#5454) * implement seq char command to generate single character sequence (#5453) * add tmp code * add seq char command * Add split number flag in `split row` (#5434) Signed-off-by: Yuheng Su <gipsyh.icu@gmail.com> * Add two more overlay tests * Add ModuleId to OverlayFrame * Fix env conversion accidentally activating overlay It activated overlay from permanent state prematurely which would cause `overlay add` to misbehave. * Remove unused parameter; Add overlay list test * Remove added traces * Add overlay commands examples * Modify TODO * Fix $nu.scope iteration * Disallow removing default overlay * Refactor some parser errors * Remove last overlay if no argument * Diversify overlay examples * Make it possible to update overlay's module In case the origin module updates, the overlay add loads the new module, makes it overlay's origin and applies the changes. Before, it was impossible to update the overlay if the module changed. Co-authored-by: JT <547158+jntrnr@users.noreply.github.com> Co-authored-by: pwygab <88221256+merelymyself@users.noreply.github.com> Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <WindSoilder@outlook.com> Co-authored-by: Yuheng Su <gipsyh.icu@gmail.com>
2022-05-07 21:39:22 +02:00
.find_decl("run-external".as_bytes(), &[])
2023-03-06 18:33:09 +01:00
.ok_or(ShellError::ExternalNotSupported { span: head_span })?;
let command = engine_state.get_decl(decl_id);
let mut extern_call = Call::new(head_span);
let extern_name = if let Some(name_bytes) = engine_state.find_decl_name(call.decl_id, &[]) {
String::from_utf8_lossy(name_bytes)
} else {
2023-03-06 18:33:09 +01:00
return Err(ShellError::NushellFailedSpanned {
msg: "known external name not found".to_string(),
label: "could not find name for this command".to_string(),
span: call.head,
});
};
let extern_name: Vec<_> = extern_name.split(' ').collect();
let arg_extern_name = Expression {
expr: Expr::String(extern_name[0].to_string()),
span: call.head,
ty: Type::String,
custom_completion: None,
};
extern_call.add_positional(arg_extern_name);
for subcommand in extern_name.into_iter().skip(1) {
extern_call.add_positional(Expression {
expr: Expr::String(subcommand.to_string()),
span: call.head,
ty: Type::String,
custom_completion: None,
});
}
for arg in &call.arguments {
match arg {
Argument::Positional(positional) => extern_call.add_positional(positional.clone()),
Argument::Named(named) => {
if let Some(short) = &named.1 {
extern_call.add_positional(Expression {
expr: Expr::String(format!("-{}", short.item)),
span: named.0.span,
ty: Type::String,
custom_completion: None,
});
} else {
extern_call.add_positional(Expression {
expr: Expr::String(format!("--{}", named.0.item)),
span: named.0.span,
ty: Type::String,
custom_completion: None,
});
}
if let Some(arg) = &named.2 {
extern_call.add_positional(arg.clone());
}
}
Argument::Unknown(unknown) => extern_call.add_unknown(unknown.clone()),
}
}
if call.redirect_stdout {
extern_call.add_named((
Spanned {
item: "redirect-stdout".into(),
span: call_span,
},
None,
None,
))
}
if call.redirect_stderr {
extern_call.add_named((
Spanned {
item: "redirect-stderr".into(),
span: call_span,
},
None,
None,
))
}
command.run(engine_state, stack, &extern_call, input)
}
}