diff options
| -rw-r--r-- | content_scripts/link_hints.coffee | 73 | ||||
| -rw-r--r-- | lib/utils.coffee | 1 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 4 | 
3 files changed, 17 insertions, 61 deletions
| diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 8fc1446b..07deaf61 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -416,8 +416,6 @@ class LinkHintsMode  # Use characters for hints, and do not filter links by their text.  class AlphabetHints -  logXOfBase: (x, base) -> Math.log(x) / Math.log(base) -    constructor: ->      @linkHintCharacters = Settings.get "linkHintCharacters"      # We use the keyChar from keydown if the link-hint characters are all "a-z0-9".  This is the default @@ -440,39 +438,16 @@ class AlphabetHints    # may be of different lengths.    #    hintStrings: (linkCount) -> -    # Determine how many digits the link hints will require in the worst case. Usually we do not need -    # all of these digits for every link single hint, so we can show shorter hints for a few of the links. -    digitsNeeded = Math.ceil(@logXOfBase(linkCount, @linkHintCharacters.length)) -    # Short hints are the number of hints we can possibly show which are (digitsNeeded - 1) digits in length. -    shortHintCount = Math.floor( -      (Math.pow(@linkHintCharacters.length, digitsNeeded) - linkCount) / -      @linkHintCharacters.length) -    longHintCount = linkCount - shortHintCount - -    hintStrings = [] - -    if (digitsNeeded > 1) -      for i in [0...shortHintCount] -        hintStrings.push(numberToHintString(i, @linkHintCharacters, digitsNeeded - 1)) - -    start = shortHintCount * @linkHintCharacters.length -    for i in [start...(start + longHintCount)] -      hintStrings.push(numberToHintString(i, @linkHintCharacters, digitsNeeded)) - -    @shuffleHints(hintStrings, @linkHintCharacters.length) +    hints = [""] +    offset = 0 +    while hints.length - offset < linkCount or hints.length == 1 +      hint = hints[offset++] +      hints.push ch + hint for ch in @linkHintCharacters +    hints = hints[offset...offset+linkCount] -  # -  # This shuffles the given set of hints so that they're scattered -- hints starting with the same character -  # will be spread evenly throughout the array. -  # -  shuffleHints: (hints, characterSetLength) -> -    buckets = ([] for i in [0...characterSetLength] by 1) -    for hint, i in hints -      buckets[i % buckets.length].push(hint) -    result = [] -    for bucket in buckets -      result = result.concat(bucket) -    result +    # Shuffle the hints so that they're scattered; hints starting with the same character and short hints are +    # spread evenly throughout the array. +    return hints.sort().map (str) -> str.reverse()    getMatchingHints: (hintMarkers) ->      matchString = @hintKeystrokeQueue.join "" @@ -506,7 +481,12 @@ class FilterHints          @labelMap[forElement] = labelText    generateHintString: (linkHintNumber) -> -    numberToHintString linkHintNumber, @linkHintNumbers.toUpperCase() +    base = @linkHintNumbers.length +    hint = [] +    while 0 < linkHintNumber +      hint.push @linkHintNumbers[Math.floor linkHintNumber % base] +      linkHintNumber = Math.floor linkHintNumber / base +    hint.reverse().join ""    generateLinkText: (element) ->      linkText = "" @@ -638,29 +618,6 @@ spanWrap = (hintString) ->      innerHTML.push("<span class='vimiumReset'>" + char + "</span>")    innerHTML.join("") -# -# Converts a number like "8" into a hint string like "JK". This is used to sequentially generate all of the -# hint text. The hint string will be "padded with zeroes" to ensure its length is >= numHintDigits. -# -numberToHintString = (number, characterSet, numHintDigits = 0) -> -  base = characterSet.length -  hintString = [] -  remainder = 0 -  loop -    remainder = number % base -    hintString.unshift(characterSet[remainder]) -    number -= remainder -    number /= Math.floor(base) -    break unless number > 0 - -  # Pad the hint string we're returning so that it matches numHintDigits. -  # Note: the loop body changes hintString.length, so the original length must be cached! -  hintStringLength = hintString.length -  for i in [0...(numHintDigits - hintStringLength)] by 1 -    hintString.unshift(characterSet[0]) - -  hintString.join("") -  # Suppress all keyboard events until the user stops typing for sufficiently long.  class TypingProtector extends Mode    constructor: (delay, callback) -> diff --git a/lib/utils.coffee b/lib/utils.coffee index b69b926b..c255102e 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -281,6 +281,7 @@ Array.copy = (array) -> Array.prototype.slice.call(array, 0)  String::startsWith = (str) -> @indexOf(str) == 0  String::ltrim = -> @replace /^\s+/, ""  String::rtrim = -> @replace /\s+$/, "" +String::reverse = -> @split("").reverse().join ""  globalRoot = window ? global  globalRoot.extend = (hash1, hash2) -> diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index e814ec76..1d4703c1 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -149,10 +149,8 @@ context "Alphabetical link hints",      document.getElementById("test-div").innerHTML = ""    should "label the hints correctly", -> -    # TODO(philc): This test verifies the current behavior, but the current behavior is incorrect. -    # The output here should be something like aa, ab, b.      hintMarkers = getHintMarkers() -    expectedHints = ["aa", "ba", "ab"] +    expectedHints = ["aa", "b", "ab"]      for hint, i in expectedHints        assert.equal hint, hintMarkers[i].hintString | 
