Home Download Buy Blog Forum Support

Show Scope "command" ala TextMate

Show Scope "command" ala TextMate

Postby kuroir on Sun Sep 11, 2011 10:56 am

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?
kuroir
 
Posts: 71
Joined: Thu Sep 01, 2011 11:28 pm
Location: Mexico

Re: Show Scope "command" ala TextMate

Postby jps on Mon Sep 12, 2011 12:12 pm

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.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Show Scope "command" ala TextMate

Postby natebeaty on Tue Mar 27, 2012 8:30 pm

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:

Code: Select all
   { "keys": ["super+alt+p"], "command": "show_scope_name" },
   { "keys": ["ctrl+shift+p"], "command": "show_scope_name" },
natebeaty
 
Posts: 121
Joined: Wed Apr 06, 2011 8:28 pm
Location: Chicago, IL

Re: Show Scope "command" ala TextMate

Postby facelessuser on Tue Mar 27, 2012 8:37 pm

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: Select all
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()))
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby C0D312 on Tue Mar 27, 2012 8:43 pm

Even better:
Code: Select all
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))


Just copy the scope to the clipboard.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Show Scope "command" ala TextMate

Postby facelessuser on Tue Mar 27, 2012 8:48 pm

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

Code: Select all
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)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby C0D312 on Tue Mar 27, 2012 8:57 pm

Even MORE betterer:
Code: Select all
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)


Click 'copy' to copy the scope to clipboard :)

I WIN!
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Show Scope "command" ala TextMate

Postby facelessuser on Tue Mar 27, 2012 9:08 pm

C0D312 wrote:Even MORE betterer:
Code: Select all
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)


Click 'copy' to copy the scope to clipboard :)

I WIN!


Almost, you got get the code right to win ;) . 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: Select all
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)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Show Scope "command" ala TextMate

Postby C0D312 on Tue Mar 27, 2012 9:14 pm

Weird... Why does syntax_name work, too?

Also, I'm still claiming this as a victory.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Show Scope "command" ala TextMate

Postby facelessuser on Tue Mar 27, 2012 9:16 pm

C0D312 wrote:Weird... Why does syntax_name work, too?


I was just about to post. It does work...remove foot from mouth :oops: . They both work. :)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 32 guests