Implement d3d

Delete line can take a count before the second d. When the second d is
typed it pushed a SelectLines operation. When a numeric prefix is
composed with SelectLines, it assigns its count on select lines, which
uses it to determine how many lines to select. This bypasses the normal
"repeat command" semantics of numeric prefix.
This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-23 11:37:00 -08:00
parent 9b3fc10130
commit a647fa4220
4 changed files with 33 additions and 5 deletions

View File

@ -57,6 +57,18 @@ describe "VimMode", ->
expect(editor.buffer.getText()).toBe "12345\nABCDE" expect(editor.buffer.getText()).toBe "12345\nABCDE"
expect(editor.getPosition()).toEqual(column: 0, row: 1) expect(editor.getPosition()).toEqual(column: 0, row: 1)
describe "when the second d is prefixed by a count", ->
it "deletes n lines, starting from the current", ->
editor.buffer.setText("12345\nabcde\nABCDE\nQWERT")
editor.setPosition(column: 1, row: 1)
editor.trigger keydownEvent('d')
editor.trigger keydownEvent('2')
editor.trigger keydownEvent('d')
expect(editor.buffer.getText()).toBe "12345\nQWERT"
expect(editor.getPosition()).toEqual(column: 0, row: 1)
describe "when followed by an h", -> describe "when followed by an h", ->
it "deletes the previous letter on the current line", -> it "deletes the previous letter on the current line", ->
editor.buffer.setText("abcd\n01234") editor.buffer.setText("abcd\n01234")

View File

@ -74,11 +74,16 @@ class VimMode
@pushOperator(new operators.NumericPrefix(num)) @pushOperator(new operators.NumericPrefix(num))
delete: () -> delete: () ->
if @topOperator() instanceof operators.Delete if @isDeletePending()
@pushOperator(new motions.SelectLine(@editor)) @pushOperator(new motions.SelectLines(@editor))
else else
@pushOperator(new operators.Delete(@editor)) @pushOperator(new operators.Delete(@editor))
isDeletePending: () ->
for op in @opStack
return true if op instanceof operators.Delete
false
pushOperator: (op) -> pushOperator: (op) ->
@opStack.push(op) @opStack.push(op)
@processOpStack() @processOpStack()

View File

@ -47,9 +47,17 @@ class MoveToNextWord extends Motion
column = nextLineMatch?.index or 0 column = nextLineMatch?.index or 0
{ row, column } { row, column }
class SelectLine extends Motion class SelectLines extends Motion
count: null
constructor: (@editor) ->
@count = 1
setCount: (@count) ->
select: -> select: ->
@editor.selectLine() @editor.setPosition(column: 0, row: @editor.getRow())
@editor.selectToPosition(column: 0, row: @editor.getRow() + @count)
module.exports = { MoveLeft, MoveUp, MoveDown, MoveToNextWord, SelectLine } module.exports = { MoveLeft, MoveUp, MoveDown, MoveToNextWord, SelectLines }

View File

@ -12,6 +12,9 @@ class NumericPrefix
compose: (@operatorToRepeat) -> compose: (@operatorToRepeat) ->
@complete = true @complete = true
if @operatorToRepeat.setCount?
@operatorToRepeat.setCount @count
@count = 1
addDigit: (digit) -> addDigit: (digit) ->
@count = @count * 10 + digit @count = @count * 10 + digit