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
This commit is contained in:
Kwon-Young Choi 2019-05-16 21:58:07 +02:00 committed by Justin M. Keyes
parent 3a699a790c
commit 9420a2127f
2 changed files with 67 additions and 69 deletions

View File

@ -328,9 +328,9 @@ To change the name of the gdb command, set the "termdebugger" variable before
invoking `:Termdebug`: > invoking `:Termdebug`: >
let termdebugger = "mygdb" 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: > `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 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 click to one of the terminal command like evaluate the variable under the

View File

@ -579,7 +579,7 @@ func s:HandleEvaluate(msg)
endfunc endfunc
function! s:ShouldUseFloatWindow() abort 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 return v:true
else else
return v:false return v:false
@ -593,19 +593,19 @@ function! s:CloseFloatingHoverOnCursorMove(win_id, opened) abort
" was really moved " was really moved
return return
endif endif
autocmd! plugin-LC-neovim-close-hover autocmd! nvim_termdebug_close_hover
let winnr = win_id2win(a:win_id) let winnr = win_id2win(a:win_id)
if winnr == 0 if winnr == 0
return return
endif endif
execute winnr . 'wincmd c' call nvim_win_close(a:win_id, v:true)
endfunction endfunction
function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort
let winnr = win_id2win(a:win_id) let winnr = win_id2win(a:win_id)
if winnr == 0 if winnr == 0
" Float window was already closed " Float window was already closed
autocmd! plugin-LC-neovim-close-hover autocmd! nvim_termdebug_close_hover
return return
endif endif
if winnr == winnr() 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 " When current buffer opened hover window, it's not another buffer. Skipped
return return
endif endif
autocmd! plugin-LC-neovim-close-hover autocmd! nvim_termdebug_close_hover
execute winnr . 'wincmd c' call nvim_win_close(a:win_id, v:true)
endfunction endfunction
" Open preview window. Window is open in: " Open preview window. Window is open in:
@ -630,76 +630,74 @@ function! s:OpenHoverPreview(lines, filetype) abort
let use_float_win = s:ShouldUseFloatWindow() let use_float_win = s:ShouldUseFloatWindow()
if use_float_win if use_float_win
let bufname = nvim_create_buf(v:false, v:true) let pos = getpos('.')
call nvim_buf_set_lines(buf, 0, -1, v:true, lines)
let pos = getpos('.')
" Calculate width and height and give margin to lines " Calculate width and height
let width = 0 let width = 0
for index in range(len(lines)) for index in range(len(lines))
let line = lines[index] let line = lines[index]
if line !=# '' let lw = strdisplaywidth(line)
" Give a left margin if lw > width
let line = ' ' . line let width = lw
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
endif endif
let lines[index] = line
endfor
" Prefer West, but if there is no space, fallback into East let height = len(lines)
if pos[2] + width <= &columns
let hor = 'W'
let col = 0
else
let hor = 'E'
let col = 1
endif
let float_win_id = nvim_open_win(bufnr, v:true, { " Calculate anchor
\ 'relative': 'cursor', " Prefer North, but if there is no space, fallback into South
\ 'anchor': vert . hor, let bottom_line = line('w0') + winheight(0) - 1
\ 'row': row, if pos[1] + height <= bottom_line
\ 'col': col, let vert = 'N'
\ 'width': width, let row = 1
\ 'height': height, 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('<SID>CloseFloatingHoverOnCursorMove(%d, %s)', float_win_id, string(pos))
let call_on_bufenter = printf('<SID>CloseFloatingHoverOnBufEnter(%d, %d)', float_win_id, bufnr)
augroup nvim_termdebug_close_hover
execute 'autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
execute 'autocmd BufEnter * call ' . call_on_bufenter
augroup END
else else
echomsg a:lines[0] echomsg a:lines[0]
endif 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('<SID>CloseFloatingHoverOnCursorMove(%d, %s)', float_win_id, string(pos))
let call_on_bufenter = printf('<SID>CloseFloatingHoverOnBufEnter(%d, %d)', float_win_id, bufnr)
augroup plugin-LC-neovim-close-hover
execute 'autocmd CursorMoved,CursorMovedI,InsertEnter <buffer> call ' . call_after_move
execute 'autocmd BufEnter * call ' . call_on_bufenter
augroup END
endif
endfunction endfunction
" Handle an error. " Handle an error.