From f6afddd94bd3f47e414033746d038c5b222b63a3 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 23 Jan 2014 18:07:21 -0800 Subject: [PATCH] Fix for tab-completing arguments. Closes #1261 --- complete.cpp | 8 ++++++-- fish_tests.cpp | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/complete.cpp b/complete.cpp index e5b775768..d38104dd5 100644 --- a/complete.cpp +++ b/complete.cpp @@ -1847,9 +1847,13 @@ void complete(const wcstring &cmd_with_subcmds, std::vector &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); diff --git a/fish_tests.cpp b/fish_tests.cpp index 8b08365b9..c87bee69d 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -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); }