diff options
-rw-r--r-- | src/complete.cpp | 10 | ||||
-rw-r--r-- | src/fish_tests.cpp | 18 |
2 files changed, 22 insertions, 6 deletions
diff --git a/src/complete.cpp b/src/complete.cpp index 06daaa1b..132dbb74 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -1505,8 +1505,8 @@ bool completer_t::try_complete_variable(const wcstring &str) enum {e_unquoted, e_single_quoted, e_double_quoted} mode = e_unquoted; const size_t len = str.size(); - /* Get the position of the dollar heading a run of valid variable characters. -1 means none. */ - size_t variable_start = -1; + /* Get the position of the dollar heading a (possibly empty) run of valid variable characters. npos means none. */ + size_t variable_start = wcstring::npos; for (size_t in_pos=0; in_pos<len; in_pos++) { @@ -1554,9 +1554,11 @@ bool completer_t::try_complete_variable(const wcstring &str) } } - /* Now complete if we have a variable start that's also not the last character */ + /* Now complete if we have a variable start. Note the variable text may be empty; in that case don't generate an autosuggestion, but do allow tab completion */ + bool allow_empty = ! (this->flags & COMPLETION_REQUEST_AUTOSUGGESTION); + bool text_is_empty = (variable_start == len); bool result = false; - if (variable_start != static_cast<size_t>(-1) && variable_start + 1 < len) + if (variable_start != wcstring::npos && (allow_empty || ! text_is_empty)) { result = this->complete_variable(str, variable_start + 1); } diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 9be273e6..d6972139 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2106,7 +2106,19 @@ static void test_complete(void) const env_vars_snapshot_t &vars = env_vars_snapshot_t::current(); std::vector<completion_t> completions; + complete(L"$", &completions, COMPLETION_REQUEST_DEFAULT, vars); + completions_sort_and_prioritize(&completions); + do_test(completions.size() == 6); + do_test(completions.at(0).completion == L"Bar1"); + do_test(completions.at(1).completion == L"Bar2"); + do_test(completions.at(2).completion == L"Bar3"); + do_test(completions.at(3).completion == L"Foo1"); + do_test(completions.at(4).completion == L"Foo2"); + do_test(completions.at(5).completion == L"Foo3"); + + completions.clear(); complete(L"$F", &completions, COMPLETION_REQUEST_DEFAULT, vars); + completions_sort_and_prioritize(&completions); do_test(completions.size() == 3); do_test(completions.at(0).completion == L"oo1"); do_test(completions.at(1).completion == L"oo2"); @@ -2114,13 +2126,15 @@ static void test_complete(void) completions.clear(); complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT, vars); + completions_sort_and_prioritize(&completions); do_test(completions.empty()); completions.clear(); complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_FUZZY_MATCH, vars); + completions_sort_and_prioritize(&completions); do_test(completions.size() == 2); - do_test(completions.at(0).completion == L"$Foo1"); - do_test(completions.at(1).completion == L"$Bar1"); + do_test(completions.at(0).completion == L"$Bar1"); + do_test(completions.at(1).completion == L"$Foo1"); if (system("mkdir -p '/tmp/complete_test/'")) err(L"mkdir failed"); if (system("touch '/tmp/complete_test/testfile'")) err(L"touch failed"); |