mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-14 04:29:04 +03:00
Editor recalls cursor position and scroll state when a previously edited buffer is re-assigned.
This commit is contained in:
parent
a96e09f6d1
commit
826bf6ec61
@ -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)
|
||||
|
@ -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)
|
||||
|
8
src/atom/edit-session.coffee
Normal file
8
src/atom/edit-session.coffee
Normal file
@ -0,0 +1,8 @@
|
||||
Point = require 'point'
|
||||
|
||||
module.exports =
|
||||
class EditSession
|
||||
cursorScreenPosition: new Point(0, 0)
|
||||
scrollTop: 0
|
||||
scrollLeft: 0
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user