From 0806803f405be6cbdedf1d0cf04f5ca70aa518d8 Mon Sep 17 00:00:00 2001 From: Joshua Downer Date: Fri, 29 Aug 2014 18:03:30 -0400 Subject: [PATCH 1/3] pymatcher: added filename_score --- autoload/pymatcher.vim | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/autoload/pymatcher.vim b/autoload/pymatcher.vim index 67cd38f..590a3d2 100644 --- a/autoload/pymatcher.vim +++ b/autoload/pymatcher.vim @@ -53,25 +53,24 @@ else: res = [] prog = re.compile(regex) -if mmode == 'filename-only': - for line in items: - lineLower = line +def filename_score(line): + # get filename via reverse find to improve performance + slashPos = line.rfind('/') + line = line if slashPos == -1 else line[slashPos + 1:] - # get filename via reverse find to improve performance - slashPos = lineLower.rfind('/') - if slashPos != -1: - lineLower = lineLower[slashPos + 1:] + lineLower = line.lower() + result = prog.search(lineLower) + if result: + score = result.end() - result.start() + 1 + score = score + ( len(lineLower) + 1 ) / 100.0 + score = score + ( len(line) + 1 ) / 1000.0 + return 1000.0 / score - lineLower = lineLower.lower() - result = prog.search(lineLower) - if result: - scores = [] - scores.append(result.end() - result.start() + 1) - # scores.append((1 + result.start()) * (result.end() - result.start() + 1)) - scores.append(( len(lineLower) + 1 ) / 100.0) - scores.append(( len(line) + 1 ) / 1000.0) - score = 1000.0 / sum(scores) - res.append((score, line)) + return 0 + + +if mmode == 'filename-only': + res = [(filename_score(line), line) for line in items] else: for line in items: lineLower = line.lower() From 7e243d7b13ce57a925e164163e6a084d30d10057 Mon Sep 17 00:00:00 2001 From: Joshua Downer Date: Fri, 29 Aug 2014 18:04:02 -0400 Subject: [PATCH 2/3] pymatcher: added path_score --- autoload/pymatcher.vim | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/autoload/pymatcher.vim b/autoload/pymatcher.vim index 590a3d2..f82b359 100644 --- a/autoload/pymatcher.vim +++ b/autoload/pymatcher.vim @@ -69,18 +69,21 @@ def filename_score(line): return 0 +def path_score(line): + lineLower = line.lower() + result = prog.search(lineLower) + if result: + score = result.end() - result.start() + 1 + score = score + ( len(lineLower) + 1 ) / 100.0 + return 1000.0 / score + + return 0 + + if mmode == 'filename-only': res = [(filename_score(line), line) for line in items] else: - for line in items: - lineLower = line.lower() - result = prog.search(lineLower) - if result: - scores = [] - scores.append(result.end() - result.start() + 1) - scores.append(( len(lineLower) + 1 ) / 100.0) - score = 1000.0 / sum(scores) - res.append((score, line)) + res = [(path_score(line), line) for line in items] sortedlist = sorted(res, key=lambda x: x[0], reverse=True)[:limit] sortedlist = [x[1] for x in sortedlist] From d2c9ed28db82f7427e6a57d47a7f240045169878 Mon Sep 17 00:00:00 2001 From: Joshua Downer Date: Fri, 29 Aug 2014 18:04:20 -0400 Subject: [PATCH 3/3] pymatcher: use heapq to find n best results --- autoload/pymatcher.vim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/autoload/pymatcher.vim b/autoload/pymatcher.vim index f82b359..8ad301c 100644 --- a/autoload/pymatcher.vim +++ b/autoload/pymatcher.vim @@ -17,6 +17,7 @@ function! pymatcher#PyMatch(items, str, limit, mmode, ispath, crfile, regex) exec (has('python') ? ':py' : ':py3') ' << EOF' import vim, re +import heapq from datetime import datetime items = vim.eval('a:items') @@ -85,10 +86,7 @@ if mmode == 'filename-only': else: res = [(path_score(line), line) for line in items] -sortedlist = sorted(res, key=lambda x: x[0], reverse=True)[:limit] -sortedlist = [x[1] for x in sortedlist] - -rez.extend(sortedlist) +rez.extend([line for score, line in heapq.nlargest(limit, res)]) vim.command("let s:regex = '%s'" % regex) EOF