From 575b14a13f13ef915141e011c5dd8582a9e09d33 Mon Sep 17 00:00:00 2001 From: Calum Smith Date: Thu, 29 Sep 2016 17:25:30 -0400 Subject: [PATCH] Improved handling multiple directory needles --- bin/autojump_match.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/autojump_match.py b/bin/autojump_match.py index cf166b9..03095c7 100644 --- a/bin/autojump_match.py +++ b/bin/autojump_match.py @@ -20,14 +20,15 @@ else: def match_anywhere(needles, haystack, ignore_case=False): """ Matches needles anywhere in the path as long as they're in the same (but - not necessary consecutive) order. + not necessary consecutive) order, and the last needle is at the end. For example: needles = ['foo', 'baz'] - regex needle = r'.*foo.*baz.*' + regex needle = r'foo.*/.*baz[^/]*$' haystack = [ (path='/foo/bar/baz', weight=10), (path='/baz/foo/bar', weight=10), + (path='/foo/baz/bar', weight=11), (path='/foo/baz', weight=10), ] @@ -36,8 +37,9 @@ def match_anywhere(needles, haystack, ignore_case=False): (path='/foo/baz', weight=10), ] """ - regex_needle = '.*' + '.*'.join(imap(re.escape, needles)) + '.*' - regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE + regex_some_seps = '.*' + os.sep + '.*' + regex_no_sep_end = '[^' + os.sep + ']*$' + regex_needle = regex_some_seps.join(imap(re.escape, needles)) + regex_no_sep_end found = lambda haystack: re.search( regex_needle, haystack.path,