]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/lib/scroll.coffee
1bc1258c7cf1b37e4b042b79113a9687c543f197
[rbdr/dotfiles] / atom / packages / vim-mode / lib / scroll.coffee
1 class Scroll
2 isComplete: -> true
3 isRecordable: -> false
4 constructor: (@editorElement) ->
5 @scrolloff = 2 # atom default
6 @editor = @editorElement.getModel()
7 @rows =
8 first: @editorElement.getFirstVisibleScreenRow()
9 last: @editorElement.getLastVisibleScreenRow()
10 final: @editor.getLastScreenRow()
11
12 class ScrollDown extends Scroll
13 execute: (count=1) ->
14 @keepCursorOnScreen(count)
15 @scrollUp(count)
16
17 keepCursorOnScreen: (count) ->
18 {row, column} = @editor.getCursorScreenPosition()
19 firstScreenRow = @rows.first + @scrolloff + 1
20 if row - count <= firstScreenRow
21 @editor.setCursorScreenPosition([firstScreenRow + count, column])
22
23 scrollUp: (count) ->
24 lastScreenRow = @rows.last - @scrolloff
25 @editor.scrollToScreenPosition([lastScreenRow + count, 0])
26
27 class ScrollUp extends Scroll
28 execute: (count=1) ->
29 @keepCursorOnScreen(count)
30 @scrollDown(count)
31
32 keepCursorOnScreen: (count) ->
33 {row, column} = @editor.getCursorScreenPosition()
34 lastScreenRow = @rows.last - @scrolloff - 1
35 if row + count >= lastScreenRow
36 @editor.setCursorScreenPosition([lastScreenRow - count, column])
37
38 scrollDown: (count) ->
39 firstScreenRow = @rows.first + @scrolloff
40 @editor.scrollToScreenPosition([firstScreenRow - count, 0])
41
42 class ScrollCursor extends Scroll
43 constructor: (@editorElement, @opts={}) ->
44 super
45 cursor = @editor.getCursorScreenPosition()
46 @pixel = @editorElement.pixelPositionForScreenPosition(cursor).top
47
48 class ScrollCursorToTop extends ScrollCursor
49 execute: ->
50 @moveToFirstNonBlank() unless @opts.leaveCursor
51 @scrollUp()
52
53 scrollUp: ->
54 return if @rows.last is @rows.final
55 @pixel -= (@editor.getLineHeightInPixels() * @scrolloff)
56 @editor.setScrollTop(@pixel)
57
58 moveToFirstNonBlank: ->
59 @editor.moveToFirstCharacterOfLine()
60
61 class ScrollCursorToMiddle extends ScrollCursor
62 execute: ->
63 @moveToFirstNonBlank() unless @opts.leaveCursor
64 @scrollMiddle()
65
66 scrollMiddle: ->
67 @pixel -= (@editor.getHeight() / 2)
68 @editor.setScrollTop(@pixel)
69
70 moveToFirstNonBlank: ->
71 @editor.moveToFirstCharacterOfLine()
72
73 class ScrollCursorToBottom extends ScrollCursor
74 execute: ->
75 @moveToFirstNonBlank() unless @opts.leaveCursor
76 @scrollDown()
77
78 scrollDown: ->
79 return if @rows.first is 0
80 offset = (@editor.getLineHeightInPixels() * (@scrolloff + 1))
81 @pixel -= (@editor.getHeight() - offset)
82 @editor.setScrollTop(@pixel)
83
84 moveToFirstNonBlank: ->
85 @editor.moveToFirstCharacterOfLine()
86
87 module.exports = {ScrollDown, ScrollUp, ScrollCursorToTop, ScrollCursorToMiddle,
88 ScrollCursorToBottom}