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.
source share