Start to create session manager

Kind of works, but there's still a single global _vimspector_session
This commit is contained in:
Ben Jackson 2020-12-05 22:24:54 +00:00
commit 099431ba55
4 changed files with 61 additions and 27 deletions

View file

@ -27,10 +27,12 @@ endif
function! vimspector#internal#state#Reset() abort
try
py3 import vim
py3 _vimspector_session = __import__(
py3 _vimspector_session_manager = __import__(
\ "vimspector",
\ fromlist=[ "debug_session" ] ).debug_session.DebugSession(
\ vim.eval( 's:prefix' ) )
\ fromlist=[ "session_manager" ] ).session_manager.SessionManager()
py3 _vimspector_session = _vimspector_session_manager =
\ _vimspector_session_manager.NewSession( vim.eval( 's:prefix' ) )
catch /.*/
echohl WarningMsg
echom 'Exception while loading vimspector:' v:exception

View file

@ -29,7 +29,7 @@ class PendingRequest( object ):
class DebugAdapterConnection( object ):
def __init__( self, handlers, send_func ):
def __init__( self, handlers, session_id, send_func ):
self._logger = logging.getLogger( __name__ )
utils.SetUpLogging( self._logger )
@ -37,6 +37,7 @@ class DebugAdapterConnection( object ):
self._SetState( 'READ_HEADER' )
self._buffer = bytes()
self._handlers = handlers
self._session_id = session_id
self._next_message_id = 0
self._outstanding_requests = {}
@ -51,13 +52,12 @@ class DebugAdapterConnection( object ):
msg[ 'seq' ] = this_id
msg[ 'type' ] = 'request'
# TODO/FIXME: This is so messy
expiry_id = vim.eval(
'timer_start( {}, '
' function( "vimspector#internal#channel#Timeout", '
' [ {} ] ) )'.format(
timeout,
self._handler.session_id ) )
self._session_id ) )
request = PendingRequest( msg,
handler,

View file

@ -42,25 +42,10 @@ VIMSPECTOR_HOME = utils.GetVimspectorBase()
# cache of what the user entered for any option we ask them
USER_CHOICES = {}
NEXT_SESSION_ID = 0
SESSIONS = {}
def PushSession( session ):
global NEXT_SESSION_ID
this_id = NEXT_SESSION_ID
NEXT_SESSION_ID = NEXT_SESSION_ID + 1
SESSIONS[ this_id ] = session
return this_id
def PopSession( session ):
SESSIONS.pop( session.session_id, None )
class DebugSession( object ):
def __init__( self, api_prefix ):
self.session_id = PushSession( self )
def __init__( self, session_id, session_manager, api_prefix ):
self.session_id = session_id
self.manager = session_manager
self._logger = logging.getLogger( __name__ )
utils.SetUpLogging( self._logger )
@ -91,7 +76,7 @@ class DebugSession( object ):
def __del__( self ):
PopSession( self )
self.manager.DestroySession( self )
def _ResetServerState( self ):
@ -931,8 +916,9 @@ class DebugSession( object ):
handlers = [ self ]
self._connection = debug_adapter_connection.DebugAdapterConnection(
handlers,
lambda msg: utils.Call(
handlers = handlers,
session_id = self.session_id,
send_func = lambda msg: utils.Call(
"vimspector#internal#{}#Send".format( self._connection_type ),
self.session_id,
msg ) )

View file

@ -0,0 +1,46 @@
# vimspector - A multi-language debugging system for Vim
# Copyright 2020 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.
from vimspector.debug_session import DebugSession
class SessionManager:
next_session_id = 0
sessions = {}
current_session = None
def NewSession( self, *args, **kwargs ):
session_id = self.next_session_id
self.next_session_id += 1
session = DebugSession( session_id, self, *args, **kwargs )
self.sessions[ session_id ] = session
if self.current_session is None:
self.current_session = session.session_id
return session
def DestroySession( self, session: DebugSession ):
del self.sessions[ session.session_id ]
def GetSession( self, session_id ):
return self.sessions.get( session_id )
def CurrentSession( self ):
return self.GetSession( self.current_session )