diff --git a/src/complete.cpp b/src/complete.cpp index 06daaa1b5..132dbb74d 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_posflags & COMPLETION_REQUEST_AUTOSUGGESTION); + bool text_is_empty = (variable_start == len); bool result = false; - if (variable_start != static_cast(-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 9be273e60..d6972139b 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 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");