From fa84bf7ffc32ad0b25cb08886a98b7a9e527feee Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Mon, 21 May 2018 00:17:26 +0100 Subject: [PATCH] Refactor: Put the code window stuff in its own file --- python3/vimspector/code.py | 75 +++++++++++++++++++ .../vimspector/debug_adapter_connection.py | 2 +- python3/vimspector/debug_session.py | 54 +++---------- 3 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 python3/vimspector/code.py diff --git a/python3/vimspector/code.py b/python3/vimspector/code.py new file mode 100644 index 0000000..cf8e054 --- /dev/null +++ b/python3/vimspector/code.py @@ -0,0 +1,75 @@ +# vimspector - A multi-language debugging system for Vim +# Copyright 2018 Ben Jackson +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import vim + +_logger = logging.getLogger( __name__ ) + +SIGN_ID_OFFSET = 10000000 + + +class CodeView( object ): + def __init__( self, window ): + self._window = window + + self._next_sign_id = SIGN_ID_OFFSET + self._signs = { + 'vimspectorPC': None, + } + + + vim.current.window = self._window + + vim.command( 'nnoremenu WinBar.Continute :call vimspector#Continue()' ) + vim.command( 'nnoremenu WinBar.Next :call vimspector#StepOver()' ) + vim.command( 'nnoremenu WinBar.Step :call vimspector#StepInto()' ) + vim.command( 'nnoremenu WinBar.Finish :call vimspector#StepOut()' ) + vim.command( 'nnoremenu WinBar.Pause :call vimspector#Pause()' ) + + vim.command( 'sign define vimspectorPC text=>> texthl=Search' ) + + + def SetCurrentFrame( self, frame ): + vim.current.window = self._window + + if self._signs[ 'vimspectorPC' ]: + vim.command( 'sign unplace {0}'.format( self._signs[ 'vimspectorPC' ] ) ) + self._signs[ 'vimspectorPC' ] = None + + buffer_number = vim.eval( 'bufnr( "{0}", 1 )'.format( + frame[ 'source' ][ 'path' ] ) ) + + try: + vim.command( 'bu {0}'.format( buffer_number ) ) + except vim.error as e: + if 'E325' not in str( e ): + raise + + self._window.cursor = ( frame[ 'line' ], frame[ 'column' ] ) + + self._signs[ 'vimspectorPC' ] = self._next_sign_id + self._next_sign_id += 1 + + vim.command( 'sign place {0} line={1} name=vimspectorPC file={2}'.format( + self._signs[ 'vimspectorPC' ], + frame[ 'line' ], + frame[ 'source' ][ 'path' ] ) ) + + + def Clear( self ): + if self._signs[ 'vimspectorPC' ]: + vim.command( 'sign unplace {0}'.format( self._signs[ 'vimspectorPC' ] ) ) + self._signs[ 'vimspectorPC' ] = None diff --git a/python3/vimspector/debug_adapter_connection.py b/python3/vimspector/debug_adapter_connection.py index 3faa0f5..54b5926 100644 --- a/python3/vimspector/debug_adapter_connection.py +++ b/python3/vimspector/debug_adapter_connection.py @@ -121,5 +121,5 @@ class DebugAdapterConnection( object ): message[ 'message' ] ) ) elif message[ 'type' ] == 'event': method = 'OnEvent_' + message[ 'event' ] - if method in dir( self._handler ) and getattr( self._handler, method ): + if method in dir( self._handler ): getattr( self._handler, method )( message ) diff --git a/python3/vimspector/debug_session.py b/python3/vimspector/debug_session.py index 9319c21..c574b8c 100644 --- a/python3/vimspector/debug_session.py +++ b/python3/vimspector/debug_session.py @@ -16,7 +16,11 @@ import logging import vim -from vimspector import debug_adapter_connection, stack_trace, utils, variables +from vimspector import ( code, + debug_adapter_connection, + stack_trace, + utils, + variables ) _logger = logging.getLogger( __name__ ) @@ -32,7 +36,6 @@ class DebugSession( object ): channel_send_func ) self._uiTab = None - self._codeWindow = None self._threadsBuffer = None self._outputBuffer = None @@ -41,24 +44,12 @@ class DebugSession( object ): self._SetUpUI() - self._next_sign_id = SIGN_ID_OFFSET - self._signs = { - 'vimspectorPC': None, - } - def _SetUpUI( self ): - # Code window vim.command( 'tabnew' ) self._uiTab = vim.current.tabpage - self._codeWindow = vim.current.window - vim.command( 'nnoremenu WinBar.Continute :call vimspector#Continue()' ) - vim.command( 'nnoremenu WinBar.Next :call vimspector#StepOver()' ) - vim.command( 'nnoremenu WinBar.Step :call vimspector#StepInto()' ) - vim.command( 'nnoremenu WinBar.Finish :call vimspector#StepOut()' ) - vim.command( 'nnoremenu WinBar.Pause :call vimspector#Pause()' ) - - vim.command( 'sign define vimspectorPC text=>> texthl=Search' ) + # Code window + self._codeView = code.CodeView( vim.current.window ) # Threads vim.command( '50vspl' ) @@ -90,32 +81,7 @@ class DebugSession( object ): def SetCurrentFrame( self, frame ): self._currentFrame = frame - vim.current.window = self._codeWindow - - if self._signs[ 'vimspectorPC' ]: - vim.command( 'sign unplace {0}'.format( self._signs[ 'vimspectorPC' ] ) ) - self._signs[ 'vimspectorPC' ] = None - - buffer_number = vim.eval( 'bufnr( "{0}", 1 )'.format( - frame[ 'source' ][ 'path' ] ) ) - - try: - vim.command( 'bu {0}'.format( buffer_number ) ) - except vim.error as e: - if 'E325' not in str( e ): - raise - - self._codeWindow.cursor = ( frame[ 'line' ], frame[ 'column' ] ) - - self._signs[ 'vimspectorPC' ] = self._next_sign_id - self._next_sign_id += 1 - - vim.command( 'sign place {0} line={1} name=vimspectorPC file={2}'.format( - self._signs[ 'vimspectorPC' ], - frame[ 'line' ], - frame[ 'source' ][ 'path' ] ) ) - - + self._codeView.SetCurrentFrame( frame ) self._variablesView.LoadScopes( frame ) @@ -126,9 +92,7 @@ class DebugSession( object ): self._Initialise() def Stop( self ): - if self._signs[ 'vimspectorPC' ]: - vim.command( 'sign unplace {0}'.format( self._signs[ 'vimspectorPC' ] ) ) - self._signs[ 'vimspectorPC' ] = None + self._codeView.Clear() self._connection.DoRequest( None, { 'command': 'disconnect',