From eaf66eda5b5f0c0d6e86614b114c71ee9e0a3bf5 Mon Sep 17 00:00:00 2001 From: Jingchao Date: Thu, 29 Jun 2023 08:06:35 +0000 Subject: [PATCH 1/3] feat: using fuzzy to match query --- recent_projects.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/recent_projects.py b/recent_projects.py index cb830fb..fac2c11 100644 --- a/recent_projects.py +++ b/recent_projects.py @@ -31,6 +31,15 @@ def create_json(projects): AlfredOutput([AlfredItem(project.name, project.path, project.path) for project in projects])) +def match_fuzzy(chars, string): + index = 0 + for char in chars: + if char not in string[index:]: + return False + index = string.index(char, index) + 1 + return True + + class Project: def __init__(self, path): self.path = os.path.expanduser(path) @@ -60,7 +69,8 @@ def abbreviate(self): return abbreviation def matches_query(self, query): - return query in self.path.lower() or query in self.abbreviation.lower() or query in self.name.lower() + splits = filter(lambda x: x.strip() != '', query) + return match_fuzzy(splits, self.name.lower()) def sort_on_match_type(self, query): if query == self.abbreviation: From 30b11cb2adbca0cd66b89ddd8c6de11931435c8b Mon Sep 17 00:00:00 2001 From: Jingchao Date: Fri, 30 Jun 2023 00:26:00 +0800 Subject: [PATCH 2/3] feat: using path match, refactor fun name, add ut --- recent_projects.py | 6 +++--- recent_projects_test.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/recent_projects.py b/recent_projects.py index fac2c11..124e510 100644 --- a/recent_projects.py +++ b/recent_projects.py @@ -31,7 +31,7 @@ def create_json(projects): AlfredOutput([AlfredItem(project.name, project.path, project.path) for project in projects])) -def match_fuzzy(chars, string): +def match_partial(chars, string): index = 0 for char in chars: if char not in string[index:]: @@ -69,8 +69,8 @@ def abbreviate(self): return abbreviation def matches_query(self, query): - splits = filter(lambda x: x.strip() != '', query) - return match_fuzzy(splits, self.name.lower()) + chars = filter(lambda x: x.strip() != '', query) + return match_partial(chars, self.path.lower()) def sort_on_match_type(self, query): if query == self.abbreviation: diff --git a/recent_projects_test.py b/recent_projects_test.py index a52fe36..2061dd6 100644 --- a/recent_projects_test.py +++ b/recent_projects_test.py @@ -2,7 +2,7 @@ from unittest import mock from recent_projects import create_json, Project, find_app_data, find_recentprojects_file, read_projects_from_file, \ - filter_and_sort_projects + filter_and_sort_projects, match_partial class Unittests(unittest.TestCase): @@ -115,6 +115,20 @@ def test_project_sort_on_match_type(self): self.assertEqual(project.sort_on_match_type("spring-petclinic"), 1) self.assertEqual(project.sort_on_match_type("foobar"), 2) + def test_match_partial(self): + self.assertEqual(match_partial("y", "your-foo-bar"), True) + self.assertEqual(match_partial("yr", "your-foo-bar"), True) + self.assertEqual(match_partial("yrb", "your-foo-bar"), True) + self.assertEqual(match_partial("your-foo-bar", "your-foo-bar"), True) + self.assertEqual(match_partial("your-fb", "your-foo-bar"), True) + self.assertEqual(match_partial("", "your-foo-bar"), True) + + self.assertEqual(match_partial("not-exist", "your-foo-bar"), False) + self.assertEqual(match_partial("yr0", "your-foo-bar"), False) + self.assertEqual(match_partial("ybf", "your-foo-bar"), False) + self.assertEqual(match_partial(" ", "your-foo-bar"), False) + self.assertEqual(match_partial("oooo", "ooo"), False) + if __name__ == '__main__': # pragma: nocover unittest.main() From aafa612479395207af074763738ec4b997e23bed Mon Sep 17 00:00:00 2001 From: Jingchao Date: Fri, 30 Jun 2023 00:29:42 +0800 Subject: [PATCH 3/3] test: more unitest for path match --- recent_projects_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/recent_projects_test.py b/recent_projects_test.py index 2061dd6..23f399a 100644 --- a/recent_projects_test.py +++ b/recent_projects_test.py @@ -123,6 +123,13 @@ def test_match_partial(self): self.assertEqual(match_partial("your-fb", "your-foo-bar"), True) self.assertEqual(match_partial("", "your-foo-bar"), True) + self.assertEqual(match_partial("uyfb", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("uyr", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("uyrb", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("uyour-foo-bar", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("uyour-fb", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("h/u/your-fb", "/home/username/your-foo-bar"), True) + self.assertEqual(match_partial("not-exist", "your-foo-bar"), False) self.assertEqual(match_partial("yr0", "your-foo-bar"), False) self.assertEqual(match_partial("ybf", "your-foo-bar"), False)