diff --git a/reader.cpp b/reader.cpp index 8045467e8..3a444902f 100644 --- a/reader.cpp +++ b/reader.cpp @@ -1653,25 +1653,35 @@ bool compare_completions_by_match_type(const completion_t &a, const completion_t return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0; } -/* Order completions such that case insensitive completions come first. */ -static void prioritize_completions(std::vector &comp) +/* Determine the best match type */ +fuzzy_match_type_t get_best_match_type(const std::vector &comp) { - /* Determine the best match type */ - size_t i; fuzzy_match_type_t best_type = fuzzy_match_none; - for (i=0; i < comp.size(); i++) + for (size_t i=0; i < comp.size(); i++) { const completion_t &el = comp.at(i); if (el.match.type < best_type) + { best_type = el.match.type; + } } + /* For case exact match as best type, make it to lower level as prefix match, + because completions with prefix match cannot stay otherwise while they are + also candidates of the input along with completion with exact match. */ if (best_type == fuzzy_match_exact) { best_type = fuzzy_match_prefix; } + return best_type; +} - /* Throw out completions whose match types are not the best. */ - i = comp.size(); +/* Order completions such that case insensitive completions come first. */ +static void prioritize_completions(std::vector &comp) +{ + fuzzy_match_type_t best_type = get_best_match_type(comp); + + /* Throw out completions whose match types are less suitable than the best. */ + size_t i = comp.size(); while (i--) { if (comp.at(i).match.type > best_type) @@ -1789,21 +1799,7 @@ static bool handle_completions(const std::vector &comp) if (!done) { - - /* Determine the type of the best match(es) */ - fuzzy_match_type_t best_match_type = fuzzy_match_none; - for (size_t i=0; i < comp.size(); i++) - { - const completion_t &el = comp.at(i); - if (el.match.type < best_match_type) - { - best_match_type = el.match.type; - } - } - if (best_match_type == fuzzy_match_exact) - { - best_match_type = fuzzy_match_prefix; - } + fuzzy_match_type_t best_match_type = get_best_match_type(comp); /* Determine whether we are going to replace the token or not. If any commands of the best type do not require replacement, then ignore all those that want to use replacement */ bool will_replace_token = true; @@ -1822,7 +1818,7 @@ static bool handle_completions(const std::vector &comp) for (size_t i=0; i < comp.size(); i++) { const completion_t &el = comp.at(i); - /* Only use completions with the best match type */ + /* Ignore completions with less suitable match type than the best. */ if (el.match.type > best_match_type) continue;