aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader.cpp
diff options
context:
space:
mode:
authorGravatar Tony Wang <wwwjfy@gmail.com>2013-08-25 17:10:22 +0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2013-08-25 19:33:39 -0700
commitd2ffdc8986996c55aba7c59efe96022cdfa2e99b (patch)
treea68bd5c1a1e91513a588895583afa6d69950aaa5 /reader.cpp
parent7e1a3148fb373fc1c75c2350f1a256b4f06bea1f (diff)
improve comments and extract a common function
Diffstat (limited to 'reader.cpp')
-rw-r--r--reader.cpp42
1 files changed, 19 insertions, 23 deletions
diff --git a/reader.cpp b/reader.cpp
index 8045467e..3a444902 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<completion_t> &comp)
+/* Determine the best match type */
+fuzzy_match_type_t get_best_match_type(const std::vector<completion_t> &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;
+}
+
+/* Order completions such that case insensitive completions come first. */
+static void prioritize_completions(std::vector<completion_t> &comp)
+{
+ fuzzy_match_type_t best_type = get_best_match_type(comp);
- /* Throw out completions whose match types are not the best. */
- i = comp.size();
+ /* 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<completion_t> &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<completion_t> &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;