Fix handling of {undo: 'skip'} in TextEditor.insertText

Signed-off-by: Nathan Sobo <nathan@github.com>
This commit is contained in:
Max Brunsfeld
2018-01-03 13:00:53 -08:00
committed by Nathan Sobo
parent 75f43b0b0e
commit 733d6381cc
2 changed files with 19 additions and 7 deletions

View File

@@ -3507,13 +3507,16 @@ describe('TextEditor', () => {
})
describe("when the undo option is set to 'skip'", () => {
beforeEach(() => editor.setSelectedBufferRange([[1, 2], [1, 2]]))
it('does not undo the skipped operation', () => {
let range = editor.insertText('x')
range = editor.insertText('y', {undo: 'skip'})
it('groups the change with the previous change for purposes of undo and redo', () => {
editor.setSelectedBufferRanges([
[[0, 0], [0, 0]],
[[1, 0], [1, 0]]
])
editor.insertText('x')
editor.insertText('y', {undo: 'skip'})
editor.undo()
expect(buffer.lineForRow(1)).toBe(' yvar sort = function(items) {')
expect(buffer.lineForRow(0)).toBe('var quicksort = function () {')
expect(buffer.lineForRow(1)).toBe(' var sort = function(items) {')
})
})
})

View File

@@ -1330,15 +1330,24 @@ class TextEditor {
insertText (text, options = {}) {
if (!this.emitWillInsertTextEvent(text)) return false
let groupLastChanges = false
if (options.undo === 'skip') {
options = Object.assign({}, options)
delete options.undo
groupLastChanges = true
}
const groupingInterval = options.groupUndo ? this.undoGroupingInterval : 0
if (options.autoIndentNewline == null) options.autoIndentNewline = this.shouldAutoIndent()
if (options.autoDecreaseIndent == null) options.autoDecreaseIndent = this.shouldAutoIndent()
return this.mutateSelectedText(selection => {
const result = this.mutateSelectedText(selection => {
const range = selection.insertText(text, options)
const didInsertEvent = {text, range}
this.emitter.emit('did-insert-text', didInsertEvent)
return range
}, groupingInterval)
if (groupLastChanges) this.buffer.groupLastChanges()
return result
}
// Essential: For each selection, replace the selected text with a newline.