Editor recalls cursor position and scroll state when a previously edited buffer is re-assigned.

This commit is contained in:
Nathan Sobo 2012-03-13 15:25:20 -06:00
parent a96e09f6d1
commit 826bf6ec61
4 changed files with 48 additions and 1 deletions

View File

@ -894,6 +894,29 @@ describe "Editor", ->
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.scrollTop()
previousScrollLeft = editor.horizontalScroller.scrollLeft()
console.log "setting empty"
console.log editor.scrollTop()
editor.setBuffer(new Buffer)
expect(editor.getCursorScreenPosition()).toEqual [0, 0]
expect(editor.scrollTop()).toBe 0
expect(editor.horizontalScroller.scrollLeft()).toBe 0
console.log "setting back to buffer"
editor.setBuffer(buffer)
expect(editor.getCursorScreenPosition()).toEqual [8, 28]
expect(editor.scrollTop()).toBe previousScrollTop
expect(editor.horizontalScroller.scrollLeft()).toBe previousScrollLeft
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

@ -5,9 +5,11 @@ EventEmitter = require 'event-emitter'
module.exports =
class Buffer
@idCounter = 1
lines: null
constructor: (@path) ->
@id = @constructor.idCounter++
@url = @path # we want this to be path on master, but let's not break it on a branch
@lines = ['']
if @path and fs.exists(@path)

View File

@ -0,0 +1,8 @@
Point = require 'point'
module.exports =
class EditSession
cursorScreenPosition: new Point(0, 0)
scrollTop: 0
scrollLeft: 0

View File

@ -8,6 +8,7 @@ Point = require 'point'
Range = require 'range'
Selection = require 'selection'
UndoManager = require 'undo-manager'
EditSession = require 'edit-session'
$ = require 'jquery'
_ = require 'underscore'
@ -40,6 +41,7 @@ class Editor extends View
initialize: () ->
requireStylesheet 'editor.css'
requireStylesheet 'theme/twilight.css'
@editSessionsByBufferId = {}
@bindKeys()
@buildCursorAndSelection()
@handleEvents()
@ -154,17 +156,29 @@ class Editor extends View
@screenLineCount() - 1
setBuffer: (@buffer) ->
@saveEditSession() if @editSession
document.title = @buffer.path
@renderer = new Renderer(@buffer)
@undoManager = new UndoManager(@buffer)
@renderLines()
@gutter.renderLineNumbers()
@setCursorScreenPosition(row: 0, column: 0)
@loadEditSessionForBuffer(@buffer)
@buffer.on 'change', (e) => @cursor.bufferChanged(e)
@renderer.on 'change', (e) => @handleRendererChange(e)
loadEditSessionForBuffer: (buffer) ->
@editSession = (@editSessionsByBufferId[buffer.id] ?= new EditSession)
@setCursorScreenPosition(@editSession.cursorScreenPosition)
@scrollTop(@editSession.scrollTop)
@horizontalScroller.scrollLeft(@editSession.scrollLeft)
saveEditSession: ->
@editSession.cursorScreenPosition = @getCursorScreenPosition()
@editSession.scrollTop = @scrollTop()
@editSession.scrollLeft = @horizontalScroller.scrollLeft()
handleRendererChange: (e) ->
{ oldRange, newRange } = e
unless newRange.isSingleLine() and newRange.coversSameRows(oldRange)