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

Commit 55ab38f

Browse files
Merge pull request #30 from Shekeen/exclude-subfolders
Remove possible subfolders from a list of folders to search in.
2 parents bb67dbf + bc0d658 commit 55ab38f

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
@@ -81,7 +81,7 @@ def perform_search(self, text):
8181
self.last_search_string = text
8282
folders = self.search_folders()
8383

84-
self.common_path = self.find_common_path(folders)
84+
self.common_path = os.path.commonprefix(folders)
8585
try:
8686
self.results = self.engine.run(text, folders)
8787
if self.results:
@@ -163,18 +163,6 @@ def search_folders(self):
163163
search_folders = [os.path.expanduser("~")]
164164
return search_folders
165165

166-
def find_common_path(self, paths):
167-
paths = [path.replace("\"", "") for path in paths]
168-
paths = [path.split("/") for path in paths]
169-
common_path = []
170-
while 0 not in [len(path) for path in paths]:
171-
next_segment = list(set([path.pop(0) for path in paths]))
172-
if len(next_segment) == 1:
173-
common_path += next_segment
174-
else:
175-
break
176-
return "\"" + "/".join(common_path) + "/\""
177-
178166
class SearchInProjectResultsCommand(sublime_plugin.TextCommand):
179167
def format_result(self, common_path, filename, lines):
180168
lines_text = "\n".join([" %s: %s" % (location, text) for location, text in lines])
@@ -201,4 +189,3 @@ def run(self, edit, common_path, results, query):
201189
self.view.insert(edit, self.view.text_point(0,0), results_text)
202190
self.view.sel().clear()
203191
self.view.sel().add(sublime.Region(0,0))
204-

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)