mirror of
https://github.com/atom/atom.git
synced 2026-02-09 14:15:24 -05:00
Implement select all matches command (x) with Buffer.traverseRegexMatchesInRange
This commit is contained in:
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user