Allow variable completion from just a $

Previously there had to be some variable text, now you can
tab complete from just a naked $.
This commit is contained in:
ridiculousfish 2016-02-27 16:28:47 -08:00
parent 99be3ee96f
commit 6cb48c6380
2 changed files with 22 additions and 6 deletions

View File

@ -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);
}

View File

@ -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");