diff --git a/lib/minimap.coffee b/lib/minimap.coffee index fdbdab7d..2abe557c 100644 --- a/lib/minimap.coffee +++ b/lib/minimap.coffee @@ -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) => diff --git a/spec/minimap-spec.coffee b/spec/minimap-spec.coffee index 4a2b2b04..3ba16601 100644 --- a/spec/minimap-spec.coffee +++ b/spec/minimap-spec.coffee @@ -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)