Fix to properly handle case insensitive autosuggestions

This commit is contained in:
ridiculousfish 2012-03-01 17:31:45 -08:00
parent fd4df6f9bb
commit 36622c3578
3 changed files with 31 additions and 8 deletions

View file

@ -1961,6 +1961,11 @@ bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &val
return prefix_size <= value.size() && value.compare(0, prefix_size, proposed_prefix) == 0; return prefix_size <= value.size() && value.compare(0, prefix_size, proposed_prefix) == 0;
} }
bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix, const wcstring &value) {
size_t prefix_size = proposed_prefix.size();
return prefix_size <= value.size() && wcsncasecmp(proposed_prefix.c_str(), value.c_str(), prefix_size) == 0;
}
bool list_contains_string(const wcstring_list_t &list, const wcstring &str) bool list_contains_string(const wcstring_list_t &list, const wcstring &str)
{ {
return std::find(list.begin(), list.end(), str) != list.end(); return std::find(list.begin(), list.end(), str) != list.end();

View file

@ -252,6 +252,9 @@ std::string wcs2string(const wcstring &input);
/** Test if a string prefixes another. Returns true if a is a prefix of b */ /** Test if a string prefixes another. Returns true if a is a prefix of b */
bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &value); bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &value);
/** Test if a string prefixes another without regard to case. Returns true if a is a prefix of b */
bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix, const wcstring &value);
/** Test if a list contains a string using a linear search. */ /** Test if a list contains a string using a linear search. */
bool list_contains_string(const wcstring_list_t &list, const wcstring &str); bool list_contains_string(const wcstring_list_t &list, const wcstring &str);

View file

@ -935,9 +935,9 @@ static void completion_insert( const wchar_t *val, int flags )
wchar_t *replaced; wchar_t *replaced;
wchar_t quote; wchar_t quote;
int add_space = !(flags & COMPLETE_NO_SPACE); bool add_space = !(flags & COMPLETE_NO_SPACE);
int do_replace = (flags & COMPLETE_NO_CASE); bool do_replace = !!(flags & COMPLETE_NO_CASE);
int do_escape = !(flags & COMPLETE_DONT_ESCAPE); bool do_escape = !(flags & COMPLETE_DONT_ESCAPE);
// debug( 0, L"Insert completion %ls with flags %d", val, flags); // debug( 0, L"Insert completion %ls with flags %d", val, flags);
@ -1283,9 +1283,24 @@ struct autosuggestion_context_t {
/* Try normal completions */ /* Try normal completions */
std::vector<completion_t> completions; std::vector<completion_t> completions;
complete(search_string, completions, COMPLETE_AUTOSUGGEST, &this->commands_to_load); complete(search_string, completions, COMPLETE_AUTOSUGGEST, &this->commands_to_load);
if (! completions.empty()) { if (! completions.empty()) {
const completion_t &comp = completions.at(0);
this->autosuggestion = this->search_string; this->autosuggestion = this->search_string;
this->autosuggestion.append(completions.at(0).completion);
if (comp.flags & COMPLETE_NO_CASE) {
/* The completion contains the whole current token, not merely a suffix */
const wchar_t *begin;
const wchar_t *buff = data->command_line.c_str();
parse_util_token_extent( buff, data->buff_pos, &begin, 0, 0, 0 );
size_t start = begin - buff;
assert(data->buff_pos >= start);
this->autosuggestion.replace(start, data->buff_pos - start, comp.completion);
} else {
/* The completion contains only a suffix */
this->autosuggestion.append(comp.completion);
}
return 1; return 1;
} }
@ -1325,11 +1340,11 @@ static void autosuggest_completed(autosuggestion_context_t *ctx, int result) {
iothread_perform(threaded_autosuggest, autosuggest_completed, ctx); iothread_perform(threaded_autosuggest, autosuggest_completed, ctx);
return; return;
} }
if (result && if (result &&
can_autosuggest() && can_autosuggest() &&
ctx->search_string == data->command_line && ctx->search_string == data->command_line &&
string_prefixes_string(ctx->search_string, ctx->autosuggestion)) { string_prefixes_string_case_insensitive(ctx->search_string, ctx->autosuggestion)) {
/* Autosuggestion is active and the search term has not changed, so we're good to go */ /* Autosuggestion is active and the search term has not changed, so we're good to go */
data->autosuggestion = ctx->autosuggestion; data->autosuggestion = ctx->autosuggestion;
sanity_check(); sanity_check();
@ -2482,7 +2497,7 @@ static void reader_super_highlight_me_plenty( int match_highlight_pos )
/* Here's a hack. Check to see if our autosuggestion still applies; if so, don't recompute it. Since the autosuggestion computation is asynchronous, this avoids "flashing" as you type into the autosuggestion. */ /* Here's a hack. Check to see if our autosuggestion still applies; if so, don't recompute it. Since the autosuggestion computation is asynchronous, this avoids "flashing" as you type into the autosuggestion. */
const wcstring &cmd = data->command_line, &suggest = data->autosuggestion; const wcstring &cmd = data->command_line, &suggest = data->autosuggestion;
if (can_autosuggest() && ! suggest.empty() && string_prefixes_string(cmd, suggest)) { if (can_autosuggest() && ! suggest.empty() && string_prefixes_string_case_insensitive(cmd, suggest)) {
/* The autosuggestion is still reasonable, so do nothing */ /* The autosuggestion is still reasonable, so do nothing */
} else { } else {
update_autosuggestion(); update_autosuggestion();