mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 09:42:24 +02:00
Add search terms to Command and Signature (#4980)
* Add search terms to command * Rename Signature desc to usage To be named uniformly with extra_usage * Throw in foldl search term for reduce * Add missing usage to post * Add search terms to signature * Try to add capnp Signature serialization
This commit is contained in:
@@ -61,6 +61,11 @@ pub trait Command: Send + Sync + CommandClone {
|
||||
fn get_block_id(&self) -> Option<BlockId> {
|
||||
None
|
||||
}
|
||||
|
||||
// Related terms to help with command search
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CommandClone {
|
||||
|
@@ -11,6 +11,7 @@ use std::{
|
||||
|
||||
use crate::Value;
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
@@ -527,11 +528,7 @@ impl EngineState {
|
||||
.map(|id| {
|
||||
let decl = self.get_decl(id);
|
||||
|
||||
let mut signature = (*decl).signature();
|
||||
signature.usage = decl.usage().to_string();
|
||||
signature.extra_usage = decl.extra_usage().to_string();
|
||||
|
||||
signature
|
||||
(*decl).signature().update_from_command(decl.borrow())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -549,9 +546,7 @@ impl EngineState {
|
||||
.map(|id| {
|
||||
let decl = self.get_decl(id);
|
||||
|
||||
let mut signature = (*decl).signature();
|
||||
signature.usage = decl.usage().to_string();
|
||||
signature.extra_usage = decl.extra_usage().to_string();
|
||||
let signature = (*decl).signature().update_from_command(decl.borrow());
|
||||
|
||||
(
|
||||
signature,
|
||||
|
@@ -95,6 +95,7 @@ pub struct Signature {
|
||||
pub name: String,
|
||||
pub usage: String,
|
||||
pub extra_usage: String,
|
||||
pub search_terms: Vec<String>,
|
||||
pub required_positional: Vec<PositionalArg>,
|
||||
pub optional_positional: Vec<PositionalArg>,
|
||||
pub rest_positional: Option<PositionalArg>,
|
||||
@@ -135,6 +136,7 @@ impl Signature {
|
||||
name: name.into(),
|
||||
usage: String::new(),
|
||||
extra_usage: String::new(),
|
||||
search_terms: vec![],
|
||||
required_positional: vec![],
|
||||
optional_positional: vec![],
|
||||
rest_positional: None,
|
||||
@@ -144,13 +146,38 @@ impl Signature {
|
||||
category: Category::Default,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(name: impl Into<String>) -> Signature {
|
||||
Signature::new(name.into())
|
||||
}
|
||||
|
||||
/// Add a description to the signature
|
||||
pub fn desc(mut self, usage: impl Into<String>) -> Signature {
|
||||
self.usage = usage.into();
|
||||
pub fn usage(mut self, msg: impl Into<String>) -> Signature {
|
||||
self.usage = msg.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an extra description to the signature
|
||||
pub fn extra_usage(mut self, msg: impl Into<String>) -> Signature {
|
||||
self.extra_usage = msg.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Add search terms to the signature
|
||||
pub fn search_terms(mut self, terms: Vec<String>) -> Signature {
|
||||
self.search_terms = terms;
|
||||
self
|
||||
}
|
||||
|
||||
/// Update signature's fields from a Command trait implementation
|
||||
pub fn update_from_command(mut self, command: &dyn Command) -> Signature {
|
||||
self.search_terms = command
|
||||
.search_terms()
|
||||
.into_iter()
|
||||
.map(|term| term.to_string())
|
||||
.collect();
|
||||
self.extra_usage = command.extra_usage().to_string();
|
||||
self.usage = command.usage().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
|
@@ -9,14 +9,14 @@ fn test_signature() {
|
||||
assert_eq!(signature, from_build);
|
||||
|
||||
// constructing signature with description
|
||||
let signature = Signature::new("signature").desc("example usage");
|
||||
let signature = Signature::new("signature").usage("example usage");
|
||||
assert_eq!(signature.usage, "example usage".to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signature_chained() {
|
||||
let signature = Signature::new("new_signature")
|
||||
.desc("description")
|
||||
.usage("description")
|
||||
.required("required", SyntaxShape::String, "required description")
|
||||
.optional("optional", SyntaxShape::String, "optional description")
|
||||
.required_named(
|
||||
@@ -129,7 +129,7 @@ fn test_signature_same_name() {
|
||||
#[test]
|
||||
fn test_signature_round_trip() {
|
||||
let signature = Signature::new("new_signature")
|
||||
.desc("description")
|
||||
.usage("description")
|
||||
.required("first", SyntaxShape::String, "first required")
|
||||
.required("second", SyntaxShape::Int, "second required")
|
||||
.optional("optional", SyntaxShape::String, "optional description")
|
||||
|
Reference in New Issue
Block a user