1
0

auto-highlight-spec.coffee 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. _ = require 'underscore-plus'
  2. # Helpers
  3. # -------------------------
  4. getDecorations = (editor) ->
  5. pattern = /^auto-highlight/
  6. decorations = []
  7. for id, decoration of editor.decorationsStateForScreenRowRange(0, editor.getLineCount())
  8. if decoration.properties.class?.match(pattern)
  9. decorations.push(decoration)
  10. decorations
  11. ensureDecorations = (editor, options) ->
  12. decorations = getDecorations(editor)
  13. groupedDecoration = _.groupBy decorations, (decoration) ->
  14. decoration.properties.class.replace(/^auto-highlight /, '')
  15. toText = ({bufferRange}) -> editor.getTextInBufferRange(bufferRange)
  16. for color, texts of options
  17. decoratedTexts = groupedDecoration[color].map(toText)
  18. expect(decoratedTexts).toEqual(texts)
  19. delete groupedDecoration[color]
  20. expect(groupedDecoration).toEqual({})
  21. # Main
  22. # -------------------------
  23. describe "auto-highlight", ->
  24. [editor, editorContent, editorElement, main] = []
  25. dispatchCommand = (command, {element}={}) ->
  26. element ?= editorElement
  27. atom.commands.dispatch(element, command)
  28. beforeEach ->
  29. jasmine.attachToDOM(atom.views.getView(atom.workspace))
  30. editorContent = """
  31. orange
  32. apple
  33. orange
  34. apple
  35. orange
  36. apple
  37. """
  38. waitsForPromise ->
  39. atom.packages.activatePackage("auto-highlight").then (pack) ->
  40. main = pack.mainModule
  41. waitsForPromise ->
  42. atom.workspace.open('sample-1').then (e) ->
  43. editor = e
  44. editor.setText(editorContent)
  45. editor.setCursorBufferPosition([0, 0])
  46. editorElement = editor.element
  47. describe "auto-highlight:toggle", ->
  48. it "highlight keyword under cursor", ->
  49. dispatchCommand('auto-highlight:toggle')
  50. ensureDecorations(editor, "underline-01": ['orange', 'orange', 'orange'])
  51. it "remove decoration when if already decorated", ->
  52. dispatchCommand('auto-highlight:toggle')
  53. ensureDecorations(editor, "underline-01": ['orange', 'orange', 'orange'])
  54. dispatchCommand('auto-highlight:toggle')
  55. expect(getDecorations(editor)).toHaveLength(0)
  56. it "can decorate multiple keyword simultaneously", ->
  57. dispatchCommand('auto-highlight:toggle')
  58. ensureDecorations editor,
  59. "underline-01": ['orange', 'orange', 'orange']
  60. editor.setCursorBufferPosition([1, 3])
  61. dispatchCommand('auto-highlight:toggle')
  62. ensureDecorations editor,
  63. "underline-01": ['orange', 'orange', 'orange']
  64. "underline-02": ['apple', 'apple', 'apple']
  65. describe "auto-highlight:clear", ->
  66. it "clear all decorations", ->
  67. dispatchCommand('auto-highlight:toggle')
  68. editor.setCursorBufferPosition([1, 3])
  69. dispatchCommand('auto-highlight:toggle')
  70. expect(getDecorations(editor)).toHaveLength(6)
  71. dispatchCommand('auto-highlight:clear')
  72. expect(getDecorations(editor)).toHaveLength(0)
  73. describe "multiple editors is displayed", ->
  74. [editor2, editor2Element] = []
  75. beforeEach ->
  76. waitsForPromise ->
  77. atom.workspace.open('sample-2', split: 'right').then (e) ->
  78. editor2 = e
  79. editor2.setText(editorContent)
  80. editor2Element = editor2.element
  81. editor2.setCursorBufferPosition [0, 0]
  82. runs ->
  83. expect(atom.workspace.getActiveTextEditor()).toBe(editor2)
  84. it "can highlight keyword across editors", ->
  85. dispatchCommand('auto-highlight:toggle', editor2Element)
  86. ensureDecorations(editor, "underline-01": ['orange', 'orange', 'orange'])
  87. ensureDecorations(editor2, "underline-01": ['orange', 'orange', 'orange'])
  88. it "decorate keywords when new editor was opened", ->
  89. dispatchCommand('auto-highlight:toggle', editor2Element)
  90. editor3 = null
  91. pathSample3 = atom.project.resolvePath("sample-3")
  92. waitsForPromise ->
  93. atom.workspace.open(pathSample3, split: 'right').then (e) ->
  94. editor3 = e
  95. runs ->
  96. ensureDecorations(editor, "underline-01": ['orange', 'orange', 'orange'])
  97. ensureDecorations(editor2, "underline-01": ['orange', 'orange', 'orange'])
  98. ensureDecorations(editor3, "underline-01": ['orange', 'orange', 'orange'])
  99. describe "selection changed when highlightSelection", ->
  100. beforeEach ->
  101. spyOn(_._, "now").andCallFake -> window.now
  102. it "decorate selected keyword", ->
  103. dispatchCommand('editor:select-word')
  104. advanceClock(150)
  105. ensureDecorations(editor, "box-selection": ['orange', 'orange', 'orange'])
  106. it "clear highlight when selection is cleared", ->
  107. dispatchCommand('editor:select-word')
  108. advanceClock(150)
  109. ensureDecorations(editor, "box-selection": ['orange', 'orange', 'orange'])
  110. editor.clearSelections()
  111. advanceClock(150)
  112. expect(getDecorations(editor)).toHaveLength(0)
  113. it "won't highlight selectedText length is less than highlightSelectionMinimumLength", ->
  114. atom.config.set("auto-highlight.highlightSelectionMinimumLength", 3)
  115. dispatchCommand('core:select-right')
  116. advanceClock(150)
  117. expect(editor.getSelectedText()).toBe "o"
  118. expect(getDecorations(editor)).toHaveLength 0
  119. dispatchCommand('core:select-right')
  120. advanceClock(150)
  121. expect(editor.getSelectedText()).toBe "or"
  122. expect(getDecorations(editor)).toHaveLength 0
  123. dispatchCommand('core:select-right')
  124. advanceClock(150)
  125. expect(editor.getSelectedText()).toBe "ora"
  126. ensureDecorations(editor, "box-selection": ['ora', 'ora', 'ora'])
  127. it "won't highlight when selection is all white space", ->
  128. editor.setCursorBufferPosition([1, 0])
  129. dispatchCommand('core:select-right')
  130. advanceClock(150)
  131. expect(editor.getSelectedText()).toBe " "
  132. expect(getDecorations(editor)).toHaveLength 0
  133. it "won't highlight when selection is multi-line", ->
  134. dispatchCommand('core:select-down')
  135. dispatchCommand('core:select-down')
  136. expect(editor.getSelectedText()).toBe "orange\n apple\n"
  137. advanceClock(150)
  138. expect(getDecorations(editor)).toHaveLength 0
  139. it "won't highlight when highlightSelection is disabled", ->
  140. atom.config.set('auto-highlight.highlightSelection', false)
  141. dispatchCommand('editor:select-word')
  142. advanceClock(150)
  143. expect(editor.getSelectedText()).toBe "orange"
  144. expect(getDecorations(editor)).toHaveLength 0
  145. describe "highlightSelectionExcludeScopes", ->
  146. beforeEach ->
  147. atom.config.set('auto-highlight.highlightSelectionExcludeScopes', [
  148. 'foo.bar',
  149. 'hoge',
  150. ])
  151. it "won't highlight when editor have specified scope case-1", ->
  152. editorElement.classList.add('foo', 'bar')
  153. dispatchCommand('editor:select-word')
  154. advanceClock(150)
  155. expect(editor.getSelectedText()).toBe "orange"
  156. expect(getDecorations(editor)).toHaveLength 0
  157. it "won't highlight when editor have specified scope case-2", ->
  158. editorElement.classList.add('hoge')
  159. dispatchCommand('editor:select-word')
  160. advanceClock(150)
  161. expect(editor.getSelectedText()).toBe "orange"
  162. expect(getDecorations(editor)).toHaveLength 0
  163. describe "highlightSelectionDelay", ->
  164. beforeEach ->
  165. atom.config.set('auto-highlight.highlightSelectionDelay', 300)
  166. it "highlight selection after specified delay", ->
  167. dispatchCommand('editor:select-word')
  168. expect(editor.getSelectedText()).toBe "orange"
  169. expect(getDecorations(editor)).toHaveLength 0
  170. advanceClock(100)
  171. expect(getDecorations(editor)).toHaveLength 0
  172. advanceClock(100)
  173. expect(getDecorations(editor)).toHaveLength 0
  174. advanceClock(100)
  175. expect(getDecorations(editor)).toHaveLength 3
  176. ensureDecorations(editor, "box-selection": ['orange', 'orange', 'orange'])
  177. describe "displayCountOnStatusBar", ->
  178. [editor3, container, span] = []
  179. beforeEach ->
  180. editor.setText """
  181. apple orange
  182. orange lemon orange
  183. apple
  184. """
  185. waitsForPromise -> atom.packages.activatePackage("status-bar")
  186. waitsFor -> main.keywordManager.statusBarManager.tile?
  187. runs ->
  188. container = atom.views.getView(atom.workspace).querySelector('#status-bar-auto-highlight')
  189. span = container.querySelector('span')
  190. it 'display latest highlighted count on statusbar', ->
  191. editor.setCursorBufferPosition([0, 0])
  192. dispatchCommand('auto-highlight:toggle')
  193. ensureDecorations(editor, "underline-01": ['apple', 'apple'])
  194. expect(container.style.display).toBe 'inline-block'
  195. expect(span.textContent).toBe '2'
  196. editor.setCursorBufferPosition([1, 0])
  197. dispatchCommand('auto-highlight:toggle')
  198. ensureDecorations editor,
  199. "underline-01": ['apple', 'apple']
  200. "underline-02": ['orange', 'orange', 'orange']
  201. expect(container.style.display).toBe 'inline-block'
  202. expect(span.textContent).toBe '3'
  203. editor.setCursorBufferPosition([1, 10])
  204. dispatchCommand('auto-highlight:toggle')
  205. ensureDecorations editor,
  206. "underline-01": ['apple', 'apple']
  207. "underline-02": ['orange', 'orange', 'orange']
  208. "underline-03": ['lemon']
  209. expect(container.style.display).toBe 'inline-block'
  210. expect(span.textContent).toBe '1'