]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/spec/text-objects-spec.coffee
6991dc7e055f1a36a72614ef6206edf218ed7b15
[rbdr/dotfiles] / atom / packages / vim-mode / spec / text-objects-spec.coffee
1 helpers = require './spec-helper'
2
3 describe "TextObjects", ->
4 [editor, editorElement, vimState] = []
5
6 beforeEach ->
7 vimMode = atom.packages.loadPackage('vim-mode')
8 vimMode.activateResources()
9
10 helpers.getEditorElement (element) ->
11 editorElement = element
12 editor = editorElement.getModel()
13 vimState = editorElement.vimState
14 vimState.activateCommandMode()
15 vimState.resetCommandMode()
16
17 keydown = (key, options={}) ->
18 options.element ?= editorElement
19 helpers.keydown(key, options)
20
21 commandModeInputKeydown = (key, opts = {}) ->
22 editor.commandModeInputView.editorElement.getModel().setText(key)
23
24 describe "the 'iw' text object", ->
25 beforeEach ->
26 editor.setText("12345 abcde ABCDE")
27 editor.setCursorScreenPosition([0, 9])
28
29 it "applies operators inside the current word in operator-pending mode", ->
30 keydown('d')
31 keydown('i')
32 keydown('w')
33
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)
39
40 it "selects inside the current word in visual mode", ->
41 keydown('v')
42 keydown('i')
43 keydown('w')
44
45 expect(editor.getSelectedScreenRange()).toEqual [[0, 6], [0, 11]]
46
47 it "works with multiple cursors", ->
48 editor.addCursorAtBufferPosition([0, 1])
49 keydown("v")
50 keydown("i")
51 keydown("w")
52 expect(editor.getSelectedBufferRanges()).toEqual [
53 [[0, 6], [0, 11]]
54 [[0, 0], [0, 5]]
55 ]
56
57 describe "the 'i(' text object", ->
58 beforeEach ->
59 editor.setText("( something in here and in (here) )")
60 editor.setCursorScreenPosition([0, 9])
61
62 it "applies operators inside the current word in operator-pending mode", ->
63 keydown('d')
64 keydown('i')
65 keydown('(')
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)
70
71 it "applies operators inside the current word in operator-pending mode (second test)", ->
72 editor.setCursorScreenPosition([0, 29])
73 keydown('d')
74 keydown('i')
75 keydown('(')
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)
80
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])
85
86 keydown("v")
87 keydown("i")
88 keydown("(")
89
90 expect(editor.getSelectedBufferRanges()).toEqual [
91 [[0, 1], [0, 6]]
92 [[0, 13], [0, 20]]
93 ]
94
95 describe "the 'i{' text object", ->
96 beforeEach ->
97 editor.setText("{ something in here and in {here} }")
98 editor.setCursorScreenPosition([0, 9])
99
100 it "applies operators inside the current word in operator-pending mode", ->
101 keydown('d')
102 keydown('i')
103 keydown('{')
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)
108
109 it "applies operators inside the current word in operator-pending mode (second test)", ->
110 editor.setCursorScreenPosition([0, 29])
111 keydown('d')
112 keydown('i')
113 keydown('{')
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)
118
119 describe "the 'i<' text object", ->
120 beforeEach ->
121 editor.setText("< something in here and in <here> >")
122 editor.setCursorScreenPosition([0, 9])
123
124 it "applies operators inside the current word in operator-pending mode", ->
125 keydown('d')
126 keydown('i')
127 keydown('<')
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)
132
133 it "applies operators inside the current word in operator-pending mode (second test)", ->
134 editor.setCursorScreenPosition([0, 29])
135 keydown('d')
136 keydown('i')
137 keydown('<')
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)
142
143 describe "the 'it' text object", ->
144 beforeEach ->
145 editor.setText("<something>here</something><again>")
146 editor.setCursorScreenPosition([0, 5])
147
148 it "applies only if in the value of a tag", ->
149 keydown('d')
150 keydown('i')
151 keydown('t')
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)
156
157 it "applies operators inside the current word in operator-pending mode", ->
158 editor.setCursorScreenPosition([0, 13])
159 keydown('d')
160 keydown('i')
161 keydown('t')
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)
166
167 describe "the 'ip' text object", ->
168 beforeEach ->
169 editor.setText("\nParagraph-1\nParagraph-1\nParagraph-1\n\n")
170 editor.setCursorScreenPosition([2, 2])
171
172 it "applies operators inside the current paragraph in operator-pending mode", ->
173
174 keydown('y')
175 keydown('i')
176 keydown('p')
177
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)
183
184 it "selects inside the current paragraph in visual mode", ->
185 keydown('v')
186 keydown('i')
187 keydown('p')
188
189 expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [4, 0]]
190
191 describe "the 'ap' text object", ->
192 beforeEach ->
193 editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext")
194 editor.setCursorScreenPosition([3, 2])
195
196 it "applies operators around the current paragraph in operator-pending mode", ->
197
198 keydown('y')
199 keydown('a')
200 keydown('p')
201
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)
207
208 it "selects around the current paragraph in visual mode", ->
209 keydown('v')
210 keydown('a')
211 keydown('p')
212
213 expect(editor.getSelectedScreenRange()).toEqual [[2, 0], [6, 0]]
214
215 describe "the 'i[' text object", ->
216 beforeEach ->
217 editor.setText("[ something in here and in [here] ]")
218 editor.setCursorScreenPosition([0, 9])
219
220 it "applies operators inside the current word in operator-pending mode", ->
221 keydown('d')
222 keydown('i')
223 keydown('[')
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)
228
229 it "applies operators inside the current word in operator-pending mode (second test)", ->
230 editor.setCursorScreenPosition([0, 29])
231 keydown('d')
232 keydown('i')
233 keydown('[')
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)
238
239 describe "the 'i\'' text object", ->
240 beforeEach ->
241 editor.setText("' something in here and in 'here' ' and over here")
242 editor.setCursorScreenPosition([0, 9])
243
244 it "applies operators inside the current string in operator-pending mode", ->
245 keydown('d')
246 keydown('i')
247 keydown('\'')
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)
252
253 it "applies operators inside the next string in operator-pending mode (if not in a string)", ->
254 editor.setCursorScreenPosition([0, 29])
255 keydown('d')
256 keydown('i')
257 keydown('\'')
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)
262
263 it "makes no change if past the last string on a line", ->
264 editor.setCursorScreenPosition([0, 39])
265 keydown('d')
266 keydown('i')
267 keydown('\'')
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)
272
273 describe "the 'i\"' text object", ->
274 beforeEach ->
275 editor.setText("\" something in here and in \"here\" \" and over here")
276 editor.setCursorScreenPosition([0, 9])
277
278 it "applies operators inside the current string in operator-pending mode", ->
279 keydown('d')
280 keydown('i')
281 keydown('"')
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)
286
287 it "applies operators inside the next string in operator-pending mode (if not in a string)", ->
288 editor.setCursorScreenPosition([0, 29])
289 keydown('d')
290 keydown('i')
291 keydown('"')
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)
296
297 it "makes no change if past the last string on a line", ->
298 editor.setCursorScreenPosition([0, 39])
299 keydown('d')
300 keydown('i')
301 keydown('"')
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)
305
306 describe "the 'aw' text object", ->
307 beforeEach ->
308 editor.setText("12345 abcde ABCDE")
309 editor.setCursorScreenPosition([0, 9])
310
311 it "applies operators from the start of the current word to the start of the next word in operator-pending mode", ->
312 keydown('d')
313 keydown('a')
314 keydown('w')
315
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)
321
322 it "selects from the start of the current word to the start of the next word in visual mode", ->
323 keydown('v')
324 keydown('a')
325 keydown('w')
326
327 expect(editor.getSelectedScreenRange()).toEqual [[0, 6], [0, 12]]
328
329 it "doesn't span newlines", ->
330 editor.setText("12345\nabcde ABCDE")
331 editor.setCursorBufferPosition([0, 3])
332
333 keydown("v")
334 keydown("a")
335 keydown("w")
336
337 expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 5]]]
338
339 describe "the 'a(' text object", ->
340 beforeEach ->
341 editor.setText("( something in here and in (here) )")
342 editor.setCursorScreenPosition([0, 9])
343
344 it "applies operators around the current parentheses in operator-pending mode", ->
345 keydown('d')
346 keydown('a')
347 keydown('(')
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)
352
353 it "applies operators around the current parentheses in operator-pending mode (second test)", ->
354 editor.setCursorScreenPosition([0, 29])
355 keydown('d')
356 keydown('a')
357 keydown('(')
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)
362
363 describe "the 'a{' text object", ->
364 beforeEach ->
365 editor.setText("{ something in here and in {here} }")
366 editor.setCursorScreenPosition([0, 9])
367
368 it "applies operators around the current curly brackets in operator-pending mode", ->
369 keydown('d')
370 keydown('a')
371 keydown('{')
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)
376
377 it "applies operators around the current curly brackets in operator-pending mode (second test)", ->
378 editor.setCursorScreenPosition([0, 29])
379 keydown('d')
380 keydown('a')
381 keydown('{')
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)
386
387 describe "the 'a<' text object", ->
388 beforeEach ->
389 editor.setText("< something in here and in <here> >")
390 editor.setCursorScreenPosition([0, 9])
391
392 it "applies operators around the current angle brackets in operator-pending mode", ->
393 keydown('d')
394 keydown('a')
395 keydown('<')
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)
400
401 it "applies operators around the current angle brackets in operator-pending mode (second test)", ->
402 editor.setCursorScreenPosition([0, 29])
403 keydown('d')
404 keydown('a')
405 keydown('<')
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)
410
411 describe "the 'a[' text object", ->
412 beforeEach ->
413 editor.setText("[ something in here and in [here] ]")
414 editor.setCursorScreenPosition([0, 9])
415
416 it "applies operators around the current square brackets in operator-pending mode", ->
417 keydown('d')
418 keydown('a')
419 keydown('[')
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)
424
425 it "applies operators around the current square brackets in operator-pending mode (second test)", ->
426 editor.setCursorScreenPosition([0, 29])
427 keydown('d')
428 keydown('a')
429 keydown('[')
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)
434
435 describe "the 'a\'' text object", ->
436 beforeEach ->
437 editor.setText("' something in here and in 'here' '")
438 editor.setCursorScreenPosition([0, 9])
439
440 it "applies operators around the current single quotes in operator-pending mode", ->
441 keydown('d')
442 keydown('a')
443 keydown('\'')
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)
448
449 it "applies operators around the current single quotes in operator-pending mode (second test)", ->
450 editor.setCursorScreenPosition([0, 29])
451 keydown('d')
452 keydown('a')
453 keydown('\'')
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)
458
459 describe "the 'a\"' text object", ->
460 beforeEach ->
461 editor.setText("\" something in here and in \"here\" \"")
462 editor.setCursorScreenPosition([0, 9])
463
464 it "applies operators around the current double quotes in operator-pending mode", ->
465 keydown('d')
466 keydown('a')
467 keydown('""')
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)
472
473 it "applies operators around the current double quotes in operator-pending mode (second test)", ->
474 editor.setCursorScreenPosition([0, 29])
475 keydown('d')
476 keydown('a')
477 keydown('"')
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)