Use CommandType in more places (#12832)

# Description
Kind of a vague title, but this PR does two main things:
1. Rather than overriding functions like `Command::is_parser_keyword`,
this PR instead changes commands to override `Command::command_type`.
The `CommandType` returned by `Command::command_type` is then used to
automatically determine whether `Command::is_parser_keyword` and the
other `is_{type}` functions should return true. These changes allow us
to remove the `CommandType::Other` case and should also guarantee than
only one of the `is_{type}` functions on `Command` will return true.
2. Uses the new, reworked `Command::command_type` function in the `scope
commands` and `which` commands.


# User-Facing Changes
- Breaking change for `scope commands`: multiple columns (`is_builtin`,
`is_keyword`, `is_plugin`, etc.) have been merged into the `type`
column.
- Breaking change: the `which` command can now report `plugin` or
`keyword` instead of `built-in` in the `type` column. It may also now
report `external` instead of `custom` in the `type` column for known
`extern`s.
This commit is contained in:
Ian Manske
2024-05-18 23:37:31 +00:00
committed by GitHub
parent 580c60bb82
commit cc9f41e553
68 changed files with 224 additions and 217 deletions

View File

@ -1,5 +1,5 @@
use log::trace;
use nu_engine::{command_prelude::*, env};
use nu_protocol::engine::CommandType;
use std::{ffi::OsStr, path::Path};
#[derive(Clone)]
@ -51,14 +51,14 @@ impl Command for Which {
fn entry(
arg: impl Into<String>,
path: impl Into<String>,
cmd_type: impl Into<String>,
cmd_type: CommandType,
span: Span,
) -> Value {
Value::record(
record! {
"command" => Value::string(arg.into(), span),
"path" => Value::string(path.into(), span),
"type" => Value::string(cmd_type.into(), span),
"command" => Value::string(arg, span),
"path" => Value::string(path, span),
"type" => Value::string(cmd_type.to_string(), span),
},
span,
)
@ -66,17 +66,8 @@ fn entry(
fn get_entry_in_commands(engine_state: &EngineState, name: &str, span: Span) -> Option<Value> {
if let Some(decl_id) = engine_state.find_decl(name.as_bytes(), &[]) {
let cmd_type = if engine_state.get_decl(decl_id).is_custom_command() {
"custom"
} else if engine_state.get_decl(decl_id).is_alias() {
"alias"
} else {
"built-in"
};
trace!("Found command: {}", name);
Some(entry(name, "", cmd_type, span))
let decl = engine_state.get_decl(decl_id);
Some(entry(name, "", decl.command_type(), span))
} else {
None
}
@ -109,7 +100,7 @@ fn get_first_entry_in_path(
paths: impl AsRef<OsStr>,
) -> Option<Value> {
which::which_in(item, Some(paths), cwd)
.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
.map(|path| entry(item, path.to_string_lossy(), CommandType::External, span))
.ok()
}
@ -132,7 +123,7 @@ fn get_all_entries_in_path(
) -> Vec<Value> {
which::which_in_all(&item, Some(paths), cwd)
.map(|iter| {
iter.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
iter.map(|path| entry(item, path.to_string_lossy(), CommandType::External, span))
.collect()
})
.unwrap_or_default()