Home Download Buy Blog Forum Support

Displaying current function in the statusbar

Displaying current function in the statusbar

Postby nizur on Thu Jan 12, 2012 11:45 pm

Is there a way to display the current function in the statusbar? There are times where I'm jumping between parts of a file and it would be handy to know which function I'm currently working within.

If that's not currently available, how hard would it be to make a plugin that does this?
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby C0D312 on Fri Jan 13, 2012 12:22 am

it's not currently possible but can be done very easily with a plugin. If you're looking to make it yourself, here's ST2's api: http://www.sublimetext.com/docs/2/api_reference.html
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Displaying current function in the statusbar

Postby nizur on Fri Jan 13, 2012 2:49 am

I'm not that familiar with Python, but maybe this would be a good reason to get into it some more. It's going to be slow going, but I may have the plugin ready by the end of the year. :D
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby C0D312 on Fri Jan 13, 2012 2:56 am

nizur wrote:I'm not that familiar with Python, but maybe this would be a good reason to get into it some more. It's going to be slow going, but I may have the plugin ready by the end of the year. :D


Sounds good. If you need help, just ask
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Displaying current function in the statusbar

Postby nizur on Fri Jan 13, 2012 3:43 am

OK, first question. I've figured out how to display a string in the statusbar (easiest part of the plugin), but I'm completely lost on how to gather functions for the given view. I'd have to figure out the language and then scan for functions based on that language.

Would it be possible to piggyback on SublimeCodeIntel somehow? Keep in mind I'm a complete Python noob.
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby C0D312 on Fri Jan 13, 2012 3:46 am

Since you're new to Python, don't try to go big. It doesn't have to work with every language right away. What language do you use primarily?
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Displaying current function in the statusbar

Postby nizur on Fri Jan 13, 2012 3:55 am

Right now I'm mostly concerned with JavaScript and PHP.
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby C0D312 on Fri Jan 13, 2012 4:21 am

So I did some playing around and made this:
Code: Select all
import sublime, sublime_plugin

class FunctionInStatusListener(sublime_plugin.EventListener):
   def on_deactived(self,view):
      erase_status('function name')
   def on_selection_modified(self, view):
      sel = view.sel()[0]
      functionRegs = view.find_by_selector('entity.name.function.js')
      for r in reversed(functionRegs):
         if r.a < sel.a:
            view.set_status('function name', view.substr(r))
            break


It only works for javascript at the moment and is very hackish but you can play around with it. I'm going to bed
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Displaying current function in the statusbar

Postby nizur on Fri Jan 13, 2012 4:53 am

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!
nizur
 
Posts: 43
Joined: Thu Jan 12, 2012 11:40 pm

Re: Displaying current function in the statusbar

Postby tanepiper on Fri Jan 13, 2012 9:19 am

Very nice nizur - I can see me using some of this code in my own plugin now to link todo's to function names!
tanepiper
 
Posts: 68
Joined: Sun Nov 06, 2011 6:40 am

Next

Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 15 guests