Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created December 14, 2020 07:59
Show Gist options
  • Save OdatNurd/db26854a4ddb82078877198c8d9c694c to your computer and use it in GitHub Desktop.
Save OdatNurd/db26854a4ddb82078877198c8d9c694c to your computer and use it in GitHub Desktop.
Plugin example: How to work with Points and Regions in Sublime Text
import sublime
import sublime_plugin
class RegionExampleCommand(sublime_plugin.TextCommand):
def run(self, edit, example):
if example == "1": region = sublime.Region(0, 10)
elif example == "2": region = sublime.Region(10, 20)
elif example == "3": region = sublime.Region(5)
elif example == "4": region = self.view.word(5)
elif example == "5": region = self.view.line(5)
elif example == "6": region = self.view.full_line(5)
elif example == "7": region = self.view.lines(sublime.Region(10, 20))
elif example == "8": region = sublime.Region(self.view.text_point(1, 7))
elif example == "9": region = self.view.find_all("example")
elif example == "10": region = self.view.find_by_selector("comment")
elif example == "11": region = [s for s in self.view.sel()]
# The API method we use below expects a list of regions, but not all of
# the examples above provide one.
if not isinstance(region, list):
region = [region]
self.view.add_regions("test", region, "region.orangish", "bookmark",
flags=sublime.DRAW_EMPTY | sublime.DRAW_NO_FILL)
# content = [self.view.substr(r) for r in region]
# sublime.message_dialog("\n".join(content))
@OdatNurd
Copy link
Author

This is the example plugin from Lesson #18 in Plugin 101 for Sublime Text, [https://youtu.be/Ohb_pYwO5wE](How do plugins access file content in Sublime?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment