Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit bc0d658

Browse files
author
Anton Guryanov
committed
Remove possible subfolders from a list of folders to search in.
1 parent e32d23f commit bc0d658

2 files changed

Lines changed: 14 additions & 16 deletions

File tree

search_in_project.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def perform_search(self, text):
5656
self.last_search_string = text
5757
folders = self.search_folders()
5858

59-
self.common_path = self.find_common_path(folders)
59+
self.common_path = os.path.commonprefix(folders)
6060
try:
6161
self.results = self.engine.run(text, folders)
6262
if self.results:
@@ -99,18 +99,6 @@ def search_folders(self):
9999
search_folders = [os.path.expanduser("~")]
100100
return search_folders
101101

102-
def find_common_path(self, paths):
103-
paths = [path.replace("\"", "") for path in paths]
104-
paths = [path.split("/") for path in paths]
105-
common_path = []
106-
while 0 not in [len(path) for path in paths]:
107-
next_segment = list(set([path.pop(0) for path in paths]))
108-
if len(next_segment) == 1:
109-
common_path += next_segment
110-
else:
111-
break
112-
return "\"" + "/".join(common_path) + "/\""
113-
114102
class SearchInProjectResultsCommand(sublime_plugin.TextCommand):
115103
def format_result(self, common_path, filename, lines):
116104
lines_text = "\n".join([" %s: %s" % (location, text) for location, text in lines])
@@ -137,4 +125,3 @@ def run(self, edit, common_path, results, query):
137125
self.view.insert(edit, self.view.text_point(0,0), results_text)
138126
self.view.sel().clear()
139127
self.view.sel().add(sublime.Region(0,0))
140-

searchengines/base.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def run(self, query, folders):
3737
the absolute file path, and optionally row information, separated
3838
by a semicolon, and the second element is the result string
3939
"""
40-
arguments = self._arguments(query, folders)
40+
arguments = self._arguments(query, self._remove_subfolders(folders))
4141
print("Running: %s" % " ".join(arguments))
4242

4343
try:
@@ -49,7 +49,7 @@ def run(self, query, folders):
4949
pipe = subprocess.Popen(arguments,
5050
stdout=subprocess.PIPE,
5151
stderr=subprocess.PIPE,
52-
cwd=folders[0],
52+
cwd=os.path.commonprefix(folders),
5353
startupinfo=startupinfo
5454
)
5555
except OSError: # Not FileNotFoundError for compatibility with Sublime Text 2
@@ -61,6 +61,17 @@ def run(self, query, folders):
6161
raise RuntimeError(self._sanitize_output(error))
6262
return self._parse_output(self._sanitize_output(output))
6363

64+
def _remove_subfolders(self, folders):
65+
"""
66+
Optimize folder list by removing possible subfolders.
67+
"""
68+
unique_folders = []
69+
for folder in sorted(folders):
70+
if (len(unique_folders) == 0 or
71+
not folder.startswith(unique_folders[-1])):
72+
unique_folders.append(folder)
73+
return unique_folders
74+
6475
def _arguments(self, query, folders):
6576
"""
6677
Prepare arguments list for the search engine.

0 commit comments

Comments
 (0)