Add Buffer.backwardsTraverseRegexMatchesInRange

This commit is contained in:
Nathan Sobo
2012-03-28 14:20:41 -07:00
parent cce1218fda
commit e7a9ee5bee
2 changed files with 87 additions and 1 deletions

View File

@@ -179,4 +179,26 @@ class Buffer
endIndex = @characterIndexForPosition(range.end)
traverseRecursively(@getText(), startIndex, endIndex, 0)
backwardsTraverseRegexMatchesInRange: (regex, range, iterator) ->
global = regex.global
regex = new RegExp(regex.source, 'gm')
matches = []
@traverseRegexMatchesInRange regex, range, (match, matchRange) ->
matches.push([match, matchRange])
matches.reverse()
recurse = true
stop = -> recurse = false
replacementText = null
replace = (text) -> replacementText = text
for [match, matchRange] in matches
replacementText = null
iterator(match, matchRange, { stop, replace })
@change(matchRange, replacementText) if replacementText
return unless global and recurse
_.extend(Buffer.prototype, EventEmitter)