Adding '-' in front of a RegexAddress causes it to search in reverse

This commit is contained in:
Corey Johnson
2012-06-13 18:10:54 -07:00
committed by Corey Johnson & Nathan Sobo
parent 940b7b0e7a
commit c98ad2973d
3 changed files with 20 additions and 6 deletions

View File

@@ -105,6 +105,11 @@ describe "CommandInterpreter", ->
interpreter.eval(editor, '/mike tyson')
expect(editor.getSelection().getBufferRange()).toEqual [[3,8], [3,13]]
it "searches in reverse when prefixed with a -", ->
editor.setSelectedBufferRange([[5, 0], [5,1]])
interpreter.eval(editor, '-/pivot')
expect(editor.getSelection().getBufferRange()).toEqual [[3,8], [3,13]]
describe "address range", ->
describe "when two addresses are specified", ->
it "selects from the begining of the left address to the end of the right address", ->

View File

@@ -28,7 +28,10 @@ primitiveAddress
= lineNumber:integer { return new LineAddress(lineNumber) }
/ '$' { return new EofAddress() }
/ '.' { return new CurrentSelectionAddress() }
/ '/' pattern:pattern '/'? { return new RegexAddress(pattern)}
/ regexAddress
regexAddress
= reverse:'-'? '/' pattern:pattern '/'? { return new RegexAddress(pattern, reverse.length > 0)}
command = substitution / selectAllMatches

View File

@@ -4,22 +4,28 @@ Range = require 'range'
module.exports =
class RegexAddress extends Address
regex: null
reverse: null
constructor: (pattern) ->
constructor: (pattern, reverse) ->
@reverse = reverse
@regex = new RegExp(pattern)
getRange: (editor, currentRange) ->
rangeToSearch = new Range(currentRange.end, editor.getEofPosition())
rangeBefore = new Range([0, 0], currentRange.end)
rangeAfter = new Range(currentRange.end, editor.getEofPosition())
rangeToSearch = if @reverse then rangeBefore else rangeAfter
rangeToReturn = null
editor.buffer.scanInRange @regex, rangeToSearch, (match, range) ->
scanMethodName = if @reverse then "backwardsScanInRange" else "scanInRange"
editor[scanMethodName] @regex, rangeToSearch, (match, range) ->
rangeToReturn = range
if rangeToReturn
rangeToReturn
else
rangeToSearch = new Range([0, 0], rangeToSearch.start)
editor.buffer.scanInRange @regex, rangeToSearch, (match, range) ->
rangeToSearch = if @reverse then rangeAfter else rangeBefore
editor[scanMethodName] @regex, rangeToSearch, (match, range) ->
rangeToReturn = range
rangeToReturn or currentRange