| 1 | package Perinci::Sub::XCompletion::perl_sorter_modname_with_optional_args; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Log::ger; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | ||
| 9 | our $DATE = '2024-01-23'; # DATE | ||
| 10 | our $DIST = 'Sah-Schemas-Sorter'; # DIST | ||
| 11 | our $VERSION = '0.001'; # VERSION | ||
| 12 | |||
| 13 | sub gen_completion { | ||
| 14 | my %gcargs = @_; | ||
| 15 | |||
| 16 | sub { | ||
| 17 | no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict | ||
| 18 | |||
| 19 | my %cargs = @_; | ||
| 20 | |||
| 21 | my $word = $cargs{word}; | ||
| 22 | |||
| 23 | my ($word_mod, $word_eq, $word_modargs) = $word =~ /\A([^=,]*)([=,])?(.*?)\z/; | ||
| 24 | #log_trace "TMP: word_mod, word_eq, word_modargs = %s, %s, %s", $word_mod, $word_eq, $word_modargs; | ||
| 25 | |||
| 26 | unless ($word_eq) { | ||
| 27 | require Complete::Module; | ||
| 28 | my $modres = Complete::Module::complete_module( | ||
| 29 | word => $word_mod, | ||
| 30 | ns_prefix => "Sorter::", | ||
| 31 | recurse => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | # no module matches, we can't complete | ||
| 35 | return [] unless @{ $modres->{words} }; | ||
| 36 | |||
| 37 | # multiple module matches, we also don't complete args, just the modules | ||
| 38 | return $modres if @{ $modres->{words} } > 1; | ||
| 39 | |||
| 40 | # normalize the module part | ||
| 41 | $word_mod = ref $modres->{words}[0] eq 'HASH' ? $modres->{words}[0]{word} : $modres->{words}[0]; | ||
| 42 | |||
| 43 | # to start args we use "," by default instead of "=" because "=" is | ||
| 44 | # problematic in bash completion because it is used as a | ||
| 45 | # word-breaking character by default (along with @><;|&(: | ||
| 46 | $word_eq = ","; | ||
| 47 | } | ||
| 48 | |||
| 49 | (my $wl_module = $word_mod) =~ s![/.]!::!g; | ||
| 50 | |||
| 51 | my $module = "Sorter::$wl_module"; | ||
| 52 | (my $module_pm = "$module.pm") =~ s!::!/!g; | ||
| 53 | eval { require $module_pm; 1 }; | ||
| 54 | do { log_trace "Can't load module $module: $@. Skipped checking for arguments"; return [$word_mod] } if $@; | ||
| 55 | |||
| 56 | my $meta = &{"$module\::meta"}->(); | ||
| 57 | do { log_trace "Module $module does not define meta"; return [$word_mod] } unless $meta; | ||
| 58 | |||
| 59 | do { log_trace "Module $module does not define args"; return [$word_mod] } unless $meta->{args} && keys(%{ $meta->{args} }); | ||
| 60 | |||
| 61 | my @args = sort keys %{ $meta->{args} }; | ||
| 62 | my @args_summaries = map { $meta->{args}{$_}{summary} } @args; | ||
| 63 | require Complete::Util; | ||
| 64 | my $ccsp_res = Complete::Util::complete_comma_sep_pair( | ||
| 65 | word => $word_modargs, | ||
| 66 | keys => \@args, | ||
| 67 | keys_summaries => \@args_summaries, | ||
| 68 | complete_value => sub { | ||
| 69 | my %cvargs = @_; | ||
| 70 | my $key = $cvargs{key}; | ||
| 71 | return [] unless $meta->{args}{$key}; | ||
| 72 | return [] unless $meta->{args}{$key}{schema}; | ||
| 73 | |||
| 74 | require Perinci::Sub::Complete; | ||
| 75 | Perinci::Sub::Complete::complete_from_schema( | ||
| 76 | word => $cvargs{word}, | ||
| 77 | schema => $meta->{args}{$key}{schema}, | ||
| 78 | ); | ||
| 79 | }, | ||
| 80 | ); | ||
| 81 | Complete::Util::modify_answer(answer => $ccsp_res, prefix => "$word_mod$word_eq"); | ||
| 82 | }, | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | # ABSTRACT: Generate completion for Sorter::* module name with optional params | ||
| 87 | |||
| 88 | __END__ | ||