Allow overriding the variables/stack trace mappings in config

This commit is contained in:
Ben Jackson 2021-02-24 22:10:23 +00:00
commit 26452289a8
4 changed files with 106 additions and 26 deletions

View file

@ -134,21 +134,43 @@ function! vimspector#internal#balloon#MouseFilter( winid, key ) abort
return handled return handled
endfunction endfunction
function! s:MatchKey( key, candidates ) abort
let mapped = ''
for candidate in a:candidates
try
" Try and expand the key code
execute 'let mapped = "\' . candidate . '"'
if mapped ==# a:key
return v:true
endif
endtry
endfor
return v:false
endfunction
function! vimspector#internal#balloon#CursorFilter( winid, key ) abort function! vimspector#internal#balloon#CursorFilter( winid, key ) abort
if a:key ==? "\<CR>" let mappings = py3eval(
\ "__import__( 'vimspector',"
\." fromlist = [ 'settings' ] ).settings.Dict("
\." 'mappings' )[ 'variables' ]" )
if index( [ "\<LeftMouse>", "\<2-LeftMouse>" ], a:key ) >= 0
return vimspector#internal#balloon#MouseFilter( a:winid, a:key )
endif
if s:MatchKey( a:key, mappings.expand_collapse )
call py3eval( '_vimspector_session.ExpandVariable(' call py3eval( '_vimspector_session.ExpandVariable('
\ . 'buf = vim.buffers[ ' . winbufnr( a:winid ) . ' ],' \ . 'buf = vim.buffers[ ' . winbufnr( a:winid ) . ' ],'
\ . 'line_num = ' . line( '.', a:winid ) \ . 'line_num = ' . line( '.', a:winid )
\ . ')' ) \ . ')' )
return 1 return 1
elseif a:key ==? "\<C-CR>" elseif s:MatchKey( a:key, mappings.set_value )
call py3eval( '_vimspector_session.SetVariableValue(' call py3eval( '_vimspector_session.SetVariableValue('
\ . 'buf = vim.buffers[ ' . winbufnr( a:winid ) . ' ],' \ . 'buf = vim.buffers[ ' . winbufnr( a:winid ) . ' ],'
\ . 'line_num = ' . line( '.', a:winid ) \ . 'line_num = ' . line( '.', a:winid )
\ . ')' ) \ . ')' )
return 1 return 1
elseif index( [ "\<LeftMouse>", "\<2-LeftMouse>" ], a:key ) >= 0
return vimspector#internal#balloon#MouseFilter( a:winid, a:key )
endif endif
return popup_filter_menu( a:winid, a:key ) return popup_filter_menu( a:winid, a:key )

View file

