Home Download Buy Blog Forum Support

Show Scope "command" ala TextMate

Re: Show Scope "command" ala TextMate

Postby facelessuser on Wed Mar 28, 2012 2:13 pm

quodlibet wrote:I use the code provided by adzenith here: viewtopic.php?f=3&t=1646&p=7545&hilit=scope+clipboard#p7545

YMMV, but I find this plugin less intrusive as it prints the scope to the statusbar (allowing for quick inspection), but also to the console (which allows you to build lists as well as selective copy & paste). It's also faster if you want to inspect several sections, as you don't have to deal with the popup every time.

I still find the status bar not a good option. Doesn't show long enough. But I do like the idea of logging it to the console. What I would probably do is put it in an output panel that pops up at the bottom. So you could do multi-select; which would be nice, the info would be separate from all the other console noise, it is at the bottom and fairly low profile, and you could even have it auto-popup (conditionally with an additional toggle scope panel command if you find it too intrusive).

quodlibet wrote:I general, I find this functionality to be super useful. Does anyone maybe want to round this out with a couple of options (e.g., disabling the pop-up) and throw it in Package Control?


I think the victor has "won" the right to throw this on Package Control. :) If no one really has any interest to do it, I will do it, but I will first leave it up to COD312.
facelessuser
 
Posts: 1543
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby nick. on Wed Mar 28, 2012 2:25 pm

This is a related plugin I wrote. It displays the scope in the status bar as you move the cursor around. Just thought I'd share it:
Code: Select all
import sublime, sublime_plugin

class PrintScopeNameCommand(sublime_plugin.EventListener):
   def on_selection_modified(self, view):
      sublime.status_message(view.scope_name(view.sel()[0].a))
Last edited by nick. on Wed Mar 28, 2012 2:27 pm, edited 2 times in total.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Show Scope "command" ala TextMate

Postby sublimator on Wed Mar 28, 2012 2:25 pm

facelessuser wrote:I still find the status bar not a good option


It's a multiple choice question. Tick a) console and d) status bar etc
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Show Scope "command" ala TextMate

Postby facelessuser on Wed Mar 28, 2012 2:37 pm

castles_made_of_sand wrote:
facelessuser wrote:I still find the status bar not a good option


It's a multiple choice question. Tick a) console and d) status bar etc


I'm not saying status bar option has to be banished :), just saying I never use it because it doesn't suite my needs. It can be left as an option by the author.
facelessuser
 
Posts: 1543
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby facelessuser on Wed Mar 28, 2012 5:00 pm

I couldn't help myself:

You can configure the command how you like via arguments. It accepts multi-select, and can select whether you want it to log to the statusbar(only shows first), console, clipboard (scopes only for each selection), and/or auto-popup panel.

You can also have it give you the extent of the scope (pts and/or row/col).

Screenshot.png
Screenshot.png (9.93 KiB) Viewed 803 times


Code: Select all
import sublime
import sublime_plugin


class GetSelectionScopeCommand(sublime_plugin.TextCommand):
    def get_scope(self, pt):
        if self.rowcol or self.points:
            pts = self.view.extract_scope(pt)
            if self.points:
                self.scope_bfr.append("%-25s (%d, %d)\n" % ("Scope Extent pts:", pts.begin(), pts.end()))
            if self.rowcol:
                row1, col1 = self.view.rowcol(pts.begin())
                row2, col2 = self.view.rowcol(pts.end())
                self.scope_bfr.append(
                    "%-25s (line: %d char: %d, line: %d char: %d)\n" % ("Scope Extent row/col:", row1 + 1, col1 + 1, row2 + 1, col2 + 1)
                )
        scope = self.view.scope_name(pt)

        if self.clipboard:
            self.clips.append(scope)

        if self.first and self.show_statusbar:
            self.status = scope
            self.first = False

        self.scope_bfr.append("%-25s %s\n\n" % ("Scope:", self.view.scope_name(pt)))

    def run(self, edit, show_statusbar=False, show_panel=False, clipboard=False, rowcol=False, points=False, multiselect=False, console_log=False):
        self.window = self.view.window()
        view = self.window.get_output_panel('scope_viewer')
        self.scope_bfr = []
        self.clips = []
        self.status = ""
        self.show_statusbar = show_statusbar
        self.show_panel = show_panel
        self.clipboard = clipboard
        self.rowcol = rowcol
        self.points = points
        self.first = True

        # Get scope info for each selection wanted
        if len(self.view.sel()):
            if multiselect:
                for sel in self.view.sel():
                    self.get_scope(sel.b)
            else:
                self.get_scope(self.view.sel()[0].b)

        # Copy scopes to clipboard
        if clipboard:
            sublime.set_clipboard('\n'.join(self.clips))

        # Display in status bar
        if show_statusbar:
            sublime.status_message(self.status)

        # Show panel
        if show_panel:
            self.window.run_command("show_panel", {"panel": "output.scope_viewer"})
            view.insert(edit, 0, ''.join(self.scope_bfr))

        if console_log:
            print ''.join(self.scope_bfr)


