known-words-checker.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. class KnownWordsChecker {
  2. static initClass() {
  3. this.prototype.enableAdd = false;
  4. this.prototype.spelling = null;
  5. this.prototype.checker = null;
  6. }
  7. constructor(knownWords) {
  8. // Set up the spelling manager we'll be using.
  9. const spellingManager = require('spelling-manager');
  10. this.spelling = new spellingManager.TokenSpellingManager();
  11. this.checker = new spellingManager.BufferSpellingChecker(this.spelling);
  12. // Set our known words.
  13. this.setKnownWords(knownWords);
  14. }
  15. deactivate() {}
  16. getId() {
  17. return 'spell-check:known-words';
  18. }
  19. getName() {
  20. return 'Known Words';
  21. }
  22. getPriority() {
  23. return 10;
  24. }
  25. isEnabled() {
  26. return this.spelling.sensitive || this.spelling.insensitive;
  27. }
  28. getStatus() {
  29. return 'Working correctly.';
  30. }
  31. providesSpelling(args) {
  32. return true;
  33. }
  34. providesSuggestions(args) {
  35. return true;
  36. }
  37. providesAdding(args) {
  38. return this.enableAdd;
  39. }
  40. check(args, text) {
  41. const ranges = [];
  42. const checked = this.checker.check(text);
  43. const id = this.getId();
  44. for (let token of checked) {
  45. if (token.status === 1) {
  46. ranges.push({ start: token.start, end: token.end });
  47. }
  48. }
  49. return { id, correct: ranges };
  50. }
  51. suggest(args, word) {
  52. return this.spelling.suggest(word);
  53. }
  54. getAddingTargets(args) {
  55. if (this.enableAdd) {
  56. return [{ sensitive: false, label: 'Add to ' + this.getName() }];
  57. } else {
  58. return [];
  59. }
  60. }
  61. add(args, target) {
  62. const c = atom.config.get('spell-check.knownWords');
  63. c.push(target.word);
  64. return atom.config.set('spell-check.knownWords', c);
  65. }
  66. setAddKnownWords(newValue) {
  67. return (this.enableAdd = newValue);
  68. }
  69. setKnownWords(knownWords) {
  70. // Clear out the old list.
  71. this.spelling.sensitive = {};
  72. this.spelling.insensitive = {};
  73. // Add the new ones into the list.
  74. if (knownWords) {
  75. return knownWords.map((ignore) => this.spelling.add(ignore));
  76. }
  77. }
  78. }
  79. KnownWordsChecker.initClass();
  80. module.exports = KnownWordsChecker;