Add CommandInterpreter.repeatLastRelativeAddress

This commit is contained in:
Nathan Sobo
2012-03-23 11:32:58 -06:00
parent 49be6986a0
commit 3bda7c4d3f
12 changed files with 75 additions and 13 deletions

View File

@@ -6,7 +6,11 @@ class CommandInterpreter
constructor: ->
@parser = PEG.buildParser(fs.read(require.resolve 'commands.pegjs'))
eval: (editor, command) ->
operations = @parser.parse(command)
operation.execute(editor) for operation in operations
eval: (editor, string) ->
command = @parser.parse(string)
@lastRelativeAddress = command if command.isRelativeAddress()
command.execute(editor)
repeatLastRelativeAddress: (editor) ->
@lastRelativeAddress.execute(editor)

View File

@@ -7,3 +7,6 @@ class AddressRange extends Address
getRange: (editor) ->
new Range(@startAddress.getRange(editor).start, @endAddress.getRange(editor).end)
isRelative: ->
@startAddress.isRelative() or @endAddress.isRelative()

View File

@@ -1,4 +1,8 @@
Command = require 'command-interpreter/command'
module.exports =
class Address
class Address extends Command
execute: (editor) ->
editor.getSelection().setBufferRange(@getRange(editor))
isAddress: -> true

View File

@@ -0,0 +1,5 @@
_ = require 'underscore'
module.exports =
class Command
isAddress: -> false

View File

@@ -0,0 +1,12 @@
_ = require 'underscore'
module.exports =
class CompositeCommand
constructor: (@subcommands) ->
execute: (editor) ->
command.execute(editor) for command in @subcommands
isRelativeAddress: ->
_.all(@subcommands, (command) -> command.isAddress() and command.isRelative())

View File

@@ -5,3 +5,5 @@ module.exports =
class CurrentSelectionAddress extends Address
getRange: (editor) ->
editor.getSelection().getBufferRange()
isRelative: -> true

View File

@@ -6,4 +6,6 @@ class EofAddress extends Address
getRange: (editor) ->
lastRow = editor.getLastBufferRow()
column = editor.getBufferLineLength(lastRow)
new Range([lastRow, column], [lastRow, column])
new Range([lastRow, column], [lastRow, column])
isRelative: -> false

View File

@@ -8,3 +8,5 @@ class LineAddress extends Address
getRange: ->
new Range([@row, 0], [@row + 1, 0])
isRelative: -> false

View File

@@ -23,4 +23,6 @@ class RegexAddress extends Address
return new Range(startPosition, endPosition)
else
return selectedRange
return selectedRange
isRelative: -> true

View File

@@ -1,5 +1,7 @@
Command = require 'command-interpreter/command'
module.exports =
class Substitution
class Substitution extends Command
global: false
constructor: (@findText, @replaceText, @options) ->
@@ -28,3 +30,4 @@ class Substitution
text = text[(match.index + match[0].length)..]
startIndex = matchStartIndex + @replaceText.length
@replace(editor, text, startIndex)

View File

@@ -1,4 +1,5 @@
{
var CompositeCommand = require('command-interpreter/composite-command')
var Substitution = require('command-interpreter/substitution');
var LineAddress = require('command-interpreter/line-address');
var AddressRange = require('command-interpreter/address-range');
@@ -9,10 +10,11 @@
start
= address:address? _ command:substitution? {
var operations = [];
if (address) operations.push(address);
if (command) operations.push(command);
return operations;
var commands = [];
if (address) commands.push(address);
if (command) commands.push(command);
return new CompositeCommand(commands);
}
address = addressRange / primitiveAddress