| 1 | # ============================================================================ | ||
| 2 | package MooseX::App::Plugin::Typo::Meta::Class; | ||
| 3 | # ============================================================================ | ||
| 4 | |||
| 5 | use 5.010; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use namespace::autoclean; | ||
| 9 | use Moose::Role; | ||
| 10 | |||
| 11 | use Text::WagnerFischer qw(distance); | ||
| 12 | |||
| 13 | around 'command_candidates' => sub { | ||
| 14 | my ($orig,$self,$command) = @_; | ||
| 15 | |||
| 16 | my $lc_command = lc($command); | ||
| 17 | my $commands = $self->app_commands; | ||
| 18 | |||
| 19 | # Fuzzy match | ||
| 20 | my @candidates; | ||
| 21 | my $candidate_length = length($command); | ||
| 22 | |||
| 23 | # Compare all commands to find matching candidates | ||
| 24 | foreach my $command_name (keys %$commands) { | ||
| 25 | my $candidate_substr = substr($command_name,0,$candidate_length+1); | ||
| 26 | if ($lc_command eq $command_name) { | ||
| 27 | return $command_name; | ||
| 28 | } elsif ($lc_command eq $candidate_substr | ||
| 29 | || distance($lc_command,$candidate_substr) <= 1) { | ||
| 30 | push(@candidates,$command_name); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | return [ sort @candidates ]; | ||
| 34 | }; | ||
| 35 | |||
| 36 | 1; |