Use deleteSelectedText() instead of delete()

Selection.delete() adds to the selection if it is empty which
we don't want to do when replacing selected text.

Closes #603
This commit is contained in:
Kevin Sawicki
2013-06-26 19:00:14 -07:00
parent f37ae7e4e7
commit e67676b01c
2 changed files with 22 additions and 2 deletions

View File

@@ -2117,6 +2117,26 @@ describe "EditSession", ->
expect(editSession.isFoldedAtScreenRow(4)).toBeTruthy()
expect(buffer.lineForRow(3)).toBe ' var pivot = items.shift(), current, left = [], right = [];'
describe ".replaceSelectedText(options, fn)", ->
describe "when no text is selected", ->
it "inserts the text returned from the function at the cursor position", ->
editSession.replaceSelectedText {}, -> '123'
expect(buffer.lineForRow(0)).toBe '123var quicksort = function () {'
editSession.replaceSelectedText {selectWordIfEmpty: true}, -> 'var'
editSession.setCursorBufferPosition([0])
expect(buffer.lineForRow(0)).toBe 'var quicksort = function () {'
editSession.setCursorBufferPosition([10])
editSession.replaceSelectedText null, -> ''
expect(buffer.lineForRow(10)).toBe ''
describe "when text is selected", ->
it "replaces the selected text with the text returned from the function", ->
editSession.setSelectedBufferRange([[0, 1], [0, 3]])
editSession.replaceSelectedText {}, -> 'ia'
expect(buffer.lineForRow(0)).toBe 'via quicksort = function () {'
describe ".transpose()", ->
it "swaps two characters", ->
editSession.buffer.setText("abc")

View File

@@ -729,12 +729,12 @@ class EditSession
replaceSelectedText: (options={}, fn) ->
{selectWordIfEmpty} = options
@mutateSelectedText (selection) =>
@mutateSelectedText (selection) ->
range = selection.getBufferRange()
if selectWordIfEmpty and selection.isEmpty()
selection.selectWord()
text = selection.getText()
selection.delete()
selection.deleteSelectedText()
selection.insertText(fn(text))
selection.setBufferRange(range)