Created
February 24, 2018 17:35
-
-
Save t9md/df84d800a8519339d91732e4eeb67544 to your computer and use it in GitHub Desktop.
narrow-bookmarks provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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