From 3ed35574d48f400cf5bd838604988548266e40bf Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 24 Mar 2013 15:45:15 +0800 Subject: [PATCH] Use node-nsspellchecker to replace $native's spell checker. --- native/v8_extensions/native.mm | 31 +------------------ package.json | 1 + .../spell-check/lib/misspelling-view.coffee | 3 +- .../lib/spell-check-handler.coffee | 4 ++- 4 files changed, 7 insertions(+), 32 deletions(-) diff --git a/native/v8_extensions/native.mm b/native/v8_extensions/native.mm index 8211c7958..03c92eda0 100644 --- a/native/v8_extensions/native.mm +++ b/native/v8_extensions/native.mm @@ -24,8 +24,7 @@ namespace v8_extensions { const char* methodNames[] = { "writeToPasteboard", "readFromPasteboard", "quit", "watchPath", "unwatchPath", "getWatchedPaths", "unwatchAllPaths", "moveToTrash", - "reload", "setWindowState", "getWindowState", "isMisspelled", - "getCorrectionsForMisspelling", "beep" + "reload", "setWindowState", "getWindowState", "beep" }; CefRefPtr nativeObject = CefV8Value::CreateObject(NULL); @@ -163,34 +162,6 @@ namespace v8_extensions { return true; } - else if (name == "isMisspelled") { - NSString *word = stringFromCefV8Value(arguments[0]); - NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker]; - @synchronized(spellChecker) { - NSRange range = [spellChecker checkSpellingOfString:word startingAt:0]; - retval = CefV8Value::CreateBool(range.length > 0); - } - return true; - } - - else if (name == "getCorrectionsForMisspelling") { - NSString *misspelling = stringFromCefV8Value(arguments[0]); - NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker]; - @synchronized(spellChecker) { - NSString *language = [spellChecker language]; - NSRange range; - range.location = 0; - range.length = [misspelling length]; - NSArray *guesses = [spellChecker guessesForWordRange:range inString:misspelling language:language inSpellDocumentWithTag:0]; - CefRefPtr v8Guesses = CefV8Value::CreateArray([guesses count]); - for (int i = 0; i < [guesses count]; i++) { - v8Guesses->SetValue(i, CefV8Value::CreateString([[guesses objectAtIndex:i] UTF8String])); - } - retval = v8Guesses; - } - return true; - } - else if (name == "beep") { NSBeep(); } diff --git a/package.json b/package.json index 8f1d8129a..584bc949d 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "coffee-cache": "0.1.0", "pegjs": "0.7.0", "async": "0.2.6", + "nsspellchecker": "0.1.1", "plist": "git://github.com/nathansobo/node-plist.git" }, diff --git a/src/packages/spell-check/lib/misspelling-view.coffee b/src/packages/spell-check/lib/misspelling-view.coffee index ba57e4164..4f1d898d2 100644 --- a/src/packages/spell-check/lib/misspelling-view.coffee +++ b/src/packages/spell-check/lib/misspelling-view.coffee @@ -1,6 +1,7 @@ {View} = require 'space-pen' Range = require 'range' CorrectionsView = require './corrections-view' +NSSpellChecker = require 'nsspellchecker' module.exports = class MisspellingView extends View @@ -30,7 +31,7 @@ class MisspellingView extends View screenRange = @getScreenRange() misspelling = @editor.getTextInRange(@editor.bufferRangeForScreenRange(screenRange)) - corrections = $native.getCorrectionsForMisspelling(misspelling) + corrections = NSSpellChecker.getCorrectionsForMisspelling(misspelling) @correctionsView?.remove() @correctionsView = new CorrectionsView(@editor, corrections, screenRange) diff --git a/src/packages/spell-check/lib/spell-check-handler.coffee b/src/packages/spell-check/lib/spell-check-handler.coffee index 8040f4991..9dc59ef80 100644 --- a/src/packages/spell-check/lib/spell-check-handler.coffee +++ b/src/packages/spell-check/lib/spell-check-handler.coffee @@ -1,3 +1,5 @@ +NSSpellChecker = require 'nsspellchecker' + module.exports = findMisspellings: (text) -> wordRegex = /(?:^|[\s\[\]])([a-zA-Z']+)(?=[\s\.\[\]]|$)/g @@ -6,7 +8,7 @@ module.exports = for line in text.split('\n') while matches = wordRegex.exec(line) word = matches[1] - continue unless $native.isMisspelled(word) + continue unless NSSpellChecker.isMisspelled(word) startColumn = matches.index + matches[0].length - word.length endColumn = startColumn + word.length misspellings.push([[row, startColumn], [row, endColumn]])