Skip to content

Commit

Permalink
Add first methods in the new Minimap model
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Dec 12, 2014
1 parent 82d2e9b commit 93651f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
{CompositeDisposable} = require 'event-kit'

module.exports =
class Minimap
constructor: ({@textEditor}) ->
@subscriptions = new CompositeDisposable
@subscribeToConfig()

getTextEditor: -> @textEditor

getTextEditorHeight: -> @textEditor.getHeight() * @getScaleFactor()

getTextEditorScroll: -> @textEditor.getScrollTop() * @getScaleFactor()

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

getScaleFactor: -> @getLineHeight() / @textEditor.getLineHeightInPixels()

getLineHeight: -> @charHeight + @interline

subscribeToConfig: ->
@subscriptions.add atom.config.observe 'minimap.charHeight', (@charHeight) =>
@subscriptions.add atom.config.observe 'minimap.charWidth', (@charWidth) =>

@subscriptions.add atom.config.observe 'minimap.interline', (@interline) =>
35 changes: 35 additions & 0 deletions spec/minimap-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,38 @@ Minimap = require '../lib/minimap'
describe 'Minimap', ->
[editor, minimap, largeSample, smallSample] = []

beforeEach ->
atom.config.set 'minimap.charHeight', 4
atom.config.set 'minimap.charWidth', 2
atom.config.set 'minimap.interline', 1

editor = new TextEditor({})
editor.setLineHeightInPixels(10)
editor.setHeight(50)

minimap = new Minimap({textEditor: editor})
largeSample = fs.readFileSync(atom.project.resolve('large-file.coffee')).toString()
smallSample = fs.readFileSync(atom.project.resolve('sample.coffee')).toString()

it 'has an associated editor', ->
expect(minimap.getTextEditor()).toEqual(editor)

it 'measures the minimap size based on the current editor content', ->
editor.setText(smallSample)
expect(minimap.getHeight()).toEqual(20)

editor.setText(largeSample)
expect(minimap.getHeight()).toEqual(editor.getLineCount() * 5)

it 'measures the scaling factor between the editor and the minimap', ->
expect(minimap.getScaleFactor()).toEqual(0.5)

it 'measures the editor visible area size at minimap scale', ->
editor.setText(largeSample)
expect(minimap.getTextEditorHeight()).toEqual(25)

it 'scales the editor scroll based on the minimap scale factor', ->
editor.setText(largeSample)
editor.setScrollTop(1000)

expect(minimap.getTextEditorScroll()).toEqual(500)

0 comments on commit 93651f2

Please sign in to comment.