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

Commit abedc16

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 e10cf07 commit abedc16

File tree

4 files changed

+118
-13
lines changed

4 files changed

+118
-13
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-
confirmTransaction: (transaction) ->
15+
confirmTransaction: (transaction, interrupted) ->
1516
bundler = new TransactionBundler(transaction)
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
@@ -111,7 +116,7 @@ class Change extends Insert
111116
else
112117
@editor.delete()
113118

114-
return super if @typingCompleted
119+
return super(1) if @typingCompleted
115120

116121
@vimState.activateInsertMode()
117122
@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
@@ -157,7 +162,7 @@ class SubstituteLine extends Insert
157162

158163
if @typingCompleted
159164
@typedText = @typedText.trimLeft()
160-
return super
165+
return super(1)
161166

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

lib/vim-state.coffee

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,12 @@ class VimState
402402
return unless @mode in [null, 'insert']
403403
@editorElement.component.setInputEnabled(false)
404404
@editor.groupChangesSinceCheckpoint(@insertionCheckpoint)
405-
@insertionCheckpoint = null
406405
transaction = _.last(@editor.buffer.history.undoStack)
407406
item = @inputOperator(@history[0])
408407
if item? and transaction?
409408
item.confirmTransaction(transaction)
409+
@editor.groupChangesSinceCheckpoint(@insertionCheckpoint)
410+
@insertionCheckpoint = null
410411
for cursor in @editor.getCursors()
411412
cursor.moveLeft() unless cursor.isAtBeginningOfLine()
412413

@@ -417,7 +418,7 @@ class VimState
417418
transaction = _.last(@editor.buffer.history.undoStack)
418419
item = @inputOperator(@history[0])
419420
if item? and transaction?
420-
item.confirmTransaction(transaction)
421+
item.confirmTransaction(transaction, interrupted: true)
421422
@setInsertionCheckpoint()
422423

423424

spec/operators-spec.coffee

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

123+
it "is repeatable with a new count", ->
124+
keydown('3')
125+
keydown('s')
126+
editor.insertText("ab")
127+
keydown('escape')
128+
expect(editor.getText()).toBe '0ab45'
129+
keydown('1')
130+
keydown('.')
131+
expect(editor.getText()).toBe '0aab45'
132+
editor.setCursorScreenPosition([0, 0])
133+
keydown('3')
134+
keydown('.')
135+
expect(editor.getText()).toBe 'abb45'
136+
123137
it "is undoable", ->
124138
editor.setCursorScreenPosition([0, 0])
125139
keydown('3')
@@ -1231,14 +1245,33 @@ describe "Operators", ->
12311245

12321246
expect(editor.getText()).toBe ""
12331247

1234-
it "composes with motions", ->
1248+
it "composes with prefix", ->
12351249
keydown 'd'
12361250
keydown 'd'
12371251
keydown '2'
12381252
keydown '.'
12391253

12401254
expect(editor.getText()).toBe "78"
12411255

1256+
it "changes previous prefix", ->
1257+
keydown '2'
1258+
keydown 'd'
1259+
keydown 'd'
1260+
keydown '1'
1261+
keydown '.'
1262+
1263+
expect(editor.getText()).toBe "78"
1264+
1265+
it "adds prefix if none was there", ->
1266+
keydown 'd'
1267+
keydown 'd'
1268+
keydown 'u'
1269+
keydown '2'
1270+
keydown '.'
1271+
keydown '.'
1272+
1273+
expect(editor.getText()).toBe ""
1274+
12421275
describe "the r keybinding", ->
12431276
beforeEach ->
12441277
editor.setText("12\n34\n\n")
@@ -1410,6 +1443,29 @@ describe "Operators", ->
14101443
keydown 'u'
14111444
expect(editor.getText()).toBe "123\n4567"
14121445

1446+
it "allows count prefix", ->
1447+
keydown '3'
1448+
keydown 'i'
1449+
editor.insertText("abc")
1450+
keydown 'escape'
1451+
expect(editor.getText()).toBe "abcabcabc123\nabcabcabc4567"
1452+
1453+
keydown 'u'
1454+
expect(editor.getText()).toBe "123\n4567"
1455+
1456+
keydown '.'
1457+
keydown '.'
1458+
expect(editor.getText()).toBe "abcabcababcabcabcc123\nabcabcababcabcabcc4567"
1459+
1460+
keydown 'u'
1461+
expect(editor.getText()).toBe "abcabcabc123\nabcabcabc4567"
1462+
1463+
editor.setText('123\n4567')
1464+
editor.setCursorBufferPosition([0, 0])
1465+
keydown '2'
1466+
keydown '.'
1467+
expect(editor.getText()).toBe "abcabc123\n4567"
1468+
14131469
it "allows repeating typing", ->
14141470
keydown 'i'
14151471
editor.insertText("abcXX")
@@ -1424,7 +1480,21 @@ describe "Operators", ->
14241480
keydown '.'
14251481
expect(editor.getText()).toBe "abababccc123\nabababccc4567"
14261482

1483+
it "gets prefix from .", ->
1484+
keydown 'i'
1485+
editor.insertText("abc")
1486+
keydown 'escape'
1487+
expect(editor.getText()).toBe "abc123\nabc4567"
1488+
1489+
keydown '2'
1490+
keydown '.'
1491+
expect(editor.getText()).toBe "ababcabcc123\nababcabcc4567"
1492+
1493+
keydown '.'
1494+
expect(editor.getText()).toBe "ababcababcabccc123\nababcababcabccc4567"
1495+
14271496
it "stores for repeating only the last batch of characters", ->
1497+
keydown '2'
14281498
keydown 'i'
14291499
editor.insertText("abc")
14301500
atom.commands.dispatch editorElement, 'vim-mode:move-left-insert'
@@ -1458,6 +1528,21 @@ describe "Operators", ->
14581528
expect(editor.getText()).toBe "abcabc"
14591529
expect(editor.getCursorScreenPosition()).toEqual [0, 5]
14601530

1531+
it "combines with a prefix", ->
1532+
keydown '2'
1533+
keydown 'a'
1534+
editor.insertText("abc")
1535+
keydown 'escape'
1536+
expect(editor.getText()).toBe "abcabc"
1537+
expect(editor.getCursorScreenPosition()).toEqual [0, 5]
1538+
keydown '.'
1539+
expect(editor.getText()).toBe "abcabcabcabc"
1540+
expect(editor.getCursorScreenPosition()).toEqual [0, 11]
1541+
keydown '1'
1542+
keydown '.'
1543+
expect(editor.getText()).toBe "abcabcabcabcabc"
1544+
expect(editor.getCursorScreenPosition()).toEqual [0, 14]
1545+
14611546
it "stores for repeating only the last batch of characters, repeats as insert", ->
14621547
keydown 'a'
14631548
editor.insertText("abc")

0 commit comments

Comments
 (0)