From 065ac9c2a9cb0bdc5029cfc157318c9cbdb29e86 Mon Sep 17 00:00:00 2001 From: solendil Date: Wed, 15 Jun 2016 21:28:38 +0200 Subject: [PATCH 1/2] Initial commit of bookmark-cycling-option --- lib/bookmarks.coffee | 14 +++++++++-- lib/main.coffee | 8 +++++++ spec/bookmarks-view-spec.coffee | 42 +++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/lib/bookmarks.coffee b/lib/bookmarks.coffee index 20e359b..ea487da 100644 --- a/lib/bookmarks.coffee +++ b/lib/bookmarks.coffee @@ -70,7 +70,12 @@ class Bookmarks if marker.getBufferRange then marker.getBufferRange().start.row else marker bookmarkIndex-- - bookmarkIndex = markers.length - 1 if bookmarkIndex < 0 + + if bookmarkIndex < 0 + if atom.config.get('bookmarks.wrapBuffer') + bookmarkIndex = markers.length - 1 + else + null markers[bookmarkIndex] @@ -83,7 +88,12 @@ class Bookmarks if marker.getBufferRange then marker.getBufferRange().start.row else marker bookmarkIndex++ if markers[bookmarkIndex] and markers[bookmarkIndex].getBufferRange().start.row is bufferRow - bookmarkIndex = 0 if bookmarkIndex >= markers.length + + if bookmarkIndex >= markers.length + if atom.config.get('bookmarks.wrapBuffer') + bookmarkIndex = 0 + else + null markers[bookmarkIndex] diff --git a/lib/main.coffee b/lib/main.coffee index e3e6de1..bd8eef8 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -7,6 +7,14 @@ editorsBookmarks = null disposables = null module.exports = + + config: + wrapBuffer: + title: 'Wrap Buffer' + description: 'When enabled, jumping to next or previous bookmark can wrap around the buffer.' + type: 'boolean' + default: true + activate: (bookmarksByEditorId) -> editorsBookmarks = [] bookmarksView = null diff --git a/spec/bookmarks-view-spec.coffee b/spec/bookmarks-view-spec.coffee index abab6e3..0176880 100644 --- a/spec/bookmarks-view-spec.coffee +++ b/spec/bookmarks-view-spec.coffee @@ -217,7 +217,8 @@ describe "Bookmarks package", -> editor.setSelectedBufferRanges([[[8, 4], [10, 0]]]) atom.commands.dispatch editorElement, 'bookmarks:toggle-bookmark' - it "jump-to-next-bookmark finds next bookmark", -> + it "jump-to-next-bookmark finds next bookmark with wrapping", -> + editor.config.set('bookmarks.wrapBuffer', true) editor.setCursorBufferPosition([0, 0]) atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' @@ -234,7 +235,8 @@ describe "Bookmarks package", -> atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' expect(editor.getLastCursor().getBufferPosition()).toEqual [2, 0] - it "jump-to-previous-bookmark finds previous bookmark", -> + it "jump-to-previous-bookmark finds previous bookmark with wrapping", -> + editor.config.set('bookmarks.wrapBuffer', true) editor.setCursorBufferPosition([0, 0]) atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' @@ -251,6 +253,42 @@ describe "Bookmarks package", -> atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' expect(editor.getLastCursor().getMarker().getBufferRange()).toEqual [[8, 4], [10, 0]] + it "jump-to-next-bookmark locks without wrapping", -> + editor.config.set('bookmarks.wrapBuffer', false) + editor.setCursorBufferPosition([0, 0]) + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(editor.getLastCursor().getBufferPosition()).toEqual [2, 0] + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(editor.getLastCursor().getMarker().getBufferRange()).toEqual [[8, 4], [10, 0]] + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(atom.beep.callCount).toBe 1 + + editor.setCursorBufferPosition([11, 0]) + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(atom.beep.callCount).toBe 2 + + it "jump-to-previous-bookmark locks without wrapping", -> + editor.config.set('bookmarks.wrapBuffer', false) + editor.setCursorBufferPosition([11, 0]) + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' + expect(editor.getLastCursor().getMarker().getBufferRange()).toEqual [[8, 4], [10, 0]] + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' + expect(editor.getLastCursor().getBufferPosition()).toEqual [2, 0] + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' + expect(atom.beep.callCount).toBe 1 + + editor.setCursorBufferPosition([0, 0]) + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' + expect(atom.beep.callCount).toBe 2 + describe "browsing bookmarks", -> it "displays a select list of all bookmarks", -> editor.setCursorBufferPosition([0]) From b05aa529a4e6bfbe27ab7bdeeb5d328217c8682e Mon Sep 17 00:00:00 2001 From: solendil Date: Wed, 20 Jul 2016 21:49:58 +0200 Subject: [PATCH 2/2] Show a wrap around icon when navigating bookmarks --- lib/bookmarks.coffee | 22 ++++++++++++++++++++++ lib/main.coffee | 4 ++++ spec/bookmarks-view-spec.coffee | 17 +++++++++++++++++ styles/bookmarks.less | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/lib/bookmarks.coffee b/lib/bookmarks.coffee index ea487da..53d26cb 100644 --- a/lib/bookmarks.coffee +++ b/lib/bookmarks.coffee @@ -1,4 +1,5 @@ _ = require 'underscore-plus' +{$} = require 'atom-space-pen-views' {CompositeDisposable} = require 'atom' module.exports = @@ -18,6 +19,12 @@ class Bookmarks @markerLayer ?= @editor.addMarkerLayer(markerLayerOptions) @decorationLayer = @editor.decorateMarkerLayer(@markerLayer, {type: 'line-number', class: 'bookmarked'}) @disposables.add @editor.onDidDestroy(@destroy.bind(this)) + @createWrapIcon() + + createWrapIcon: -> + wrapIcon = document.createElement('div') + wrapIcon.classList.add('bookmark-wrap-icon') + @wrapIcon = $(wrapIcon) destroy: -> @deactivate() @@ -61,6 +68,19 @@ class Bookmarks else atom.beep() + showWrapIcon: (icon) -> + editorView = atom.views.getView(@editor) + return unless editorView?.parentNode? + + # Attach to the parent of the active editor, that way we can position it + # correctly over the active editor. + editorView.parentNode.appendChild(@wrapIcon[0]) + + # FIXME: This animation should be in CSS + @wrapIcon.attr('class', "bookmark-wrap-icon #{icon}").fadeIn() + clearTimeout(@wrapTimeout) + @wrapTimeout = setTimeout (=> @wrapIcon.fadeOut()), 1000 + getPreviousBookmark: (bufferRow) -> markers = @markerLayer.getMarkers() return null unless markers.length @@ -73,6 +93,7 @@ class Bookmarks if bookmarkIndex < 0 if atom.config.get('bookmarks.wrapBuffer') + @showWrapIcon('icon-move-down') bookmarkIndex = markers.length - 1 else null @@ -91,6 +112,7 @@ class Bookmarks if bookmarkIndex >= markers.length if atom.config.get('bookmarks.wrapBuffer') + @showWrapIcon('icon-move-up') bookmarkIndex = 0 else null diff --git a/lib/main.coffee b/lib/main.coffee index bd8eef8..9881ea4 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -43,6 +43,10 @@ module.exports = bookmarks.deactivate() for bookmarks in editorsBookmarks disposables.dispose() + getBookmarkForEditor: (editor) -> + for bookmark in editorsBookmarks + return bookmark if bookmark.editor.id is editor.id + serialize: -> bookmarksByEditorId = {} for bookmarks in editorsBookmarks diff --git a/spec/bookmarks-view-spec.coffee b/spec/bookmarks-view-spec.coffee index 0176880..8a66efa 100644 --- a/spec/bookmarks-view-spec.coffee +++ b/spec/bookmarks-view-spec.coffee @@ -289,6 +289,23 @@ describe "Bookmarks package", -> atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' expect(atom.beep.callCount).toBe 2 + it "shows an icon when editor wraps around", -> + editor.config.set('bookmarks.wrapBuffer', true) + editor.setCursorBufferPosition([0, 0]) + wrapIcon = bookmarks.getBookmarkForEditor(editor).wrapIcon + + expect(wrapIcon).not.toBeVisible() + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(wrapIcon).not.toBeVisible() + atom.commands.dispatch editorElement, 'bookmarks:jump-to-next-bookmark' + expect(wrapIcon).toBeVisible() + expect(wrapIcon).toHaveClass 'icon-move-up' + + atom.commands.dispatch editorElement, 'bookmarks:jump-to-previous-bookmark' + expect(wrapIcon).toBeVisible() + expect(wrapIcon).toHaveClass 'icon-move-down' + describe "browsing bookmarks", -> it "displays a select list of all bookmarks", -> editor.setCursorBufferPosition([0]) diff --git a/styles/bookmarks.less b/styles/bookmarks.less index 128ebdb..bb56494 100644 --- a/styles/bookmarks.less +++ b/styles/bookmarks.less @@ -1,4 +1,5 @@ @import "octicon-utf-codes.less"; +@import "syntax-variables"; atom-text-editor, atom-text-editor::shadow { @@ -19,3 +20,34 @@ atom-text-editor::shadow { } } } + +.bookmark-wrap-icon { + @wrap-size: @font-size * 10; + + display: none; + position: absolute; + + // These are getting placed in the DOM as a pane item, so override the pane + // item positioning styles. :/ + top: 50% !important; + left: 50% !important; + right: initial !important; + bottom: initial !important; + + margin-top: @wrap-size * -0.5; + margin-left: @wrap-size * -0.5; + + background: fadeout(darken(@syntax-background-color, 4%), 55%); + border-radius: @component-border-radius * 2; + text-align: center; + pointer-events: none; + &:before { + // Octicons look best in sizes that are multiples of 16px + font-size: @wrap-size - mod(@wrap-size, 16px) - 32px; + line-height: @wrap-size; + height: @wrap-size; + width: @wrap-size; + color: @syntax-text-color; + opacity: .5; + } +}