Skip to content

Commit

Permalink
Merge pull request #28 from jdowner/staging
Browse files Browse the repository at this point in the history
python performance improvements
  • Loading branch information
FelikZ committed Jun 21, 2016
2 parents 8a80326 + 7ad13f5 commit aa4c850
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions autoload/pymatcher.py
@@ -1,6 +1,5 @@
import vim, re
import heapq
from datetime import datetime

def CtrlPPyMatch():
items = vim.eval('a:items')
Expand All @@ -12,27 +11,23 @@ def CtrlPPyMatch():

rez = vim.eval('s:rez')

specialChars = ['^','$','.','{','}','(',')','[',']','\\','/','+']
escape = {c : "\\" + c for c in ['^','$','.','{','}','(',')','[',']','\\','/','+']}

regex = ''
if aregex == 1:
regex = astr
else:
if len(lowAstr) == 1:
c = lowAstr
if c in specialChars:
c = '\\' + c
regex += c
else:
for c in lowAstr[:-1]:
if c in specialChars:
c = '\\' + c
regex += c + '[^' + c + ']*'
else:
c = lowAstr[-1]
if c in specialChars:
c = '\\' + c
regex += c
# Escape all of the characters as necessary
escaped = [escape.get(c, c) for c in lowAstr]

# If the string is longer that one character, append a mismatch
# expression to each character (except the last).
if len(lowAstr) > 1:
mismatch = ["[^" + c + "]*" for c in escaped[:-1]]
regex = ''.join([c for pair in zip(escaped[:-1], mismatch) for c in pair])

# Append the last character in the string to the regex
regex += escaped[-1]

res = []
prog = re.compile(regex)
Expand Down Expand Up @@ -76,10 +71,7 @@ def path_score(line):
else:
res = [(path_score(line), line) for line in items]

#rez.extend([line for score, line in heapq.nlargest(limit, res)])
for score, line in heapq.nlargest(limit, res):
if score != 0:
rez.extend([line])
rez.extend([line for score, line in heapq.nlargest(limit, res) if score != 0])

# Use double quoted vim strings and escape \
vimrez = ['"' + line.replace('\\', '\\\\').replace('"', '\\"') + '"' for line in rez]
Expand Down

0 comments on commit aa4c850

Please sign in to comment.