Skip to content

Commit

Permalink
Merge pull request atom#938 from atom/mb-scroll-commands-use-model-sc…
Browse files Browse the repository at this point in the history
…roll-api

Use model scroll api in scroll commands
  • Loading branch information
Max Brunsfeld committed Dec 15, 2015
2 parents 1aef1e3 + b8650d8 commit 140c951
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
49 changes: 27 additions & 22 deletions lib/scroll.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@ class Scroll

class ScrollDown extends Scroll
execute: (count=1) ->
@keepCursorOnScreen(count)
@scrollUp(count)
oldFirstRow = @editor.getFirstVisibleScreenRow()
@editor.setFirstVisibleScreenRow(oldFirstRow + count)
newFirstRow = @editor.getFirstVisibleScreenRow()

keepCursorOnScreen: (count) ->
{row, column} = @editor.getCursorScreenPosition()
firstScreenRow = @rows.first + @scrolloff + 1
if row - count <= firstScreenRow
@editor.setCursorScreenPosition([firstScreenRow + count, column])
for cursor in @editor.getCursors()
position = cursor.getScreenPosition()
if position.row <= newFirstRow + @scrolloff
cursor.setScreenPosition([position.row + newFirstRow - oldFirstRow, position.column], autoscroll: false)

scrollUp: (count) ->
lastScreenRow = @rows.last - @scrolloff
@editor.scrollToScreenPosition([lastScreenRow + count, 0])
# TODO: remove
# This is a workaround for a bug fixed in atom/atom#10062
@editorElement.component.updateSync()

return

class ScrollUp extends Scroll
execute: (count=1) ->
@keepCursorOnScreen(count)
@scrollDown(count)

keepCursorOnScreen: (count) ->
{row, column} = @editor.getCursorScreenPosition()
lastScreenRow = @rows.last - @scrolloff - 1
if row + count >= lastScreenRow
@editor.setCursorScreenPosition([lastScreenRow - count, column])

scrollDown: (count) ->
firstScreenRow = @rows.first + @scrolloff
@editor.scrollToScreenPosition([firstScreenRow - count, 0])
oldFirstRow = @editor.getFirstVisibleScreenRow()
oldLastRow = @editor.getLastVisibleScreenRow()
@editor.setFirstVisibleScreenRow(oldFirstRow - count)
newLastRow = @editor.getLastVisibleScreenRow()

for cursor in @editor.getCursors()
position = cursor.getScreenPosition()
if position.row >= newLastRow - @scrolloff
cursor.setScreenPosition([position.row - (oldLastRow - newLastRow), position.column], autoscroll: false)

# TODO: remove
# This is a workaround for a bug fixed in atom/atom#10062
@editorElement.component.updateSync()

return

class ScrollCursor extends Scroll
constructor: (@editorElement, @opts={}) ->
Expand Down
51 changes: 31 additions & 20 deletions spec/scroll-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,41 @@ describe "Scrolling", ->

describe "scrolling keybindings", ->
beforeEach ->
editor.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n10")
spyOn(editorElement, 'getFirstVisibleScreenRow').andReturn(2)
spyOn(editorElement, 'getLastVisibleScreenRow').andReturn(8)
spyOn(editor, 'scrollToScreenPosition')

describe "the ctrl-e keybinding", ->
beforeEach ->
spyOn(editor, 'getCursorScreenPosition').andReturn({row: 4, column: 0})
spyOn(editor, 'setCursorScreenPosition')

it "moves the screen down by one and keeps cursor onscreen", ->
editor.setText """
100
200
300
400
500
600
700
800
900
1000
"""

editor.setCursorBufferPosition([1, 2])
editorElement.setHeight(editorElement.getHeight() * 4 / 10)
expect(editor.getVisibleRowRange()).toEqual [0, 4]

describe "the ctrl-e and ctrl-y keybindings", ->
it "moves the screen up and down by one and keeps cursor onscreen", ->
keydown('e', ctrl: true)
expect(editor.scrollToScreenPosition).toHaveBeenCalledWith([7, 0])
expect(editor.setCursorScreenPosition).toHaveBeenCalledWith([6, 0])
expect(editor.getFirstVisibleScreenRow()).toBe 1
expect(editor.getLastVisibleScreenRow()).toBe 5
expect(editor.getCursorScreenPosition()).toEqual [2, 2]

describe "the ctrl-y keybinding", ->
beforeEach ->
spyOn(editor, 'getCursorScreenPosition').andReturn({row: 6, column: 0})
spyOn(editor, 'setCursorScreenPosition')
keydown('2')
keydown('e', ctrl: true)
expect(editor.getFirstVisibleScreenRow()).toBe 3
expect(editor.getLastVisibleScreenRow()).toBe 7
expect(editor.getCursorScreenPosition()).toEqual [4, 2]

it "moves the screen up by one and keeps the cursor onscreen", ->
keydown('2')
keydown('y', ctrl: true)
expect(editor.scrollToScreenPosition).toHaveBeenCalledWith([3, 0])
expect(editor.setCursorScreenPosition).toHaveBeenCalledWith([4, 0])
expect(editor.getFirstVisibleScreenRow()).toBe 1
expect(editor.getLastVisibleScreenRow()).toBe 5
expect(editor.getCursorScreenPosition()).toEqual [2, 2]

describe "scroll cursor keybindings", ->
beforeEach ->
Expand Down

0 comments on commit 140c951

Please sign in to comment.