Discl: Not an answer that will help you much.
A very long time ago, I wrote a ftplugin able to extract current scope and display it in the status line ; I have a trace of it somewhere in lh-cpp.
It was slow as hell, and thus I have never really used it. In order to continously display an up-to-date context in the status line, we should be able to improve its performances a little bit thank to list functions (map()
...), but I'm not very confident.
Another approach is to display the information on-demand. In that case performances aren't critical as they are regarding the statusline.
Yet, today I would rely on lh-cpp and lh-dev where I've already defined everything to extract the current scope namespace(s) + class(es) + function.
It could look like:
" autoload/lh/cpp/analyse.vim (unpublished yet)
" Function: lh#cpp#analyse#context([line]) {{{3
function! lh#cpp#analyse#context(...) abort
let line = get(a:, 1, line('.'))
let fn = lh#dev#find_function_boundaries(line)
if fn.lines[0] <= line && line <= fn.lines[1]
" This is a function
call lh#assert#value(fn).has_key('fn')
let kinds = filter(['struct', 'class', 'namespace'], 'has_key(fn.fn, v:val)')
call lh#assert#value(kinds).not().empty()
let scope = join(
\ [ get({'public': '+', 'private': '-', 'protected': '#'}, get(fn.fn, 'access', ''), '')
\ , substitute(get(fn.fn, 'typeref', ''), '^typename:', '', '')
\ , fn.fn[kinds[0]] . '::' . fn.fn.name . get(fn.fn, 'signature')
\ ], ' ')
else
" classes, structs, namespaces
let scope = lh#cpp#AnalysisLib_Class#CurrentScope(line, 'any')
endif
return scope
endfunction
" ftplugin/c/c_show_context.vim (unpublished yet)
nnoremap <silent> <buffer> <localleader>sc :echo lh#cpp#analyse#context()<cr>
Note: this almost works correclty on code such as this one: https://github.com/LucHermitte/NamedParameter/blob/master/include/named-parameters.hpp. We are now restricted by (universal!) ctags ability to understand C++(98/11/14/17/...) constructs.
Internally it extracts the current function boundaries (it launches ctags to do it correctly in C++), and the current scope (multiple and recursive calls to searchpair ()
). I use it in my :GOTOIMPL
feature.
May be we could achieve something faster nowadays thanks to clang. I don't know if some people have explored this path.
ctags
understands. You'll want to use universal ctags instead of exuberant ctags since it's actively maintained.