Sublime Forum

Displaying current function in the statusbar

#1

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?

1 Like

#2

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

0 Likes

#3

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. :smiley:

0 Likes

#4

Sounds good. If you need help, just ask

0 Likes

#5

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.

0 Likes

#6

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?

0 Likes

#7

Right now I’m mostly concerned with JavaScript and PHP.

0 Likes

#8

So I did some playing around and made this: [code]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®)
break[/code]

It only works for javascript at the moment and is very hackish but you can play around with it. I’m going to bed

0 Likes

#9

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

[code]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

[/code]

That seems to work for JS, PHP and Python for me. It’s rough, but it does the job well enough for me!

0 Likes

#10

Very nice nizur - I can see me using some of this code in my own plugin now to link todo’s to function names!

0 Likes

#11

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.

0 Likes

#12

Thanks for making this! I’ve been wanting this ever since switching to ST2 last summer.

0 Likes

#13

docs.python.org/library/bisect.html

0 Likes

#14

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


	{
		"button": "button1", "count": 1,
		"press_command": "drag_select",
		"command": "status_bar_function"
	}
]

…\Sublime Text 2\Packages\User\status_bar_function.py

[code]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®)
return
view.erase_status(‘function’)[/code]

0 Likes

#15

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++

0 Likes

#16

github.com/SublimeText/CurrentFunction

Feel free to improve it.

0 Likes

#17

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

Regards,

Rob…

0 Likes

#18

Good. Let’s join forces =) using your package.

0 Likes

#19

:smile:

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

Regards,

Rob…

0 Likes

#20

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

[quote=“nizur”]Very nice and very much appreciated! I took what you started and changed it to:

[code]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

[/code]

That seems to work for JS, PHP and Python for me. It’s rough, but it does the job well enough for me![/quote]

0 Likes