public
anonymous / AddFind.py
Created

  • Download Gist
AddFind.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import sublime, sublime_plugin
 
class AddFindCommand(sublime_plugin.TextCommand):
def run(self, edit):
self._orig_sel = list(self.view.sel())
self.view.window().show_input_panel('find', '',
self.on_done, self.on_change, self.on_cancel)
 
def on_done(self, s):
self.view.erase_regions('addfind')
self.expand_sel(s)
 
def on_change(self, s):
if s != '':
#print(repr(s))
self.show_regions(s)
 
def on_cancel(self):
pass
 
def show_regions(self, s):
regs = []
for r in self._orig_sel:
new_begin = r.end()
new_end = self.view.find(s, r.end(), sublime.LITERAL).end()
if not r.empty():
new_begin -= 1
regs.append(sublime.Region(new_begin, new_end))
 
for r in regs:
self.view.add_regions('addfind', regs, 'comment', flags=sublime.DRAW_OUTLINED)
 
def expand_sel(self, s):
regs = []
for r in self._orig_sel:
new_begin = r.end()
new_end = self.view.find(s, r.end(), sublime.LITERAL).end()
if not r.empty():
new_begin -= 1
regs.append(sublime.Region(new_begin, new_end))
 
for r in regs:
self.view.sel().add(r)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.