Here is an example command enabling all (but obviously you would probably want to limit it to what you actually use):
Code: Select all
    // Get Selection Scope
    {
        "caption": "Get Selection Scope: Show Scope Under Cursor",
        "command": "get_selection_scope",
        "args": {"show_panel": true, "clipboard": true, "points": true, "rowcol": true, "multiselect": true, "console_log": true, "show_statusbar": true}
    },
facelessuser
 
Posts: 1543
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby sublimator on Thu Mar 29, 2012 3:30 am

@facelessuser

panel ftw
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Show Scope "command" ala TextMate

Postby quodlibet on Thu Mar 29, 2012 9:52 am

castles_made_of_sand wrote:panel ftw


What he said.

I had to throw out a couple of annoying carriage returns from the output, but other than that, this is a great plugin. (The removal of the carriage returns messes up the output on multi-select, but I don't think I'll mind.)

I also took a look at nick's plugin, which I think I'll use if I can toggle on and off. I can write the toggling bit, but I failed to include it under facelessuser's code. Help?

Also, what is "Extent pts"?
quodlibet
 
Posts: 498
Joined: Sat Jun 18, 2011 5:53 pm

Re: Show Scope "command" ala TextMate

Postby facelessuser on Thu Mar 29, 2012 1:51 pm

quodlibet wrote:I had to throw out a couple of annoying carriage returns from the output, but other than that, this is a great plugin. (The removal of the carriage returns messes up the output on multi-select, but I don't think I'll mind.)

Can you elaborate?

quodlibet wrote:I also took a look at nick's plugin, which I think I'll use if I can toggle on and off. I can write the toggling bit, but I failed to include it under facelessuser's code. Help?

Yeah, toggling such a feature is the only way I would use @nick's code. I wouldn't want all of that work going on on every cursor move unless I specifically wanted to see the scope. I could add this no problem.

quodlibet wrote:Also, what is "Extent pts"?

How far does the current scope extend. Like if you were in a string, it would give you the start of the string and the end of the string. It can be useful if you want to see what is included in that particular scope.
facelessuser
 
Posts: 1543
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby facelessuser on Thu Mar 29, 2012 8:13 pm

Added instant scoping toggle.

Repo is now here.
https://github.com/facelessuser/ScopeHunter

I will request it be added to Package Control. If you have issues or suggestions, please create an issue on github.
facelessuser
 
Posts: 1543
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby quodlibet on Thu Mar 29, 2012 8:17 pm

facelessuser wrote:Can you elaborate?


Yes, sorry.

I removed the two `\n`s from following line:

Code: Select all
            self.scope_bfr.append("%-25s %s\n\n" % ("Scope:", self.view.scope_name(pt)))


They were spacing out the output in the console, which I didn't like.

Removing both `\n`s makes the output run-on when selecting multiple selections. (With "points" and "rowcol" set to false.)


facelessuser wrote:I could add this no problem.


Thanks!

I think the "points" and "rowcol" features are too arithmetical for me. @nick's plugin's behavior seems preferable for identifying extent.

In an ideal universe, highlighting the scope would probably be best way. (I tried to avoid mentioning this on the off-chance that you would find it interesting :) ) But even if it can be done, from what I've seen in other plugins (e.g., https://github.com/a-sk/livecss), trying to hijack the color scheme makes a mess of things.
quodlibet
 
Posts: 498
Joined: Sat Jun 18, 2011 5:53 pm

PreviousNext

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], RyanJ, Webdoc, Yahoo [Bot] and 26 guests