Make @editor instance var on motions, commands & operators

We were passing it around all over the place. Now access to the editor
can always be assumed.
This commit is contained in:
Nathan Sobo
2012-01-14 08:28:34 -08:00
parent 1c12773a7a
commit 30faf9b043
4 changed files with 36 additions and 34 deletions

View File

@@ -36,10 +36,10 @@ class VimMode
@handleCommands
'insert': => @activateInsertMode()
'delete': => @delete()
'delete-char': => new commands.DeleteChar
'move-left': => new motions.MoveLeft
'move-up': => new motions.MoveUp
'move-to-next-word': => new motions.MoveToNextWord
'delete-char': => new commands.DeleteChar(@editor)
'move-left': => new motions.MoveLeft(@editor)
'move-up': => new motions.MoveUp(@editor)
'move-to-next-word': => new motions.MoveToNextWord(@editor)
'numeric-prefix': (e) => @numericPrefix(e)
bindCommandModeKeys: (bindings) ->
@@ -73,9 +73,9 @@ class VimMode
delete: () ->
if @topOperator() instanceof operators.Delete
@pushOperator(new motions.SelectLine)
@pushOperator(new motions.SelectLine(@editor))
else
@pushOperator(new operators.Delete)
@pushOperator(new operators.Delete(@editor))
pushOperator: (op) ->
@opStack.push(op)
@@ -88,7 +88,7 @@ class VimMode
@topOperator().compose(poppedOperator)
@processOpStack()
else
poppedOperator.execute(@editor)
poppedOperator.execute()
topOperator: ->
_.last @opStack

View File

@@ -1,9 +1,10 @@
class Command
constructor: (@editor) ->
isComplete: -> true
class DeleteChar extends Command
execute: (editor) ->
editor.deleteChar()
execute: ->
@editor.deleteChar()
module.exports = { DeleteChar }

View File

@@ -1,29 +1,30 @@
getWordRegex = -> /(\w+)|([^\w\s]+)/g
class Motion
constructor: (@editor) ->
isComplete: -> true
class MoveLeft extends Motion
execute: (editor) ->
{column, row} = editor.getPosition()
editor.moveLeft() if column > 0
execute: ->
{column, row} = @editor.getPosition()
@editor.moveLeft() if column > 0
class MoveUp extends Motion
execute: (editor) ->
{column, row} = editor.getPosition()
editor.moveUp() if row > 0
execute: ->
{column, row} = @editor.getPosition()
@editor.moveUp() if row > 0
class MoveToNextWord extends Motion
execute: (editor) ->
editor.setPosition(@nextWordPosition(editor))
execute: ->
@editor.setPosition(@nextWordPosition())
select: (editor) ->
editor.selectToPosition(@nextWordPosition(editor))
select: ->
@editor.selectToPosition(@nextWordPosition())
nextWordPosition: (editor) ->
nextWordPosition: ->
regex = getWordRegex()
{ row, column } = editor.getPosition()
rightOfCursor = editor.getLineText(row).substring(column)
{ row, column } = @editor.getPosition()
rightOfCursor = @editor.getLineText(row).substring(column)
match = regex.exec(rightOfCursor)
# If we're on top of part of a word, match the next one.
@@ -32,13 +33,13 @@ class MoveToNextWord extends Motion
if match
column += match.index
else
nextLineMatch = regex.exec(editor.getLineText(++row))
nextLineMatch = regex.exec(@editor.getLineText(++row))
column = nextLineMatch?.index or 0
{ row, column }
class SelectLine extends Motion
select: (editor) ->
editor.selectLine()
select: ->
@editor.selectLine()
module.exports = { MoveLeft, MoveUp, MoveToNextWord, SelectLine }

View File

@@ -16,27 +16,27 @@ class NumericPrefix
addDigit: (digit) ->
@count = @count * 10 + digit
execute: (editor) ->
_.times @count, => @operatorToRepeat.execute(editor)
execute: ->
_.times @count, => @operatorToRepeat.execute()
select: (editor) ->
_.times @count, => @operatorToRepeat.select(editor)
select: ->
_.times @count, => @operatorToRepeat.select()
class Delete
motion: null
complete: null
constructor: ->
constructor: (@editor) ->
@complete = false
isComplete: -> @complete
execute: (editor) ->
execute: ->
if @motion
@motion.select(editor)
editor.delete()
@motion.select()
@editor.delete()
else
editor.deleteLine()
@editor.deleteLine()
compose: (motion) ->
@motion = motion