Sublime Forum

Use entire scope as input in plugin

#1

One of the nice things about Textmate was the ability to pipe the contents of an entire scope into a command, like so:

You could then specify the scope to be used, such as “meta.class.python” or whatever.

I’m trying to write a small plugin that will pipe the entire current scope in as the input for the plugin (for example (not exactly what I’m trying to do, but close), a function that lets you comment out an entire Python class without selecting it all)

Using the current selection(s) as input is quite easy:

[code]import sublime, sublime_plugin
import re

class DoStuffWithSelection(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
changed = region # Do something to the selection
self.view.replace(edit, region, changed) # Replace the selection[/code]

I’ve scoured the Sublime Text plugin API for some way to do something like for region in self.view.scope(), but without success.

Is there a way, then, to use the contents of the current scope under the cursor as input for a plugin function? Or, even better, a way to use the entire scope if there isn’t a selection, but use the selection if there is one.

Thanks!

0 Likes