Skip to content

Instantly share code, notes, and snippets.

@t9md
Created February 24, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t9md/df84d800a8519339d91732e4eeb67544 to your computer and use it in GitHub Desktop.
Save t9md/df84d800a8519339d91732e4eeb67544 to your computer and use it in GitHub Desktop.
narrow-bookmarks provider
const {Range} = require('atom')
const Provider = require('./provider')
const {compareByPoint} = require('../utils')
const Config = {
showProjectHeader: true,
showFileHeader: true
}
// HACK: Core bookmarks package
// I need to get all bookmarks instances, but bookmarks package currently not have service for this.
// But it have serialize/deserialize.
// So I can get all bookrmaks indirectly by serialize then deserialize.
// But I also can't call Bookmarks.deserialize direcltly since it's also register `bookmarks:toggle-bookmark`
// What I need is marker information only so I manually restore marker from serialized value.
function getBookmarks () {
const {mainModule} = atom.packages.getActivePackage('bookmarks')
const bookmarksByEditorId = mainModule.serialize()
const bookmarks = []
for (const editor of atom.workspace.getTextEditors()) {
const state = bookmarksByEditorId[editor.id]
if (state) {
bookmarks.push({editor, markerLayer: editor.getMarkerLayer(state.markerLayerId)})
}
}
return bookmarks
}
module.exports = class Bookmarks {
constructor (state) {
this.provider = Provider.create({
name: this.constructor.name,
state: state,
config: Config,
getItems: this.getItems.bind(this)
})
}
start (options) {
return this.provider.start(options)
}
getItemsForEditor (editor, markerLayer) {
const filePath = editor.getPath()
return markerLayer
.getMarkers()
.map(function (marker) {
const point = marker.getStartBufferPosition()
const text = editor.lineTextForBufferRow(point.row)
return {point, text, filePath}
})
.sort(compareByPoint)
}
getItems () {
const items = []
for (let {editor, markerLayer} of getBookmarks()) {
if (markerLayer.getMarkerCount() > 0) {
items.push(...this.getItemsForEditor(editor, markerLayer))
}
}
this.provider.finishUpdateItems(items)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment