Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: components/spellcheck/renderer/spellcheck.cc

Issue 2911253003: [Reland] Allow storing multiple replacements on SpellCheckResult (Closed)
Patch Set: Filter apostrophe logic into helper CL Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/spellcheck/renderer/spellcheck.h" 5 #include "components/spellcheck/renderer/spellcheck.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (IsApostrophe(c)) { 106 if (IsApostrophe(c)) {
107 it = std::find_if(it, spelling_suggestion->end(), IsApostrophe); 107 it = std::find_if(it, spelling_suggestion->end(), IsApostrophe);
108 if (it == spelling_suggestion->end()) 108 if (it == spelling_suggestion->end())
109 return; 109 return;
110 110
111 *it++ = c; 111 *it++ = c;
112 } 112 }
113 } 113 }
114 } 114 }
115 115
116 std::vector<WebString> FilterReplacementSuggestions(
rlanday 2017/05/30 21:48:44 Ok, I've filtered out this logic into a helper fun
117 const base::string16& misspelled_word,
118 const std::vector<base::string16>& replacements) {
119 std::vector<WebString> replacements_filtered;
120 for (base::string16 replacement : replacements) {
121 // Use the same types of appostrophes as in the mispelled word.
Xiaocheng 2017/05/31 02:05:25 nit: s/appostrophes/apostrophes/
122 PreserveOriginalApostropheTypes(misspelled_word, &replacement);
123
124 // Ignore suggestions that are just changing the apostrophe type
125 // (straight vs. typographical)
126 if (replacement == misspelled_word)
127 continue;
128
129 replacements_filtered.push_back(WebString::FromUTF16(replacement));
130 }
131
132 return replacements_filtered;
133 }
134
116 } // namespace 135 } // namespace
117 136
118 class SpellCheck::SpellcheckRequest { 137 class SpellCheck::SpellcheckRequest {
119 public: 138 public:
120 SpellcheckRequest(const base::string16& text, 139 SpellcheckRequest(const base::string16& text,
121 blink::WebTextCheckingCompletion* completion) 140 blink::WebTextCheckingCompletion* completion)
122 : text_(text), completion_(completion) { 141 : text_(text), completion_(completion) {
123 DCHECK(completion); 142 DCHECK(completion);
124 } 143 }
125 ~SpellcheckRequest() {} 144 ~SpellcheckRequest() {}
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 std::vector<WebTextCheckingResult> results; 485 std::vector<WebTextCheckingResult> results;
467 for (const SpellCheckResult& spellcheck_result : spellcheck_results) { 486 for (const SpellCheckResult& spellcheck_result : spellcheck_results) {
468 DCHECK_LE(static_cast<size_t>(spellcheck_result.location), 487 DCHECK_LE(static_cast<size_t>(spellcheck_result.location),
469 line_text.length()); 488 line_text.length());
470 DCHECK_LE(static_cast<size_t>(spellcheck_result.location + 489 DCHECK_LE(static_cast<size_t>(spellcheck_result.location +
471 spellcheck_result.length), 490 spellcheck_result.length),
472 line_text.length()); 491 line_text.length());
473 492
474 const base::string16& misspelled_word = 493 const base::string16& misspelled_word =
475 line_text.substr(spellcheck_result.location, spellcheck_result.length); 494 line_text.substr(spellcheck_result.location, spellcheck_result.length);
476 base::string16 replacement = spellcheck_result.replacement; 495 const std::vector<base::string16>& replacements =
496 spellcheck_result.replacements;
477 SpellCheckResult::Decoration decoration = spellcheck_result.decoration; 497 SpellCheckResult::Decoration decoration = spellcheck_result.decoration;
478 498
479 // Ignore words in custom dictionary. 499 // Ignore words in custom dictionary.
480 if (custom_dictionary_.SpellCheckWord(misspelled_word, 0, 500 if (custom_dictionary_.SpellCheckWord(misspelled_word, 0,
481 misspelled_word.length())) { 501 misspelled_word.length())) {
482 continue; 502 continue;
483 } 503 }
484 504
485 // Use the same types of appostrophes as in the mispelled word. 505 std::vector<WebString> replacements_filtered =
486 PreserveOriginalApostropheTypes(misspelled_word, &replacement); 506 FilterReplacementSuggestions(misspelled_word, replacements);
487 507
488 // Ignore misspellings due the typographical apostrophe. 508 // If the spellchecker suggested replacements, but they were all just
489 if (misspelled_word == replacement) 509 // changing apostrophe styles, ignore this misspelling. If there were never
510 // any suggested replacements, keep the misspelling.
511 if (replacements_filtered.empty() && !replacements.empty())
490 continue; 512 continue;
491 513
492 if (filter == USE_NATIVE_CHECKER) { 514 if (filter == USE_NATIVE_CHECKER) {
493 // Double-check misspelled words with out spellchecker and attach grammar 515 // Double-check misspelled words with out spellchecker and attach grammar
494 // markers to them if our spellchecker tells us they are correct words, 516 // markers to them if our spellchecker tells us they are correct words,
495 // i.e. they are probably contextually-misspelled words. 517 // i.e. they are probably contextually-misspelled words.
496 int unused_misspelling_start = 0; 518 int unused_misspelling_start = 0;
497 int unused_misspelling_length = 0; 519 int unused_misspelling_length = 0;
498 if (decoration == SpellCheckResult::SPELLING && 520 if (decoration == SpellCheckResult::SPELLING &&
499 SpellCheckWord(misspelled_word.c_str(), kNoOffset, 521 SpellCheckWord(misspelled_word.c_str(), kNoOffset,
500 misspelled_word.length(), kNoTag, 522 misspelled_word.length(), kNoTag,
501 &unused_misspelling_start, &unused_misspelling_length, 523 &unused_misspelling_start, &unused_misspelling_length,
502 nullptr)) { 524 nullptr)) {
503 decoration = SpellCheckResult::GRAMMAR; 525 decoration = SpellCheckResult::GRAMMAR;
504 } 526 }
505 } 527 }
506 528
507 results.push_back(WebTextCheckingResult( 529 results.push_back(
508 static_cast<WebTextDecorationType>(decoration), 530 WebTextCheckingResult(static_cast<WebTextDecorationType>(decoration),
509 line_offset + spellcheck_result.location, spellcheck_result.length, 531 line_offset + spellcheck_result.location,
510 blink::WebString::FromUTF16(replacement))); 532 spellcheck_result.length, replacements_filtered));
511 } 533 }
512 534
513 textcheck_results->Assign(results); 535 textcheck_results->Assign(results);
514 } 536 }
515 537
516 bool SpellCheck::IsSpellcheckEnabled() { 538 bool SpellCheck::IsSpellcheckEnabled() {
517 #if defined(OS_ANDROID) 539 #if defined(OS_ANDROID)
518 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false; 540 if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false;
519 #endif 541 #endif
520 return spellcheck_enabled_; 542 return spellcheck_enabled_;
521 } 543 }
OLDNEW
« no previous file with comments | « components/spellcheck/common/spellcheck_result.cc ('k') | components/spellcheck/renderer/spellcheck_provider_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698