mirror of
https://github.com/atom/atom.git
synced 2026-01-25 14:59:03 -05:00
Fix bugs when switching between EditSessions
This commit is contained in:
@@ -92,20 +92,24 @@ describe "Editor", ->
|
||||
expect(otherBuffer.subscriptionCount()).toBe 1
|
||||
|
||||
describe ".setBuffer(buffer)", ->
|
||||
otherBuffer = null
|
||||
|
||||
beforeEach ->
|
||||
otherBuffer = new Buffer
|
||||
|
||||
it "sets the cursor to the beginning of the file", ->
|
||||
expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0)
|
||||
|
||||
it "recalls the cursor position and scroll position when the same buffer is re-assigned", ->
|
||||
editor.attachToDom()
|
||||
|
||||
editor.height(editor.lineHeight * 5)
|
||||
editor.width(editor.charWidth * 30)
|
||||
editor.setCursorScreenPosition([8, 28])
|
||||
advanceClock()
|
||||
|
||||
previousScrollTop = editor.verticalScrollbar.scrollTop()
|
||||
previousScrollLeft = editor.scrollView.scrollLeft()
|
||||
|
||||
editor.setBuffer(new Buffer)
|
||||
editor.setBuffer(otherBuffer)
|
||||
expect(editor.getCursorScreenPosition()).toEqual [0, 0]
|
||||
expect(editor.verticalScrollbar.scrollTop()).toBe 0
|
||||
expect(editor.scrollView.scrollLeft()).toBe 0
|
||||
@@ -118,7 +122,6 @@ describe "Editor", ->
|
||||
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'
|
||||
@@ -135,15 +138,15 @@ describe "Editor", ->
|
||||
editor.redo()
|
||||
expect(otherBuffer.lineForRow(0)).toBe 'abc'
|
||||
|
||||
it "fully unsubscribes from the previously assigned buffer", ->
|
||||
otherBuffer = new Buffer
|
||||
previousSubscriptionCount = otherBuffer.subscriptionCount()
|
||||
|
||||
it "unsubscribes from the previously assigned buffer", ->
|
||||
editor.setBuffer(otherBuffer)
|
||||
expect(otherBuffer.subscriptionCount()).toBeGreaterThan previousSubscriptionCount
|
||||
|
||||
previousSubscriptionCount = buffer.subscriptionCount()
|
||||
|
||||
editor.setBuffer(buffer)
|
||||
expect(otherBuffer.subscriptionCount()).toBe previousSubscriptionCount
|
||||
editor.setBuffer(otherBuffer)
|
||||
|
||||
expect(buffer.subscriptionCount()).toBe previousSubscriptionCount
|
||||
|
||||
it "resizes the vertical scrollbar based on the new buffer's height", ->
|
||||
editor.attachToDom(heightInLines: 5)
|
||||
@@ -153,6 +156,17 @@ describe "Editor", ->
|
||||
editor.setBuffer(new Buffer(require.resolve('fixtures/sample.txt')))
|
||||
expect(editor.verticalScrollbar.prop('scrollHeight')).toBeLessThan originalHeight
|
||||
|
||||
it "handles buffer manipulation correctly after switching to a new buffer", ->
|
||||
editor.attachToDom()
|
||||
editor.insertText("abc\n")
|
||||
expect(editor.lineElementForScreenRow(0).text()).toBe 'abc'
|
||||
|
||||
editor.setBuffer(otherBuffer)
|
||||
expect(editor.lineElementForScreenRow(0).html()).toBe ' '
|
||||
|
||||
editor.insertText("def\n")
|
||||
expect(editor.lineElementForScreenRow(0).text()).toBe 'def'
|
||||
|
||||
describe ".clipScreenPosition(point)", ->
|
||||
it "selects the nearest valid position to the given point", ->
|
||||
expect(editor.clipScreenPosition(row: 1000, column: 0)).toEqual(row: buffer.getLastRow(), column: buffer.lineForRow(buffer.getLastRow()).length)
|
||||
|
||||
@@ -20,6 +20,9 @@ class EditSession
|
||||
constructor: (@editor, @buffer) ->
|
||||
@setCursorScreenPosition([0, 0])
|
||||
|
||||
destroy: ->
|
||||
@renderer.destroy()
|
||||
|
||||
serialize: ->
|
||||
buffer: @buffer.serialize()
|
||||
scrollTop: @getScrollTop()
|
||||
|
||||
@@ -317,36 +317,31 @@ class Editor extends View
|
||||
@renderer.destroyFoldsContainingBufferRow(bufferRow)
|
||||
|
||||
setBuffer: (buffer) ->
|
||||
if @buffer
|
||||
@saveCurrentEditSession()
|
||||
@unsubscribeFromBuffer()
|
||||
|
||||
@buffer = buffer
|
||||
@trigger 'editor-path-change'
|
||||
@buffer.on "path-change.editor#{@id}", => @trigger 'editor-path-change'
|
||||
|
||||
@loadEditSessionForBuffer(@buffer)
|
||||
|
||||
@buffer.on "change.editor#{@id}", (e) => @handleBufferChange(e)
|
||||
@renderer.on 'change', (e) => @handleRendererChange(e)
|
||||
@loadEditSessionForBuffer(buffer)
|
||||
|
||||
setRenderer: (renderer) ->
|
||||
@renderer?.off()
|
||||
@renderer = renderer
|
||||
@renderer.on 'change', (e) => @handleRendererChange(e)
|
||||
|
||||
editSessionForBuffer: (buffer) ->
|
||||
@unsubscribeFromBuffer() if @buffer
|
||||
@buffer = renderer.buffer
|
||||
@buffer.on "path-change.editor#{@id}", => @trigger 'editor-path-change'
|
||||
@buffer.on "change.editor#{@id}", (e) => @handleBufferChange(e)
|
||||
@trigger 'editor-path-change'
|
||||
|
||||
editSessionIndexForBuffer: (buffer) ->
|
||||
for editSession, index in @editSessions
|
||||
return [editSession, index] if editSession.buffer == buffer
|
||||
[undefined, -1]
|
||||
return index if editSession.buffer == buffer
|
||||
null
|
||||
|
||||
loadEditSessionForBuffer: (buffer) ->
|
||||
[editSession, index] = @editSessionForBuffer(buffer)
|
||||
if editSession
|
||||
@activeEditSessionIndex = index
|
||||
else
|
||||
index = @editSessionIndexForBuffer(buffer)
|
||||
unless index?
|
||||
index = @editSessions.length
|
||||
@editSessions.push(new EditSession(this, buffer))
|
||||
@activeEditSessionIndex = @editSessions.length - 1
|
||||
@loadEditSession()
|
||||
|
||||
@loadEditSession(index)
|
||||
|
||||
loadNextEditSession: ->
|
||||
nextIndex = (@activeEditSessionIndex + 1) % @editSessions.length
|
||||
@@ -360,7 +355,8 @@ class Editor extends View
|
||||
loadEditSession: (index=@activeEditSessionIndex) ->
|
||||
editSession = @editSessions[index]
|
||||
throw new Error("Edit session not found") unless editSession
|
||||
@setBuffer(editSession.buffer) unless @buffer == editSession.buffer
|
||||
@saveCurrentEditSession() if @activeEditSessionIndex?
|
||||
|
||||
@activeEditSessionIndex = index
|
||||
@setRenderer(editSession.getRenderer())
|
||||
if @attached
|
||||
@@ -752,7 +748,10 @@ class Editor extends View
|
||||
return super if keepData
|
||||
|
||||
@trigger 'before-remove'
|
||||
|
||||
@destroyEditSessions()
|
||||
@unsubscribeFromBuffer()
|
||||
|
||||
$(window).off ".editor#{@id}"
|
||||
rootView = @rootView()
|
||||
rootView?.off ".editor#{@id}"
|
||||
@@ -761,7 +760,9 @@ class Editor extends View
|
||||
|
||||
unsubscribeFromBuffer: ->
|
||||
@buffer.off ".editor#{@id}"
|
||||
@renderer.destroy()
|
||||
|
||||
destroyEditSessions: ->
|
||||
session.destroy() for session in @editSessions
|
||||
|
||||
stateForScreenRow: (row) ->
|
||||
@renderer.lineForRow(row).state
|
||||
|
||||
Reference in New Issue
Block a user