Fix for tab-completing arguments. Closes #1261

This commit is contained in:
ridiculousfish 2014-01-23 18:07:21 -08:00
parent 9edf9ad2ac
commit f6afddd94b
2 changed files with 14 additions and 2 deletions

View file

@ -1847,9 +1847,13 @@ void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> &comps
parse_node_tree_t tree;
parse_tree_from_string(cmd, parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens, &tree, NULL);
/* Find the plain statement that contains the position. We have to backtrack past spaces (#1261). So this will be at either the last space character, or after the end of the string */
size_t adjusted_pos = pos;
while (adjusted_pos > 0 && cmd.at(adjusted_pos - 1) == L' ')
adjusted_pos--;
/* Find the plain statement that contains the position */
const parse_node_t *plain_statement = tree.find_node_matching_source_location(symbol_plain_statement, pos, NULL);
const parse_node_t *plain_statement = tree.find_node_matching_source_location(symbol_plain_statement, adjusted_pos, NULL);
if (plain_statement != NULL)
{
assert(plain_statement->has_source() && plain_statement->type == symbol_plain_statement);

View file

@ -1510,6 +1510,14 @@ static void test_complete(void)
completions.clear();
complete(L"echo (builtin scuttlebut", completions, COMPLETION_REQUEST_DEFAULT);
assert(completions.size() == 0);
/* Trailing spaces (#1261) */
complete_add(L"foobarbaz", false, 0, NULL, 0, NO_FILES, NULL, L"qux", NULL, COMPLETE_AUTO_SPACE);
completions.clear();
complete(L"foobarbaz ", completions, COMPLETION_REQUEST_DEFAULT);
assert(completions.size() == 1);
assert(completions.at(0).completion == L"qux");
complete_set_variable_names(NULL);
}