Skip to content

Commit f6c66e7

Browse files
committed
Use Chrome's policy for auto correct word
1 parent c6a18b1 commit f6c66e7

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

atom/renderer/api/atom_api_spell_check_client.cc

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace api {
3838

3939
namespace {
4040

41+
const int kMaxAutoCorrectWordSize = 8;
42+
4143
bool HasWordCharacters(const base::string16& text, int index) {
4244
const base::char16* data = text.data();
4345
int length = text.length();
@@ -139,11 +141,7 @@ void SpellCheckClient::requestCheckingOfText(
139141

140142
blink::WebString SpellCheckClient::autoCorrectWord(
141143
const blink::WebString& misspelledWord) {
142-
base::string16 result;
143-
if (!CallProviderMethod("autoCorrectWord", misspelledWord, &result))
144-
return blink::WebString();
145-
146-
return result;
144+
return GetAutoCorrectionWord(base::string16(misspelledWord));
147145
}
148146

149147
void SpellCheckClient::showSpellingUI(bool show) {
@@ -190,6 +188,53 @@ bool SpellCheckClient::CheckSpelling(const base::string16& word_to_check) {
190188
return true;
191189
}
192190

191+
base::string16 SpellCheckClient::GetAutoCorrectionWord(
192+
const base::string16& word) {
193+
base::string16 autocorrect_word;
194+
195+
int word_length = static_cast<int>(word.size());
196+
if (word_length < 2 || word_length > kMaxAutoCorrectWordSize)
197+
return autocorrect_word;
198+
199+
base::char16 misspelled_word[kMaxAutoCorrectWordSize + 1];
200+
const base::char16* word_char = word.c_str();
201+
for (int i = 0; i <= kMaxAutoCorrectWordSize; ++i) {
202+
if (i >= word_length)
203+
misspelled_word[i] = 0;
204+
else
205+
misspelled_word[i] = word_char[i];
206+
}
207+
208+
// Swap adjacent characters and spellcheck.
209+
int misspelling_start, misspelling_len;
210+
for (int i = 0; i < word_length - 1; i++) {
211+
// Swap.
212+
std::swap(misspelled_word[i], misspelled_word[i + 1]);
213+
214+
// Check spelling.
215+
misspelling_start = misspelling_len = 0;
216+
spellCheck(blink::WebString(misspelled_word, word_length),
217+
misspelling_start,
218+
misspelling_len,
219+
NULL);
220+
221+
// Make decision: if only one swap produced a valid word, then we want to
222+
// return it. If we found two or more, we don't do autocorrection.
223+
if (misspelling_len == 0) {
224+
if (autocorrect_word.empty()) {
225+
autocorrect_word.assign(misspelled_word);
226+
} else {
227+
autocorrect_word.clear();
228+
break;
229+
}
230+
}
231+
232+
// Restore the swapped characters.
233+
std::swap(misspelled_word[i], misspelled_word[i + 1]);
234+
}
235+
return autocorrect_word;
236+
}
237+
193238
// Returns whether or not the given string is a valid contraction.
194239
// This function is a fall-back when the SpellcheckWordIterator class
195240
// returns a concatenated word which is not in the selected dictionary

atom/renderer/api/atom_api_spell_check_client.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
5353
// Call JavaScript to check spelling.
5454
bool CheckSpelling(const base::string16& word_to_check);
5555

56+
// Find a possible correctly spelled word for a misspelled word. Computes an
57+
// empty string if input misspelled word is too long, there is ambiguity, or
58+
// the correct spelling cannot be determined.
59+
base::string16 GetAutoCorrectionWord(const base::string16& word);
60+
5661
// Returns whether or not the given word is a contraction of valid words
5762
// (e.g. "word:word").
5863
bool IsValidContraction(const base::string16& word);

0 commit comments

Comments
 (0)