VIM: Show PHP function / class on command line?

Is there a way to show the current PHP function or class name on the VIM command line? I found a plugin for displaying C function names in the status bar , but it does not work for PHP, and in any case, I prefer the command line to be used to save valuable vertical lines.

Thanks.

+4
source share
2 answers

EDIT

When searching for something completely unrelated to the TagList tag, I just found these two functions:

Tlist_Get_Tagname_By_Line() Tlist_Get_Tag_Prototype_By_Line() 

Adding this to my status bar works great:

 %{Tlist_Get_Tagname_By_Line()} 

Also, did you read the Vim Wiki? He has a bunch of tips , trying to satisfy the same need. There is also this (untested) plugin .

Endedit

If you have little vertical space, maybe you do not mind using a little horizontal space?

TagList and TagBar show a vertical list of tags used in the current buffer (and other open documents in the TagList tag) that you can use to navigate your code.

However, I am not a fan of all types of information (list of files, VCS status, list of tags, list of buffers / tabs ...) displayed at any time: the ability to read the name of the function you are in is only useful when you really need to know it, otherwise it will be a mess. Vim [{ enough for me, followed by <Co> .

+3
source

I don’t know anything about PHP, and I don’t try to step on all fingers, but looking at some PHP code, I came up with this function, which, it seems to me, uses a simpler approach than the plugins that were mentioned.

My statement is that PHP functions are declared using function MyFunction(){} syntax function MyFunction(){} and classes declared with class MyClass{} (possibly preceded by public ). The following function searches back from the cursor position to find the last declared class or function (and sets the startline ). Then we look for the first { and find a match } by setting endline . If the start line of the cursor is between startline and endline , we return the text startline . Otherwise, we return an empty string.

 function! PHP_Cursor_Position() let pos = getpos(".") let curline = pos[1] let win = winsaveview() let decl = "" let startline = search('^\s*\(public\)\=\s*\(function\|class\)\s*\w\+','cbW') call search('{','cW') sil exe "normal %" let endline = line(".") if curline >= startline && curline <= endline let decl = getline(startline) endif call cursor(pos) call winrestview(win) return decl endfunction set statusline=%{PHP_Cursor_Position()} 

Since it does not return anything when it is outside the function / class, it does not display the error code in the status bar, as the proposed plugin does.

Of course, I can quite simplify the problem, and in this case ignore me, but this seems like a reasonable approach.

+2
source

Source: https://habr.com/ru/post/1398498/


All Articles