From 2673d732dd329b012b1f319de8ba289dbf5a281e Mon Sep 17 00:00:00 2001 From: Carlos Ramos Date: Fri, 9 Aug 2019 09:37:44 -0400 Subject: [PATCH] Nvim: make :SignifyPreviewHunk use floating window (#302) --- autoload/sy/repo.vim | 22 +++++++++++++--------- autoload/sy/util.vim | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/autoload/sy/repo.vim b/autoload/sy/repo.vim index b525ca6..1f60269 100644 --- a/autoload/sy/repo.vim +++ b/autoload/sy/repo.vim @@ -306,16 +306,20 @@ function! s:preview_hunk(_sy, vcs, diff) abort return endif - silent! wincmd P - if !&previewwindow - noautocmd botright new + if exists('*nvim_open_win') + call sy#util#renderPopup(hunk) + else + silent! wincmd P + if !&previewwindow + noautocmd botright new + endif + call setline(1, hunk) + silent! %foldopen! + setlocal previewwindow filetype=diff buftype=nofile bufhidden=delete + " With :noautocmd wincmd p, the first line of the preview window would show + " the 'cursorline', although it's not focused. Use feedkeys() instead. + noautocmd call feedkeys("\p", 'nt') endif - call setline(1, hunk) - silent! %foldopen! - setlocal previewwindow filetype=diff buftype=nofile bufhidden=delete - " With :noautocmd wincmd p, the first line of the preview window would show - " the 'cursorline', although it's not focused. Use feedkeys() instead. - noautocmd call feedkeys("\p", 'nt') endfunction function! s:is_cur_line_in_hunk(hunkline) abort diff --git a/autoload/sy/util.vim b/autoload/sy/util.vim index 43a9cc6..fc5688a 100644 --- a/autoload/sy/util.vim +++ b/autoload/sy/util.vim @@ -107,3 +107,42 @@ function! sy#util#execute(cmd) abort silent! execute 'language message' lang return output endfunction + +let s:window = 0 + +function! sy#util#closePopup() + if s:window + let id = win_id2win(s:window) + if id > 0 + execute id . 'close!' + endif + let s:window = 0 + endif +endfunction + +function! sy#util#renderPopup(input, ...) + call sy#util#closePopup() + let s:buf = nvim_create_buf(0, 1) + call nvim_buf_set_option(s:buf, 'syntax', 'diff') + let max_width = 100 + let max_height = 16 + let width = max(map(copy(a:input), {_, v -> len(v)})) + 1 + let width = (width > max_width) ? max_width : width + let height = len(a:input) + let height = (height > max_height) ? max_height : height + call nvim_buf_set_lines(s:buf, 0, -1, 0, a:input) + let s:window = call('nvim_open_win', [s:buf, v:false, { + \ 'relative': 'cursor', + \ 'row': 0, + \ 'col': 0, + \ 'width': width, + \ 'height': height, + \ }]) + call nvim_win_set_option(s:window, 'cursorline', v:false) + call nvim_win_set_option(s:window, 'foldenable', v:false) + call nvim_win_set_option(s:window, 'number', v:false) + call nvim_win_set_option(s:window, 'relativenumber', v:false) + call nvim_win_set_option(s:window, 'statusline', '') + call nvim_win_set_option(s:window, 'wrap', v:true) + autocmd CursorMoved * call sy#util#closePopup() +endfunction \ No newline at end of file