keyword-manager.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const {Emitter, CompositeDisposable} = require('atom')
  2. const QuickHighlightView = require('./quick-highlight-view')
  3. const StatusBarManager = require('./status-bar-manager')
  4. const Colors = {
  5. colorNumbers: ['01', '02', '03', '04', '05', '06', '07'],
  6. next () {
  7. this.index = (this.index + 1) % this.colorNumbers.length
  8. return this.colorNumbers[this.index]
  9. },
  10. reset () {
  11. this.index = -1
  12. }
  13. }
  14. module.exports = class KeywordManager {
  15. constructor (mainEmitter) {
  16. this.colorByKeyword = new Map()
  17. this.reset()
  18. this.latestKeyword = null
  19. this.emitter = new Emitter()
  20. this.statusBarManager = new StatusBarManager()
  21. this.disposables = new CompositeDisposable(
  22. atom.workspace.observeTextEditors(editor => {
  23. /* eslint-disable no-new */
  24. new QuickHighlightView(editor, {
  25. keywordManager: this,
  26. statusBarManager: this.statusBarManager,
  27. emitter: mainEmitter
  28. })
  29. /* eslint-enable */
  30. }),
  31. atom.workspace.onDidChangeActivePaneItem(() => QuickHighlightView.refreshVisibles()),
  32. atom.config.observe('auto-highlight.decorate', newValue => {
  33. QuickHighlightView.clearAll()
  34. QuickHighlightView.refreshVisibles()
  35. })
  36. )
  37. }
  38. reset () {
  39. this.colorByKeyword.clear()
  40. Colors.reset()
  41. }
  42. setStatusBarService (service) {
  43. this.statusBarManager.initialize(service)
  44. this.statusBarManager.attach()
  45. }
  46. toggle (keyword) {
  47. if (this.colorByKeyword.has(keyword)) {
  48. this.colorByKeyword.delete(keyword)
  49. } else {
  50. this.colorByKeyword.set(keyword, Colors.next())
  51. this.latestKeyword = keyword
  52. }
  53. QuickHighlightView.refreshVisibles()
  54. }
  55. getColorForKeyword (keyword) {
  56. return this.colorByKeyword.get(keyword)
  57. }
  58. getKeywords () {
  59. return [...this.colorByKeyword.keys()]
  60. }
  61. clear () {
  62. this.reset()
  63. QuickHighlightView.clearAll()
  64. }
  65. destroy () {
  66. QuickHighlightView.destroyAll()
  67. this.disposables.dispose()
  68. this.disposables = null
  69. this.statusBarManager.detach()
  70. this.statusBarManager = null
  71. }
  72. }