Home Download Buy Blog Forum Support

Displaying current function in the statusbar

Re: Displaying current function in the statusbar

Postby nizur on Fri Jan 13, 2012 6:35 pm

Feel free to use any of the code, tanepiper, since I just built off of C0D312's example. I'm a complete Python noob too, so the code could be better I'm sure.
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby davermont on Sun Jan 15, 2012 11:32 am

Thanks for making this! I've been wanting this ever since switching to ST2 last summer.
davermont
 
Posts: 2
Joined: Thu Sep 29, 2011 6:33 am

Re: Displaying current function in the statusbar

Postby sublimator on Sun Jan 15, 2012 11:53 am

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: Displaying current function in the statusbar

Postby tito on Sun Jan 15, 2012 8:47 pm

on_selection_modified is a listener you should try to avoid.

Add a print and look into the console, you will notice this listener is dispatched hundred of times when selecting big portions of text making the editor to crawl.

I suggest you listen the click event. Inspired on your code I use this:

...\Sublime Text 2\Packages\User\Default.sublime-mousemap
Code: Select all
[
   {
      "button": "button1", "count": 1,
      "press_command": "drag_select",
      "command": "status_bar_function"
   }
]


...\Sublime Text 2\Packages\User\status_bar_function.py
Code: Select all
import sublime_plugin

class StatusBarFunctionCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      view = self.view
      region = view.sel()[0]
      functionRegs = view.find_by_selector('entity.name.function')
      for r in reversed(functionRegs):
         if r.a < region.a:
            view.set_status('function', view.substr(r))
            return
      view.erase_status('function')
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 877
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Displaying current function in the statusbar

Postby cgibby on Fri Feb 24, 2012 8:20 pm

I love this -- you should package it up as a full-blown plug-in! It's one of the things I missed most after moving over from Notepad++
cgibby
 
Posts: 5
Joined: Thu Feb 02, 2012 3:54 pm

Re: Displaying current function in the statusbar

Postby tito on Sat Feb 25, 2012 5:10 pm

Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 877
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Displaying current function in the statusbar

Postby akrabat on Mon Feb 27, 2012 9:49 am

Ironically, I created https://github.com/akrabat/SublimeFunctionNameDisplay Saturday too...

Regards,

Rob...
akrabat
 
Posts: 18
Joined: Wed Dec 21, 2011 3:41 pm

Re: Displaying current function in the statusbar

Postby tito on Mon Feb 27, 2012 11:19 am

Good. Let's join forces =) using your package.
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 877
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Displaying current function in the statusbar

Postby akrabat on Mon Feb 27, 2012 12:46 pm

tito wrote:Good. Let's join forces =) using your package.

:)

Thanks for the updates. I'm learning more about ST2 plugin development just by looking at your code!

Regards,

Rob..
akrabat
 
Posts: 18
Joined: Wed Dec 21, 2011 3:41 pm

Re: Displaying current function in the statusbar

Postby wprater on Wed Mar 21, 2012 8:04 am

Nice, I'd like to expand this to css selectors as well. Especially when in scss files, the nesting can get out of control!


nizur wrote:Very nice and very much appreciated! I took what you started and changed it to:

Code: Select all
import sublime, sublime_plugin


class FunctionInStatusListener(sublime_plugin.EventListener):
    def on_deactived(self, view):
        view.erase_status('function name')

    def on_close(self, view):
        view.erase_status('function name')

    def on_activated(self, view):
        cf = self.get_current_function(view)
        if cf is None:
            view.erase_status('function name')
        else:
            view.set_status('function name', 'Function: ' + cf)

    def on_selection_modified(self, view):
        cf = self.get_current_function(view)
        if cf is None:
            view.erase_status('function name')
        else:
            view.set_status('function name', 'Function: ' + cf)

    def get_current_function(self, view):
        sel = view.sel()[0]
        functionRegs = view.find_by_selector('entity.name.function')
        cf = None
        for r in reversed(functionRegs):
            if r.a < sel.a:
                cf = view.substr(r)
                break

        return cf


That seems to work for JS, PHP and Python for me. It's rough, but it does the job well enough for me!
wprater
 
Posts: 8
Joined: Wed Feb 22, 2012 11:54 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: Yahoo [Bot] and 23 guests