Sublime Forum

Show Scope "command" ala TextMate

#12

Magic.

TBH, I don’t remember where. I actually think Jon posted it. Someone asked about copying the scope to the clipboard and he stepped it with his undocumented black magic.

0 Likes

#13

[quote=“C0D312”]Magic.

TBH, I don’t remember where. I actually think Jon posted it. Someone asked about copying the scope to the clipboard and he stepped it with his undocumented black magic.[/quote]

Interesting. Well if someone was going to know hidden API stuff, it would be Jon.

Oh, and yes I concede to your victory :smile:.

0 Likes

#14

syntax_name is deprecated, and only exists for backwards compatibility - use scope_name instead. They format the returned scope differently, with the version returned by scope_name() making more sense.

0 Likes

#15

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 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?

0 Likes

#16

This… is a knife…

hahaha :smile:

0 Likes

#17

[quote=“castles_made_of_sand”]

This… is a knife…

hahaha :smile:[/quote]

I’m sorry, you’re too late. This competition has ended. You can submit your entry next year though. The judge has already ruled C0D312 to be victorious. Ruling still stands.

0 Likes

#18

0 Likes

#19

[quote=“quodlibet”]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.
[/quote]

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).

[quote=“quodlibet”]
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?[/quote]

I think the victor has “won” the right to throw this on Package Control. :smile: If no one really has any interest to do it, I will do it, but I will first leave it up to COD312.

0 Likes

#20

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:

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))
0 Likes

#21

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

0 Likes

#22

[quote=“castles_made_of_sand”]

It’s a multiple choice question. Tick a) console and d) status bar etc[/quote]

I’m not saying status bar option has to be banished :smile:, just saying I never use it because it doesn’t suite my needs. It can be left as an option by the author.

0 Likes

#23

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).


[code]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)

[/code]

Here is an example command enabling all (but obviously you would probably want to limit it to what you actually use):

// 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} },

0 Likes

#24

@facelessuser

panel ftw

0 Likes

#25

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”?

0 Likes

#26

Can you elaborate?

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.

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.

0 Likes

#27

Added instant scoping toggle.

Repo is now here.
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.

0 Likes

#28

Yes, sorry.

I removed the two \ns from following line:

            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 \ns makes the output run-on when selecting multiple selections. (With “points” and “rowcol” set to false.)

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 :smile: ) But even if it can be done, from what I’ve seen in other plugins (e.g., github.com/a-sk/livecss), trying to hijack the color scheme makes a mess of things.

0 Likes

#29

I could cut it down to one, but that is as far as I would go. I can put it in a setting.

@nick’s doesn’t show extent, it just shows the scopes. These are two different things, useful for two different purposes. But that is why I made the settings and not on by default; not everyone will want them.

[quote=“quodlibet”]
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 :smile: ) But even if it can be done, from what I’ve seen in other plugins (e.g., github.com/a-sk/livecss), trying to hijack the color scheme makes a mess of things.[/quote]

Highlighting the scopes is easy. LiveCSS is a mess for different reasons; it is trying to dynamically add special color scopes to highlight CSS colors. If all you want is an underline of the entire scope’s extent, I can do that no problem. I can even use the scopes internal color (but I don’t think that is necessary). LiveCSS is something different entirely. If you want something like I described, create an issue, and I will consider it when I have time.

0 Likes

#30

[quote=“facelessuser”]

[quote=“quodlibet”]
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 :smile: ) But even if it can be done, from what I’ve seen in other plugins (e.g., github.com/a-sk/livecss), trying to hijack the color scheme makes a mess of things.[/quote]

Highlighting the scopes is easy. LiveCSS is a mess for different reasons; it is trying to dynamically add special color scopes to highlight CSS colors. If all you want is an underline of the entire scope’s extent, I can do that no problem. I can even use the scopes internal color (but I don’t think that is necessary). LiveCSS is something different entirely. If you want something like I described, create an issue, and I will consider it when I have time.[/quote]

I interpreted his statement to mean that the plugin would highlight all instances of the scope (under the cursor or on keypress or whathaveyou). Similar to how Find/Replace highlights all matches.

0 Likes

#31

[quote=“nick.”]

[quote=“quodlibet”]
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 :smile: ) But even if it can be done, from what I’ve seen in other plugins (e.g., github.com/a-sk/livecss), trying to hijack the color scheme makes a mess of things.[/quote]

Highlighting the scopes is easy. LiveCSS is a mess for different reasons; it is trying to dynamically add special color scopes to highlight CSS colors. If all you want is an underline of the entire scope’s extent, I can do that no problem. I can even use the scopes internal color (but I don’t think that is necessary). LiveCSS is something different entirely. If you want something like I described, create an issue, and I will consider it when I have time.

I interpreted his statement to mean that the plugin would highlight all instances of the scope (under the cursor or on keypress or whathaveyou). Similar to how Find/Replace highlights all matches.[/quote]

Cake

ST2 API

find_by_selector(selector)	[Regions]	Finds all regions in the file matching the given selector, returning them as a list.
0 Likes