-
-
Save gwenzek/a16d40ba36268ac84716 to your computer and use it in GitHub Desktop.
| 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) |
The navigate_to_symbol function is adapted from the same function taken from default Package symbol.py shipped with Sublime Text 3
Here is what I got:
Traceback (most recent call last):
File "/opt/sublime_text/sublime_plugin.py", line 574, in run_
return self.run(edit)
File "/home/dj/.config/sublime-text-3/Packages/User/find_in_scope.py", line 13, in run
pattern = self.pattern
AttributeError: 'FindInScope' object has no attribute 'pattern'
@PROSTI: that mean you're trying to search without telling the code what it should search.
does this work for sublime 3?
not really sure if this is working correctly. I get a dropdown menu with all the results. Is this correct? is it possible to use any of the search options such as case sensitive, whole word, etc?
@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.
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)
Plugin for Sublime Text that finds the given pattern in a specific scope.
For using it add something like:
in your key binding file. By default it will use the current selection to search, and will search outside of comments.