]> khamidou — GitHub mirrors - electron.git/commitdiff
Use Chrome's policy for auto correct word
authorCheng Zhao <zcbenz@gmail.com>
Sat, 20 Dec 2014 04:56:16 +0000 (20:56 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 20 Dec 2014 04:56:16 +0000 (20:56 -0800)
atom/renderer/api/atom_api_spell_check_client.cc
atom/renderer/api/atom_api_spell_check_client.h

index 198c42a189c399f1c290c9bd96979194e07bcb40..e98f53d7dca9576cfe4162b0d58f785cb2e27a9f 100644 (file)
@@ -38,6 +38,8 @@ namespace api {
 
 namespace {
 
+const int kMaxAutoCorrectWordSize = 8;
+
 bool HasWordCharacters(const base::string16& text, int index) {
   const base::char16* data = text.data();
   int length = text.length();
@@ -139,11 +141,7 @@ void SpellCheckClient::requestCheckingOfText(
 
 blink::WebString SpellCheckClient::autoCorrectWord(
     const blink::WebString& misspelledWord) {
-  base::string16 result;
-  if (!CallProviderMethod("autoCorrectWord", misspelledWord, &result))
-    return blink::WebString();
-
-  return result;
+  return GetAutoCorrectionWord(base::string16(misspelledWord));
 }
 
 void SpellCheckClient::showSpellingUI(bool show) {
@@ -190,6 +188,53 @@ bool SpellCheckClient::CheckSpelling(const base::string16& word_to_check) {
     return true;
 }
 
+base::string16 SpellCheckClient::GetAutoCorrectionWord(
+    const base::string16& word) {
+  base::string16 autocorrect_word;
+
+  int word_length = static_cast<int>(word.size());
+  if (word_length < 2 || word_length > kMaxAutoCorrectWordSize)
+    return autocorrect_word;
+
+  base::char16 misspelled_word[kMaxAutoCorrectWordSize + 1];
+  const base::char16* word_char = word.c_str();
+  for (int i = 0; i <= kMaxAutoCorrectWordSize; ++i) {
+    if (i >= word_length)
+      misspelled_word[i] = 0;
+    else
+      misspelled_word[i] = word_char[i];
+  }
+
+  // Swap adjacent characters and spellcheck.
+  int misspelling_start, misspelling_len;
+  for (int i = 0; i < word_length - 1; i++) {
+    // Swap.
+    std::swap(misspelled_word[i], misspelled_word[i + 1]);
+
+    // Check spelling.
+    misspelling_start = misspelling_len = 0;
+    spellCheck(blink::WebString(misspelled_word, word_length),
+               misspelling_start,
+               misspelling_len,
+               NULL);
+
+    // Make decision: if only one swap produced a valid word, then we want to
+    // return it. If we found two or more, we don't do autocorrection.
+    if (misspelling_len == 0) {
+      if (autocorrect_word.empty()) {
+        autocorrect_word.assign(misspelled_word);
+      } else {
+        autocorrect_word.clear();
+        break;
+      }
+    }
+
+    // Restore the swapped characters.
+    std::swap(misspelled_word[i], misspelled_word[i + 1]);
+  }
+  return autocorrect_word;
+}
+
 // Returns whether or not the given string is a valid contraction.
 // This function is a fall-back when the SpellcheckWordIterator class
 // returns a concatenated word which is not in the selected dictionary
index e8c921b6a376a0476c8b4c7a92b3154b042e3af2..65aaf10c71ad88a243bb13fc228c62dfaeb6dfda 100644 (file)
@@ -53,6 +53,11 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
   // Call JavaScript to check spelling.
   bool CheckSpelling(const base::string16& word_to_check);
 
+  // Find a possible correctly spelled word for a misspelled word. Computes an
+  // empty string if input misspelled word is too long, there is ambiguity, or
+  // the correct spelling cannot be determined.
+  base::string16 GetAutoCorrectionWord(const base::string16& word);
+
   // Returns whether or not the given word is a contraction of valid words
   // (e.g. "word:word").
   bool IsValidContraction(const base::string16& word);