Implement select all matches command (x) with Buffer.traverseRegexMatchesInRange

This commit is contained in:
Nathan Sobo
2012-03-27 17:52:44 -07:00
parent d43296a1c4
commit e867c43b17

View File

@@ -3,34 +3,16 @@ Range = require 'range'
module.exports =
class SelectAllMatches extends Command
@regex: null
regex: null
constructor: (pattern) ->
@regex = @regexForPattern(pattern)
@regex = new RegExp(pattern, 'g')
execute: (editor) ->
rangesToSelect = []
for selection in editor.getSelections()
selectedText = selection.getText()
selectionStartIndex = editor.buffer.characterIndexForPosition(selection.getBufferRange().start)
for range in @findMatchingRanges(editor, selectedText, selectionStartIndex)
editor.buffer.traverseRegexMatchesInRange @regex, selection.getBufferRange(), (match, range) ->
rangesToSelect.push(range)
editor.clearSelections()
editor.addSelectionForBufferRange(range) for range in rangesToSelect
findMatchingRanges: (editor, text, startIndex) ->
return [] unless match = text.match(@regex)
matchStartIndex = startIndex + match.index
matchEndIndex = matchStartIndex + match[0].length
buffer = editor.buffer
startPosition = buffer.positionForCharacterIndex(matchStartIndex)
endPosition = buffer.positionForCharacterIndex(matchEndIndex)
range = new Range(startPosition, endPosition)
text = text[(match.index + match[0].length)..]
startIndex = matchEndIndex
[range].concat(@findMatchingRanges(editor, text, startIndex))