Skip to content

Commit

Permalink
fix: decaffeinate
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Nov 28, 2020
1 parent a35cd89 commit d9ad626
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 174 deletions.
52 changes: 0 additions & 52 deletions lib/minimap-bookmarks-binding.coffee

This file was deleted.

65 changes: 65 additions & 0 deletions lib/minimap-bookmarks-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { CompositeDisposable } = require('atom')

class MinimapBookmarksBinding {
constructor (minimap, bookmarks) {
this.minimap = minimap
this.bookmarks = bookmarks
if ((this.minimap == null) || (this.bookmarks == null)) { return }

this.subscriptions = new CompositeDisposable()
this.editor = this.minimap.getTextEditor()
this.decorationsByMarkerId = {}
this.decorationSubscriptionsByMarkerId = {}

// We need to wait until the bookmarks package had created its marker
// layer before retrieving its id from the state.
requestAnimationFrame(() => {
// Also, targeting private properties on atom.packages is very brittle.
// DO NOT DO THAT!
//
// If we really have to get the marker layer id from the
// state (which can already break easily) it's better to get it from the
// package `serialize` method since it's an API that is public and is
// unlikely to change in a near future.
const bookmarks = this.bookmarks.serialize()[this.editor.id]
if (!bookmarks) { return }

const markerLayer = this.editor.getMarkerLayer(bookmarks.markerLayerId)

if (!markerLayer) { return }

this.subscriptions.add(markerLayer.onDidCreateMarker(marker => {
this.handleMarker(marker)
}))

markerLayer.findMarkers().forEach(marker => this.handleMarker(marker))
})
}

handleMarker (marker) {
const { id } = marker
const decoration = this.minimap.decorateMarker(marker, { type: 'line', class: 'bookmark', plugin: 'bookmarks' })
this.decorationsByMarkerId[id] = decoration
this.decorationSubscriptionsByMarkerId[id] = decoration.onDidDestroy(() => {
this.decorationSubscriptionsByMarkerId[id].dispose()

delete this.decorationsByMarkerId[id]
delete this.decorationSubscriptionsByMarkerId[id]
})
}

destroy () {
for (const id in this.decorationsByMarkerId) {
const decoration = this.decorationsByMarkerId[id]
this.decorationSubscriptionsByMarkerId[id].dispose()
decoration.destroy()

delete this.decorationsByMarkerId[id]
delete this.decorationSubscriptionsByMarkerId[id]
}

this.subscriptions.dispose()
}
}

module.exports = MinimapBookmarksBinding
49 changes: 0 additions & 49 deletions lib/minimap-bookmarks.coffee

This file was deleted.

73 changes: 73 additions & 0 deletions lib/minimap-bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS201: Simplify complex destructure assignments
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const { CompositeDisposable } = require('atom')
let MinimapBookmarksBinding

module.exports = {
isActive () {
return this.active
},

activate () {
this.active = false
this.subscriptions = new CompositeDisposable()
this.bindings = {}
require('atom-package-deps').install('minimap-git-diff')
},

consumeMinimapServiceV1 (minimap) {
this.minimap = minimap
this.minimap.registerPlugin('bookmarks', this)
},

deactivate () {
if (this.minimap) {
this.minimap.unregisterPlugin('bookmarks')
}
this.minimap = null
},

activatePlugin () {
if (this.active) {
return
}

const bookmarksPkg = atom.packages.getLoadedPackage('bookmarks')
if (!bookmarksPkg) {
return
}
const bookmarks = bookmarksPkg.mainModule
this.active = true

this.minimapsSubscription = this.minimap.observeMinimaps(minimap => {
if (!MinimapBookmarksBinding) {
MinimapBookmarksBinding = require('./minimap-bookmarks-binding')
}

const binding = new MinimapBookmarksBinding(minimap, bookmarks)
this.bindings[minimap.id] = binding

const subscription = minimap.onDidDestroy(() => {
binding.destroy()
this.subscriptions.remove(subscription)
subscription.dispose()
delete this.bindings[minimap.id]
})
this.subscriptions.add(subscription)
})
},

deactivatePlugin () {
if (!this.active) { return }

for (const id in this.bindings) { const binding = this.bindings[id]; binding.destroy() }
this.bindings = {}
this.active = false
this.minimapsSubscription.dispose()
this.subscriptions.dispose()
},
}
4 changes: 0 additions & 4 deletions spec/fixtures/sample.coffee

This file was deleted.

3 changes: 3 additions & 0 deletions spec/fixtures/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @babel */

export const a = 1
69 changes: 0 additions & 69 deletions spec/minimap-bookmarks-spec.coffee

This file was deleted.

67 changes: 67 additions & 0 deletions spec/minimap-bookmarks-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// const MinimapBookmarks = require('../lib/minimap-bookmarks')

describe('MinimapBookmarks', () => {
let editor, editorElement, bookmarks

const bookmarkedRangesForEditor = (editor) => {
const obj = editor.decorationsStateForScreenRowRange(0, editor.getLastScreenRow())
return Object.keys(obj)
.map(k => obj[k])
.filter(decoration => decoration.properties.class === 'bookmarked')
.map(decoration => decoration.screenRange)
}

beforeEach(async () => {
const workspace = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspace)

// Package activation will be deferred to the configured, activation hook, which is then triggered
// Activate activation hook
atom.packages.triggerDeferredActivationHooks()
atom.packages.triggerActivationHook('core:loaded-shell-environment')

editor = await atom.workspace.open('sample.js')
editorElement = atom.views.getView(editor)

bookmarks = (await atom.packages.activatePackage('bookmarks')).mainModule

await atom.packages.activatePackage('minimap')
await atom.packages.activatePackage('minimap-bookmarks')

atom.packages.packageStates.bookmarks = bookmarks.serialize()
})

describe('with an open editor that have a minimap', () => describe('when toggle switch bookmarks markers to the editor', () => {
beforeEach(() => {
editor.setCursorScreenPosition([2, 0])
atom.commands.dispatch(editorElement, 'bookmarks:toggle-bookmark')

editor.setCursorScreenPosition([3, 0])
atom.commands.dispatch(editorElement, 'bookmarks:toggle-bookmark')

editor.setCursorScreenPosition([1, 0])
atom.commands.dispatch(editorElement, 'bookmarks:toggle-bookmark')
atom.commands.dispatch(editorElement, 'bookmarks:toggle-bookmark')
})

it('creates tow markers', () => {
expect(bookmarkedRangesForEditor(editor).length).toBe(2)
})

it('creates state of bookmarks', () => {
expect(Object.keys(atom.packages.packageStates.bookmarks).length).toBe(1)
})

it('gets markerLayerId from state of bookmarks by editorId', () => {
const { markerLayerId } = atom.packages.packageStates.bookmarks[editor.id]

expect(markerLayerId).toBeDefined()
})

it('finds marks by markerLayerId', () => {
const { markerLayerId } = atom.packages.packageStates.bookmarks[editor.id]
const markerLayer = editor.getMarkerLayer(markerLayerId)
expect(markerLayer.findMarkers().length).toBe(2)
})
}))
})

0 comments on commit d9ad626

Please sign in to comment.