Hi - i'm using this https://github.com/tyru/current-func-info.vim to get the current function/method name in my statusline. Would it be possible to achieve something similar with coc?
Sure, you can just query for document symbols and find the nearest function symbol, but it doesn't have params information, need to extract from the line contains that function.
hello, @robertocarta , I have implemented this feature in vista.vim, both the ctags and LSP symbols are supported. The result has not been polished and only shows the original text in the LSP symbol, which could be refined later.
To play with for now:
:Vista coc to invoke vista, then back to the source buffer, the nearest method/function should in your statusline.
You can show the nearest method/function in the statusline as follows:
function! NearestMethodOrFunction() abort
return get(b:, 'vista_nearest_method_or_function', '')
endfunction
set statusline+=%{NearestMethodOrFunction()}
Also refer to https://github.com/liuchengxu/eleline.vim/pull/18.
In case anyone uses lightline, this is how you can use the above (modified example of README in this repo):
let g:lightline = {
\ 'active': {
\ 'left': [ [ 'mode', 'paste' ],
\ [ 'cocstatus', 'readonly', 'filename', 'modified', 'vistanearest' ] ]
\ },
\ 'component_function': {
\ 'cocstatus': 'coc#status',
\ 'vistanearest': 'NearestMethodOrFunction'
\ }
\}
@liuchengxu Is there any way to "silently" trigger vista.vim ? Because it seems like I have to open and close it in order for this to work. If I haven't missed something really obvious that is....
@robertocarta No way at present. That feature is also what I want, you can file an issue in vista.vim and I'll work on it when I get some time.
@liuchengxu Thanks. I'll take the discussion to your repo.
Inspired by @liuchengxu 's work on Vista I started to look into coc's code to see how to implement the feature natively in coc.
Like @chemzqm said, getting the document symbols (navtree) is enough since it has all the symbols for the current document including line numbers and kinds. So we can easily find which function the cursor is currently in (using something similar to the binary search @liuchengxu did in Vista.vim).
@chemzqm I'm not sure what the best way to implement this is.
I've created a new provider interface (CurrentFunctionProvider), that has a manager implementing the provideCurrentFunction from the provider interface.
That manager would call the provideDocumentSymbols from the DocumentSymbolManager.
I'm really not sure I'm going in the right direction, do you think that is the best way to implement this or can you suggest an alternative?
Thanks again your hard work on coc!
@pyrho No need to add new provider I think, add a function like currentFunctionSymbol to handler and add a CocAction branch in plugin.ts to call it should be fine.
I do prefer to use tools like ctags for this instead of language server since server need time to start and parse the document, but you can always parse code by use ctags.
Most helpful comment
@pyrho No need to add new provider I think, add a function like
currentFunctionSymbolto handler and add aCocActionbranch inplugin.tsto call it should be fine.I do prefer to use tools like ctags for this instead of language server since server need time to start and parse the document, but you can always parse code by use ctags.