Sublime Forum

Show Scope "command" ala TextMate

#1

Do we have a “Show Scope” command like TextMate does? basically what it does is show the current scope tree where the cursor is located, it’s useful when working with themes and grammar files (my specialty).

Any ideas?

0 Likes

#2

The show_scope_name command will do this. The key bindings for it vary between platforms, but on OS X I believe it’s the same as TextMate.

0 Likes

#3

I’ve landed on this post a few times when I need to remember this key command, here is what’s in the current Default Key Bindings in case anyone else does this:

	{ "keys": "super+alt+p"], "command": "show_scope_name" },
	{ "keys": "ctrl+shift+p"], "command": "show_scope_name" },
0 Likes

#4

I have found this method to disappear too quick, and you cannot copy it.

This is what I use. You can bind it to whatever. It just uses the beginning of the first selection and gives you the scope in a pop up that you can copy the text from.

[code]import sublime
import sublime_plugin

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

0 Likes

#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