From 2dff21cc78fd2d6a4fa4f227f6ad94e3f7baa943 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 28 Jul 2026 19:11:14 +0800 Subject: feat(mingling_macros, mingling_pathf): add `#[command]` attribute macro Add a new `#[command]` attribute macro (feature-gated behind `extra_macros`) that converts a plain function with a `Vec` parameter into a fully wired Mingling command. The macro generates a `dispatcher!` call, a `#[chain]` wrapper, and a hidden module re-exporting all generated types. Also add `CommandPattern` to the pathf pattern analyzer to recognize `#[command]`-annotated functions and track their generated hidden modules. --- mingling_pathf/src/pattern_analyzer.rs | 1 + mingling_pathf/src/patterns.rs | 2 + mingling_pathf/src/patterns/command.rs | 81 ++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 mingling_pathf/src/patterns/command.rs (limited to 'mingling_pathf/src') diff --git a/mingling_pathf/src/pattern_analyzer.rs b/mingling_pathf/src/pattern_analyzer.rs index e88d9f6..79b1c5a 100644 --- a/mingling_pathf/src/pattern_analyzer.rs +++ b/mingling_pathf/src/pattern_analyzer.rs @@ -33,6 +33,7 @@ pub fn init_with_config(config: PathfinderConfig) -> PatternAnalyzer { analyzer.add_pattern(GroupPattern); analyzer.add_pattern(GroupedDerivePattern); analyzer.add_pattern(ChainPattern); + analyzer.add_pattern(CommandPattern); analyzer.add_pattern(RendererPattern); analyzer.add_pattern(HelpPattern); analyzer.add_pattern(CompletionPattern); diff --git a/mingling_pathf/src/patterns.rs b/mingling_pathf/src/patterns.rs index 9845b73..964fe7c 100644 --- a/mingling_pathf/src/patterns.rs +++ b/mingling_pathf/src/patterns.rs @@ -2,6 +2,7 @@ pub use basic_struct::*; pub use chain::*; +pub use command::*; pub use completion::*; pub use dispatcher::*; pub use dispatcher_clap::*; @@ -13,6 +14,7 @@ pub use renderer::*; mod basic_struct; mod chain; +mod command; mod completion; mod dispatcher; mod dispatcher_clap; diff --git a/mingling_pathf/src/patterns/command.rs b/mingling_pathf/src/patterns/command.rs new file mode 100644 index 0000000..fac70af --- /dev/null +++ b/mingling_pathf/src/patterns/command.rs @@ -0,0 +1,81 @@ +//! The `CommandPattern` matches functions annotated with `#[command]` and +//! extracts the generated hidden module name (`__command__module`). +//! +//! Supported forms: +//! ```rust,ignore +//! #[command] +//! fn greet(args: Vec) -> Next { ... } +//! +//! #[command(node = "foo.foo-bar")] +//! fn greet(args: Vec) -> Next { ... } +//! +//! #[command(name = CMDGreet, entry = EntryGreet)] +//! fn greet(args: Vec) -> Next { ... } +//! +//! #[command(node = "greet", name = CMDGreet, entry = EntryGreet, buffer)] +//! fn greet(args: Vec) { ... } +//! ``` + +use syn::Item; + +use crate::pattern_analyzer::{AnalyzeItem, AnalyzePattern}; + +/// Match `#[command]` functions. +/// +/// The `#[command]` macro generates a hidden `__command__module` that +/// re-exports all internal types (`Entry*`, `CMD*`, chain struct, dispatcher +/// static). This pattern tracks that module; the build system generates a +/// glob re-export `use path::__command__module::*;` to bring everything +/// into scope. +pub struct CommandPattern; + +impl AnalyzePattern for CommandPattern { + fn contains(&self, content: &str) -> bool { + content.contains("command") + } + + fn analyze(&self, content: &str) -> Vec { + let Ok(syntax) = syn::parse_file(content) else { + return Vec::new(); + }; + + let mut items = Vec::new(); + for item in &syntax.items { + collect_from_item(item, "", &mut items); + } + items + } +} + +fn collect_from_item(item: &Item, current_mod: &str, items: &mut Vec) { + match item { + Item::Fn(f) if has_command_attr(&f.attrs) => { + let fn_name = f.sig.ident.to_string(); + let mod_name = format!("__command_{}_module", &fn_name); + items.push(AnalyzeItem::local_module(current_mod.to_string(), mod_name)); + } + Item::Mod(item_mod) => { + if let Some((_, nested)) = &item_mod.content { + let mod_name = &item_mod.ident.to_string(); + let nested_mod = if current_mod.is_empty() { + mod_name.clone() + } else { + format!("{current_mod}::{mod_name}") + }; + for n in nested { + collect_from_item(n, &nested_mod, items); + } + } + } + _ => {} + } +} + +fn has_command_attr(attrs: &[syn::Attribute]) -> bool { + attrs.iter().any(|a| { + a.path() + .segments + .last() + .is_some_and(|s| s.ident == "command") + }) +} -- cgit