Skip to content

Instantly share code, notes, and snippets.

@gwenzek
Last active March 1, 2023 09:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwenzek/a16d40ba36268ac84716 to your computer and use it in GitHub Desktop.
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)
@gwenzek
Copy link
Author

gwenzek commented Mar 12, 2016

Plugin for Sublime Text that finds the given pattern in a specific scope.
For using it add something like:

{ "command": "find_in_scope", "keys": ["ctrl+e"] },

in your key binding file. By default it will use the current selection to search, and will search outside of comments.

@gwenzek
Copy link
Author

gwenzek commented Mar 12, 2016

The navigate_to_symbol function is adapted from the same function taken from default Package symbol.py shipped with Sublime Text 3

@prorev
Copy link

prorev commented Apr 23, 2016

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'

@gwenzek
Copy link
Author

gwenzek commented Apr 18, 2017

@PROSTI: that mean you're trying to search without telling the code what it should search.

@trespitt
Copy link

does this work for sublime 3?

@fabeit
Copy link

fabeit commented Jan 9, 2020

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?

@gwenzek
Copy link
Author

gwenzek commented Feb 27, 2020

@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.

@eugenesvk
Copy link

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