diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-10 16:47:40 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-10 16:47:40 +0800 |
| commit | b18749170b6006e53976dbb6df9f59a3b9c34127 (patch) | |
| tree | a0f9288fdc9082e26daab218167da1f54521d32b /mingling_core | |
| parent | 3bb5afcbe01ad16293a66084dc1ad35f3378a833 (diff) | |
Add completion macro infrastructure without logic
Diffstat (limited to 'mingling_core')
| -rw-r--r-- | mingling_core/src/asset/comp.rs | 32 | ||||
| -rw-r--r-- | mingling_core/src/asset/comp/flags.rs | 1 | ||||
| -rw-r--r-- | mingling_core/src/asset/comp/shell_ctx.rs | 1 | ||||
| -rw-r--r-- | mingling_core/src/asset/comp/suggest.rs | 2 | ||||
| -rw-r--r-- | mingling_core/src/program.rs | 8 |
5 files changed, 43 insertions, 1 deletions
diff --git a/mingling_core/src/asset/comp.rs b/mingling_core/src/asset/comp.rs index 4815b5a..eeef0c0 100644 --- a/mingling_core/src/asset/comp.rs +++ b/mingling_core/src/asset/comp.rs @@ -2,6 +2,8 @@ mod flags; mod shell_ctx; mod suggest; +use std::fmt::Display; + #[doc(hidden)] pub use flags::*; #[doc(hidden)] @@ -9,6 +11,8 @@ pub use shell_ctx::*; #[doc(hidden)] pub use suggest::*; +use crate::{ProgramCollect, this}; + /// Trait for implementing completion logic. /// /// This trait defines the interface for generating command-line completions. @@ -16,5 +20,31 @@ pub use suggest::*; /// based on the current shell context. pub trait Completion { type Previous; - fn comp(ctx: ShellContext) -> Suggest; + fn comp(ctx: &ShellContext) -> Suggest; +} + +/// Trait for extracting user input arguments for completion. +/// +/// When the `feat comp` feature is enabled, the `dispatcher!` macro will +/// automatically implement this trait for `Entry` types to extract the +/// arguments from user input for completion suggestions. +pub trait CompletionEntry { + fn get_input(self) -> Vec<String>; +} + +pub struct CompletionHelper; +impl CompletionHelper { + pub fn exec_completion<P>(ctx: &ShellContext) -> Suggest + where + P: ProgramCollect + Display + 'static, + { + let program = this::<P>(); + Suggest::FileCompletion + } + + pub fn render_suggest<P>(ctx: ShellContext, suggest: Suggest) + where + P: ProgramCollect + Display + 'static, + { + } } diff --git a/mingling_core/src/asset/comp/flags.rs b/mingling_core/src/asset/comp/flags.rs index b432b08..0762d0d 100644 --- a/mingling_core/src/asset/comp/flags.rs +++ b/mingling_core/src/asset/comp/flags.rs @@ -1,6 +1,7 @@ use just_fmt::snake_case; #[derive(Default, Debug, Clone)] +#[cfg_attr(feature = "general_renderer", derive(serde::Serialize))] pub enum ShellFlag { #[default] Bash, diff --git a/mingling_core/src/asset/comp/shell_ctx.rs b/mingling_core/src/asset/comp/shell_ctx.rs index 081337f..4771e63 100644 --- a/mingling_core/src/asset/comp/shell_ctx.rs +++ b/mingling_core/src/asset/comp/shell_ctx.rs @@ -4,6 +4,7 @@ use crate::ShellFlag; /// providing information about the current command line state /// to guide how completions should be generated. #[derive(Default, Debug)] +#[cfg_attr(feature = "general_renderer", derive(serde::Serialize))] pub struct ShellContext { /// The full command line (-f / --command-line) pub command_line: String, diff --git a/mingling_core/src/asset/comp/suggest.rs b/mingling_core/src/asset/comp/suggest.rs index 4e7ce82..55a874f 100644 --- a/mingling_core/src/asset/comp/suggest.rs +++ b/mingling_core/src/asset/comp/suggest.rs @@ -3,6 +3,7 @@ use std::collections::BTreeSet; /// A completion suggestion that tells the shell how to perform completion. /// This can be either a set of specific suggestion items or a request for file completion. #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "general_renderer", derive(serde::Serialize))] pub enum Suggest { /// A set of specific suggestion items for the shell to display. Suggest(BTreeSet<SuggestItem>), @@ -59,6 +60,7 @@ impl std::ops::DerefMut for Suggest { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "general_renderer", derive(serde::Serialize))] pub enum SuggestItem { Simple(String), WithDescription(String, String), diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs index 8c8b4cb..7b9f8d4 100644 --- a/mingling_core/src/program.rs +++ b/mingling_core/src/program.rs @@ -1,5 +1,9 @@ +#[cfg(feature = "comp")] +use crate::{ShellContext, Suggest}; + #[cfg(feature = "general_renderer")] use crate::error::GeneralRendererSerializeError; + use crate::{ AnyOutput, ChainProcess, RenderResult, asset::dispatcher::Dispatcher, error::ProgramExecuteError, @@ -183,6 +187,10 @@ pub trait ProgramCollect { any: AnyOutput<Self::Enum>, ) -> Pin<Box<dyn Future<Output = ChainProcess<Self::Enum>> + Send>>; + /// Match and execute specific completion logic based on any Entry + #[cfg(feature = "comp")] + fn do_comp(any: &AnyOutput<Self::Enum>, ctx: &ShellContext) -> Suggest; + /// Whether the program has a renderer that can handle the current [AnyOutput](./struct.AnyOutput.html) fn has_renderer(any: &AnyOutput<Self::Enum>) -> bool; |
