add initial bookmark plugin

This commit is contained in:
Ben Ogle
2013-07-19 21:14:56 -07:00
parent afa92e51f6
commit e632b091f9
4 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
'.editor':
'meta-f2': 'bookmarks:toggle-bookmark'
'f2': 'bookmarks:jump-to-next-bookmark'
'shift-f2': 'bookmarks:jump-to-previous-bookmark'

View File

@@ -0,0 +1,54 @@
_ = require 'underscore'
module.exports =
class BookmarksView
@activate: ->
rootView.eachEditor (editor) =>
new BookmarksView(editor) if editor.attached and editor.getPane()?
editor: null
constructor: (@editor) ->
@editor.on 'editor:display-updated', @updateBookmarkedLines
rootView.command 'bookmarks:toggle-bookmark', '.editor', @toggleBookmark
rootView.command 'bookmarks:jump-to-next-bookmark', '.editor', @jumpToNextBookmark
rootView.command 'bookmarks:jump-to-previous-bookmark', '.editor', @jumpToPreviousBookmark
toggleBookmark: =>
cursors = @editor.getCursors()
for cursor in cursors
position = cursor.getBufferPosition()
bookmarks = @findBookmarkMarkers(position.row)
if bookmarks and bookmarks.length
bookmark.destroy() for bookmark in bookmarks
console.log('removing mark', position, bookmark)
else
newmark = @createBookmarkMarker(position.row)
console.log('bookmarking', position, newmark)
updateBookmarkedLines: =>
console.log('update!', @editor)
jumpToNextBookmark: =>
console.log('next bm', @editor)
jumpToPreviousBookmark: =>
console.log('prev bm', @editor)
### Internal ###
createBookmarkMarker: (bufferRow) ->
range = [[bufferRow, 0], [bufferRow, 0]]
@displayBuffer().markBufferRange(range, @bookmarkMarkerAttributes(invalidationStrategy: 'never'))
findBookmarkMarkers: (bufferRow) ->
@displayBuffer().findMarkers(@bookmarkMarkerAttributes(startBufferRow: bufferRow))
bookmarkMarkerAttributes: (attributes={}) ->
_.extend(attributes, class: 'bookmark', displayBufferId: @displayBuffer().id)
displayBuffer: ->
@editor.activeEditSession.displayBuffer

View File

@@ -0,0 +1,2 @@
'main': './lib/bookmarks-view'
'description': 'Can mark lines, then jump back to them'

View File

@@ -0,0 +1,39 @@
RootView = require 'root-view'
_ = require 'underscore'
fdescribe "Bookmarks package", ->
editor = null
editSession = null
displayBuffer = null
beforeEach ->
window.rootView = new RootView
rootView.open('sample.js')
rootView.enableKeymap()
atom.activatePackage('bookmarks', immediate: true)
rootView.attachToDom()
editor = rootView.getActiveView()
editSession = editor.activeEditSession
displayBuffer = editSession.displayBuffer
describe "toggling bookmarks", ->
it "creates a marker when toggled", ->
editSession.setCursorBufferPosition([3, 10])
expect(displayBuffer.findMarkers(class: 'bookmark').length).toEqual 0
editor.trigger 'bookmarks:toggle-bookmark'
markers = displayBuffer.findMarkers(class: 'bookmark')
expect(markers.length).toEqual 1
expect(markers[0].getBufferRange()).toEqual [[3, 0], [3, 0]]
it "removes marker when toggled", ->
editSession.setCursorBufferPosition([3, 10])
expect(displayBuffer.findMarkers(class: 'bookmark').length).toEqual 0
editor.trigger 'bookmarks:toggle-bookmark'
expect(displayBuffer.findMarkers(class: 'bookmark').length).toEqual 1
editor.trigger 'bookmarks:toggle-bookmark'
expect(displayBuffer.findMarkers(class: 'bookmark').length).toEqual 0