diff --git a/README.md b/README.md index 23914fee..30aac4da 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,31 @@ let g:lightline = { \ }, \ } ``` +### Show the nearest method/function's scope in the statusline + +```vim +function! NearestScope() abort + let info = get(b:, 'vista_cursor_info', {}) + return get(info, 'scope', '') +endfunction + +set statusline+=%{NearestScope()} +``` +#### [lightline.vim](https://github.com/itchyny/lightline.vim) + +```vim +let g:lightline = { + \... + \ 'active': { + \ 'left': [ [...], + \ [ ..., 'scope', 'method'] ] + \ }, + \ 'component_function': { + \ ... + \ 'scope': 'NearestScope', + \ }, + \ } +``` ### Commands diff --git a/autoload/vista/autocmd.vim b/autoload/vista/autocmd.vim index 1a93cd2a..8b5188a1 100644 --- a/autoload/vista/autocmd.vim +++ b/autoload/vista/autocmd.vim @@ -152,5 +152,8 @@ function! vista#autocmd#InitMOF() abort augroup VistaMOF autocmd! autocmd CursorMoved * call vista#cursor#FindNearestMethodOrFunction() + if g:vista_update_scope_on_cursor_move + autocmd CursorMoved * call vista#cursor#FindDetailWithDelay() + endif augroup END endfunction diff --git a/autoload/vista/cursor.vim b/autoload/vista/cursor.vim index 031c443c..e44d7601 100644 --- a/autoload/vista/cursor.vim +++ b/autoload/vista/cursor.vim @@ -6,6 +6,7 @@ scriptencoding utf8 let s:find_timer = -1 let s:cursor_timer = -1 +let s:find_detail_timer = -1 let s:highlight_timer = -1 let s:echo_cursor_opts = ['echo', 'floating_win', 'scroll', 'both'] @@ -28,6 +29,10 @@ function! s:StopCursorTimer() abort call s:GenericStopTimer('s:cursor_timer') endfunction +function! s:StopFindDetailTimer() abort + call s:GenericStopTimer('s:find_detail_timer') +endfunction + function! s:StopHighlightTimer() abort call s:GenericStopTimer('s:highlight_timer') endfunction @@ -203,6 +208,48 @@ function! vista#cursor#ShowDetailWithDelay() abort \ ) endfunction +" Show the detail of current tag/symbol under cursor. +function! vista#cursor#FindDetail(_timer) abort + if empty(getline('.')) + \ || !exists('g:vista') + \ || !has_key(g:vista, 'functions') + \ || !has_key(g:vista, 'source') + call setbufvar(g:vista.source.bufnr, 'vista_cursor_info', {}) + return + endif + + let found = vista#util#BinarySearch(g:vista.raw, line('.'), 'line', '') + if empty(found) + let found = {} + endif + + "echo found + let scope = vista#util#Trim(get(found, 'scope', '')) + if !empty(scope) + let scopeKind = get(found, 'scopeKind', '') + if !empty(scopeKind) + let scope = scopeKind.' '.scope + endif + let access = get(found, 'access', '') + if !empty(access) + let scope = scope.' '.access + endif + else + let scope = vista#util#Trim(get(found, 'namespace', '').' '.get(found, 'class', '')) + endif + let symbolname = vista#util#Trim(get(found, 'name', '')) + call setbufvar(g:vista.source.bufnr, 'vista_cursor_info', {'scope': scope, 'symbol': symbolname}) + +endfunction + +function! vista#cursor#FindDetailWithDelay() abort + call s:StopFindDetailTimer() + let s:find_detail_timer = timer_start( + \ g:vista_update_scope_delay, + \ function('vista#cursor#FindDetail'), + \ ) +endfunction + " This happens on calling `:Vista show` but the vista window is still invisible. function! vista#cursor#ShowTagFor(lnum) abort if !s:HasVlnum() diff --git a/plugin/vista.vim b/plugin/vista.vim index 3e43407f..2b285f65 100644 --- a/plugin/vista.vim +++ b/plugin/vista.vim @@ -18,6 +18,8 @@ let g:vista_fold_toggle_icons = get(g:, 'vista_fold_toggle_icons', ['▼', '▶' let g:vista_update_on_text_changed = get(g:, 'vista_update_on_text_changed', 0) let g:vista_update_on_text_changed_delay = get(g:, 'vista_update_on_text_changed_delay', 500) let g:vista_echo_cursor = get(g:, 'vista_echo_cursor', 1) +let g:vista_update_scope_on_cursor_move = get(g:, 'vista_update_scope_on_cursor_move', 1) +let g:vista_update_scope_delay = get(g:, 'vista_update_scope_delay', 300) let g:vista_no_mappings = get(g:, 'vista_no_mappings', 0) let g:vista_stay_on_open = get(g:, 'vista_stay_on_open', 1) let g:vista_close_on_jump = get(g:, 'vista_close_on_jump', 0)