dynamically adjusting window size for nvim's floating window based on the buffer size
This commit is contained in:
parent
2f1c93a2ac
commit
3c857cebf4
3 changed files with 61 additions and 15 deletions
|
|
@ -38,6 +38,12 @@ endfunction
|
||||||
|
|
||||||
let s:float_win = 0
|
let s:float_win = 0
|
||||||
let s:nvim_related_win = 0
|
let s:nvim_related_win = 0
|
||||||
|
"
|
||||||
|
" tooltip dimensions
|
||||||
|
let s:min_width = 1
|
||||||
|
let s:min_height = 1
|
||||||
|
let s:max_width = 50
|
||||||
|
let s:max_height = 5
|
||||||
|
|
||||||
function! vimspector#internal#balloon#closeCallback() abort
|
function! vimspector#internal#balloon#closeCallback() abort
|
||||||
if has('nvim')
|
if has('nvim')
|
||||||
|
|
@ -52,33 +58,66 @@ function! vimspector#internal#balloon#closeCallback() abort
|
||||||
return py3eval('_vimspector_session._CleanUpTooltip()')
|
return py3eval('_vimspector_session._CleanUpTooltip()')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! vimspector#internal#balloon#nvim_generate_border(width, height)
|
||||||
|
let top = "╭" . repeat("─",a:width + 2) . "╮"
|
||||||
|
let mid = "│" . repeat(" ",a:width + 2) . "│"
|
||||||
|
let bot = "╰" . repeat("─",a:width + 2) . "╯"
|
||||||
|
let lines = [top] + repeat([mid], a:height) + [bot]
|
||||||
|
|
||||||
|
return lines
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! vimspector#internal#balloon#nvim_resize_tooltip()
|
||||||
|
if !has('nvim') || s:float_win <= 0 || s:nvim_related_win <= 0
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
noa call win_gotoid(s:float_win)
|
||||||
|
let buf_lines = getline(1, '$')
|
||||||
|
|
||||||
|
let width = s:min_width
|
||||||
|
let height = min([max([s:min_height, len(buf_lines)]), s:max_height])
|
||||||
|
|
||||||
|
" calculate the longest line
|
||||||
|
for l in buf_lines
|
||||||
|
let width = max([width, len(l)])
|
||||||
|
endfor
|
||||||
|
|
||||||
|
let width = min([width, s:max_width])
|
||||||
|
|
||||||
|
let opts = {
|
||||||
|
\ 'width': width,
|
||||||
|
\ 'height': height,
|
||||||
|
\ }
|
||||||
|
" resize the content window
|
||||||
|
call nvim_win_set_config(s:float_win, opts)
|
||||||
|
|
||||||
|
" resize the border window
|
||||||
|
let opts['width'] = width + 4
|
||||||
|
let opts['height'] = height + 2
|
||||||
|
call nvim_win_set_config(s:nvim_related_win, opts)
|
||||||
|
call nvim_buf_set_lines(nvim_win_get_buf(s:nvim_related_win), 0, -1, v:true, vimspector#internal#balloon#nvim_generate_border(width, height))
|
||||||
|
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! vimspector#internal#balloon#CreateTooltip(is_hover, ...)
|
function! vimspector#internal#balloon#CreateTooltip(is_hover, ...)
|
||||||
let body = []
|
let body = []
|
||||||
if a:0 > 0
|
if a:0 > 0
|
||||||
let body = a:1
|
let body = a:1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" tooltip dimensions
|
|
||||||
let max_height = 5
|
|
||||||
let max_width = 50
|
|
||||||
|
|
||||||
if has('nvim')
|
if has('nvim')
|
||||||
" generate border for the float window by creating a background buffer and
|
" generate border for the float window by creating a background buffer and
|
||||||
" overlaying the content buffer
|
" overlaying the content buffer
|
||||||
" see https://github.com/neovim/neovim/issues/9718#issuecomment-546603628
|
" see https://github.com/neovim/neovim/issues/9718#issuecomment-546603628
|
||||||
let top = "╭" . repeat("─", max_width) . "╮"
|
|
||||||
let mid = "│" . repeat(" ", max_width) . "│"
|
|
||||||
let bot = "╰" . repeat("─", max_width) . "╯"
|
|
||||||
let lines = [top] + repeat([mid], max_height) + [bot]
|
|
||||||
|
|
||||||
let buf_id = nvim_create_buf(v:false, v:true)
|
let buf_id = nvim_create_buf(v:false, v:true)
|
||||||
call nvim_buf_set_lines(buf_id, 0, -1, v:true, lines)
|
call nvim_buf_set_lines(buf_id, 0, -1, v:true, vimspector#internal#balloon#nvim_generate_border(s:max_width, s:max_height))
|
||||||
|
|
||||||
" default the dimensions for now. they can be easily overwritten later
|
" default the dimensions for now. they can be easily overwritten later
|
||||||
let opts = {
|
let opts = {
|
||||||
\ 'relative': 'cursor',
|
\ 'relative': 'cursor',
|
||||||
\ 'width': max_width + 2,
|
\ 'width': s:max_width + 2,
|
||||||
\ 'height': max_height + 2,
|
\ 'height': s:max_height + 2,
|
||||||
\ 'col': 0,
|
\ 'col': 0,
|
||||||
\ 'row': 1,
|
\ 'row': 1,
|
||||||
\ 'anchor': 'NW',
|
\ 'anchor': 'NW',
|
||||||
|
|
@ -122,6 +161,7 @@ function! vimspector#internal#balloon#CreateTooltip(is_hover, ...)
|
||||||
augroup vimspector#internal#balloon#nvim_float
|
augroup vimspector#internal#balloon#nvim_float
|
||||||
autocmd!
|
autocmd!
|
||||||
autocmd WinLeave * :call vimspector#internal#balloon#closeCallback() | autocmd! vimspector#internal#balloon#nvim_float
|
autocmd WinLeave * :call vimspector#internal#balloon#closeCallback() | autocmd! vimspector#internal#balloon#nvim_float
|
||||||
|
autocmd BufModifiedSet * :echo execute(eval("nvim_win_get_buf(".s:float_win.")")."bufdo echom &modified")
|
||||||
augroup END
|
augroup END
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
@ -177,8 +217,8 @@ function! vimspector#internal#balloon#CreateTooltip(is_hover, ...)
|
||||||
\ 'cursorline': 1,
|
\ 'cursorline': 1,
|
||||||
\ 'wrap': 0,
|
\ 'wrap': 0,
|
||||||
\ 'filtermode': "n",
|
\ 'filtermode': "n",
|
||||||
\ 'maxwidth': max_width,
|
\ 'maxwidth': s:max_width,
|
||||||
\ 'maxheight': max_height,
|
\ 'maxheight': s:max_height,
|
||||||
\ 'scrollbar': 1,
|
\ 'scrollbar': 1,
|
||||||
\ 'border': []
|
\ 'border': []
|
||||||
\ }
|
\ }
|
||||||
|
|
|
||||||
|
|
@ -640,7 +640,11 @@ def DisplayBaloon( is_term, display, is_hover = False ):
|
||||||
# Refer https://github.com/vim/vim/issues/1512#issuecomment-492070685
|
# Refer https://github.com/vim/vim/issues/1512#issuecomment-492070685
|
||||||
display = '\n'.join( display )
|
display = '\n'.join( display )
|
||||||
|
|
||||||
return int( vim.eval( "vimspector#internal#balloon#CreateTooltip({}, {})".format(is_hover, json.dumps( display )) ) )
|
rc = int( vim.eval( "vimspector#internal#balloon#CreateTooltip({}, {})".format(is_hover, json.dumps( display )) ) )
|
||||||
|
|
||||||
|
vim.eval("vimspector#internal#balloon#nvim_resize_tooltip()")
|
||||||
|
|
||||||
|
return rc
|
||||||
|
|
||||||
|
|
||||||
def GetBufferFilepath( buf ):
|
def GetBufferFilepath( buf ):
|
||||||
|
|
|
||||||
|
|
@ -284,6 +284,8 @@ class VariablesView( object ):
|
||||||
else:
|
else:
|
||||||
self._DrawVariables( self._variable_eval_view, self._variable_eval.variables, 2 , True)
|
self._DrawVariables( self._variable_eval_view, self._variable_eval.variables, 2 , True)
|
||||||
|
|
||||||
|
vim.eval("vimspector#internal#balloon#nvim_resize_tooltip()")
|
||||||
|
|
||||||
def _CleanUpTooltip(self):
|
def _CleanUpTooltip(self):
|
||||||
# remove reference to old tooltip window
|
# remove reference to old tooltip window
|
||||||
self._variable_eval_view = None
|
self._variable_eval_view = None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue