From 98defa865c45ea29e96dd30f08b041c58e8cd62e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 1 Mar 2012 18:00:35 -0700 Subject: [PATCH] Cursor screen position is updated when lines are rewrapped. --- spec/atom/editor-spec.coffee | 4 +++- src/atom/cursor.coffee | 8 ++++++-- src/atom/editor.coffee | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 6eb4f4063..c374f848e 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -83,12 +83,14 @@ describe "Editor", -> expect(editor.lines.find('.line:eq(8)').text()).toBe ': right.push(current);' expect(editor.lines.find('.line:eq(9)').text()).toBe ' }' - it "changes the max line length when the window size changes", -> + it "changes the max line length and repositions the cursor when the window size changes", -> + editor.setCursorBufferPosition([3, 60]) editor.width(editor.charWidth * 40) $(window).trigger 'resize' expect(editor.lines.find('.line').length).toBe 19 expect(editor.lines.find('.line:eq(4)').text()).toBe "left = [], right = [];" expect(editor.lines.find('.line:eq(5)').text()).toBe " while(items.length > 0) {" + expect(editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition())).toEqual [3, 60] it "unwraps lines and cancels window resize listener when softwrap is disabled", -> editor.toggleSoftWrap() diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index b3f1ab9b8..11a4d3a71 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -8,6 +8,8 @@ class Cursor extends View @pre class: 'cursor idle', style: 'position: absolute;', => @raw ' ' editor: null + screenPosition: null + bufferPosition: null initialize: (@editor) -> @one 'attach', => @updateAppearance() @@ -18,6 +20,7 @@ class Cursor extends View setScreenPosition: (position) -> position = Point.fromObject(position) @screenPosition = @editor.clipScreenPosition(position) + @bufferPosition = @editor.bufferPositionForScreenPosition(position) @goalColumn = null @updateAppearance() @trigger 'cursor:position-changed' @@ -29,9 +32,10 @@ class Cursor extends View setBufferPosition: (bufferPosition) -> @setScreenPosition(@editor.screenPositionForBufferPosition(bufferPosition)) - getBufferPosition: -> - @editor.bufferPositionForScreenPosition(@getScreenPosition()) + refreshScreenPosition: -> + @setBufferPosition(@bufferPosition) + getBufferPosition: -> _.clone(@bufferPosition) getScreenPosition: -> _.clone(@screenPosition) getColumn: -> diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index ba0628428..06a204411 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -170,6 +170,7 @@ class Editor extends View @cursor.bufferChanged(e) @lineWrapper.on 'change', (e) => + @cursor.refreshScreenPosition() { oldRange, newRange } = e screenLines = @linesForScreenRows(newRange.start.row, newRange.end.row) if newRange.end.row > oldRange.end.row