Use Editor.abort to clear autocomplete's buffer changes on cancel

Using this new `abort` mechanism ensures that autocomplete's changes can never be redone, while eliminating the need to track operation counts explicitly.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-04 12:44:44 -07:00
parent ec13c38c7d
commit 29f371b347
3 changed files with 25 additions and 10 deletions

View File

@@ -246,6 +246,9 @@ class Editor extends View
paste: -> @activeEditSession.pasteText()
undo: -> @activeEditSession.undo()
redo: -> @activeEditSession.redo()
transact: (fn) -> @activeEditSession.transact(fn)
commit: -> @activeEditSession.commit()
abort: -> @activeEditSession.abort()
createFold: (startRow, endRow) -> @activeEditSession.createFold(startRow, endRow)
foldCurrentRow: -> @activeEditSession.foldCurrentRow()
unfoldCurrentRow: -> @activeEditSession.unfoldCurrentRow()

View File

@@ -223,6 +223,19 @@ describe "Autocomplete", ->
expect(editor.lineForBufferRow(10)).toBe "extra:S:extra"
expect(editor.getCursorBufferPosition()).toEqual [10,7]
it "restores the original buffer contents even if there was an additional operation after autocomplete attached (regression)", ->
editor.getBuffer().insert([10,0] ,"extra:s:extra")
editor.setCursorBufferPosition([10,7])
autocomplete.attach()
editor.getBuffer().append('hi')
expect(editor.lineForBufferRow(10)).toBe "extra:sort:extra"
autocomplete.trigger 'core:cancel'
expect(editor.lineForBufferRow(10)).toBe "extra:s:extra"
editor.redo()
expect(editor.lineForBufferRow(10)).toBe "extra:s:extra"
describe 'move-up event', ->
it "highlights the previous match and replaces the selection with it", ->
editor.getBuffer().insert([10,0] ,"extra:t:extra")

View File

@@ -17,7 +17,6 @@ class Autocomplete extends SelectList
originalSelectionBufferRange: null
originalCursorPosition: null
aboveCursor: false
undoCount: 0
filterKey: 'word'
initialize: (@editor) ->
@@ -75,14 +74,15 @@ class Autocomplete extends SelectList
@editor.setCursorBufferPosition([position.row, position.column + match.suffix.length])
cancelled: ->
@editor.undo() for undo in [0...@undoCount]
@editor.abort()
@editor.setSelectedBufferRange(@originalSelectionBufferRange)
@miniEditor.setText('')
@editor.rootView()?.focus() if @miniEditor.isFocused
attach: ->
@undoCount = 0
@editor.transact()
@aboveCursor = false
@originalSelectionBufferRange = @editor.getSelection().getBufferRange()
@originalCursorPosition = @editor.getCursorScreenPosition()
@@ -133,13 +133,12 @@ class Autocomplete extends SelectList
selection = @editor.getSelection()
startPosition = selection.getBufferRange().start
buffer = @editor.getBuffer()
@editor.activeEditSession.transact =>
selection.deleteSelectedText()
cursorPosition = @editor.getCursorBufferPosition()
buffer.delete(Range.fromPointWithDelta(cursorPosition, 0, match.suffix.length))
buffer.delete(Range.fromPointWithDelta(cursorPosition, 0, -match.prefix.length))
@editor.insertText(match.word)
@undoCount++
selection.deleteSelectedText()
cursorPosition = @editor.getCursorBufferPosition()
buffer.delete(Range.fromPointWithDelta(cursorPosition, 0, match.suffix.length))
buffer.delete(Range.fromPointWithDelta(cursorPosition, 0, -match.prefix.length))
@editor.insertText(match.word)
infixLength = match.word.length - match.prefix.length - match.suffix.length
@editor.setSelectedBufferRange([startPosition, [startPosition.row, startPosition.column + infixLength]])