1 helpers = require './spec-helper'
3 describe "Scrolling", ->
4 [editor, editorElement, vimState] = []
7 vimMode = atom.packages.loadPackage('vim-mode')
8 vimMode.activateResources()
10 helpers.getEditorElement (element) ->
11 editorElement = element
12 editor = editorElement.getModel()
13 vimState = editorElement.vimState
14 vimState.activateNormalMode()
15 vimState.resetNormalMode()
17 keydown = (key, options={}) ->
18 options.element ?= editorElement
19 helpers.keydown(key, options)
21 describe "scrolling keybindings", ->
23 editor.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n10")
24 spyOn(editor, 'getFirstVisibleScreenRow').andReturn(2)
25 spyOn(editor, 'getLastVisibleScreenRow').andReturn(8)
26 spyOn(editor, 'scrollToScreenPosition')
28 describe "the ctrl-e keybinding", ->
30 spyOn(editor, 'getCursorScreenPosition').andReturn({row: 4, column: 0})
31 spyOn(editor, 'setCursorScreenPosition')
33 it "moves the screen down by one and keeps cursor onscreen", ->
34 keydown('e', ctrl: true)
35 expect(editor.scrollToScreenPosition).toHaveBeenCalledWith([7, 0])
36 expect(editor.setCursorScreenPosition).toHaveBeenCalledWith([6, 0])
38 describe "the ctrl-y keybinding", ->
40 spyOn(editor, 'getCursorScreenPosition').andReturn({row: 6, column: 0})
41 spyOn(editor, 'setCursorScreenPosition')
43 it "moves the screen up by one and keeps the cursor onscreen", ->
44 keydown('y', ctrl: true)
45 expect(editor.scrollToScreenPosition).toHaveBeenCalledWith([3, 0])
46 expect(editor.setCursorScreenPosition).toHaveBeenCalledWith([4, 0])
48 describe "scroll cursor keybindings", ->
55 spyOn(editor, 'moveToFirstCharacterOfLine')
56 spyOn(editor, 'getLineHeightInPixels').andReturn(20)
57 spyOn(editor, 'setScrollTop')
58 spyOn(editor, 'getHeight').andReturn(200)
59 spyOn(editor, 'getFirstVisibleScreenRow').andReturn(90)
60 spyOn(editor, 'getLastVisibleScreenRow').andReturn(110)
62 describe "the z<CR> keybinding", ->
63 keydownCodeForEnter = '\r'
66 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
68 it "moves the screen to position cursor at the top of the window and moves cursor to first non-blank in the line", ->
70 keydown(keydownCodeForEnter)
71 expect(editor.setScrollTop).toHaveBeenCalledWith(960)
72 expect(editor.moveToFirstCharacterOfLine).toHaveBeenCalled()
74 describe "the zt keybinding", ->
76 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
78 it "moves the screen to position cursor at the top of the window and leave cursor in the same column", ->
81 expect(editor.setScrollTop).toHaveBeenCalledWith(960)
82 expect(editor.moveToFirstCharacterOfLine).not.toHaveBeenCalled()
84 describe "the z. keybinding", ->
86 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
88 it "moves the screen to position cursor at the center of the window and moves cursor to first non-blank in the line", ->
91 expect(editor.setScrollTop).toHaveBeenCalledWith(900)
92 expect(editor.moveToFirstCharacterOfLine).toHaveBeenCalled()
94 describe "the zz keybinding", ->
96 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
98 it "moves the screen to position cursor at the center of the window and leave cursor in the same column", ->
101 expect(editor.setScrollTop).toHaveBeenCalledWith(900)
102 expect(editor.moveToFirstCharacterOfLine).not.toHaveBeenCalled()
104 describe "the z- keybinding", ->
106 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
108 it "moves the screen to position cursor at the bottom of the window and moves cursor to first non-blank in the line", ->
111 expect(editor.setScrollTop).toHaveBeenCalledWith(860)
112 expect(editor.moveToFirstCharacterOfLine).toHaveBeenCalled()
114 describe "the zb keybinding", ->
116 spyOn(editor, 'pixelPositionForScreenPosition').andReturn({top: 1000, left: 0})
118 it "moves the screen to position cursor at the bottom of the window and leave cursor in the same column", ->
121 expect(editor.setScrollTop).toHaveBeenCalledWith(860)
122 expect(editor.moveToFirstCharacterOfLine).not.toHaveBeenCalled()
124 describe "horizontal scroll cursor keybindings", ->
127 editor.setLineHeightInPixels(10)
128 editor.setDefaultCharWidth(10)
133 editor.setCursorBufferPosition([0, 0])
135 describe "the zs keybinding", ->
137 editor.setCursorBufferPosition([0, pos])
140 editor.getScrollLeft()
145 startPosition = editor.getScrollLeft()
147 it "does nothing near the start of the line", ->
149 expect(pos1).toEqual(startPosition)
151 it "moves the cursor the nearest it can to the left edge of the editor", ->
153 expect(pos10).toBeGreaterThan(startPosition)
156 expect(pos11 - pos10).toEqual(10)
158 it "does nothing near the end of the line", ->
160 expect(editor.getCursorBufferPosition()).toEqual [0, 399]
163 expect(pos390).toEqual(posEnd)
164 expect(editor.getCursorBufferPosition()).toEqual [0, 390]
167 expect(pos340).toBeLessThan(posEnd)
169 expect(pos342 - pos340).toEqual(20)
171 it "does nothing if all lines are short", ->
172 editor.setText('short')
173 startPosition = editor.getScrollLeft()
175 expect(pos1).toEqual(startPosition)
176 expect(editor.getCursorBufferPosition()).toEqual [0, 1]
178 expect(pos10).toEqual(startPosition)
179 expect(editor.getCursorBufferPosition()).toEqual [0, 4]
182 describe "the ze keybinding", ->
184 editor.setCursorBufferPosition([0, pos])
187 editor.getScrollLeft()
192 startPosition = editor.getScrollLeft()
194 it "does nothing near the start of the line", ->
196 expect(pos1).toEqual(startPosition)
199 expect(pos40).toEqual(startPosition)
201 it "moves the cursor the nearest it can to the right edge of the editor", ->
203 expect(pos110).toBeGreaterThan(startPosition)
206 expect(pos110 - pos109).toEqual(10)
208 it "does nothing when very near the end of the line", ->
210 expect(editor.getCursorBufferPosition()).toEqual [0, 399]
213 expect(pos397).toEqual(posEnd)
214 expect(editor.getCursorBufferPosition()).toEqual [0, 397]
217 expect(pos380).toBeLessThan(posEnd)
220 expect(pos382 - pos380).toEqual(20)
222 it "does nothing if all lines are short", ->
223 editor.setText('short')
224 startPosition = editor.getScrollLeft()
226 expect(pos1).toEqual(startPosition)
227 expect(editor.getCursorBufferPosition()).toEqual [0, 1]
229 expect(pos10).toEqual(startPosition)
230 expect(editor.getCursorBufferPosition()).toEqual [0, 4]