From e928f85cd79b302cea51a47f00cda582bfccb20d Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 11 Dec 2015 15:35:10 -0500 Subject: [PATCH] fix(spellcheck): add tests and merge in Linux changes --- spec/spellchecker-spec.coffee | 35 +++++++++++++++++++++++++++++++---- src/nylas-spellchecker.coffee | 28 ++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/spec/spellchecker-spec.coffee b/spec/spellchecker-spec.coffee index bb294b783..f3f7a1de4 100644 --- a/spec/spellchecker-spec.coffee +++ b/spec/spellchecker-spec.coffee @@ -40,6 +40,34 @@ describe "NylasSpellchecker", -> spyOn(@spellchecker, "getAvailableDictionaries").andReturn @fullDictList expect(@spellchecker.isLanguageAvailable("en")).toBe true + it "sets the correct default dictionary", -> + nodeSpellchecker = require('spellchecker') + spyOn(nodeSpellchecker, "setDictionary") + @spellchecker.setDictionary("en_US") + expect(nodeSpellchecker.setDictionary).toHaveBeenCalled() + expect(nodeSpellchecker.setDictionary.calls[0].args[0]).toBe "en_US" + dict = nodeSpellchecker.setDictionary.calls[0].args[1] + if process.platform is "darwin" + expect(dict.length).toBe 0 + else if process.platform is "win32" + expect(dict.length).toBe 0 + else if process.platform is "linux" + expect(dict.length).toBeGreaterThan 0 + + it "uses the passed-in dictionary", -> + nodeSpellchecker = require('spellchecker') + spyOn(nodeSpellchecker, "setDictionary") + @spellchecker.setDictionary("fr", "/path/to/dict") + expect(nodeSpellchecker.setDictionary).toHaveBeenCalled() + expect(nodeSpellchecker.setDictionary.calls[0].args[0]).toBe "fr" + dict = nodeSpellchecker.setDictionary.calls[0].args[1] + if process.platform is "darwin" + expect(dict).toBe "/path/to/dict" + else if process.platform is "win32" + expect(dict).toBe "/path/to/dict" + else if process.platform is "linux" + expect(dict).toBe "/path/to/dict" + describe "when we don't recognize the language", -> beforeEach -> spyOn(@spellchecker, "getAvailableDictionaries").andReturn ["foo"] @@ -87,12 +115,11 @@ describe "NylasSpellchecker", -> it "provides options for misspelled words", -> expect(@spellchecker.getCorrectionsForMisspelling("")).toEqual [] - # TODO: Determine the reason OS X and/or Windows return an empty array - # for the string. -mbilker + if process.platform is 'linux' expect(@spellchecker.getCorrectionsForMisspelling("asdfk")).toEqual ['asked', 'acidify', 'Assad'] - else - expect(@spellchecker.getCorrectionsForMisspelling("asdfk")).toEqual [] + else if process.platofrm is "darwin" + expect(@spellchecker.getCorrectionsForMisspelling("testt")).toEqual [ 'test', 'tests', 'testy', 'testa' ] it "still provides options for correctly spelled workds", -> expect(@spellchecker.getCorrectionsForMisspelling("hello").length).toBeGreaterThan 1 diff --git a/src/nylas-spellchecker.coffee b/src/nylas-spellchecker.coffee index 132f84670..5aa56d7b5 100644 --- a/src/nylas-spellchecker.coffee +++ b/src/nylas-spellchecker.coffee @@ -16,7 +16,7 @@ class NylasSpellchecker isSpelledCorrectly: (args...) => not @isMisspelled(args...) - setLanguage: (lang="", dict=@_getDictionaryPath()) -> + setLanguage: (lang="", dict="") -> @languageAvailable = @isLanguageAvailable(lang) if @languageAvailable spellCheck = @isSpelledCorrectly @@ -28,15 +28,35 @@ class NylasSpellchecker if lang.length is 0 then lang = "en-US" @_setWebframeSpellchecker(lang, spellCheck) + + if process.platform is "linux" + dictionaryPath = @_getHunspellDictionary() + else + # On Mac we defer to NSSpellChecker + # On Windows we use the Windows Spell Check API + # + # Both of those automatically provide a set of dictionaries based on + # the language string. + # + # On Windows 10 you can see the dictionaries that are available by + # looking in: C:\Users\YourName\AppData\Roaming\Microsoft\Spelling + # + # The `dictionaryPath` parameter is ignored on Mac & Windows by + # node-spellchecker + dictionaryPath = "" + + if dict.length is 0 then dict = dictionaryPath + spellchecker.setDictionary(lang, dict) # Separate method for testing _setWebframeSpellchecker: (lang, spellCheck) -> require('web-frame').setSpellCheckProvider(lang, false, {spellCheck}) - # node-spellchecker's method for resolving the builtin hunspell dictionaries for Linux - # (From https://github.com/atom/node-spellchecker/blob/master/lib/spellchecker.js#L50-L61) - _getDictionaryPath: -> + # node-spellchecker's method for resolving the builtin hunspell + # dictionaries for Linux (From + # https://github.com/atom/node-spellchecker/blob/master/lib/spellchecker.js#L50-L61) + _getHunspellDictionary: -> dict = path.join(require.resolve('spellchecker'), '..', '..', 'vendor', 'hunspell_dictionaries') try # HACK: Special case being in an asar archive