Fix <Esc> in nvim floating preview window closes it

Previously the mapping was set in the original window not the floating
window, so <Esc> in the floating window did nothing (when it should have
closed it) while <Esc> in the original window closed the floating window
(when it should have done nothing).

Fixes #868.
This commit is contained in:
Andy Stewart
2023-08-17 14:14:45 +01:00
parent a7a83c376c
commit 5f525476f3

View File

@@ -462,18 +462,16 @@ function! s:open_hunk_preview_window()
call nvim_buf_set_option(buf, 'swapfile', v:false)
call nvim_buf_set_name(buf, 'gitgutter://hunk-preview')
if g:gitgutter_close_preview_on_escape
let winnr = nvim_win_get_number(s:winid)
execute winnr.'wincmd w'
nnoremap <buffer> <silent> <Esc> :<C-U>call gitgutter#hunk#close_hunk_preview_window()<CR>
wincmd w
endif
" Assumes cursor is in original window.
autocmd CursorMoved,TabLeave <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
endif