From 4e994968ffff31cdf30030f66b94a36b861468d7 Mon Sep 17 00:00:00 2001 From: dsych Date: Sat, 12 Dec 2020 17:48:50 -0500 Subject: [PATCH] added popup support for vim, still need to figure out how to expand variables --- autoload/vimspector/internal/state.vim | 97 +++++++++++++++++++++----- python3/vimspector/variables.py | 21 ++---- 2 files changed, 88 insertions(+), 30 deletions(-) diff --git a/autoload/vimspector/internal/state.vim b/autoload/vimspector/internal/state.vim index b80a4b7..bf2abde 100644 --- a/autoload/vimspector/internal/state.vim +++ b/autoload/vimspector/internal/state.vim @@ -47,24 +47,89 @@ function! vimspector#internal#state#GetAPIPrefix() abort return s:prefix endfunction +let s:float_win = 0 + function! vimspector#internal#state#CreateTooltip() abort - let buf = nvim_create_buf(v:false, v:true) - call nvim_buf_set_lines(buf, 0, -1, v:true, []) + if has('nvim') + let buf = nvim_create_buf(v:false, v:true) + call nvim_buf_set_lines(buf, 0, -1, v:true, []) - " default the dimensions for now. they can be easily overwritten later - let opts = { 'relative': 'cursor', 'width': 50, 'height': 2, 'col': 0, 'row': 1, 'anchor': 'NW', 'style': 'minimal' } - let g:float_win = nvim_open_win(buf, 0, opts) - call setwinvar(g:float_win, '&wrap', 0) + " default the dimensions for now. they can be easily overwritten later + let opts = { + \ 'relative': 'cursor', + \ 'width': 50, + \ 'height': 2, + \ 'col': 0, + \ 'row': 1, + \ 'anchor': 'NW', + \ 'style': 'minimal' + \ } + let s:float_win = nvim_open_win(buf, 0, opts) + call setwinvar(s:float_win, '&wrap', 1) + call setwinvar(s:float_win, '&cursorline', 1) - call win_gotoid(g:float_win) + call win_gotoid(s:float_win) - " make sure we clean up the float after it loses focus - augroup vimspector#internal#balloon#nvim_float - autocmd! - autocmd WinLeave * :call nvim_win_close(g:float_win, 1) | autocmd! vimspector#internal#balloon#nvim_float - augroup END + nnoremap :call vimspector#ExpandVariable() + nnoremap :quit + nnoremap <2-LeftMouse>:call vimspector#ExpandVariable() - return g:float_win + " make sure we clean up the float after it loses focus + augroup vimspector#internal#balloon#nvim_float + autocmd! + autocmd WinLeave * :call nvim_win_close(s:float_win, 1) | autocmd! vimspector#internal#balloon#nvim_float + augroup END + + else + " assume we are inside vim + func! MyFilter(winid, key) + if index(['j', 'k', 'h', 'l'], a:key) >= 0 + call win_execute(a:winid, ':normal '.a:key) + " do something + return 1 + elseif a:key == "\" + echo 'pressed left mouse' + let mouse_coords = getmousepos() + " close the popup if mouse is clicked outside the window + if mouse_coords['winid'] != a:winid + call popup_close(a:winid) + endif + + echo 'clicked line '.mouse_coords['line'] + call win_execute(a:winid, ":call cursor(".mouse_coords['line'].", ".mouse_coords['column'].")") + return 1 + elseif a:key == "\" + echo 'pressed enter at line '.line(".", a:winid) + echo 'here' + call vimspector#ExpandVariable() + + return 1 + elseif a:key == "\" + call popup_close(a:winid) + let s:float_win = 0 + return 1 + endif + return 0 + endfunc + + if s:float_win != 0 + popup_close(s:float_win) + endif + + let s:float_win = popup_create([], #{ + \ filter: "MyFilter", + \ cursorline: 1, + \ wrap: 1, + \ filtermode: "n", + \ maxwidth: 50, + \ maxheight: 5, + \ scrollbar: 1, + \ moved: "any", + \ }) + + endif + + return s:float_win endfunction function! vimspector#internal#state#ShowTooltip() abort @@ -90,12 +155,12 @@ function! vimspector#internal#state#TooltipExec(body) abort let opts = { 'relative': 'cursor', 'width': width, 'height': len(a:body), 'col': 0, 'row': 1, 'anchor': 'NW', 'style': 'minimal' } - let g:float_win = nvim_open_win(buf, 0, opts) - call setwinvar(g:float_win, '&wrap', 0) + let s:float_win = nvim_open_win(buf, 0, opts) + call setwinvar(s:float_win, '&wrap', 0) augroup vimspector#internal#balloon#nvim_float autocmd! - autocmd CursorMoved * :call nvim_win_close(g:float_win, 1) | autocmd! vimspector#internal#balloon#nvim_float + autocmd CursorMoved * :call nvim_win_close(s:float_win, 1) | autocmd! vimspector#internal#balloon#nvim_float augroup END endfunction " Boilerplate {{{ diff --git a/python3/vimspector/variables.py b/python3/vimspector/variables.py index 7019168..f7f8f02 100644 --- a/python3/vimspector/variables.py +++ b/python3/vimspector/variables.py @@ -140,7 +140,6 @@ class VariablesView( object ): self._variable_eval = None self._variable_eval_view = None - self._variable_eval_win = None def AddExpandMappings(): vim.command( 'nnoremap ' @@ -272,7 +271,6 @@ class VariablesView( object ): } ) def _DrawEval(self): - self._variable_eval_win.height = min(5, len(self._variable_eval.variables)) with utils.RestoreCursorPosition(): utils.ClearBuffer( self._variable_eval_view.buf ) icon = '+' if self._variable_eval.IsExpandable() and not self._variable_eval.IsExpanded() else '-' @@ -291,19 +289,14 @@ class VariablesView( object ): self._variable_eval = Scope(body) float_win_id = vim.eval("vimspector#internal#state#CreateTooltip()") - self._variable_eval_win = vim.current.window - - with utils.LetCurrentWindow( self._variable_eval_win ): - vim.command( 'nnoremap ' - ':call vimspector#ExpandVariable()' ) - vim.command( 'nnoremap ' - ':quit' ) - vim.command( 'nnoremap <2-LeftMouse> ' - ':call vimspector#ExpandVariable()' ) + float_buf_nr = int(vim.eval("winbufnr({})".format(float_win_id))) + # since vim's popup cant be focused there is no way + # to get a reference to its window + # we will emulate python's window object overselves + self._variable_eval_view = View(type('__vim__window__', (object,), {'options': {}, 'buffer': vim.buffers[float_buf_nr]}), {}, self._DrawEval) if(self._variable_eval.VariablesReference() > 0): - self._variable_eval_view = View(self._variable_eval_win, {}, self._DrawEval) self._connection.DoRequest( partial( self._ConsumeVariables, self._DrawEval, self._variable_eval ), { @@ -315,7 +308,6 @@ class VariablesView( object ): else: # in case that there is nothing to expand, we need to simulate a response from 'variables' request # it returns [Variable] - self._variable_eval_view = View(self._variable_eval_win, {}, self._DrawEval) self._variable_eval.variables = [Variable({'name': expression, 'value': body['result']})] self._DrawEval() @@ -418,7 +410,8 @@ class VariablesView( object ): else: return - current_line = vim.current.window.cursor[ 0 ] + current_line = int(vim.eval("getbufinfo({})['lnum']".format(view.buf.name))) + print(current_line) if current_line not in view.lines: return