Every Buffer gets its own UndoManager.

This commit is contained in:
Nathan Sobo
2012-03-13 16:24:12 -06:00
parent 7d2d0f2037
commit da3e1d4553
3 changed files with 30 additions and 5 deletions

View File

@@ -935,6 +935,26 @@ describe "Editor", ->
expect(editor.scrollTop()).toBe previousScrollTop
expect(editor.horizontalScroller.scrollLeft()).toBe previousScrollLeft
it "recalls the undo history of the buffer when it is re-assigned", ->
editor.insertText('xyz')
otherBuffer = new Buffer
editor.setBuffer(otherBuffer)
editor.insertText('abc')
expect(otherBuffer.lineForRow(0)).toBe 'abc'
editor.undo()
expect(otherBuffer.lineForRow(0)).toBe ''
editor.setBuffer(buffer)
editor.undo()
expect(buffer.lineForRow(0)).toBe 'var quicksort = function () {'
editor.redo()
expect(buffer.lineForRow(0)).toBe 'xyzvar quicksort = function () {'
editor.setBuffer(otherBuffer)
editor.redo()
expect(otherBuffer.lineForRow(0)).toBe 'abc'
describe ".clipScreenPosition(point)", ->
it "selects the nearest valid position to the given point", ->
expect(editor.clipScreenPosition(row: 1000, column: 0)).toEqual(row: buffer.lastRow(), column: buffer.lineForRow(buffer.lastRow()).length)

View File

@@ -2,6 +2,7 @@ _ = require 'underscore'
fs = require 'fs'
Range = require 'range'
EventEmitter = require 'event-emitter'
UndoManager = require 'undo-manager'
module.exports =
class Buffer
@@ -16,6 +17,7 @@ class Buffer
@setText(fs.read(@path))
else
@setText('')
@undoManager = new UndoManager(this)
getText: ->
@lines.join('\n')
@@ -94,6 +96,12 @@ class Buffer
@lines[oldRange.start.row..oldRange.end.row] = newTextLines
@trigger 'change', { oldRange, newRange, oldText, newText }
undo: ->
@undoManager.undo()
redo: ->
@undoManager.redo()
save: ->
if not @path then throw new Error("Tried to save buffer with no url")
fs.write @path, @getText()

View File

@@ -7,7 +7,6 @@ Renderer = require 'renderer'
Point = require 'point'
Range = require 'range'
Selection = require 'selection'
UndoManager = require 'undo-manager'
EditSession = require 'edit-session'
$ = require 'jquery'
@@ -34,7 +33,6 @@ class Editor extends View
buffer: null
highlighter: null
renderer: null
undoManager: null
autoIndent: null
lineCache: null
@@ -165,7 +163,6 @@ class Editor extends View
@saveEditSession() if @editSession
document.title = @buffer.path
@renderer = new Renderer(@buffer)
@undoManager = new UndoManager(@buffer)
@renderLines()
@gutter.renderLineNumbers()
@@ -379,10 +376,10 @@ class Editor extends View
@selection.delete()
undo: ->
@undoManager.undo()
@buffer.undo()
redo: ->
@undoManager.redo()
@buffer.redo()
destroyFold: (foldId) ->
fold = @renderer.foldsById[foldId]