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

Commit 8e82a3d

Browse files
Merge pull request #35 from ahuff44/improvements
Improvements (clear marks, jump to next/prev result, show previews on hover)
2 parents 6ef98c7 + 7763401 commit 8e82a3d

6 files changed

Lines changed: 92 additions & 30 deletions

Default (Linux).sublime-keymap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[
2-
{ "keys": ["alt+ctrl+shift+f"], "command": "search_in_project" }
2+
{ "keys": ["alt+ctrl+shift+f"], "command": "search_in_project", "args": { "type": "search" } }
33
]

Default (OSX).sublime-keymap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[
2-
{ "keys": ["alt+super+shift+f"], "command": "search_in_project" }
2+
{ "keys": ["alt+super+shift+f"], "command": "search_in_project", "args": { "type": "search" } }
33
]

Default (Windows).sublime-keymap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[
2-
{ "keys": ["alt+ctrl+shift+f"], "command": "search_in_project" }
2+
{ "keys": ["alt+ctrl+shift+f"], "command": "search_in_project", "args": { "type": "search" } }
33
]

Default.sublime-commands

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
[
22
{
3-
"caption": "Search In Project",
4-
"command": "search_in_project"
3+
"caption": "SearchInProject: Search",
4+
"command": "search_in_project", "args": {"type": "search"},
5+
},
6+
{
7+
"caption": "SearchInProject: Clear results",
8+
"command": "search_in_project", "args": {"type": "clear"},
9+
},
10+
{
11+
"caption": "SearchInProject: Next result",
12+
"command": "search_in_project", "args": {"type": "next"},
13+
},
14+
{
15+
"caption": "SearchInProject: Previous result",
16+
"command": "search_in_project", "args": {"type": "prev"},
517
},
618
{
719
"caption": "Preferences: Search in Project Settings – Default",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ It opens a quick selection panel to browse results, and highlights matches insid
99
## Usage
1010

1111
* Use the key binding (`⌘⌥⇧F` on OS X, `Ctrl+Alt+Shift+F` on Windows and Linux), or
12-
* Call the "Search in Project" command;
12+
* Call the "SearchInProject: Search" command;
1313
* Enter the search query;
1414
* Hit `Enter` (`Return`). You'll be presented with a "quick select" panel with the search results. Select any file from that panel (it supports fuzzy searching) to go to the match. The search string will be highlighted with an outline and a circle symbol in the gutter area.
1515
* The last item on the quick select panel is "List results in view". Pick it to see results in a regular editor view. (Tip: if you enter three ticks ("`") in the search box - it's going to be to be the first item.)
@@ -32,7 +32,7 @@ My idea is that if you use this plugin it's because you already use one of the s
3232

3333
The supported search engines are:
3434

35-
Name | Description | Search in Project key
35+
Name | Description | Search in Project key
3636
---- | ----------- | ---------------------
3737
**[pt (The Platinum Searcher)](https://github.com/monochromegane/the_platinum_searcher)** | **fast, has binaries for every platform, recommended.** | `the_platinum_searcher`
3838
**[ag (The Silver Searcher)](http://geoff.greer.fm/ag/)** | **equally fast, only 3rd party binaries for Windows, also recommended** | `the_silver_searcher`

search_in_project.py

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,43 @@ class SearchInProjectCommand(sublime_plugin.WindowCommand):
3535

3636
def __init__(self, window):
3737
sublime_plugin.WindowCommand.__init__(self, window)
38+
self.results = []
3839
self.last_search_string = ''
39-
pass
40-
41-
def run(self):
42-
self.settings = sublime.load_settings('SearchInProject.sublime-settings')
43-
self.engine_name = self.settings.get("search_in_project_engine")
44-
pushd = os.getcwd()
45-
os.chdir(basedir)
46-
__import__("searchengines.%s" % self.engine_name)
47-
self.engine = searchengines.__dict__[self.engine_name].engine_class(self.settings)
48-
os.chdir(pushd)
49-
view = self.window.active_view()
50-
selection_text = view.substr(view.sel()[0])
51-
self.window.show_input_panel(
52-
"Search in project:",
53-
not "\n" in selection_text and selection_text or self.last_search_string,
54-
self.perform_search, None, None)
55-
pass
40+
self.last_selected_result_index = 0
41+
self.saved_view = None
42+
43+
def run(self, type="search"):
44+
if type == "search":
45+
self.settings = sublime.load_settings('SearchInProject.sublime-settings')
46+
self.engine_name = self.settings.get("search_in_project_engine")
47+
pushd = os.getcwd()
48+
os.chdir(basedir)
49+
__import__("searchengines.%s" % self.engine_name)
50+
self.engine = searchengines.__dict__[self.engine_name].engine_class(self.settings)
51+
os.chdir(pushd)
52+
view = self.window.active_view()
53+
selection_text = view.substr(view.sel()[0])
54+
self.saved_view = view
55+
panel_view = self.window.show_input_panel(
56+
"Search in project:",
57+
not "\n" in selection_text and selection_text or self.last_search_string,
58+
self.perform_search, None, None)
59+
panel_view.run_command("select_all")
60+
elif type == "clear":
61+
self.clear_markup()
62+
elif type == "next":
63+
self.goto_relative_result(1)
64+
elif type == "prev":
65+
self.goto_relative_result(-1)
66+
else:
67+
raise Exception("unrecognized type \"%s\""%type)
5668

5769
def perform_search(self, text):
5870
if not text:
5971
return
6072

73+
if self.last_search_string != text:
74+
self.last_selected_result_index = 0
6175
self.last_search_string = text
6276
folders = self.search_folders()
6377

@@ -67,24 +81,60 @@ def perform_search(self, text):
6781
if self.results:
6882
self.results = [[result[0].replace(self.common_path.replace('\"', ''), ''), result[1][:self.MAX_RESULT_LINE_LENGTH]] for result in self.results]
6983
self.results.append("``` List results in view ```")
70-
self.window.show_quick_panel(self.results, self.goto_result)
84+
flags = 0
85+
self.window.show_quick_panel(
86+
self.results,
87+
self.goto_result,
88+
flags,
89+
self.last_selected_result_index,
90+
self.on_highlighted)
7191
else:
7292
self.results = []
7393
sublime.message_dialog('No results')
7494
except Exception as e:
7595
self.results = []
7696
sublime.error_message("%s running search engine %s:"%(e.__class__.__name__,self.engine_name) + "\n" + str(e))
7797

98+
def on_highlighted(self, file_no):
99+
self.last_selected_result_index = file_no
100+
if file_no != -1 and file_no != len(self.results) - 1: # last result is "list in view"
101+
self.open_and_highlight_file(file_no, transient=True)
102+
103+
def open_and_highlight_file(self, file_no, transient=False):
104+
file_name_and_col = self.common_path.replace('\"', '') + self.results[file_no][0]
105+
flags = sublime.ENCODED_POSITION
106+
if transient:
107+
flags |= sublime.TRANSIENT
108+
view = self.window.open_file(file_name_and_col, flags)
109+
110+
regions = view.find_all(self.last_search_string, sublime.IGNORECASE)
111+
view.add_regions("search_in_project", regions, "entity.name.filename.find-in-files", "circle", sublime.DRAW_OUTLINED)
78112

79113
def goto_result(self, file_no):
80-
if file_no != -1:
114+
if file_no == -1:
115+
self.clear_markup()
116+
self.window.focus_view(self.saved_view)
117+
else:
81118
if file_no == len(self.results) - 1: # last result is "list in view"
82119
self.list_in_view()
83120
else:
84-
file_name = self.common_path.replace('\"', '') + self.results[file_no][0]
85-
view = self.window.open_file(file_name, sublime.ENCODED_POSITION)
86-
regions = view.find_all(self.last_search_string)
87-
view.add_regions("search_in_project", regions, "entity.name.filename.find-in-files", "circle", sublime.DRAW_OUTLINED)
121+
self.open_and_highlight_file(file_no)
122+
123+
def goto_relative_result(self, offset):
124+
if self.last_search_string:
125+
new_index = self.last_selected_result_index + offset
126+
if 0 <= new_index < len(self.results) - 1: # last result is "list in view"
127+
self.last_selected_result_index = new_index
128+
self.goto_result(new_index)
129+
130+
def clear_markup(self):
131+
for result in self.results[:-1]: # every result except the last one (the "list in view")
132+
file_name_and_col = self.common_path.replace('\"', '') + result[0]
133+
file_name = file_name_and_col.split(':')[0]
134+
view = self.window.find_open_file(file_name)
135+
if view: # if the view is no longer open, do nothing
136+
view.erase_regions("search_in_project")
137+
self.results = []
88138

89139
def list_in_view(self):
90140
self.results.pop()

0 commit comments

Comments
 (0)