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

@ -17,6 +17,7 @@
import vim
import builtins
from vimspector import utils
from collections.abc import Mapping
DEFAULTS = {
# UI
@ -38,6 +39,19 @@ DEFAULTS = {
# Installer
'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
def Dict( option: str ):
d = DICT_TYPE( DEFAULTS.get( option, {} ) )
d.update( utils.GetVimValue( vim.vars,
f'vimspector_{ option }',
{} ) )
return d
def Dict( option ):
return _UpdateDict( DICT_TYPE( DEFAULTS.get( option, {} ) ),
utils.GetVimValue( vim.vars,
f'vimspector_{ option }',
{} ) )
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 typing
from vimspector import utils, signs
from vimspector import utils, signs, settings
class Thread:
@ -107,13 +107,16 @@ class StackTraceView( object ):
utils.SetUpHiddenBuffer( self._buf, 'vimspector.StackTrace' )
utils.SetUpUIWindow( win )
mappings = settings.Dict( 'mappings' )[ 'stack_trace' ]
with utils.LetCurrentWindow( win ):
vim.command( 'nnoremap <silent> <buffer> <CR> '
':<C-U>call vimspector#GoToFrame()<CR>' )
vim.command( 'nnoremap <silent> <buffer> <leader><CR> '
':<C-U>call vimspector#SetCurrentThread()<CR>' )
vim.command( 'nnoremap <silent> <buffer> <2-LeftMouse> '
':<C-U>call vimspector#GoToFrame()<CR>' )
for mapping in utils.GetVimList( mappings, 'expand_or_jump' ):
vim.command( f'nnoremap <silent> <buffer> { mapping } '
':<C-U>call vimspector#GoToFrame()<CR>' )
for mapping in utils.GetVimList( mappings, 'focus_thread' ):
vim.command( f'nnoremap <silent> <buffer> { mapping } '
':<C-U>call vimspector#SetCurrentThread()<CR>' )
if utils.UseWinBar():
vim.command( 'nnoremenu <silent> 1.1 WinBar.Pause/Continue '

View file

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