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

Commit a514207

Browse files
fixes to feed to listbox formatting
1 parent cdbc308 commit a514207

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

search_in_project.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import inspect
7+
from collections import defaultdict
78

89
### Start of fixing import paths
910
# realpath() with make your script run, even if you symlink it :)
@@ -60,7 +61,7 @@ def perform_search(self, text):
6061
self.results = self.engine.run(text, folders)
6162
if self.results:
6263
self.results = [[result[0].replace(self.common_path.replace('\"', ''), ''), result[1]] for result in self.results]
63-
self.results.append("``` List results in view")
64+
self.results.append("``` List results in view ```")
6465
self.window.show_quick_panel(self.results, self.goto_result)
6566
else:
6667
self.results = []
@@ -85,7 +86,7 @@ def list_in_view(self):
8586
view = sublime.active_window().new_file()
8687
view.run_command('search_in_project_results',
8788
{'query': self.last_search_string,
88-
'results': self.results,
89+
'results': self.results[0:-1], # last result is "list in view"
8990
'common_path': self.common_path.replace('\"', '')})
9091

9192
def search_folders(self):
@@ -111,15 +112,26 @@ def find_common_path(self, paths):
111112
return "\"" + "/".join(common_path) + "/\""
112113

113114
class SearchInProjectResultsCommand(sublime_plugin.TextCommand):
114-
def format_result(self, common_path, result):
115-
filename, row, column = result[0].split(':')
116-
text = result[1]
117-
return "%s%s:\n %s: %s\n" % (common_path, filename, row, text)
115+
def format_result(self, common_path, filename, lines):
116+
lines_text = "\n".join([" %s: %s" % (location, text) for location, text in lines])
117+
return "%s%s:\n%s\n" % (common_path, filename, lines_text)
118+
119+
def format_results(self, common_path, results, query):
120+
grouped_by_filename = defaultdict(list)
121+
for result in results:
122+
filename, location = result[0].split(':', 1)
123+
text = result[1]
124+
grouped_by_filename[filename].append((location, text))
125+
126+
file_results = [self.format_result(common_path, filename, grouped_by_filename[filename]) for filename in grouped_by_filename]
127+
return ("Search In Project results for \"%s\"\n\n" % query) + "\n".join(file_results)
118128

119129
def run(self, edit, common_path, results, query):
120130
self.view.set_name('Find Results')
121131
self.view.set_scratch(True)
122132
self.view.set_syntax_file('Packages/Default/Find Results.hidden-tmLanguage')
123-
results_text = ("Search In Project results for \"%s\"\n\n" % query) + "\n".join([self.format_result(common_path, result) for result in results])
133+
results_text = self.format_results(common_path, results, query)
124134
self.view.insert(edit, self.view.text_point(0,0), results_text)
135+
self.view.sel().clear()
136+
self.view.sel().add(sublime.Region(0,0))
125137

0 commit comments

Comments
 (0)