From 3931fb63eff3e82b1db364b7e8589f50492b03b9 Mon Sep 17 00:00:00 2001 From: Raghu Rajagopalan Date: Thu, 18 Oct 2018 08:16:05 +0530 Subject: [PATCH] (fix) return topN when query and current are both empty. refs #14 --- rplugin/fruzzy_test.py | 13 ++++++++++--- rplugin/python3/fruzzy_mod.nim | 17 +++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/rplugin/fruzzy_test.py b/rplugin/fruzzy_test.py index b2ee9e3..0866386 100644 --- a/rplugin/fruzzy_test.py +++ b/rplugin/fruzzy_test.py @@ -9,20 +9,20 @@ from python3.fruzzy import scoreMatches -def scoreMatchesProxy(q, c, limit, key=None, ispath=True): +def scoreMatchesProxy(q, c, limit=10, current="", key=None, ispath=True): def idfn(x): return x if key is None: key = idfn if useNative: idxArr = scoreMatchesStr(q, [key(x) for x in c], - "", limit, ispath) + current, limit, ispath) results = [] for i in idxArr: results.append((c[i[0]], i[1])) return results else: - return scoreMatches(q, c, "", limit, key, ispath) + return scoreMatches(q, c, current, limit, key, ispath) lines = [] @@ -129,3 +129,10 @@ def test_for_bug_08(): results = list(scoreMatchesProxy("fmf", c, 10, ispath=True)) assert results[0][0] == \ 'rplugin/python3/denite/filter/matcher/fruzzymatcher.py' + +def test_must_return_topN_when_query_and_current_are_empty(): + c = ["/this/is/fileone.txt", "/that/was/FileOne.txt"] + results = list(scoreMatchesProxy("", c, 10, ispath=False)) + assert len(results) == 2 + assert results[0][0] == c[0] + assert results[1][0] == c[1] diff --git a/rplugin/python3/fruzzy_mod.nim b/rplugin/python3/fruzzy_mod.nim index 3f2102d..513c3ef 100644 --- a/rplugin/python3/fruzzy_mod.nim +++ b/rplugin/python3/fruzzy_mod.nim @@ -300,15 +300,20 @@ iterator fuzzyMatches(query:string, candidates: openarray[string], current: stri heap.push((i, rank)) if findFirstN and count == limit * 5: break - else: # if blank string just take N items based on levenshtien (rev) + elif query == "" and current != "" and ispath: # if query is empty just take N items based on levenshtien (rev) for i, x in candidates: if current != x: heap.push((i, 300 - current.editDistance(x))) - count = 0 - while count < limit and heap.size > 0: - let item = heap.pop - yield item - count.inc + else: # just return top N items from candidates as is + for j in 0 ..< min(limit, candidates.len): + yield (j, 0) + + if heap.size > 0: + count = 0 + while count < limit and heap.size > 0: + let item = heap.pop + yield item + count.inc proc scoreMatchesStr(query: string, candidates: openarray[string], current: string, limit: int, ispath:bool=true): seq[tuple[i:int, r:int]] {.exportpy.} = result = newSeq[tuple[i:int, r:int]](limit)