Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Commit 44db89a

Browse files
committed
insert with count
Repeated insert such as `3i` and `3a` now works. Also, `.` with a count (such as `3.`) now changes the old prefix: * for example `2dd3.` will delete 2 and then 3 lines, a subsequent `.` will again delete 3 lines; * similarly `dl3.` will delete a character and then 3 more, a subsequent `.` will again delete 3 characters.
1 parent 93445fc commit 44db89a

File tree

4 files changed

+119
-15
lines changed

4 files changed

+119
-15
lines changed

lib/operators/general-operators.coffee

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
_ = require 'underscore-plus'
22
{Point, Range} = require 'atom'
33
{ViewModel} = require '../view-models/view-model'
4+
Prefixes = require '../prefixes'
45
Utils = require '../utils'
56
settings = require '../settings'
67

@@ -228,11 +229,24 @@ class Repeat extends Operator
228229

229230
isRecordable: -> false
230231

231-
execute: (count=1) ->
232+
execute: (count) ->
233+
return unless (cmd = @vimState.history[0])?
234+
232235
@editor.transact =>
233-
_.times count, =>
234-
cmd = @vimState.history[0]
235-
cmd?.execute()
236+
237+
if count?
238+
# try to propagate prefix to inner operation
239+
if cmd instanceof Prefixes.Repeat
240+
cmd.count = count
241+
else if cmd.motion instanceof Prefixes.Repeat
242+
cmd.motion.count = count
243+
else
244+
repeat = new Prefixes.Repeat(count)
245+
repeat.compose(cmd)
246+
cmd = @vimState.history[0] = repeat
247+
248+
cmd.execute()
249+
236250
#
237251
# It creates a mark at the current cursor position
238252
#

lib/operators/input.coffee

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ settings = require '../settings'
88
# tells the operation to repeat itself instead of enter insert mode.
99
class Insert extends Operator
1010
standalone: true
11+
count: 1
1112

1213
isComplete: -> @standalone or super
1314

14-
confirmChanges: (changes) ->
15+
confirmChanges: (changes, interrupted) ->
1516
bundler = new TransactionBundler(changes)
1617
@typedText = bundler.buildInsertText()
18+
if @count > 1 and not interrupted
19+
@editor.insertText(@typedText) for i in [2..@count]
1720

18-
execute: ->
21+
execute: (count) ->
22+
@count = count if count?
1923
if @typingCompleted
2024
return unless @typedText? and @typedText.length > 0
21-
@editor.insertText(@typedText, normalizeLineEndings: true)
25+
@editor.transact =>
26+
@editor.insertText(@typedText, normalizeLineEndings: true) for i in [1..@count]
2227
for cursor in @editor.getCursors()
2328
cursor.moveLeft() unless cursor.isAtBeginningOfLine()
2429
else
@@ -112,7 +117,7 @@ class Change extends Insert
112117
for selection in @editor.getSelections()
113118
selection.deleteSelectedText()
114119

115-
return super if @typingCompleted
120+
return super(1) if @typingCompleted
116121

117122
@vimState.activateInsertMode()
118123
@typingCompleted = true
@@ -132,7 +137,7 @@ class Substitute extends Insert
132137

133138
if @typingCompleted
134139
@typedText = @typedText.trimLeft()
135-
return super
140+
return super(1)
136141

137142
@vimState.activateInsertMode()
138143
@typingCompleted = true
@@ -156,7 +161,7 @@ class SubstituteLine extends Insert
156161

157162
if @typingCompleted
158163
@typedText = @typedText.trimLeft()
159-
return super
164+
return super(1)
160165

161166
@vimState.activateInsertMode()
162167
@typingCompleted = true

lib/vim-state.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ class VimState
421421
interruptInsertMode: ->
422422
return unless @mode is 'insert'
423423
@editor.groupChangesSinceCheckpoint(@insertionCheckpoint)
424-
@insertionCheckpoint = null
425-
transaction = _.last(@editor.buffer.history.undoStack)
424+
changes = getChangesSinceCheckpoint(@editor.buffer, @insertionCheckpoint)
426425
item = @inputOperator(@history[0])
427-
if item? and transaction?
428-
item.confirmTransaction(transaction)
426+
@insertionCheckpoint = null
427+
if item?
428+
item.confirmChanges(changes, interrupted: true)
429429
@setInsertionCheckpoint()
430430

431431

spec/operators-spec.coffee

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ describe "Operators", ->
235235
keydown('.')
236236
expect(editor.getText()).toBe 'abab'
237237

