NAME
Complete::Util - General completion routine
VERSION
This document describes version 0.55 of Complete::Util (from Perl distribution Complete-Util), released on 2016-10-26.
DESCRIPTION
FUNCTIONS
arrayify_answer($arg) -> array
Make sure we return completion answer in array form.
This is the reverse of hashify_answer
. It accepts a hash or an array. If it receives a hash, will return its words
key.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
$arg* => array|hash
Return value: (array)
combine_answers($answers, ...) -> hash
Given two or more answers, combine them into one.
This function is useful if you want to provide a completion answer that is gathered from multiple sources. For example, say you are providing completion for the Perl tool cpanm, which accepts a filename (a tarball like *.tar.gz
), a directory, or a module name. You can do something like this:
combine_answers(
complete_file(word=>$word),
complete_module(word=>$word),
);
But if a completion answer has a metadata final
set to true, then that answer is used as the final answer without any combining with the other answers.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
$answers* => array[hash|array]
Return value: (hash)
Return a combined completion answer. Words from each input answer will be combined, order preserved and duplicates removed. The other keys from each answer will be merged.
complete_array_elem(%args) -> array
Complete from array.
Try to find completion from an array of strings. Will attempt several methods, from the cheapest and most discriminating to the most expensive and least discriminating: normal string prefix matching, word-mode matching (see Complete::Common::OPT_WORD_MODE
for more details), char-mode matching (see Complete::Common::OPT_CHAR_MODE
for more details), and fuzzy matching (see Complete::Common::OPT_FUZZY
for more details).
Will sort the resulting completion list, so you don't have to presort the array.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
array* => array[str]
exclude => array
replace_map => hash
You can supply correction entries in this option. An example is when array if
['mount','unmount']
andumount
is a popular "typo" forunmount
. When someone already typesum
it cannot be completed into anything (even the current fuzzy mode will return both so it cannot complete immediately).One solution is to add replace_map
{'unmount'=>['umount']}
. This way,umount
will be regarded the same asunmount
and when user typesum
it can be completed unambiguously intounmount
.word* => str (default: "")
Word to complete.
Return value: (array)
complete_comma_sep(%args) -> array
Complete a comma-separated list string.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
elems* => array[str]
exclude => array
replace_map => hash
You can supply correction entries in this option. An example is when array if
['mount','unmount']
andumount
is a popular "typo" forunmount
. When someone already typesum
it cannot be completed into anything (even the current fuzzy mode will return both so it cannot complete immediately).One solution is to add replace_map
{'unmount'=>['umount']}
. This way,umount
will be regarded the same asunmount
and when user typesum
it can be completed unambiguously intounmount
.sep => str (default: ",")
uniq => str
Whether list contains unique elements.
word* => str (default: "")
Word to complete.
Return value: (array)
complete_hash_key(%args) -> array
Complete from hash keys.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
hash* => hash
word* => str (default: "")
Word to complete.
Return value: (array)
hashify_answer($arg, $meta) -> hash
Make sure we return completion answer in hash form.
This function accepts a hash or an array. If it receives an array, will convert the array into `{words=>$ary}' first to make sure the completion answer is in hash form.
Then will add keys from meta
to the hash.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
$arg* => array|hash
$meta => hash
Metadata (extra keys) for the hash.
Return value: (hash)
FAQ
Why is fuzzy matching slow?
Example:
use Benchmark qw(timethis);
use Complete::Util qw(complete_array_elem);
# turn off the other non-exact matching methods
$Complete::Common::OPT_CI = 0;
$Complete::Common::OPT_WORD_MODE = 0;
$Complete::Common::OPT_CHAR_MODE = 0;
my @ary = ("aaa".."zzy"); # 17575 elems
timethis(20, sub { complete_array_elem(array=>\@ary, word=>"zzz") });
results in:
timethis 20: 7 wallclock secs ( 6.82 usr + 0.00 sys = 6.82 CPU) @ 2.93/s (n=20)
Answer: fuzzy matching is slower than exact matching due to having to calculate Levenshtein distance. But if you find fuzzy matching too slow using the default pure-perl implementation, you might want to install Text::Levenshtein::Flexible (an optional prereq) to speed up fuzzy matching. After Text::Levenshtein::Flexible is installed:
timethis 20: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 19.23/s (n=20)
ENVIRONMENT
COMPLETE_UTIL_TRACE => bool
If set to true, will display more log statements for debugging.
COMPLETE_UTIL_LEVENSHTEIN => str ('pp'|'xs'|'flexible')
Can be used to force which Levenshtein distance implementation to use. pp
means the included PP implementation, which is the slowest (1-2 orders of magnitude slower than XS implementations), xs
which means Text::Levenshtein::XS, or flexible
which means Text::Levenshtein::Flexible (performs best).
If this is not set, the default is to use Text::Levenshtein::Flexible when it's available, then fallback to the included PP implementation.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Complete-Util.
SOURCE
Source repository is at https://github.com/perlancar/perl-Complete-Util.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Complete-Util
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
If you want to do bash tab completion with Perl, take a look at Complete::Bash or Getopt::Long::Complete or Perinci::CmdLine.
Other Complete::*
modules.
Bencher::Scenarios::CompleteUtil
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.