x command loops over all selected regions

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-27 14:32:20 -07:00
parent ad3aa0709c
commit 7d969e145a
6 changed files with 42 additions and 14 deletions

View File

@@ -90,6 +90,20 @@ describe "CommandInterpreter", ->
expect(selections[2].getBufferRange()).toEqual [[6,34], [6,41]]
expect(selections[3].getBufferRange()).toEqual [[6,56], [6,63]]
it "loops through current selections and selects text matching the regex", ->
editor.setSelectionBufferRange [[3,0], [3,62]]
editor.addSelectionForBufferRange [[6,0], [6,65]]
interpreter.eval(editor, 'x/current')
selections = editor.getSelections()
expect(selections.length).toBe 4
expect(selections[0].getBufferRange()).toEqual [[3,31], [3,38]]
expect(selections[1].getBufferRange()).toEqual [[6,6], [6,13]]
expect(selections[2].getBufferRange()).toEqual [[6,34], [6,41]]
expect(selections[3].getBufferRange()).toEqual [[6,56], [6,63]]
describe "substitution", ->
it "does nothing if there are no matches", ->
editor.getSelection().setBufferRange([[6, 0], [6, 44]])

View File

@@ -9,22 +9,19 @@ class SelectAllMatches extends Command
@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..]
rangesToSelect = []
for selection in editor.getSelections()
selectedText = selection.getText()
selectionStartIndex = editor.buffer.characterIndexForPosition(selection.getBufferRange().start)
for range in @findMatchingRanges(editor, selectedText, selectionStartIndex)
rangesToSelect.push(range)
editor.clearSelections()
editor.addSelectionForBufferRange(range) for range in rangesToSelect
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

View File

@@ -12,17 +12,31 @@ class CompositeSeleciton
getSelections: -> new Array(@selections...)
clearSelections: ->
for selection in @getSelections()[1..]
selection.cursor.remove()
@getLastSelection().clearSelection()
addSelectionForCursor: (cursor) ->
selection = new Selection({@editor, cursor})
@selections.push(selection)
@editor.lines.append(selection)
addSelectionForBufferRange: (bufferRange, options) ->
cursor = @editor.compositeCursor.addCursor()
selections = @getSelections()
cursor = if selections.length == 1 and selections[0].isEmpty()
selections[0].cursor
else
@editor.compositeCursor.addCursor()
@selectionForCursor(cursor).setBufferRange(bufferRange, options)
removeSelectionForCursor: (cursor) ->
_.remove(@selections, @selectionForCursor(cursor))
selection = @selectionForCursor(cursor)
selection.cursor = null
selection.remove()
_.remove(@selections, selection)
selectionForCursor: (cursor) ->
_.find @selections, (selection) -> selection.cursor == cursor

View File

@@ -33,7 +33,7 @@ class Cursor extends View
remove: ->
@editor.compositeCursor.removeCursor(this)
@editor.compositeSelection.removeSelectionForCursor(this)
super()
super
setScreenPosition: (position, options={}) ->
position = Point.fromObject(position)

View File

@@ -365,6 +365,7 @@ class Editor extends View
selectUp: -> @compositeSelection.selectUp()
selectDown: -> @compositeSelection.selectDown()
selectToScreenPosition: (position) -> @compositeSelection.selectToScreenPosition(position)
clearSelections: -> @compositeSelection.clearSelections()
setText: (text) -> @buffer.setText(text)
getText: -> @buffer.getText()

View File

@@ -43,6 +43,8 @@ class Selection extends View
@updateAppearance()
updateAppearance: ->
return unless @cursor
@clearRegions()
range = @getScreenRange()