238+
it "is repeatable with a new count", ->
239+
keydown('3')
240+
keydown('s')
241+
editor.insertText("ab")
242+
keydown('escape')
243+
expect(editor.getText()).toBe '0ab45'
244+
keydown('1')
245+
keydown('.')
246+
expect(editor.getText()).toBe '0aab45'
247+
editor.setCursorScreenPosition([0, 0])
248+
keydown('3')
249+
keydown('.')
250+
expect(editor.getText()).toBe 'abb45'
251+
238252
it "is undoable", ->
239253
editor.setCursorScreenPosition([0, 0])
240254
keydown('3')
@@ -1373,14 +1387,33 @@ describe "Operators", ->
13731387

13741388
expect(editor.getText()).toBe ""
13751389

1376-
it "composes with motions", ->
1390+
it "composes with prefix", ->
13771391
keydown 'd'
13781392
keydown 'd'
13791393
keydown '2'
13801394
keydown '.'
13811395

13821396
expect(editor.getText()).toBe "78"
13831397

1398+
it "changes previous prefix", ->
1399+
keydown '2'
1400+
keydown 'd'
1401+
keydown 'd'
1402+
keydown '1'
1403+
keydown '.'
1404+
1405+
expect(editor.getText()).toBe "78"
1406+
1407+
it "adds prefix if none was there", ->
1408+
keydown 'd'
1409+
keydown 'd'
1410+
keydown 'u'
1411+
keydown '2'
1412+
keydown '.'
1413+
keydown '.'
1414+
1415+
expect(editor.getText()).toBe ""
1416+
13841417
describe "the r keybinding", ->
13851418
beforeEach ->
13861419
editor.setText("12\n34\n\n")
@@ -1560,6 +1593,29 @@ describe "Operators", ->
15601593
keydown 'u'
15611594
expect(editor.getText()).toBe "123\n4567"
15621595

1596+
it "allows count prefix", ->
1597+
keydown '3'
1598+
keydown 'i'
1599+
editor.insertText("abc")
1600+
keydown 'escape'
1601+
expect(editor.getText()).toBe "abcabcabc123\nabcabcabc4567"
1602+
1603+
keydown 'u'
1604+
expect(editor.getText()).toBe "123\n4567"
1605+
1606+
keydown '.'
1607+
keydown '.'
1608+
expect(editor.getText()).toBe "abcabcababcabcabcc123\nabcabcababcabcabcc4567"
1609+
1610+
keydown 'u'
1611+
expect(editor.getText()).toBe "abcabcabc123\nabcabcabc4567"
1612+
1613+
editor.setText('123\n4567')
1614+
editor.setCursorBufferPosition([0, 0])
1615+
keydown '2'
1616+
keydown '.'
1617+
expect(editor.getText()).toBe "abcabc123\n4567"
1618+
15631619
it "allows repeating typing", ->
15641620
keydown 'i'
15651621
editor.insertText("abcXX")
@@ -1574,7 +1630,21 @@ describe "Operators", ->
15741630
keydown '.'
15751631
expect(editor.getText()).toBe "abababccc123\nabababccc4567"
15761632

1633+
it "gets prefix from .", ->
1634+
keydown 'i'
1635+
editor.insertText("abc")
1636+
keydown 'escape'
1637+
expect(editor.getText()).toBe "abc123\nabc4567"
1638+
1639+
keydown '2'
1640+
keydown '.'
1641+
expect(editor.getText()).toBe "ababcabcc123\nababcabcc4567"
1642+
1643+
keydown '.'
1644+
expect(editor.getText()).toBe "ababcababcabccc123\nababcababcabccc4567"
1645+
15771646
it "stores for repeating only the last batch of characters", ->
1647+
keydown '2'
15781648
keydown 'i'
15791649
editor.insertText("abc")
15801650
atom.commands.dispatch editorElement, 'vim-mode:move-left-insert'
@@ -1608,6 +1678,21 @@ describe "Operators", ->
16081678
expect(editor.getText()).toBe "abcabc"
16091679
expect(editor.getCursorScreenPosition()).toEqual [0, 5]
16101680

1681+
it "combines with a prefix", ->
1682+
keydown '2'
1683+
keydown 'a'
1684+
editor.insertText("abc")
1685+
keydown 'escape'
1686+
expect(editor.getText()).toBe "abcabc"
1687+
expect(editor.getCursorScreenPosition()).toEqual [0, 5]
1688+
keydown '.'
1689+
expect(editor.getText()).toBe "abcabcabcabc"
1690+
expect(editor.getCursorScreenPosition()).toEqual [0, 11]
1691+
keydown '1'
1692+
keydown '.'
1693+
expect(editor.getText()).toBe "abcabcabcabcabc"
1694+
expect(editor.getCursorScreenPosition()).toEqual [0, 14]
1695+
16111696
it "stores for repeating only the last batch of characters, repeats as insert", ->
16121697
keydown 'a'
16131698
editor.insertText("abc")

0 commit comments

Comments
 (0)