1 helpers = require './spec-helper'
3 describe "TextObjects", ->
4 [editor, editorElement, vimState] = []
7 vimMode = atom.packages.loadPackage('vim-mode')
8 vimMode.activateResources()
10 helpers.getEditorElement (element) ->
11 editorElement = element
12 editor = editorElement.getModel()
13 vimState = editorElement.vimState
14 vimState.activateCommandMode()
15 vimState.resetCommandMode()
17 keydown = (key, options={}) ->
18 options.element ?= editorElement
19 helpers.keydown(key, options)
21 commandModeInputKeydown = (key, opts = {}) ->
22 editor.commandModeInputView.editorElement.getModel().setText(key)
24 describe "the 'iw' text object", ->
26 editor.setText("12345 abcde ABCDE")
27 editor.setCursorScreenPosition([0, 9])
29 it "applies operators inside the current word in operator-pending mode", ->
34 expect(editor.getText()).toBe "12345 ABCDE"
35 expect(editor.getCursorScreenPosition()).toEqual [0, 6]
36 expect(vimState.getRegister('"').text).toBe "abcde"
37 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
38 expect(editorElement.classList.contains('command-mode')).toBe(true)
40 it "selects inside the current word in visual mode", ->
45 expect(editor.getSelectedScreenRange()).toEqual [[0, 6], [0, 11]]
47 it "works with multiple cursors", ->
48 editor.addCursorAtBufferPosition([0, 1])
52 expect(editor.getSelectedBufferRanges()).toEqual [
57 describe "the 'i(' text object", ->
59 editor.setText("( something in here and in (here) )")
60 editor.setCursorScreenPosition([0, 9])
62 it "applies operators inside the current word in operator-pending mode", ->
66 expect(editor.getText()).toBe "()"
67 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
68 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
69 expect(editorElement.classList.contains('command-mode')).toBe(true)
71 it "applies operators inside the current word in operator-pending mode (second test)", ->
72 editor.setCursorScreenPosition([0, 29])
76 expect(editor.getText()).toBe "( something in here and in () )"
77 expect(editor.getCursorScreenPosition()).toEqual [0, 28]
78 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
79 expect(editorElement.classList.contains('command-mode')).toBe(true)
81 it "works with multiple cursors", ->
82 editor.setText("( a b ) cde ( f g h ) ijk")
83 editor.setCursorBufferPosition([0, 2])
84 editor.addCursorAtBufferPosition([0, 18])
90 expect(editor.getSelectedBufferRanges()).toEqual [
95 describe "the 'i{' text object", ->
97 editor.setText("{ something in here and in {here} }")
98 editor.setCursorScreenPosition([0, 9])
100 it "applies operators inside the current word in operator-pending mode", ->
104 expect(editor.getText()).toBe "{}"
105 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
106 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
107 expect(editorElement.classList.contains('command-mode')).toBe(true)
109 it "applies operators inside the current word in operator-pending mode (second test)", ->
110 editor.setCursorScreenPosition([0, 29])
114 expect(editor.getText()).toBe "{ something in here and in {} }"
115 expect(editor.getCursorScreenPosition()).toEqual [0, 28]
116 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
117 expect(editorElement.classList.contains('command-mode')).toBe(true)
119 describe "the 'i<' text object", ->
121 editor.setText("< something in here and in <here> >")
122 editor.setCursorScreenPosition([0, 9])
124 it "applies operators inside the current word in operator-pending mode", ->
128 expect(editor.getText()).toBe "<>"
129 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
130 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
131 expect(editorElement.classList.contains('command-mode')).toBe(true)
133 it "applies operators inside the current word in operator-pending mode (second test)", ->
134 editor.setCursorScreenPosition([0, 29])
138 expect(editor.getText()).toBe "< something in here and in <> >"
139 expect(editor.getCursorScreenPosition()).toEqual [0, 28]
140 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
141 expect(editorElement.classList.contains('command-mode')).toBe(true)
143 describe "the 'it' text object", ->
145 editor.setText("<something>here</something><again>")
146 editor.setCursorScreenPosition([0, 5])
148 it "applies only if in the value of a tag", ->
152 expect(editor.getText()).toBe "<something>here</something><again>"
153 expect(editor.getCursorScreenPosition()).toEqual [0, 5]
154 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
155 expect(editorElement.classList.contains('command-mode')).toBe(true)
157 it "applies operators inside the current word in operator-pending mode", ->
158 editor.setCursorScreenPosition([0, 13])
162 expect(editor.getText()).toBe "<something></something><again>"
163 expect(editor.getCursorScreenPosition()).toEqual [0, 11]
164 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
165 expect(editorElement.classList.contains('command-mode')).toBe(true)
167 describe "the 'ip' text object", ->
169 editor.setText("\nParagraph-1\nParagraph-1\nParagraph-1\n\n")
170 editor.setCursorScreenPosition([2, 2])
172 it "applies operators inside the current paragraph in operator-pending mode", ->
178 expect(editor.getText()).toBe "\nParagraph-1\nParagraph-1\nParagraph-1\n\n"
179 expect(editor.getCursorScreenPosition()).toEqual [1, 0]
180 expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n"
181 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
182 expect(editorElement.classList.contains('command-mode')).toBe(true)
184 it "selects inside the current paragraph in visual mode", ->
189 expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [4, 0]]
191 describe "the 'ap' text object", ->
193 editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext")
194 editor.setCursorScreenPosition([3, 2])
196 it "applies operators around the current paragraph in operator-pending mode", ->
202 expect(editor.getText()).toBe "text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext"
203 expect(editor.getCursorScreenPosition()).toEqual [2, 0]
204 expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n\n"
205 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
206 expect(editorElement.classList.contains('command-mode')).toBe(true)
208 it "selects around the current paragraph in visual mode", ->
213 expect(editor.getSelectedScreenRange()).toEqual [[2, 0], [6, 0]]
215 describe "the 'i[' text object", ->
217 editor.setText("[ something in here and in [here] ]")
218 editor.setCursorScreenPosition([0, 9])
220 it "applies operators inside the current word in operator-pending mode", ->
224 expect(editor.getText()).toBe "[]"
225 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
226 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
227 expect(editorElement.classList.contains('command-mode')).toBe(true)
229 it "applies operators inside the current word in operator-pending mode (second test)", ->
230 editor.setCursorScreenPosition([0, 29])
234 expect(editor.getText()).toBe "[ something in here and in [] ]"
235 expect(editor.getCursorScreenPosition()).toEqual [0, 28]
236 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
237 expect(editorElement.classList.contains('command-mode')).toBe(true)
239 describe "the 'i\'' text object", ->
241 editor.setText("' something in here and in 'here' ' and over here")
242 editor.setCursorScreenPosition([0, 9])
244 it "applies operators inside the current string in operator-pending mode", ->
248 expect(editor.getText()).toBe "''here' ' and over here"
249 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
250 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
251 expect(editorElement.classList.contains('command-mode')).toBe(true)
253 it "applies operators inside the next string in operator-pending mode (if not in a string)", ->
254 editor.setCursorScreenPosition([0, 29])
258 expect(editor.getText()).toBe "' something in here and in 'here'' and over here"
259 expect(editor.getCursorScreenPosition()).toEqual [0, 33]
260 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
261 expect(editorElement.classList.contains('command-mode')).toBe(true)
263 it "makes no change if past the last string on a line", ->
264 editor.setCursorScreenPosition([0, 39])
268 expect(editor.getText()).toBe "' something in here and in 'here' ' and over here"
269 expect(editor.getCursorScreenPosition()).toEqual [0, 39]
270 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
271 expect(editorElement.classList.contains('command-mode')).toBe(true)
273 describe "the 'i\"' text object", ->
275 editor.setText("\" something in here and in \"here\" \" and over here")
276 editor.setCursorScreenPosition([0, 9])
278 it "applies operators inside the current string in operator-pending mode", ->
282 expect(editor.getText()).toBe "\"\"here\" \" and over here"
283 expect(editor.getCursorScreenPosition()).toEqual [0, 1]
284 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
285 expect(editorElement.classList.contains('command-mode')).toBe(true)
287 it "applies operators inside the next string in operator-pending mode (if not in a string)", ->
288 editor.setCursorScreenPosition([0, 29])
292 expect(editor.getText()).toBe "\" something in here and in \"here\"\" and over here"
293 expect(editor.getCursorScreenPosition()).toEqual [0, 33]
294 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
295 expect(editorElement.classList.contains('command-mode')).toBe(true)
297 it "makes no change if past the last string on a line", ->
298 editor.setCursorScreenPosition([0, 39])
302 expect(editor.getText()).toBe "\" something in here and in \"here\" \" and over here"
303 expect(editor.getCursorScreenPosition()).toEqual [0, 39]
304 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
306 describe "the 'aw' text object", ->
308 editor.setText("12345 abcde ABCDE")
309 editor.setCursorScreenPosition([0, 9])
311 it "applies operators from the start of the current word to the start of the next word in operator-pending mode", ->
316 expect(editor.getText()).toBe "12345 ABCDE"
317 expect(editor.getCursorScreenPosition()).toEqual [0, 6]
318 expect(vimState.getRegister('"').text).toBe "abcde "
319 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
320 expect(editorElement.classList.contains('command-mode')).toBe(true)
322 it "selects from the start of the current word to the start of the next word in visual mode", ->
327 expect(editor.getSelectedScreenRange()).toEqual [[0, 6], [0, 12]]
329 it "doesn't span newlines", ->
330 editor.setText("12345\nabcde ABCDE")
331 editor.setCursorBufferPosition([0, 3])
337 expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 5]]]
339 describe "the 'a(' text object", ->
341 editor.setText("( something in here and in (here) )")
342 editor.setCursorScreenPosition([0, 9])
344 it "applies operators around the current parentheses in operator-pending mode", ->
348 expect(editor.getText()).toBe ""
349 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
350 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
351 expect(editorElement.classList.contains('command-mode')).toBe(true)
353 it "applies operators around the current parentheses in operator-pending mode (second test)", ->
354 editor.setCursorScreenPosition([0, 29])
358 expect(editor.getText()).toBe "( something in here and in )"
359 expect(editor.getCursorScreenPosition()).toEqual [0, 27]
360 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
361 expect(editorElement.classList.contains('command-mode')).toBe(true)
363 describe "the 'a{' text object", ->
365 editor.setText("{ something in here and in {here} }")
366 editor.setCursorScreenPosition([0, 9])
368 it "applies operators around the current curly brackets in operator-pending mode", ->
372 expect(editor.getText()).toBe ""
373 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
374 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
375 expect(editorElement.classList.contains('command-mode')).toBe(true)
377 it "applies operators around the current curly brackets in operator-pending mode (second test)", ->
378 editor.setCursorScreenPosition([0, 29])
382 expect(editor.getText()).toBe "{ something in here and in }"
383 expect(editor.getCursorScreenPosition()).toEqual [0, 27]
384 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
385 expect(editorElement.classList.contains('command-mode')).toBe(true)
387 describe "the 'a<' text object", ->
389 editor.setText("< something in here and in <here> >")
390 editor.setCursorScreenPosition([0, 9])
392 it "applies operators around the current angle brackets in operator-pending mode", ->
396 expect(editor.getText()).toBe ""
397 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
398 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
399 expect(editorElement.classList.contains('command-mode')).toBe(true)
401 it "applies operators around the current angle brackets in operator-pending mode (second test)", ->
402 editor.setCursorScreenPosition([0, 29])
406 expect(editor.getText()).toBe "< something in here and in >"
407 expect(editor.getCursorScreenPosition()).toEqual [0, 27]
408 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
409 expect(editorElement.classList.contains('command-mode')).toBe(true)
411 describe "the 'a[' text object", ->
413 editor.setText("[ something in here and in [here] ]")
414 editor.setCursorScreenPosition([0, 9])
416 it "applies operators around the current square brackets in operator-pending mode", ->
420 expect(editor.getText()).toBe ""
421 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
422 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
423 expect(editorElement.classList.contains('command-mode')).toBe(true)
425 it "applies operators around the current square brackets in operator-pending mode (second test)", ->
426 editor.setCursorScreenPosition([0, 29])
430 expect(editor.getText()).toBe "[ something in here and in ]"
431 expect(editor.getCursorScreenPosition()).toEqual [0, 27]
432 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
433 expect(editorElement.classList.contains('command-mode')).toBe(true)
435 describe "the 'a\'' text object", ->
437 editor.setText("' something in here and in 'here' '")
438 editor.setCursorScreenPosition([0, 9])
440 it "applies operators around the current single quotes in operator-pending mode", ->
444 expect(editor.getText()).toBe "here' '"
445 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
446 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
447 expect(editorElement.classList.contains('command-mode')).toBe(true)
449 it "applies operators around the current single quotes in operator-pending mode (second test)", ->
450 editor.setCursorScreenPosition([0, 29])
454 expect(editor.getText()).toBe "' something in here and in 'here"
455 expect(editor.getCursorScreenPosition()).toEqual [0, 31]
456 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
457 expect(editorElement.classList.contains('command-mode')).toBe(true)
459 describe "the 'a\"' text object", ->
461 editor.setText("\" something in here and in \"here\" \"")
462 editor.setCursorScreenPosition([0, 9])
464 it "applies operators around the current double quotes in operator-pending mode", ->
468 expect(editor.getText()).toBe 'here" "'
469 expect(editor.getCursorScreenPosition()).toEqual [0, 0]
470 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
471 expect(editorElement.classList.contains('command-mode')).toBe(true)
473 it "applies operators around the current double quotes in operator-pending mode (second test)", ->
474 editor.setCursorScreenPosition([0, 29])
478 expect(editor.getText()).toBe "\" something in here and in \"here"
479 expect(editor.getCursorScreenPosition()).toEqual [0, 31]
480 expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
481 expect(editorElement.classList.contains('command-mode')).toBe(true)