diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 3180d025d..2d95a1a39 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -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) diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index 45f4c5b07..798e0baf5 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -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) diff --git a/src/atom/edit-session.coffee b/src/atom/edit-session.coffee new file mode 100644 index 000000000..a640b3a5c --- /dev/null +++ b/src/atom/edit-session.coffee @@ -0,0 +1,8 @@ +Point = require 'point' + +module.exports = +class EditSession + cursorScreenPosition: new Point(0, 0) + scrollTop: 0 + scrollLeft: 0 + diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index b6260fc4f..6087a5b16 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -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)