system-checker.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. let instance;
  2. const spellchecker = require('spellchecker');
  3. const pathspec = require('atom-pathspec');
  4. const env = require('./checker-env');
  5. // Initialize the global spell checker which can take some time. We also force
  6. // the use of the system or operating system library instead of Hunspell.
  7. if (env.isSystemSupported()) {
  8. instance = new spellchecker.Spellchecker();
  9. instance.setSpellcheckerType(spellchecker.ALWAYS_USE_SYSTEM);
  10. if (!instance.setDictionary('', '')) {
  11. instance = undefined;
  12. }
  13. } else {
  14. instance = undefined;
  15. }
  16. // The `SystemChecker` is a special case to use the built-in system spell-checking
  17. // provided by some platforms, such as Windows 8+ and macOS. This also doesn't have
  18. // settings for specific locales because we need to use default, otherwise macOS
  19. // starts to throw an occasional error if you use multiple locales at the same time
  20. // due to some memory bug.
  21. class SystemChecker {
  22. constructor() {
  23. if (atom.config.get('spell-check.enableDebug')) {
  24. debug = require('debug');
  25. this.log = debug('spell-check:system-checker');
  26. } else {
  27. this.log = (str) => {};
  28. }
  29. this.log('enabled', this.isEnabled(), this.getStatus());
  30. }
  31. deactivate() {}
  32. getId() {
  33. return 'spell-check:system';
  34. }
  35. getName() {
  36. return 'System Checker';
  37. }
  38. getPriority() {
  39. return 110;
  40. }
  41. isEnabled() {
  42. return instance;
  43. }
  44. getStatus() {
  45. if (instance) {
  46. return 'working correctly';
  47. } else {
  48. return 'not supported on platform';
  49. }
  50. }
  51. providesSpelling(args) {
  52. return this.isEnabled();
  53. }
  54. providesSuggestions(args) {
  55. return this.isEnabled();
  56. }
  57. providesAdding(args) {
  58. return false;
  59. } // Users can't add yet.
  60. check(args, text) {
  61. const id = this.getId();
  62. if (this.isEnabled()) {
  63. // We use the default checker here and not the locale-specific one so it
  64. // will check all languages at the same time.
  65. return instance.checkSpellingAsync(text).then((incorrect) => {
  66. if (this.log.enabled) {
  67. this.log('check', incorrect);
  68. }
  69. return { id, invertIncorrectAsCorrect: true, incorrect };
  70. });
  71. } else {
  72. return { id, status: this.getStatus() };
  73. }
  74. }
  75. suggest(args, word) {
  76. return instance.getCorrectionsForMisspelling(word);
  77. }
  78. }
  79. module.exports = SystemChecker;