diff --git a/src/complete.cpp b/src/complete.cpp index 24e0688bf..2e4d69ce1 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -326,6 +326,13 @@ bool completion_t::is_alphabetically_equal_to(const completion_t &a, const compl return a.completion == b.completion; } +void completion_t::prepend_token_prefix(const wcstring &prefix) +{ + if (this->flags & COMPLETE_REPLACES_TOKEN) + { + this->completion.insert(0, prefix); + } +} /** Class representing an attempt to compute completions */ class completer_t @@ -1647,15 +1654,11 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file, bool debug(3, L"Error while expanding string '%ls'", sep_string.c_str()); } - /* Hack hack hack. Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by inserting our separator and prefix. */ + /* Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by inserting our separator and prefix. */ const wcstring prefix_with_sep = wcstring(str, 0, sep_index + 1); for (size_t i=0; i < local_completions.size(); i++) { - completion_t *c = &local_completions.at(i); - if (c->flags & COMPLETE_REPLACES_TOKEN) - { - c->completion.insert(0, prefix_with_sep); - } + local_completions.at(i).prepend_token_prefix(prefix_with_sep); } this->completions.insert(this->completions.end(), local_completions.begin(), diff --git a/src/complete.h b/src/complete.h index 466ddbe9c..deb0c42a5 100644 --- a/src/complete.h +++ b/src/complete.h @@ -125,6 +125,9 @@ public: /* "Naturally less than" means in a natural ordering, where digits are treated as numbers. For example, foo10 is naturally greater than foo2 (but alphabetically less than it) */ static bool is_naturally_less_than(const completion_t &a, const completion_t &b); static bool is_alphabetically_equal_to(const completion_t &a, const completion_t &b); + + /* If this completion replaces the entire token, prepend a prefix. Otherwise do nothing. */ + void prepend_token_prefix(const wcstring &prefix); }; enum diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 157ee5abd..5df91f1d8 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -1475,6 +1475,11 @@ static void test_expand() L"/tmp/fish_expand_test/bar", L"/tmp/fish_expand_test/bax", L"/tmp/fish_expand_test/bax/xxx", L"/tmp/fish_expand_test/baz", L"/tmp/fish_expand_test/baz/xxx", wnull, L"Glob did the wrong thing"); + expand_test(L"/tmp/fish_expand_test/BA", FOR_COMPLETIONS, + L"/tmp/fish_expand_test/bar", L"/tmp/fish_expand_test/bax/", L"/tmp/fish_expand_test/baz/", wnull, + L"Case insensitive test did the wrong thing"); + + if (! expand_test(L"/tmp/fish_expand_test/.*", 0, L"/tmp/fish_expand_test/.foo", 0)) { diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 6b90e6b00..6cbf2d1e9 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -826,6 +826,7 @@ void wildcard_expander_t::expand_last_segment(const wcstring &base_dir, DIR *bas } else { + // Normal wildcard expansion, not for completions if (wildcard_match(name_str, wc, true /* skip files with leading dots */)) { const wcstring abs_path = base_dir + name_str; @@ -1277,13 +1278,10 @@ static int wildcard_expand(const wchar_t *wc, for (size_t i=c; isize(); i++) { completion_t &c = out->at(i); - - if (c.flags & COMPLETE_REPLACES_TOKEN) - { - // completion = base_dir + wc_base + completion - c.completion.insert(0, wc_base); - c.completion.insert(0, base_dir); - } + + // completion = base_dir + wc_base + completion + c.prepend_token_prefix(wc_base); + c.prepend_token_prefix(base_dir); } } if (expander.interrupted())