Skip to content

Commit

Permalink
Implement sublime-like minimap scroll with scroll past end
Browse files Browse the repository at this point in the history
It prevents the minimap from going past the end while the editor is.
  • Loading branch information
abe33 committed Dec 18, 2014
1 parent c779f6f commit 5d5185b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Minimap extends Model
@subscriptions = subs = new CompositeDisposable
@initializeDecorations()

subs.add atom.config.observe 'editor.scrollPastEnd', (@scrollPastEnd) =>
@emitter.emit('did-change-config')
subs.add atom.config.observe 'minimap.charHeight', (@charHeight) =>
@emitter.emit('did-change-config')
subs.add atom.config.observe 'minimap.charWidth', (@charWidth) =>
Expand Down Expand Up @@ -56,7 +58,10 @@ class Minimap extends Model
getTextEditorScrollLeft: -> @textEditor.getScrollLeft() * @getHorizontalScaleFactor()

getTextEditorScrollRatio: ->
@textEditor.getScrollTop() / @textEditor.displayBuffer.getMaxScrollTop()
maxScrollTop = @textEditor.displayBuffer.getMaxScrollTop()
if @scrollPastEnd
maxScrollTop -= @textEditor.getHeight() - 3 * @textEditor.displayBuffer.getLineHeightInPixels()
@textEditor.getScrollTop() / maxScrollTop

getHeight: -> @textEditor.getScreenLineCount() * @getLineHeight()

Expand All @@ -81,7 +86,7 @@ class Minimap extends Model
Math.ceil((@getMinimapScrollTop() + @textEditor.getHeight()) / @getLineHeight())

getMinimapScrollTop: ->
Math.abs(@getTextEditorScrollRatio() * @getMinimapMaxScrollTop())
Math.abs(Math.min(1, @getTextEditorScrollRatio()) * @getMinimapMaxScrollTop())

getMinimapMaxScrollTop: -> Math.max(0, @getHeight() - @textEditor.getHeight())

Expand Down
16 changes: 16 additions & 0 deletions spec/minimap-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ describe 'Minimap', ->

expect(scrollSpy).toHaveBeenCalled()

describe 'when scrols past end is enabled', ->
beforeEach ->
editor.setText(largeSample)
atom.config.set 'editor.scrollPastEnd', true

it 'adjust the scrolling ratio', ->
editor.setScrollTop(editor.displayBuffer.getMaxScrollTop())

maxScrollTop = editor.displayBuffer.getMaxScrollTop() - (editor.getHeight() - 3 * editor.displayBuffer.getLineHeightInPixels())

expect(minimap.getTextEditorScrollRatio()).toEqual(editor.getScrollTop() / maxScrollTop)

it 'lock the minimap scroll top to 1', ->
editor.setScrollTop(editor.displayBuffer.getMaxScrollTop())
expect(minimap.getMinimapScrollTop()).toEqual(minimap.getMinimapMaxScrollTop())

describe 'when soft wrap is enabled', ->
beforeEach ->
atom.config.set 'editor.softWrap', true
Expand Down

0 comments on commit 5d5185b

Please sign in to comment.