mirror of
https://github.com/atom/atom.git
synced 2026-02-09 14:15:24 -05:00
Add x command, which selects all matches in the current selection
This commit is contained in:
39
src/atom/command-interpreter/select-all-matches.coffee
Normal file
39
src/atom/command-interpreter/select-all-matches.coffee
Normal file
@@ -0,0 +1,39 @@
|
||||
Command = require 'command-interpreter/command'
|
||||
Range = require 'range'
|
||||
|
||||
module.exports =
|
||||
class SelectAllMatches extends Command
|
||||
@regex: null
|
||||
|
||||
constructor: (pattern) ->
|
||||
@regex = new RegExp(pattern)
|
||||
|
||||
execute: (editor) ->
|
||||
selectedText = editor.getSelectedText()
|
||||
selectionStartIndex = editor.buffer.characterIndexForPosition(editor.getSelection().getBufferRange().start)
|
||||
|
||||
matchingRanges = @findMatchingRanges(editor, selectedText, selectionStartIndex)
|
||||
return unless matchingRanges.length
|
||||
editor.setSelectionBufferRange(matchingRanges[0])
|
||||
editor.addSelectionForBufferRange(range) for range in matchingRanges[1..]
|
||||
|
||||
|
||||
findMatchingRanges: (editor, text, startIndex) ->
|
||||
console.log text
|
||||
return [] unless match = text.match(@regex)
|
||||
|
||||
console.log match
|
||||
console.log match[0]
|
||||
|
||||
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))
|
||||
|
||||
@@ -6,16 +6,14 @@
|
||||
var EofAddress = require('command-interpreter/eof-address');
|
||||
var CurrentSelectionAddress = require('command-interpreter/current-selection-address')
|
||||
var RegexAddress = require('command-interpreter/regex-address')
|
||||
var SelectAllMatches = require('command-interpreter/select-all-matches')
|
||||
}
|
||||
|
||||
start
|
||||
= address:address? _ command:substitution? {
|
||||
var commands = [];
|
||||
if (address) commands.push(address);
|
||||
if (command) commands.push(command);
|
||||
start = expressions:(expression+) {
|
||||
return new CompositeCommand(expressions)
|
||||
}
|
||||
|
||||
return new CompositeCommand(commands);
|
||||
}
|
||||
expression = _ expression:(address / command) _ { return expression; }
|
||||
|
||||
address = addressRange / primitiveAddress
|
||||
|
||||
@@ -32,11 +30,16 @@ primitiveAddress
|
||||
/ '.' { return new CurrentSelectionAddress() }
|
||||
/ '/' pattern:pattern '/'? { return new RegexAddress(pattern)}
|
||||
|
||||
command = substitution / selectAllMatches
|
||||
|
||||
substitution
|
||||
= "s" _ "/" find:pattern "/" replace:pattern "/" _ options:[g]* {
|
||||
return new Substitution(find, replace, options);
|
||||
}
|
||||
|
||||
selectAllMatches
|
||||
= 'x' _ '/' pattern:pattern '/'? { return new SelectAllMatches(pattern) }
|
||||
|
||||
pattern
|
||||
= pattern:[^/]* { return pattern.join('') }
|
||||
|
||||
|
||||
@@ -356,6 +356,7 @@ class Editor extends View
|
||||
getCursorBufferPosition: -> @getCursor().getBufferPosition()
|
||||
|
||||
getSelection: (index) -> @compositeSelection.getSelection(index)
|
||||
getSelections: -> @compositeSelection.getSelections()
|
||||
getSelectedText: -> @compositeSelection.getSelection().getText()
|
||||
setSelectionBufferRange: (bufferRange, options) -> @compositeSelection.setBufferRange(bufferRange, options)
|
||||
addSelectionForBufferRange: (bufferRange, options) -> @compositeSelection.addSelectionForBufferRange(bufferRange, options)
|
||||
|
||||
Reference in New Issue
Block a user