aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/asset/comp.rs
blob: 3c22e12c698708534000fca0a3b350a6d35c2dfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
mod flags;
mod shell_ctx;
mod suggest;

use std::fmt::Display;

#[doc(hidden)]
pub use flags::*;
#[doc(hidden)]
pub use shell_ctx::*;
#[doc(hidden)]
pub use suggest::*;

use crate::{ProgramCollect, exec::match_user_input, this};

/// Trait for implementing completion logic.
///
/// This trait defines the interface for generating command-line completions.
/// Types implementing this trait can provide custom completion suggestions
/// based on the current shell context.
pub trait Completion {
    type Previous;
    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<Enum = P> + Display + 'static,
    {
        let program = this::<P>();
        let suggest = if let Some((dispatcher, args)) = match_user_input(program).ok() {
            let begin = dispatcher.begin(args);
            if let crate::ChainProcess::Ok((any, _)) = begin {
                Some(P::do_comp(&any, ctx))
            } else {
                None
            }
        } else {
            None
        };

        match suggest {
            Some(suggest) => suggest,
            None => default_completion(ctx),
        }
    }

    pub fn render_suggest<P>(ctx: ShellContext, suggest: Suggest)
    where
        P: ProgramCollect<Enum = P> + Display + 'static,
    {
        todo!()
    }
}

fn default_completion(ctx: &ShellContext) -> Suggest {
    todo!()
}