From 2cb66e2846aa6c6e459916ab634236f44fdbcb96 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 24 Jan 2015 17:05:36 +0100 Subject: [PATCH] Move `rotateOverlappingMarkers()` into marker.coffee It has nothing to do with hints, but much to do with markers. --- extension/lib/hints.coffee | 63 +------------------------------------ extension/lib/marker.coffee | 61 ++++++++++++++++++++++++++++++++++- extension/lib/modes.coffee | 16 +++++----- 3 files changed, 69 insertions(+), 71 deletions(-) diff --git a/extension/lib/hints.coffee b/extension/lib/hints.coffee index dd8763a..4ed7b12 100644 --- a/extension/lib/hints.coffee +++ b/extension/lib/hints.coffee @@ -316,65 +316,4 @@ getFirstNonCoveredPoint = (window, viewport, element, elementRect, parents) -> return nonCoveredPoint - -# Finds all stacks of markers that overlap each other (by using `getStackFor`) -# (#1), and rotates their `z-index`:es (#2), thus alternating which markers are -# visible. -rotateOverlappingMarkers = (originalMarkers, forward) -> - # Shallow working copy. This is necessary since `markers` will be mutated and - # eventually empty. - markers = originalMarkers[..] - - # (#1) - stacks = (getStackFor(markers.pop(), markers) while markers.length > 0) - - # (#2) - # Stacks of length 1 don't participate in any overlapping, and can therefore - # be skipped. - for stack in stacks when stack.length > 1 - # This sort is not required, but makes the rotation more predictable. - stack.sort((a, b) -> a.markerElement.style.zIndex - - b.markerElement.style.zIndex) - - # Array of z-indices. - indexStack = (marker.markerElement.style.zIndex for marker in stack) - # Shift the array of indices one item forward or back. - if forward - indexStack.unshift(indexStack.pop()) - else - indexStack.push(indexStack.shift()) - - for marker, index in stack - marker.markerElement.style.zIndex = indexStack[index] - - return - -# Get an array containing `marker` and all markers that overlap `marker`, if -# any, which is called a "stack". All markers in the returned stack are spliced -# out from `markers`, thus mutating it. -getStackFor = (marker, markers) -> - stack = [marker] - - { top, bottom, left, right } = marker.position - - index = 0 - while index < markers.length - nextMarker = markers[index] - - next = nextMarker.position - overlapsVertically = (next.bottom >= top and next.top <= bottom) - overlapsHorizontally = (next.right >= left and next.left <= right) - - if overlapsVertically and overlapsHorizontally - # Also get all markers overlapping this one. - markers.splice(index, 1) - stack = stack.concat(getStackFor(nextMarker, markers)) - else - # Continue the search. - index++ - - return stack - - -exports.injectHints = injectHints -exports.rotateOverlappingMarkers = rotateOverlappingMarkers +exports.injectHints = injectHints diff --git a/extension/lib/marker.coffee b/extension/lib/marker.coffee index 8847c10..3924d5f 100644 --- a/extension/lib/marker.coffee +++ b/extension/lib/marker.coffee @@ -109,4 +109,63 @@ class Marker markMatched: (matched) -> @markerElement.classList.toggle('VimFxMatchedHintMarker', matched) -exports.Marker = Marker +# Finds all stacks of markers that overlap each other (by using `getStackFor`) +# (#1), and rotates their `z-index`:es (#2), thus alternating which markers are +# visible. +rotateOverlappingMarkers = (originalMarkers, forward) -> + # Shallow working copy. This is necessary since `markers` will be mutated and + # eventually empty. + markers = originalMarkers[..] + + # (#1) + stacks = (getStackFor(markers.pop(), markers) while markers.length > 0) + + # (#2) + # Stacks of length 1 don't participate in any overlapping, and can therefore + # be skipped. + for stack in stacks when stack.length > 1 + # This sort is not required, but makes the rotation more predictable. + stack.sort((a, b) -> a.markerElement.style.zIndex - + b.markerElement.style.zIndex) + + # Array of z-indices. + indexStack = (marker.markerElement.style.zIndex for marker in stack) + # Shift the array of indices one item forward or back. + if forward + indexStack.unshift(indexStack.pop()) + else + indexStack.push(indexStack.shift()) + + for marker, index in stack + marker.markerElement.style.zIndex = indexStack[index] + + return + +# Get an array containing `marker` and all markers that overlap `marker`, if +# any, which is called a "stack". All markers in the returned stack are spliced +# out from `markers`, thus mutating it. +getStackFor = (marker, markers) -> + stack = [marker] + + { top, bottom, left, right } = marker.position + + index = 0 + while index < markers.length + nextMarker = markers[index] + + next = nextMarker.position + overlapsVertically = (next.bottom >= top and next.top <= bottom) + overlapsHorizontally = (next.right >= left and next.left <= right) + + if overlapsVertically and overlapsHorizontally + # Also get all markers overlapping this one. + markers.splice(index, 1) + stack = stack.concat(getStackFor(nextMarker, markers)) + else + # Continue the search. + index++ + + return stack + +exports.Marker = Marker +exports.rotateOverlappingMarkers = rotateOverlappingMarkers diff --git a/extension/lib/modes.coffee b/extension/lib/modes.coffee index 56703a1..a8eaa9e 100644 --- a/extension/lib/modes.coffee +++ b/extension/lib/modes.coffee @@ -19,14 +19,15 @@ # along with VimFx. If not, see . ### -utils = require('./utils') -hints = require('./hints') -{ updateToolbarButton } = require('./button') +utils = require('./utils') +{ injectHints } = require('./hints') +{ rotateOverlappingMarkers } = require('./marker') +{ updateToolbarButton } = require('./button') { commands , searchForMatchingCommand , escapeCommand , Command -, findStorage } = require('./commands') +, findStorage } = require('./commands') { interfaces: Ci } = Components @@ -130,8 +131,7 @@ exports['find'] = exports['hints'] = onEnter: (vim, storage, filter, callback) -> - [ markers, container ] = hints.injectHints(vim.rootWindow, vim.window, - filter) + [ markers, container ] = injectHints(vim.rootWindow, vim.window, filter) if markers.length > 0 storage.markers = markers storage.container = container @@ -159,9 +159,9 @@ exports['hints'] = return true when @commands['rotate_markers_forward'].match(keyStr) - hints.rotateOverlappingMarkers(markers, true) + rotateOverlappingMarkers(markers, true) when @commands['rotate_markers_backward'].match(keyStr) - hints.rotateOverlappingMarkers(markers, false) + rotateOverlappingMarkers(markers, false) when @commands['delete_hint_char'].match(keyStr) for marker in markers -- 2.39.3