Chromium Code Reviews| Index: chrome/browser/autocomplete/keyword_provider.cc |
| diff --git a/chrome/browser/autocomplete/keyword_provider.cc b/chrome/browser/autocomplete/keyword_provider.cc |
| index c37da20e228f36040e821a071aecc4df86b70668..e56eeaf6a5cd2ba171cf87d7fc8577d957cbde01 100644 |
| --- a/chrome/browser/autocomplete/keyword_provider.cc |
| +++ b/chrome/browser/autocomplete/keyword_provider.cc |
| @@ -8,6 +8,7 @@ |
| #include <vector> |
| #include "base/string16.h" |
| +#include "base/string_util.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/autocomplete/autocomplete_match.h" |
| #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| @@ -136,25 +137,45 @@ string16 KeywordProvider::SplitReplacementStringFromInput( |
| // static |
| const TemplateURL* KeywordProvider::GetSubstitutingTemplateURLForInput( |
| - Profile* profile, |
| - const AutocompleteInput& input, |
| - string16* remaining_input) { |
| - if (!input.allow_exact_keyword_match()) |
| + TemplateURLService* model, |
| + AutocompleteInput* input) { |
| + if (!input->allow_exact_keyword_match()) |
| return NULL; |
| - string16 keyword; |
| - if (!ExtractKeywordFromInput(input, &keyword, remaining_input)) |
| + string16 keyword, remaining_input; |
| + if (!ExtractKeywordFromInput(*input, &keyword, &remaining_input)) |
| return NULL; |
| - // Make sure the model is loaded. This is cheap and quickly bails out if |
| - // the model is already loaded. |
| - TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile); |
| DCHECK(model); |
| - model->Load(); |
| - |
| const TemplateURL* template_url = model->GetTemplateURLForKeyword(keyword); |
| - return (template_url && template_url->SupportsReplacement()) ? |
| - template_url : NULL; |
| + if (template_url && template_url->SupportsReplacement()) { |
| + // Adjust cursor position iff it was set before, otherwise leave it as is. |
| + size_t cursor_position = input->cursor_position(); |
| + // The adjustment assumes that the keyword was stripped from the beginning |
| + // of the original input. |
| + if (cursor_position != string16::npos && |
| + EndsWith(input->text(), remaining_input, true)) { |
| + int offset = input->text().length() - cursor_position; |
| + // The cursor should never be past the last character. |
| + DCHECK_GE(offset, 0); |
| + // The cursor should never be in the keyword part, which is guaranteed |
| + // by OmniboxEditModel implementation (see omnibox_edit_model.cc). |
| + DCHECK_LE(offset, static_cast<int>(remaining_input.length())); |
| + if (offset <= 0) { |
| + // Normalize the cursor to be exactly after the last character. |
| + cursor_position = remaining_input.length(); |
| + } else { |
| + // If somehow the cursor was before the remaining text, set it to 0, |
| + // otherwise adjust it with relatively to the remaining text. |
|
Mark P
2013/01/29 22:25:37
with relatively
->
relative
Bart N.
2013/01/29 22:57:05
Done.
|
| + cursor_position = offset > static_cast<int>(remaining_input.length()) ? |
| + 0u : remaining_input.length() - offset; |
| + } |
| + } |
| + input->UpdateText(remaining_input, cursor_position, input->parts()); |
| + return template_url; |
| + } |
| + |
| + return NULL; |
| } |
| string16 KeywordProvider::GetKeywordForText( |