Last active
March 1, 2023 09:06
-
-
Save gwenzek/a16d40ba36268ac84716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
class FindInScope(sublime_plugin.TextCommand): | |
def run(self, edit, pattern='', scope='-comment'): | |
view = self.view | |
sel = view.sel() | |
if len(sel) > 0 and pattern == '': | |
pattern = view.substr(sel[0]) | |
if pattern == '': | |
pattern = self.pattern | |
self.pattern = pattern | |
self.results = list(filter(lambda region: view.match_selector(region.begin(), scope), view.find_all(pattern))) | |
locations = list(map(lambda region: (region, view.rowcol(region.begin())), self.results)) | |
print('found %d results' % len(self.results)) | |
navigate_to_symbol(view, pattern, locations) | |
def navigate_to_symbol(view, symbol, locations): | |
def select_entry(window, locations, idx): | |
region, rowcol = locations[idx] | |
view.sel().clear() | |
view.sel().add(region) | |
view.show(region) | |
def format_location(l): | |
region, rowcol = l | |
row, col = rowcol | |
return str(row + 1) + ': ... ' + view.substr(sublime.Region(region.begin() - 10, region.end() + 10)) + ' ...' | |
if len(locations) == 0: | |
sublime.status_message("Unable to find " + symbol) | |
elif len(locations) == 1: | |
select_entry(view.window(), locations, 0) | |
else: | |
window = view.window() | |
window.show_quick_panel( | |
items = [format_location(l) for l in locations], | |
on_select = lambda x: select_entry(window, locations, x), | |
flags = sublime.KEEP_OPEN_ON_FOCUS_LOST) |
Am I correct that this doesn't work if scopes are mixed, e.g., you have
wordA inline_comment wordB
you wouldn't be able to search wordA wordB
with scope
set to -comment
(I've tried and it didn't work, but not sure if it's possible to tweak something to make it work)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fabeit This is working as implemented :-)
There is no API to create a regular search result page so I used the dropdown API.
You can use other search options by modifing the call to "find_all" and using the flags described in the documentation.
https://www.sublimetext.com/docs/3/api_reference.html
The best way would be to chose those flags based on an extra parameters passed to the
run
method.