Sublime Forum

Show Scope "command" ala TextMate

#5

Even better:

[code]import sublime, sublime_plugin

class ScopeToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(self.view.syntax_name(self.view.sel()[0].b))[/code]

Just copy the scope to the clipboard.

0 Likes

#6

Even even better :smile:. Sometimes you just want to view it and not have to paste it to see it, so do both.

[code]import sublime
import sublime_plugin

class GetSelectionScopeCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()
if len(sel) > 0:
msg = self.view.scope_name(sel[0].begin())
sublime.error_message(msg)
sublime.set_clipboard(msg)[/code]

0 Likes

#7

Even MORE betterer:

[code]import sublime, sublime_plugin

class ScopeToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
scope = self.view.syntax_name(self.view.sel()[0].b)
if sublime.ok_cancel_dialog(‘Scope:\n’+scope, ‘Copy’):
sublime.set_clipboard(scope)[/code]

Click ‘copy’ to copy the scope to clipboard :smile:

I WIN!

0 Likes

#8

[quote=“C0D312”]Even MORE betterer:

[code]import sublime, sublime_plugin

class ScopeToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
scope = self.view.syntax_name(self.view.sel()[0].b)
if sublime.ok_cancel_dialog(‘Scope:\n’+scope, ‘Copy’):
sublime.set_clipboard(scope)[/code]

Click ‘copy’ to copy the scope to clipboard :smile:

I WIN![/quote]

Almost, you got get the code right to win :wink: . scope_name not syntax_name. But well done. I would also add something to that asks if you want to copy. On windows the ‘Copy’ button does not change, it just says ‘OK’, but that one is easy to miss if you are not using windows.

[code]import sublime, sublime_plugin

class ScopeToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
scope = self.view.scope_name(self.view.sel()[0].b)
if sublime.ok_cancel_dialog(‘Scope:\n’ + scope + ‘\n\nCopy to clipboard?’, ‘Copy’):
sublime.set_clipboard(scope)[/code]

0 Likes

#9

Weird… Why does syntax_name work, too?

Also, I’m still claiming this as a victory.

0 Likes

#10

I was just about to post. It does work…remove foot from mouth :blush: . They both work. :smile:

0 Likes

#11

Where did you get that from? It isn’t listed in the API.

0 Likes

#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