Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created May 19, 2021 05:55
Show Gist options
  • Select an option

  • Save OdatNurd/d76a0698878034ac02fde951edc09b66 to your computer and use it in GitHub Desktop.

Select an option

Save OdatNurd/d76a0698878034ac02fde951edc09b66 to your computer and use it in GitHub Desktop.
Sample of using regions in Sublime Text plugins
import sublime
import sublime_plugin
_types = {
"IDII": 7,
"SEDOL": 7,
"DATE": 7,
"TYPE": 1
}
class MarkExtelRegionCommand(sublime_plugin.TextCommand):
last_entry = ''
def run(self, edit, span=None):
v = self.view
if span is None:
panel = v.window().show_input_panel("Span Width:", self.last_entry, self.set_span, None, None)
return panel.run_command('select_all')
if span == 0:
return v.erase_regions('extel')
regions = [sublime.Region(sel.b, sel.b + span) for sel in v.sel()]
for idx,region in enumerate(regions):
text = v.substr(region)
if '\n' in text:
regions[idx] = sublime.Region(region.a, region.b + text.count('\n'))
v.add_regions('extel', regions, 'region.redish', flags=sublime.DRAW_NO_FILL)
v.sel().clear()
for region in regions:
v.sel().add(sublime.Region(region.b))
def set_span(self, text):
self.last_entry = text
try:
value = _types.get(text.upper(), None) or int(text)
self.view.run_command('mark_extel_region', {"span": value})
except ValueError:
self.view.window().status_message('Invalid region size "%s"' % text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment