aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/link_hints.coffee
diff options
context:
space:
mode:
authorStephen Blott2015-06-10 14:23:58 +0100
committerStephen Blott2015-06-10 14:23:58 +0100
commit5aaed906b94337bb2136d74672e16679ab457c49 (patch)
tree8a576cd986eb0083174c794c95ee60b5f8d10997 /content_scripts/link_hints.coffee
parentf3e62301cf51fbeea77fb49eb90f1b7b9138d118 (diff)
downloadvimium-5aaed906b94337bb2136d74672e16679ab457c49.tar.bz2
Rank filtered hints by score.
Thus, better matches are likely to either be first (so just hitting <Enter> activates them) or just a <Tab> or two away. Scoring: - Requires that every search term be matched. - Assigns higher scores to matches at the start of a word, and higher scores still for whole-word matches.
Diffstat (limited to 'content_scripts/link_hints.coffee')
-rw-r--r--content_scripts/link_hints.coffee30
1 files changed, 29 insertions, 1 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee
index 107a292e..ae9d3f8f 100644
--- a/content_scripts/link_hints.coffee
+++ b/content_scripts/link_hints.coffee
@@ -565,13 +565,41 @@ class FilterHints
filterLinkHints: (hintMarkers) ->
idx = 0
linkSearchString = @linkTextKeystrokeQueue.join("").toLowerCase()
+ do (scoreFunction = @scoreLinkHint linkSearchString) ->
+ linkMarker.score = scoreFunction linkMarker for linkMarker in hintMarkers
+ hintMarkers = hintMarkers[..].sort (a,b) -> b.score - a.score
for linkMarker in hintMarkers
- continue unless 0 <= linkMarker.linkText.toLowerCase().indexOf linkSearchString
+ continue unless 0 < linkMarker.score
linkMarker.hintString = @generateHintString idx++
@renderMarker linkMarker
linkMarker
+ # Assign a score to a filter match (higher is better). We assign a higher score for matches at the start of
+ # a word, and a considerably higher score still for matches which are whole words.
+ # Note(smblott) if linkSearchString is empty, then every hint get a score of 2.
+ scoreLinkHint: (linkSearchString) ->
+ searchWords = linkSearchString.trim().split /\s+/
+ (linkMarker) ->
+ linkWords = linkMarker.linkWords ?= linkMarker.linkText.trim().toLowerCase().split /\s+/
+
+ searchWordScores =
+ for searchWord in searchWords
+ linkWordScores =
+ for linkWord in linkWords
+ if linkWord == searchWord
+ 5
+ else if linkWord.startsWith searchWord
+ 2
+ else if 0 <= linkWord.indexOf searchWord
+ 1
+ else
+ 0
+ Math.max linkWordScores...
+
+ addFunc = (a,b) -> a + b
+ if 0 in searchWordScores then 0 else searchWordScores.reduce addFunc, 0
+
#
# Make each hint character a span, so that we can highlight the typed characters as you type them.
#