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

Commit e897fd7

Browse files
Merge branch 'master' into new-option-show-list
2 parents 6787ee2 + 8e82a3d commit e897fd7

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

@@ -70,24 +84,60 @@ def perform_search(self, text):
7084
self.list_in_view()
7185
else:
7286
self.results.append("``` List results in view ```")
73-
self.window.show_quick_panel(self.results, self.goto_result)
87+
flags = 0
88+
self.window.show_quick_panel(
89+
self.results,
90+
self.goto_result,
91+
flags,
92+
self.last_selected_result_index,
93+
self.on_highlighted)
7494
else:
7595
self.results = []
7696
sublime.message_dialog('No results')
7797
except Exception as e:
7898
self.results = []
7999
sublime.error_message("%s running search engine %s:"%(e.__class__.__name__,self.engine_name) + "\n" + str(e))
80100

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

82116
def goto_result(self, file_no):
83-
if file_no != -1:
117+
if file_no == -1:
118+
self.clear_markup()
119+
self.window.focus_view(self.saved_view)
120+
else:
84121
if file_no == len(self.results) - 1: # last result is "list in view"
85122
self.list_in_view()
86123
else:
87-
file_name = self.common_path.replace('\"', '') + self.results[file_no][0]
88-
view = self.window.open_file(file_name, sublime.ENCODED_POSITION)
89-
regions = view.find_all(self.last_search_string)
90-
view.add_regions("search_in_project", regions, "entity.name.filename.find-in-files", "circle", sublime.DRAW_OUTLINED)
124+
self.open_and_highlight_file(file_no)
125+
126+
def goto_relative_result(self, offset):
127+
if self.last_search_string:
128+
new_index = self.last_selected_result_index + offset
129+
if 0 <= new_index < len(self.results) - 1: # last result is "list in view"
130+
self.last_selected_result_index = new_index
131+
self.goto_result(new_index)
132+
133+
def clear_markup(self):
134+
for result in self.results[:-1]: # every result except the last one (the "list in view")
135+
file_name_and_col = self.common_path.replace('\"', '') + result[0]
136+
file_name = file_name_and_col.split(':')[0]
137+
view = self.window.find_open_file(file_name)
138+
if view: # if the view is no longer open, do nothing
139+
view.erase_regions("search_in_project")
140+
self.results = []
91141

92142
def list_in_view(self):
93143
self.results.pop()

0 commit comments

Comments
 (0)