Command interpreter takes x,x as an address

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-21 17:33:05 -07:00
parent b09bd44ed7
commit a2156e39c0
4 changed files with 42 additions and 3 deletions

View File

@@ -27,3 +27,11 @@ describe "CommandInterpreter", ->
interpreter.eval(editor, 's/current/foo/g')
expect(buffer.lineForRow(6)).toBe ' foo < pivot ? left.push(foo) : right.push(current);'
describe "when prefixed with an address", ->
it "only makes substitutions within given lines", ->
interpreter.eval(editor, '4,7s/ /!/g')
expect(buffer.lineForRow(2)).toBe ' if (items.length <= 1) return items;'
expect(buffer.lineForRow(3)).toBe '!!!!var!pivot!=!items.shift(),!current,!left!=![],!right!=![];'
expect(buffer.lineForRow(4)).toBe '!!!!while(items.length!>!0)!{'
expect(buffer.lineForRow(5)).toBe '!!!!!!current!=!items.shift();'
expect(buffer.lineForRow(6)).toBe ' current < pivot ? left.push(current) : right.push(current);'

View File

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

View File

@@ -0,0 +1,14 @@
Range = require 'range'
module.exports =
class Address
startRow: null
endRow: null
constructor: (start, end) ->
@startRow = start - 1
@endRow = end - 1
execute: (editor) ->
range = new Range([@startRow, 0], [@endRow, 0])
editor.getSelection().setBufferRange(range)

View File

@@ -1,10 +1,27 @@
{
var Substitution = require('command-interpreter/substitution');
var Address = require('command-interpreter/address');
}
start
= address:address? _ command:substitution {
var operations = [];
if (address) operations.push(address);
operations.push(command);
return operations;
}
address
= start:integer _ ',' _ end:integer {
return new Address(start, end)
}
substitution
= "s" _ "/" find:([^/]*) "/" replace:([^/]*) "/" _ options:[g]* {
return new Substitution(find.join(''), replace.join(''), options)
return new Substitution(find.join(''), replace.join(''), options);
}
integer
= digits:[0-9]+ { return parseInt(digits.join('')); }
_ = " "*