@ -17,6 +17,7 @@
import vim import vim
import builtins import builtins
from vimspector import utils from vimspector import utils
from collections.abc import Mapping
DEFAULTS = { DEFAULTS = {
# UI # UI
@ -38,6 +39,19 @@ DEFAULTS = {
# Installer # Installer
'install_gadgets': [], 'install_gadgets': [],
# Mappings
'mappings': {
'variables': {
'expand_collapse': [ '<CR>', '<2-LeftMouse>' ],
'delete': [ '<Del>' ],
'set_value': [ '<C-CR>' ]
},
'stack_trace': {
'expand_or_jump': [ '<CR>', '<2-LeftMouse>' ],
'focus_thread': [ '<leader><CR>' ],
}
}
} }
@ -69,9 +83,46 @@ if hasattr( vim, 'Dictionary' ):
DICT_TYPE = vim.Dictionary DICT_TYPE = vim.Dictionary
def Dict( option: str ): def Dict( option ):
d = DICT_TYPE( DEFAULTS.get( option, {} ) ) return _UpdateDict( DICT_TYPE( DEFAULTS.get( option, {} ) ),
d.update( utils.GetVimValue( vim.vars, utils.GetVimValue( vim.vars,
f'vimspector_{ option }', f'vimspector_{ option }',
{} ) ) {} ) )
return d
def _UpdateDict( target, override ):
"""Apply the updates in |override| to the dict |target|. This is like
dict.update, but recursive. i.e. if the existing element is a dict, then
override elements of the sub-dict rather than wholesale replacing.
e.g.
UpdateDict(
{
'outer': { 'inner': { 'key': 'oldValue', 'existingKey': True } }
},
{
'outer': { 'inner': { 'key': 'newValue' } },
'newKey': { 'newDict': True },
}
)
yields:
{
'outer': {
'inner': {
'key': 'newValue',
'existingKey': True
}
},
'newKey': { newDict: True }
}
"""
for key, value in override.items():
current_value = target.get( key )
if not isinstance( current_value, Mapping ):
target[ key ] = value
elif isinstance( value, Mapping ):
target[ key ] = _UpdateDict( current_value, value )
else:
target[ key ] = value
return target

View file

@ -18,7 +18,7 @@ import os
import logging import logging
import typing import typing
from vimspector import utils, signs from vimspector import utils, signs, settings
class Thread: class Thread:
@ -107,13 +107,16 @@ class StackTraceView( object ):
utils.SetUpHiddenBuffer( self._buf, 'vimspector.StackTrace' ) utils.SetUpHiddenBuffer( self._buf, 'vimspector.StackTrace' )
utils.SetUpUIWindow( win ) utils.SetUpUIWindow( win )
mappings = settings.Dict( 'mappings' )[ 'stack_trace' ]
with utils.LetCurrentWindow( win ): with utils.LetCurrentWindow( win ):
vim.command( 'nnoremap <silent> <buffer> <CR> ' for mapping in utils.GetVimList( mappings, 'expand_or_jump' ):
':<C-U>call vimspector#GoToFrame()<CR>' ) vim.command( f'nnoremap <silent> <buffer> { mapping } '
vim.command( 'nnoremap <silent> <buffer> <leader><CR> ' ':<C-U>call vimspector#GoToFrame()<CR>' )
':<C-U>call vimspector#SetCurrentThread()<CR>' )
vim.command( 'nnoremap <silent> <buffer> <2-LeftMouse> ' for mapping in utils.GetVimList( mappings, 'focus_thread' ):
':<C-U>call vimspector#GoToFrame()<CR>' ) vim.command( f'nnoremap <silent> <buffer> { mapping } '
':<C-U>call vimspector#SetCurrentThread()<CR>' )
if utils.UseWinBar(): if utils.UseWinBar():
vim.command( 'nnoremenu <silent> 1.1 WinBar.Pause/Continue ' vim.command( 'nnoremenu <silent> 1.1 WinBar.Pause/Continue '

View file

@ -19,7 +19,7 @@ import logging
from functools import partial from functools import partial
import typing import typing
from vimspector import utils from vimspector import utils, settings
class Expandable: class Expandable:
@ -165,13 +165,16 @@ class VariablesView( object ):
self._variable_eval: Scope = None self._variable_eval: Scope = None
self._variable_eval_view: View = None self._variable_eval_view: View = None
mappings = settings.Dict( 'mappings' )[ 'variables' ]
def AddExpandMappings(): def AddExpandMappings():
vim.command( 'nnoremap <silent> <buffer> <CR> ' for mapping in utils.GetVimList( mappings, 'expand_collapse' ):
':<C-u>call vimspector#ExpandVariable()<CR>' ) vim.command( f'nnoremap <silent> <buffer> { mapping } '
vim.command( 'nnoremap <silent> <buffer> <2-LeftMouse> ' ':<C-u>call vimspector#ExpandVariable()<CR>' )
':<C-u>call vimspector#ExpandVariable()<CR>' )
vim.command( 'nnoremap <silent> <buffer> <C-CR> ' for mapping in utils.GetVimList( mappings, 'set_value' ):
':<C-u>call vimspector#SetVariableValue()<CR>' ) vim.command( f'nnoremap <silent> <buffer> { mapping } '
':<C-u>call vimspector#SetVariableValue()<CR>' )
# Set up the "Variables" buffer in the variables_win # Set up the "Variables" buffer in the variables_win
self._scopes: typing.List[ Scope ] = [] self._scopes: typing.List[ Scope ] = []
@ -194,8 +197,9 @@ class VariablesView( object ):
'vimspector#OmniFuncWatch' ) 'vimspector#OmniFuncWatch' )
with utils.LetCurrentWindow( watches_win ): with utils.LetCurrentWindow( watches_win ):
AddExpandMappings() AddExpandMappings()
vim.command( for mapping in utils.GetVimList( mappings, 'delete' ):
'nnoremap <buffer> <DEL> :call vimspector#DeleteWatch()<CR>' ) vim.command(
f'nnoremap <buffer> { mapping } :call vimspector#DeleteWatch()<CR>' )
if utils.UseWinBar(): if utils.UseWinBar():
vim.command( 'nnoremenu <silent> 1.1 WinBar.New ' vim.command( 'nnoremenu <silent> 1.1 WinBar.New '