|
| 1 | +const { CompositeDisposable } = require('atom') |
| 2 | + |
| 3 | +module.exports = { |
| 4 | + activate () { |
| 5 | + this.subscriptions = new CompositeDisposable() |
| 6 | + |
| 7 | + require('atom-package-deps').install('minimap-highlight-selected') |
| 8 | + }, |
| 9 | + |
| 10 | + consumeMinimapServiceV1 (minimap) { |
| 11 | + this.minimap = minimap |
| 12 | + this.minimap.registerPlugin('highlight-selected', this) |
| 13 | + }, |
| 14 | + |
| 15 | + consumeHighlightSelectedServiceV2 (highlightSelected) { |
| 16 | + this.highlightSelected = highlightSelected |
| 17 | + if (this.minimap && this.active) { this.init() } |
| 18 | + }, |
| 19 | + |
| 20 | + deactivate () { |
| 21 | + this.deactivatePlugin() |
| 22 | + this.minimapPackage = null |
| 23 | + this.highlightSelectedPackage = null |
| 24 | + this.highlightSelected = null |
| 25 | + this.minimap = null |
| 26 | + }, |
| 27 | + |
| 28 | + isActive () { |
| 29 | + return this.active |
| 30 | + }, |
| 31 | + |
| 32 | + activatePlugin () { |
| 33 | + if (this.active) { return } |
| 34 | + |
| 35 | + this.subscriptions.add(this.minimap.onDidActivate(this.init.bind(this))) |
| 36 | + this.subscriptions.add(this.minimap.onDidDeactivate(this.dispose.bind(this))) |
| 37 | + |
| 38 | + this.active = true |
| 39 | + |
| 40 | + if (this.highlightSelected) { |
| 41 | + this.init() |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + init () { |
| 46 | + this.decorations = [] |
| 47 | + this.highlightSelected.onDidAddMarkerForEditor(options => this.markerCreated(options)) |
| 48 | + this.highlightSelected.onDidAddSelectedMarkerForEditor(options => this.markerCreated(options, true)) |
| 49 | + this.highlightSelected.onDidRemoveAllMarkers(() => this.markersDestroyed()) |
| 50 | + }, |
| 51 | + |
| 52 | + dispose () { |
| 53 | + if (this.decorations) { |
| 54 | + this.decorations.forEach(decoration => decoration.destroy()) |
| 55 | + } |
| 56 | + this.decorations = null |
| 57 | + }, |
| 58 | + |
| 59 | + markerCreated (options, selected) { |
| 60 | + if (!selected) { selected = false } |
| 61 | + const minimap = this.minimap.minimapForEditor(options.editor) |
| 62 | + if (!minimap) { return } |
| 63 | + let className = 'highlight-selected' |
| 64 | + if (selected) { className += ' selected' } |
| 65 | + |
| 66 | + const decoration = minimap.decorateMarker(options.marker, |
| 67 | + { type: 'highlight', class: className }) |
| 68 | + this.decorations.push(decoration) |
| 69 | + }, |
| 70 | + |
| 71 | + markersDestroyed () { |
| 72 | + if (this.decorations) { |
| 73 | + this.decorations.forEach(decoration => decoration.destroy()) |
| 74 | + } |
| 75 | + this.decorations = [] |
| 76 | + }, |
| 77 | + |
| 78 | + deactivatePlugin () { |
| 79 | + if (!this.active) { return } |
| 80 | + |
| 81 | + this.active = false |
| 82 | + this.dispose() |
| 83 | + this.subscriptions.dispose() |
| 84 | + }, |
| 85 | +} |
0 commit comments