From 9420a2127fbd6a7dc28ce4115da47bb56c57e235 Mon Sep 17 00:00:00 2001 From: Kwon-Young Choi Date: Thu, 16 May 2019 21:58:07 +0200 Subject: [PATCH] runtime/termdebug.vim #10015 * bugfix * use NormalFloat for floating window background * use floating window by default * correctly use nvim_open_win() * use nvim_win_set_option to set window local option * use nvim_buf_set_option for buffer options * renamed augroup to nvim_termdebug_close_hover to be consistent with nvim_terminal_... augroup --- runtime/doc/nvim_terminal_emulator.txt | 4 +- .../dist/opt/termdebug/plugin/termdebug.vim | 132 +++++++++--------- 2 files changed, 67 insertions(+), 69 deletions(-) diff --git a/runtime/doc/nvim_terminal_emulator.txt b/runtime/doc/nvim_terminal_emulator.txt index fe5506fc4c..aba0571dc0 100644 --- a/runtime/doc/nvim_terminal_emulator.txt +++ b/runtime/doc/nvim_terminal_emulator.txt @@ -328,9 +328,9 @@ To change the name of the gdb command, set the "termdebugger" variable before invoking `:Termdebug`: > let termdebugger = "mygdb" -To use neovim floating windows for previewing variable evaluation, set the +To not use neovim floating windows for previewing variable evaluation, set the `g:termdebug_useFloatingHover` variable like this: > - let g:termdebug_useFloatingHover = 1 + let g:termdebug_useFloatingHover = 0 If you are a mouse person, you can also define a mapping using your right click to one of the terminal command like evaluate the variable under the diff --git a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim index f809c238f6..cb4f0263b7 100644 --- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim +++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim @@ -579,7 +579,7 @@ func s:HandleEvaluate(msg) endfunc function! s:ShouldUseFloatWindow() abort - if has('nvim_open_win') && exists('g:termdebug_useFloatingHover') && (g:termdebug_useFloatingHover == 1) + if exists('*nvim_open_win') && (get(g:, 'termdebug_useFloatingHover', 1) == 1) return v:true else return v:false @@ -593,19 +593,19 @@ function! s:CloseFloatingHoverOnCursorMove(win_id, opened) abort " was really moved return endif - autocmd! plugin-LC-neovim-close-hover + autocmd! nvim_termdebug_close_hover let winnr = win_id2win(a:win_id) if winnr == 0 return endif - execute winnr . 'wincmd c' + call nvim_win_close(a:win_id, v:true) endfunction function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort let winnr = win_id2win(a:win_id) if winnr == 0 " Float window was already closed - autocmd! plugin-LC-neovim-close-hover + autocmd! nvim_termdebug_close_hover return endif if winnr == winnr() @@ -616,8 +616,8 @@ function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort " When current buffer opened hover window, it's not another buffer. Skipped return endif - autocmd! plugin-LC-neovim-close-hover - execute winnr . 'wincmd c' + autocmd! nvim_termdebug_close_hover + call nvim_win_close(a:win_id, v:true) endfunction " Open preview window. Window is open in: @@ -630,76 +630,74 @@ function! s:OpenHoverPreview(lines, filetype) abort let use_float_win = s:ShouldUseFloatWindow() if use_float_win - let bufname = nvim_create_buf(v:false, v:true) - call nvim_buf_set_lines(buf, 0, -1, v:true, lines) - let pos = getpos('.') + let pos = getpos('.') - " Calculate width and height and give margin to lines - let width = 0 - for index in range(len(lines)) - let line = lines[index] - if line !=# '' - " Give a left margin - let line = ' ' . line - endif - let lw = strdisplaywidth(line) - if lw > width - let width = lw - endif - let lines[index] = line - endfor - - " Give margin - let width += 1 - let lines = [''] + lines + [''] - let height = len(lines) - - " Calculate anchor - " Prefer North, but if there is no space, fallback into South - let bottom_line = line('w0') + winheight(0) - 1 - if pos[1] + height <= bottom_line - let vert = 'N' - let row = 1 - else - let vert = 'S' - let row = 0 + " Calculate width and height + let width = 0 + for index in range(len(lines)) + let line = lines[index] + let lw = strdisplaywidth(line) + if lw > width + let width = lw endif + let lines[index] = line + endfor - " Prefer West, but if there is no space, fallback into East - if pos[2] + width <= &columns - let hor = 'W' - let col = 0 - else - let hor = 'E' - let col = 1 - endif + let height = len(lines) - let float_win_id = nvim_open_win(bufnr, v:true, { - \ 'relative': 'cursor', - \ 'anchor': vert . hor, - \ 'row': row, - \ 'col': col, - \ 'width': width, - \ 'height': height, - \ }) + " Calculate anchor + " Prefer North, but if there is no space, fallback into South + let bottom_line = line('w0') + winheight(0) - 1 + if pos[1] + height <= bottom_line + let vert = 'N' + let row = 1 + else + let vert = 'S' + let row = 0 + endif - execute 'noswapfile edit!' bufname + " Prefer West, but if there is no space, fallback into East + if pos[2] + width <= &columns + let hor = 'W' + let col = 0 + else + let hor = 'E' + let col = 1 + endif - setlocal winhl=Normal:CursorLine + let buf = nvim_create_buf(v:false, v:true) + call nvim_buf_set_lines(buf, 0, -1, v:true, lines) + " using v:true for second argument of nvim_open_win make the floating + " window disappear + let float_win_id = nvim_open_win(buf, v:false, { + \ 'relative': 'cursor', + \ 'anchor': vert . hor, + \ 'row': row, + \ 'col': col, + \ 'width': width, + \ 'height': height, + \ }) + call nvim_win_set_option(float_win_id, 'relativenumber', v:false) + call nvim_win_set_option(float_win_id, 'signcolumn', 'no') + call nvim_win_set_option(float_win_id, 'signcolumn', 'no') + if a:filetype isnot v:null + call nvim_win_set_option(float_win_id, 'filetype', a:filetype) + endif + + call nvim_buf_set_option(buf, 'modified', v:false) + call nvim_buf_set_option(buf, 'modifiable', v:false) + + " Unlike preview window, :pclose does not close window. Instead, close + " hover window automatically when cursor is moved. + let call_after_move = printf('CloseFloatingHoverOnCursorMove(%d, %s)', float_win_id, string(pos)) + let call_on_bufenter = printf('CloseFloatingHoverOnBufEnter(%d, %d)', float_win_id, bufnr) + augroup nvim_termdebug_close_hover + execute 'autocmd CursorMoved,CursorMovedI,InsertEnter call ' . call_after_move + execute 'autocmd BufEnter * call ' . call_on_bufenter + augroup END else echomsg a:lines[0] endif - - if use_float_win - " Unlike preview window, :pclose does not close window. Instead, close - " hover window automatically when cursor is moved. - let call_after_move = printf('CloseFloatingHoverOnCursorMove(%d, %s)', float_win_id, string(pos)) - let call_on_bufenter = printf('CloseFloatingHoverOnBufEnter(%d, %d)', float_win_id, bufnr) - augroup plugin-LC-neovim-close-hover - execute 'autocmd CursorMoved,CursorMovedI,InsertEnter call ' . call_after_move - execute 'autocmd BufEnter * call ' . call_on_bufenter - augroup END - endif endfunction " Handle an error.