Close preview on escape: only map <Esc> while preview open

This only applies to floating preview windows on Neovim.

Handles the following way of closing the floating preview:

- <Esc>
- Moving cursor
- 2<C-W>c (where 2 is the floating window's window number)
- <C-W><C-W> to move into and out of the floating window

Closes #786.
This commit is contained in:
Andy Stewart
2021-06-03 12:23:58 +01:00
parent 2571c8f520
commit 68a8cb300c

View File

@@ -447,8 +447,14 @@ function! s:open_hunk_preview_window()
" Assumes cursor is in original window.
autocmd CursorMoved <buffer> ++once call gitgutter#hunk#close_hunk_preview_window()
if g:gitgutter_close_preview_on_escape
" Map <Esc> to close the floating preview.
nnoremap <buffer> <silent> <Esc> :<C-U>call gitgutter#hunk#close_hunk_preview_window()<CR>
" Ensure that when the preview window is closed, the map is removed.
autocmd User GitGutterPreviewClosed silent! nunmap <buffer> <Esc>
autocmd CursorMoved <buffer> ++once silent! nunmap <buffer> <Esc>
execute "autocmd WinClosed <buffer=".winbufnr(s:winid)."> doautocmd" s:nomodeline "User GitGutterPreviewClosed"
endif
return