aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/vim-mode/spec/vim-state-spec.coffee
blob: 741c3712e7efd6e5b4573f54cc6834897e0d33f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
_ = require 'underscore-plus'
helpers = require './spec-helper'
VimState = require '../lib/vim-state'
StatusBarManager = require '../lib/status-bar-manager'

describe "VimState", ->
  [editor, editorElement, vimState] = []

  beforeEach ->
    vimMode = atom.packages.loadPackage('vim-mode')
    vimMode.activateResources()

    helpers.getEditorElement (element) ->
      editorElement = element
      editor = editorElement.getModel()
      vimState = editorElement.vimState
      vimState.activateCommandMode()
      vimState.resetCommandMode()

  keydown = (key, options={}) ->
    options.element ?= editorElement
    helpers.keydown(key, options)

  commandModeInputKeydown = (key, opts = {}) ->
    editor.commandModeInputView.editorElement.getModel().setText(key)

  describe "initialization", ->
    it "puts the editor in command-mode initially by default", ->
      expect(editorElement.classList.contains('vim-mode')).toBe(true)
      expect(editorElement.classList.contains('command-mode')).toBe(true)

    it "puts the editor in insert-mode if startInInsertMode is true", ->
      atom.config.set 'vim-mode.startInInsertMode', true
      editor.vimState = new VimState(editorElement, new StatusBarManager)
      expect(editorElement.classList.contains('insert-mode')).toBe(true)

  describe "::destroy", ->
    it "re-enables text input on the editor", ->
      expect(editorElement.component.isInputEnabled()).toBeFalsy()
      vimState.destroy()
      expect(editorElement.component.isInputEnabled()).toBeTruthy()

    it "removes the mode classes from the editor", ->
      expect(editorElement.classList.contains("command-mode")).toBeTruthy()
      vimState.destroy()
      expect(editorElement.classList.contains("command-mode")).toBeFalsy()

    it "is a noop when the editor is already destroyed", ->
      editorElement.getModel().destroy()
      vimState.destroy()

  describe "command-mode", ->
    describe "when entering an insertable character", ->
      beforeEach -> keydown('\\')

      it "stops propagation", ->
        expect(editor.getText()).toEqual ''

    describe "when entering an operator", ->
      beforeEach -> keydown('d')

      describe "with an operator that can't be composed", ->
        beforeEach -> keydown('x')

        it "clears the operator stack", ->
          expect(vimState.opStack.length).toBe 0

      describe "the escape keybinding", ->
        beforeEach -> keydown('escape')

        it "clears the operator stack", ->
          expect(vimState.opStack.length).toBe 0

      describe "the ctrl-c keybinding", ->
        beforeEach -> keydown('c', ctrl: true)

        it "clears the operator stack", ->
          expect(vimState.opStack.length).toBe 0

    describe "the escape keybinding", ->
      it "clears any extra cursors", ->
        editor.setText("one-two-three")
        editor.addCursorAtBufferPosition([0, 3])
        expect(editor.getCursors().length).toBe 2
        keydown('escape')
        expect(editor.getCursors().length).toBe 1

    describe "the v keybinding", ->
      beforeEach -> keydown('v')

      it "puts the editor into visual characterwise mode", ->
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'characterwise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

    describe "the V keybinding", ->
      beforeEach ->
        editor.setText("012345\nabcdef")
        editor.setCursorScreenPosition([0, 0])
        keydown('V', shift: true)

      it "puts the editor into visual linewise mode", ->
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'linewise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

      it "selects the current line", ->
        expect(editor.getLastSelection().getText()).toEqual '012345\n'

    describe "the ctrl-v keybinding", ->
      beforeEach -> keydown('v', ctrl: true)

      it "puts the editor into visual characterwise mode", ->
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'blockwise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

    describe "selecting text", ->
      beforeEach ->
        spyOn(_._, "now").andCallFake -> window.now
        editor.setText("abc def")

      it "puts the editor into visual mode", ->
        expect(vimState.mode).toEqual 'command'
        editor.setSelectedBufferRanges([[[0, 0], [0, 3]]])

        advanceClock(100)

        expect(vimState.mode).toEqual 'visual'
        expect(vimState.submode).toEqual 'characterwise'
        expect(editor.getSelectedBufferRanges()).toEqual([[[0, 0], [0, 3]]])

      it "handles the editor being destroyed shortly after selecting text", ->
        editor.setSelectedBufferRanges([[[0, 0], [0, 3]]])
        editor.destroy()
        vimState.destroy()
        advanceClock(100)

    describe "the i keybinding", ->
      beforeEach -> keydown('i')

      it "puts the editor into insert mode", ->
        expect(editorElement.classList.contains('insert-mode')).toBe(true)
        expect(editorElement.classList.contains('command-mode')).toBe(false)

    describe "with content", ->
      beforeEach -> editor.setText("012345\n\nabcdef")

      # FIXME: See atom/vim-mode#2
      xdescribe "on a line with content", ->
        beforeEach -> editor.setCursorScreenPosition([0, 6])

        it "does not allow the cursor to be placed on the \n character", ->
          expect(editor.getCursorScreenPosition()).toEqual [0, 5]

      describe "on an empty line", ->
        beforeEach -> editor.setCursorScreenPosition([1, 0])

        it "allows the cursor to be placed on the \n character", ->
          expect(editor.getCursorScreenPosition()).toEqual [1, 0]

    describe 'with character-input operations', ->
      beforeEach -> editor.setText('012345\nabcdef')

      it 'properly clears the opStack', ->
        keydown('d')
        keydown('r')
        expect(vimState.mode).toBe 'command'
        expect(vimState.opStack.length).toBe 0
        atom.commands.dispatch(editor.commandModeInputView.editorElement, "core:cancel")
        keydown('d')
        expect(editor.getText()).toBe '012345\nabcdef'

  describe "insert-mode", ->
    beforeEach ->
      keydown('i')

    describe "with content", ->
      beforeEach -> editor.setText("012345\n\nabcdef")

      describe "when cursor is in the middle of the line", ->
        beforeEach -> editor.setCursorScreenPosition([0, 3])

        it "moves the cursor to the left when exiting insert mode", ->
          keydown('escape')
          expect(editor.getCursorScreenPosition()).toEqual [0, 2]

      describe "when cursor is at the beginning of line", ->
        beforeEach -> editor.setCursorScreenPosition([1, 0])

        it "leaves the cursor at the beginning of line", ->
          keydown('escape')
          expect(editor.getCursorScreenPosition()).toEqual [1, 0]

      describe "on a line with content", ->
        beforeEach -> editor.setCursorScreenPosition([0, 6])

        it "allows the cursor to be placed on the \n character", ->
          expect(editor.getCursorScreenPosition()).toEqual [0, 6]

    it "puts the editor into command mode when <escape> is pressed", ->
      keydown('escape')

      expect(editorElement.classList.contains('command-mode')).toBe(true)
      expect(editorElement.classList.contains('insert-mode')).toBe(false)
      expect(editorElement.classList.contains('visual-mode')).toBe(false)

    it "puts the editor into command mode when <ctrl-c> is pressed", ->
      helpers.mockPlatform(editorElement, 'platform-darwin')
      keydown('c', ctrl: true)
      helpers.unmockPlatform(editorElement)

      expect(editorElement.classList.contains('command-mode')).toBe(true)
      expect(editorElement.classList.contains('insert-mode')).toBe(false)
      expect(editorElement.classList.contains('visual-mode')).toBe(false)

  describe "visual-mode", ->
    beforeEach ->
      editor.setText("one two three")
      editor.setCursorBufferPosition([0, 4])
      keydown('v')

    it "selects the character under the cursor", ->
      expect(editor.getSelectedBufferRanges()).toEqual [[[0, 4], [0, 5]]]
      expect(editor.getSelectedText()).toBe("t")

    it "puts the editor into command mode when <escape> is pressed", ->
      keydown('escape')

      expect(editor.getCursorBufferPositions()).toEqual [[0, 4]]
      expect(editorElement.classList.contains('command-mode')).toBe(true)
      expect(editorElement.classList.contains('visual-mode')).toBe(false)

    it "puts the editor into command mode when <escape> is pressed on selection is reversed", ->
      expect(editor.getSelectedText()).toBe("t")
      keydown("h")
      keydown("h")
      expect(editor.getSelectedText()).toBe("e t")
      expect(editor.getLastSelection().isReversed()).toBe(true)
      keydown('escape')
      expect(editorElement.classList.contains('command-mode')).toBe(true)
      expect(editor.getCursorBufferPositions()).toEqual [[0, 2]]

    describe "motions", ->
      it "transforms the selection", ->
        keydown('w')
        expect(editor.getLastSelection().getText()).toEqual 'two t'

      it "always leaves the initially selected character selected", ->
        keydown("h")
        expect(editor.getSelectedText()).toBe(" t")

        keydown("l")
        expect(editor.getSelectedText()).toBe("t")

        keydown("l")
        keydown("l")
        expect(editor.getSelectedText()).toBe("tw")

    describe "operators", ->
      beforeEach ->
        editor.setText("012345\n\nabcdef")
        editor.setCursorScreenPosition([0, 0])
        editor.selectLinesContainingCursors()
        keydown('d')

      it "operate on the current selection", ->
        expect(editor.getText()).toEqual "\nabcdef"

    describe "returning to command-mode", ->
      beforeEach ->
        editor.setText("012345\n\nabcdef")
        editor.selectLinesContainingCursors()
        keydown('escape')

      it "operate on the current selection", ->
        expect(editor.getLastSelection().getText()).toEqual ''

    describe "the o keybinding", ->
      it "reversed each selection", ->
        editor.addCursorAtBufferPosition([0, Infinity])
        keydown("i")
        keydown("w")

        expect(editor.getSelectedBufferRanges()).toEqual([
          [[0, 4], [0, 7]],
          [[0, 8], [0, 13]]
        ])
        expect(editor.getCursorBufferPositions()).toEqual([
          [0, 7]
          [0, 13]
        ])

        keydown("o")

        expect(editor.getSelectedBufferRanges()).toEqual([
          [[0, 4], [0, 7]],
          [[0, 8], [0, 13]]
        ])
        expect(editor.getCursorBufferPositions()).toEqual([
          [0, 4]
          [0, 8]
        ])

    describe "activate visualmode witin visualmode", ->
      beforeEach ->
        keydown('escape')
        expect(vimState.mode).toEqual 'command'
        expect(editorElement.classList.contains('command-mode')).toBe(true)

      it "activateVisualMode with same type puts the editor into command mode", ->
        keydown('v')
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'characterwise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

        keydown('v')
        expect(vimState.mode).toEqual 'command'
        expect(editorElement.classList.contains('command-mode')).toBe(true)

        keydown('V', shift: true)
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'linewise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

        keydown('V', shift: true)
        expect(vimState.mode).toEqual 'command'
        expect(editorElement.classList.contains('command-mode')).toBe(true)

        keydown('v', ctrl: true)
        expect(editorElement.classList.contains('visual-mode')).toBe(true)
        expect(vimState.submode).toEqual 'blockwise'
        expect(editorElement.classList.contains('command-mode')).toBe(false)

        keydown('v', ctrl: true)
        expect(vimState.mode).toEqual 'command'
        expect(editorElement.classList.contains('command-mode')).toBe(true)

      describe "change submode within visualmode", ->
        beforeEach ->
          editor.setText("line one\nline two\nline three\n")
          editor.setCursorBufferPosition([0, 5])
          editor.addCursorAtBufferPosition([2, 5])

        it "can change submode within visual mode", ->
          keydown('v')
          expect(editorElement.classList.contains('visual-mode')).toBe(true)
          expect(vimState.submode).toEqual 'characterwise'
          expect(editorElement.classList.contains('command-mode')).toBe(false)

          keydown('V', shift: true)
          expect(editorElement.classList.contains('visual-mode')).toBe(true)
          expect(vimState.submode).toEqual 'linewise'
          expect(editorElement.classList.contains('command-mode')).toBe(false)

          keydown('v', ctrl: true)
          expect(editorElement.classList.contains('visual-mode')).toBe(true)
          expect(vimState.submode).toEqual 'blockwise'
          expect(editorElement.classList.contains('command-mode')).toBe(false)

          keydown('v')
          expect(editorElement.classList.contains('visual-mode')).toBe(true)
          expect(vimState.submode).toEqual 'characterwise'
          expect(editorElement.classList.contains('command-mode')).toBe(false)


        it "recover original range when shift from linewse to characterwise", ->
          keydown('v')
          keydown('i')
          keydown('w')

          expect(_.map(editor.getSelections(), (selection) ->
            selection.getText())
          ).toEqual(['one', 'three'])

          keydown('V', shift: true)
          expect(_.map(editor.getSelections(), (selection) ->
            selection.getText())
          ).toEqual(["line one\n", "line three\n"])

          keydown('v', ctrl: true)
          expect(_.map(editor.getSelections(), (selection) ->
            selection.getText())
          ).toEqual(['one', 'three'])

  describe "marks", ->
    beforeEach ->  editor.setText("text in line 1\ntext in line 2\ntext in line 3")

    it "basic marking functionality", ->
      editor.setCursorScreenPosition([1, 1])
      keydown('m')
      commandModeInputKeydown('t')
      expect(editor.getText()).toEqual "text in line 1\ntext in line 2\ntext in line 3"
      editor.setCursorScreenPosition([2, 2])
      keydown('`')
      commandModeInputKeydown('t')
      expect(editor.getCursorScreenPosition()).toEqual [1, 1]

    it "real (tracking) marking functionality", ->
      editor.setCursorScreenPosition([2, 2])
      keydown('m')
      commandModeInputKeydown('q')
      editor.setCursorScreenPosition([1, 2])
      keydown('o')
      keydown('escape')
      keydown('`')
      commandModeInputKeydown('q')
      expect(editor.getCursorScreenPosition()).toEqual [3, 2]

    it "real (tracking) marking functionality", ->
      editor.setCursorScreenPosition([2, 2])
      keydown('m')
      commandModeInputKeydown('q')
      editor.setCursorScreenPosition([1, 2])
      keydown('d')
      keydown('d')
      keydown('escape')
      keydown('`')
      commandModeInputKeydown('q')
      expect(editor.getCursorScreenPosition()).toEqual [1, 2]