Always pass a hash to TextBuffer.scanInRange iterators

This makes it easy to only assign variables for the information you
need in the iterator. Before, we always forced you to take a match and
a range as the first two arguments even if you weren't using them.
This commit is contained in:
Nathan Sobo
2013-04-03 11:09:26 -06:00
parent 68a02fe009
commit 59a5a5bc8f
10 changed files with 40 additions and 40 deletions

View File

@@ -133,20 +133,20 @@ class Cursor
moveToFirstCharacterOfLine: ->
position = @getBufferPosition()
range = @getCurrentLineBufferRange()
scanRange = @getCurrentLineBufferRange()
newPosition = null
@editSession.scanInRange /^\s*/, range, (match, matchRange) =>
newPosition = matchRange.end
@editSession.scanInRange /^\s*/, scanRange, ({range}) =>
newPosition = range.end
return unless newPosition
newPosition = [position.row, 0] if newPosition.isEqual(position)
@setBufferPosition(newPosition)
skipLeadingWhitespace: ->
position = @getBufferPosition()
range = @getCurrentLineBufferRange()
scanRange = @getCurrentLineBufferRange()
endOfLeadingWhitespace = null
@editSession.scanInRange /^[ \t]*/, range, (match, matchRange) =>
endOfLeadingWhitespace = matchRange.end
@editSession.scanInRange /^[ \t]*/, scanRange, ({range}) =>
endOfLeadingWhitespace = range.end
@setBufferPosition(endOfLeadingWhitespace) if endOfLeadingWhitespace.isGreaterThan(position)
@@ -164,12 +164,12 @@ class Cursor
allowPrevious = options.allowPrevious ? true
currentBufferPosition = @getBufferPosition()
previousNonBlankRow = @editSession.buffer.previousNonBlankRow(currentBufferPosition.row)
range = [[previousNonBlankRow, 0], currentBufferPosition]
scanRange = [[previousNonBlankRow, 0], currentBufferPosition]
beginningOfWordPosition = null
@editSession.backwardsScanInRange (options.wordRegex ? @wordRegExp()), range, (match, matchRange, { stop }) =>
if matchRange.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = matchRange.start
@editSession.backwardsScanInRange (options.wordRegex ? @wordRegExp()), scanRange, ({range, stop}) =>
if range.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = range.start
if not beginningOfWordPosition?.isEqual(currentBufferPosition)
stop()
@@ -178,12 +178,12 @@ class Cursor
getEndOfCurrentWordBufferPosition: (options = {}) ->
allowNext = options.allowNext ? true
currentBufferPosition = @getBufferPosition()
range = [currentBufferPosition, @editSession.getEofBufferPosition()]
scanRange = [currentBufferPosition, @editSession.getEofBufferPosition()]
endOfWordPosition = null
@editSession.scanInRange (options.wordRegex ? @wordRegExp()), range, (match, matchRange, { stop }) =>
if matchRange.start.isLessThanOrEqual(currentBufferPosition) or allowNext
endOfWordPosition = matchRange.end
@editSession.scanInRange (options.wordRegex ? @wordRegExp()), scanRange, ({range, stop}) =>
if range.start.isLessThanOrEqual(currentBufferPosition) or allowNext
endOfWordPosition = range.end
if not endOfWordPosition?.isEqual(currentBufferPosition)
stop()

View File

@@ -245,7 +245,7 @@ class EditSession
normalizeTabsInBufferRange: (bufferRange) ->
return unless @softTabs
@scanInRange /\t/, bufferRange, (match, range, {replace}) => replace(@getTabText())
@scanInRange /\t/, bufferRange, ({replace}) => replace(@getTabText())
cutToEndOfLine: ->
maintainPasteboard = false

View File

@@ -404,7 +404,7 @@ class Buffer
range = new Range(startPosition, endPosition)
keepLooping = true
replacementText = null
iterator(match, range, { stop, replace })
iterator({match, range, stop, replace })
if replacementText?
@change(range, replacementText)

View File

@@ -142,7 +142,7 @@ class AutocompleteView extends SelectList
lineRange = [[selectionRange.start.row, 0], [selectionRange.end.row, @editor.lineLengthForBufferRow(selectionRange.end.row)]]
[prefix, suffix] = ["", ""]
@currentBuffer.scanInRange @wordRegex, lineRange, (match, range, {stop}) ->
@currentBuffer.scanInRange @wordRegex, lineRange, ({match, range, stop}) ->
stop() if range.start.isGreaterThan(selectionRange.end)
if range.intersectsWith(selectionRange)

View File

@@ -102,7 +102,7 @@ module.exports =
regex = new RegExp("[#{_.escapeRegExp(startPair + endPair)}]", 'g')
endPairPosition = null
unpairedCount = 0
buffer.scanInRange regex, scanRange, (match, range, {stop}) =>
buffer.scanInRange regex, scanRange, ({match, range, stop}) =>
if match[0] is startPair
unpairedCount++
else if match[0] is endPair
@@ -116,7 +116,7 @@ module.exports =
regex = new RegExp("[#{_.escapeRegExp(startPair + endPair)}]", 'g')
startPairPosition = null
unpairedCount = 0
buffer.backwardsScanInRange regex, scanRange, (match, range, {stop}) =>
buffer.backwardsScanInRange regex, scanRange, ({match, range, stop}) =>
if match[0] is endPair
unpairedCount++
else if match[0] is startPair

View File

@@ -24,12 +24,12 @@ class RegexAddress extends Address
rangeToReturn = null
scanMethodName = if @isReversed then "backwardsScanInRange" else "scanInRange"
buffer[scanMethodName] @regex, rangeToSearch, (match, range) ->
buffer[scanMethodName] @regex, rangeToSearch, ({range}) ->
rangeToReturn = range
if not rangeToReturn
rangeToSearch = if @isReversed then rangeAfter else rangeBefore
buffer[scanMethodName] @regex, rangeToSearch, (match, range) ->
buffer[scanMethodName] @regex, rangeToSearch, ({range}) ->
rangeToReturn = range
if not rangeToReturn

View File

@@ -12,12 +12,12 @@ class SelectAllMatches extends Command
compile: (project, buffer, ranges) ->
deferred = $.Deferred()
operations = []
for range in ranges
buffer.scanInRange @regex, range, (match, matchRange) ->
for scanRange in ranges
buffer.scanInRange @regex, scanRange, ({range}) ->
operations.push(new Operation(
project: project
buffer: buffer
bufferRange: matchRange
bufferRange: range
))
deferred.resolve(operations)
deferred.promise()

View File

@@ -15,12 +15,12 @@ class Substitution extends Command
compile: (project, buffer, ranges) ->
deferred = $.Deferred()
operations = []
for range in ranges
buffer.scanInRange @regex, range, (match, matchRange) =>
for scanRange in ranges
buffer.scanInRange @regex, scanRange, ({range}) =>
operations.push(new Operation(
project: project
buffer: buffer
bufferRange: matchRange
bufferRange: range
newText: @replacementText
preserveSelection: true
))

View File

@@ -8,7 +8,7 @@ module.exports =
whitespaceBeforeSave: (buffer) ->
buffer.on 'will-be-saved', ->
buffer.transact ->
buffer.scan /[ \t]+$/g, (match, range, { replace }) -> replace('')
buffer.scan /[ \t]+$/g, ({replace}) -> replace('')
if config.get('whitespace.ensureSingleTrailingNewline')
if buffer.getLastLine() is ''