diff --git a/src/packages/bookmarks/keymaps/bookmarks.cson b/src/packages/bookmarks/keymaps/bookmarks.cson new file mode 100644 index 000000000..33d626838 --- /dev/null +++ b/src/packages/bookmarks/keymaps/bookmarks.cson @@ -0,0 +1,4 @@ +'.editor': + 'meta-f2': 'bookmarks:toggle-bookmark' + 'f2': 'bookmarks:jump-to-next-bookmark' + 'shift-f2': 'bookmarks:jump-to-previous-bookmark' diff --git a/src/packages/bookmarks/lib/bookmarks-view.coffee b/src/packages/bookmarks/lib/bookmarks-view.coffee new file mode 100644 index 000000000..eca541702 --- /dev/null +++ b/src/packages/bookmarks/lib/bookmarks-view.coffee @@ -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 diff --git a/src/packages/bookmarks/package.cson b/src/packages/bookmarks/package.cson new file mode 100644 index 000000000..b5f2b67c1 --- /dev/null +++ b/src/packages/bookmarks/package.cson @@ -0,0 +1,2 @@ +'main': './lib/bookmarks-view' +'description': 'Can mark lines, then jump back to them' diff --git a/src/packages/bookmarks/spec/bookmarks-view-spec.coffee b/src/packages/bookmarks/spec/bookmarks-view-spec.coffee new file mode 100644 index 000000000..5e4ad3e5f --- /dev/null +++ b/src/packages/bookmarks/spec/bookmarks-view-spec.coffee @@ -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