Revisit usage highlighting (#851)

This commit is contained in:
Daniel Hahler
2019-10-16 22:54:29 +02:00
committed by GitHub
parent b4b2f3ef54
commit 5880f2de93
3 changed files with 434 additions and 40 deletions

View File

@@ -52,6 +52,8 @@ for [s:key, s:val] in items(s:default_settings)
endif
endfor
let s:supports_buffer_usages = has('nvim') || exists('*prop_add')
" ------------------------------------------------------------------------
" Python initialization
@@ -304,16 +306,87 @@ function! jedi#goto_stubs() abort
endfunction
function! jedi#usages() abort
call jedi#remove_usages()
if exists('#jedi_usages#BufWinEnter')
call jedi#clear_usages()
endif
PythonJedi jedi_vim.usages()
endfunction
function! jedi#remove_usages() abort
for match in getmatches()
if stridx(match['group'], 'jediUsage') == 0
call matchdelete(match['id'])
endif
if !s:supports_buffer_usages
" Hide usages in the current window.
" Only handles the current window due to matchdelete() restrictions.
function! jedi#_hide_usages_in_win() abort
let winnr = winnr()
let matchids = getwinvar(winnr, '_jedi_usages_vim_matchids', [])
for matchid in matchids[1:]
call matchdelete(matchid)
endfor
call setwinvar(winnr, '_jedi_usages_vim_matchids', [])
" Remove the autocommands that might have triggered this function.
augroup jedi_usages
exe 'autocmd! * <buffer='.winbufnr(winnr).'>'
augroup END
unlet! b:_jedi_usages_needs_clear
endfunction
" Show usages for current window (Vim without textprops only).
function! jedi#_show_usages_in_win() abort
PythonJedi jedi_vim.highlight_usages_for_vim_win()
if !exists('#jedi_usages#TextChanged#<buffer>')
augroup jedi_usages
" Unset highlights on any changes to this buffer.
" NOTE: Neovim's API handles movement of highlights, but would only
" need to clear highlights that are changed inline.
autocmd TextChanged <buffer> call jedi#_clear_buffer_usages()
" Hide usages when the buffer is removed from the window, or when
" entering insert mode (but keep them for later).
autocmd BufWinLeave,InsertEnter <buffer> call jedi#_hide_usages_in_win()
augroup END
endif
endfunction
" Remove usages for the current buffer (and all its windows).
function! jedi#_clear_buffer_usages() abort
let bufnr = bufnr('%')
let nvim_src_ids = getbufvar(bufnr, '_jedi_usages_src_ids', [])
if !empty(nvim_src_ids)
for src_id in nvim_src_ids
" TODO: could only clear highlights below/after changed line?!
call nvim_buf_clear_highlight(bufnr, src_id, 0, -1)
endfor
else
call jedi#_hide_usages_in_win()
endif
endfunction
endif
" Remove/unset global usages.
function! jedi#clear_usages() abort
augroup jedi_usages
autocmd! BufWinEnter
autocmd! WinEnter
augroup END
if !s:supports_buffer_usages
" Vim without textprops: clear current window,
" autocommands will clean others on demand.
call jedi#_hide_usages_in_win()
" Setup autocommands to clear remaining highlights on WinEnter.
augroup jedi_usages
for b in range(1, bufnr('$'))
if getbufvar(b, '_jedi_usages_needs_clear')
exe 'autocmd WinEnter <buffer='.b.'> call jedi#_hide_usages_in_win()'
endif
endfor
augroup END
endif
PythonJedi jedi_vim.clear_usages()
endfunction
function! jedi#rename(...) abort
@@ -403,20 +476,56 @@ endfunction
" helper functions
" ------------------------------------------------------------------------
function! jedi#add_goto_window(len) abort
set lazyredraw
cclose
function! jedi#add_goto_window(for_usages, len) abort
let height = min([a:len, g:jedi#quickfix_window_height])
execute 'belowright copen '.height
set nolazyredraw
if g:jedi#use_tabs_not_buffers == 1
noremap <buffer> <CR> :call jedi#goto_window_on_enter()<CR>
" Using :cwindow allows to stay in the current window in case it is opened
" already.
let win_count = winnr('$')
execute 'belowright cwindow '.height
let qfwin_was_opened = winnr('$') > win_count
if qfwin_was_opened
if &filetype !=# 'qf'
echoerr 'jedi-vim: unexpected ft with current window, please report!'
endif
if g:jedi#use_tabs_not_buffers == 1
noremap <buffer> <CR> :call jedi#goto_window_on_enter()<CR>
endif
augroup jedi_goto_window
if a:for_usages
autocmd BufWinLeave <buffer> call jedi#clear_usages()
else
autocmd WinLeave <buffer> q " automatically leave, if an option is chosen
endif
augroup END
elseif a:for_usages && !s:supports_buffer_usages
" Init current window.
call jedi#_show_usages_in_win()
endif
augroup jedi_goto_window
au!
au WinLeave <buffer> q " automatically leave, if an option is chosen
augroup END
redraw!
if a:for_usages && !has('nvim')
if s:supports_buffer_usages
" Setup autocommand for pending highlights with Vim's textprops.
" (cannot be added to unlisted buffers)
augroup jedi_usages
autocmd! BufWinEnter * call s:usages_for_pending_buffers()
augroup END
else
" Setup global autocommand to display any usages for a window.
" Gets removed when closing the quickfix window that displays them, or
" when clearing them (e.g. on TextChanged).
augroup jedi_usages
autocmd! BufWinEnter,WinEnter * call jedi#_show_usages_in_win()
augroup END
endif
endif
endfunction
" Highlight usages for a buffer if not done so yet (Neovim only).
function! s:usages_for_pending_buffers() abort
PythonJedi jedi_vim._handle_pending_usages_for_buf()
endfunction