Commands terminating in a substitution restore their initial selection

This commit is contained in:
Nathan Sobo
2012-07-02 19:35:14 -06:00
parent 81530761ba
commit 8fbbd77a23
6 changed files with 13 additions and 2 deletions

View File

@@ -239,6 +239,12 @@ describe "CommandInterpreter", ->
expect(buffer.lineForRow(5)).toBe ' foo = items.shift();'
expect(buffer.lineForRow(6)).toBe ' foo < pivot ? left.push(foo) : right.push(current);'
describe "when prefixed with an address", ->
it "restores the original selections upon completion if it is the last command", ->
editor.setSelectedBufferRanges([[[5, 0], [5, 20]], [[6, 0], [6, 44]]])
interpreter.eval(editor, ',s/current/foo/g')
expect(editor.getSelectedBufferRanges()).toEqual [[[5, 0], [5, 20]], [[6, 0], [6, 44]]]
describe "when command selects folded text", ->
it "unfolds lines that command selects", ->
editor.createFold(1, 9)

View File

@@ -351,7 +351,7 @@ class EditSession
@getLastSelection().getBufferRange()
getSelectedBufferRanges: ->
selection.getBufferRange() for selection in @selections
selection.getBufferRange() for selection in @getSelectionsOrderedByBufferPosition()
getSelectedText: ->
@getLastSelection().getText()

View File

@@ -184,6 +184,7 @@ class Editor extends View
getSelectionsOrderedByBufferPosition: -> @activeEditSession.getSelectionsOrderedByBufferPosition()
getLastSelectionInBuffer: -> @activeEditSession.getLastSelectionInBuffer()
getSelectedText: -> @activeEditSession.getSelectedText()
getSelectedBufferRanges: -> @activeEditSession.getSelectedBufferRanges()
getSelectedBufferRange: -> @activeEditSession.getSelectedBufferRange()
setSelectedBufferRange: (bufferRange, options) -> @activeEditSession.setSelectedBufferRange(bufferRange, options)
setSelectedBufferRanges: (bufferRanges, options) -> @activeEditSession.setSelectedBufferRanges(bufferRanges, options)

View File

@@ -3,3 +3,4 @@ _ = require 'underscore'
module.exports =
class Command
isAddress: -> false
restoreSelections: false

View File

@@ -5,9 +5,10 @@ class CompositeCommand
constructor: (@subcommands) ->
execute: (editor) ->
initialRanges = editor.getSelectedBufferRanges()
for command in @subcommands
newRanges = []
currentRanges = editor.getSelectionsOrderedByBufferPosition().map (selection) -> selection.getBufferRange()
currentRanges = editor.getSelectedBufferRanges()
for currentRange in currentRanges
newRanges.push(command.execute(editor, currentRange)...)
@@ -16,6 +17,7 @@ class CompositeCommand
editor.destroyFoldsContainingBufferRow(row)
editor.setSelectedBufferRanges(newRanges)
editor.setSelectedBufferRanges(initialRanges) if command.restoreSelections
reverse: ->
new CompositeCommand(@subcommands.map (command) -> command.reverse())

View File

@@ -4,6 +4,7 @@ module.exports =
class Substitution extends Command
regex: null
replacementText: null
restoreSelections: true
constructor: (pattern, replacementText, options) ->
@replacementText = replacementText