Use entire scope as input in plugin in Sublime Text 2
Question: One of the nice things about Textmate was the ability to pipe the contents of
an entire scope into a command, like so:

![Textmate screenshot](http://i.stack.imgur.com/Yiekh.png)

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:

    
    
    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
    

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!


Answer: If you want to get text that you select, the following code snippet is an
example.

    
    
    if not region.empty():
        selectText = self.view.substr(region)
        ...
    

If you want to get text where the cursor is located, the following code
snippet is an example.

    
    
    if region.empty():
        lineRegion = self.view.line(region)
        lineText = self.view.substr(lineRegion)
        ...
    

To get more information, see <http://net.tutsplus.com/tutorials/python-
tutorials/how-to-create-a-sublime-text-2-plugin/> and
<http://www.sublimetext.com/docs/api-reference>.

