Resend request when extra conf is loaded or ignored

When the client sends a request to the server, if an extra conf file is found
that is not already white/blacklisted, the server stops processing the request
and tells the client that an unknown extra conf file has been found. The client
then asks the user if that file should be loaded or not. Depending on the
user's answer, the client sends a request to the server to load or ignore the
extra conf file. Finally, the server loads the file or adds it to the
blacklist. However, the initial request was not processed by the server and
should be sent again.
This commit is contained in:
micbou 2018-04-05 17:53:54 +02:00
parent e7772347cd
commit a24d97ca6b
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
8 changed files with 137 additions and 99 deletions

View File

@ -565,6 +565,9 @@ function! s:PollFileParseResponse( ... )
endif endif
exec s:python_command "ycm_state.HandleFileParseRequest()" exec s:python_command "ycm_state.HandleFileParseRequest()"
if s:Pyeval( "ycm_state.ShouldResendFileParseRequest()" )
call s:OnFileReadyToParse( 1 )
endif
endfunction endfunction

View File

@ -66,6 +66,10 @@ class Buffer( object ):
return self._parse_tick != self._ChangedTick() return self._parse_tick != self._ChangedTick()
def ShouldResendParseRequest( self ):
return self._parse_request.ShouldResend()
def UpdateDiagnostics( self, force=False ): def UpdateDiagnostics( self, force=False ):
if force or not self._async_diags: if force or not self._async_diags:
self.UpdateWithNewDiagnostics( self._parse_request.Response() ) self.UpdateWithNewDiagnostics( self._parse_request.Response() )

View File

@ -43,7 +43,7 @@ _logger = logging.getLogger( __name__ )
class BaseRequest( object ): class BaseRequest( object ):
def __init__( self ): def __init__( self ):
pass self._should_resend = False
def Start( self ): def Start( self ):
@ -58,15 +58,22 @@ class BaseRequest( object ):
return {} return {}
@staticmethod def ShouldResend( self ):
def HandleFuture( future, display_message = True, truncate_message = False ): return self._should_resend
def HandleFuture( self,
future,
display_message = True,
truncate_message = False ):
"""Get the server response from a |future| object and catch any exception """Get the server response from a |future| object and catch any exception
while doing so. If an exception is raised because of a unknown while doing so. If an exception is raised because of a unknown
.ycm_extra_conf.py file, load the file or ignore it after asking the user. .ycm_extra_conf.py file, load the file or ignore it after asking the user.
For other exceptions, log the exception and display its message to the user An identical request should be sent again to the server. For other
on the Vim status line. Unset the |display_message| parameter to hide the exceptions, log the exception and display its message to the user on the Vim
message from the user. Set the |truncate_message| parameter to avoid status line. Unset the |display_message| parameter to hide the message from
hit-enter prompts from this message.""" the user. Set the |truncate_message| parameter to avoid hit-enter prompts
from this message."""
try: try:
try: try:
return _JsonFromFuture( future ) return _JsonFromFuture( future )
@ -75,6 +82,7 @@ class BaseRequest( object ):
_LoadExtraConfFile( e.extra_conf_file ) _LoadExtraConfFile( e.extra_conf_file )
else: else:
_IgnoreExtraConfFile( e.extra_conf_file ) _IgnoreExtraConfFile( e.extra_conf_file )
self._should_resend = True
except BaseRequest.Requests().exceptions.ConnectionError: except BaseRequest.Requests().exceptions.ConnectionError:
# We don't display this exception to the user since it is likely to happen # We don't display this exception to the user since it is likely to happen
# for each subsequent request (typically if the server crashed) and we # for each subsequent request (typically if the server crashed) and we
@ -93,12 +101,12 @@ class BaseRequest( object ):
# up; see Requests docs for details (we just pass the param along). # up; see Requests docs for details (we just pass the param along).
# See the HandleFuture method for the |display_message| and |truncate_message| # See the HandleFuture method for the |display_message| and |truncate_message|
# parameters. # parameters.
@staticmethod def GetDataFromHandler( self,
def GetDataFromHandler( handler, handler,
timeout = _READ_TIMEOUT_SEC, timeout = _READ_TIMEOUT_SEC,
display_message = True, display_message = True,
truncate_message = False ): truncate_message = False ):
return BaseRequest.HandleFuture( return self.HandleFuture(
BaseRequest._TalkToHandlerAsync( '', handler, 'GET', timeout ), BaseRequest._TalkToHandlerAsync( '', handler, 'GET', timeout ),
display_message, display_message,
truncate_message ) truncate_message )
@ -109,13 +117,13 @@ class BaseRequest( object ):
# up; see Requests docs for details (we just pass the param along). # up; see Requests docs for details (we just pass the param along).
# See the HandleFuture method for the |display_message| and |truncate_message| # See the HandleFuture method for the |display_message| and |truncate_message|
# parameters. # parameters.
@staticmethod def PostDataToHandler( self,
def PostDataToHandler( data, data,
handler, handler,
timeout = _READ_TIMEOUT_SEC, timeout = _READ_TIMEOUT_SEC,
display_message = True, display_message = True,
truncate_message = False ): truncate_message = False ):
return BaseRequest.HandleFuture( return self.HandleFuture(
BaseRequest.PostDataToHandlerAsync( data, handler, timeout ), BaseRequest.PostDataToHandlerAsync( data, handler, timeout ),
display_message, display_message,
truncate_message ) truncate_message )
@ -243,12 +251,12 @@ def _JsonFromFuture( future ):
def _LoadExtraConfFile( filepath ): def _LoadExtraConfFile( filepath ):
BaseRequest.PostDataToHandler( { 'filepath': filepath }, BaseRequest().PostDataToHandler( { 'filepath': filepath },
'load_extra_conf_file' ) 'load_extra_conf_file' )
def _IgnoreExtraConfFile( filepath ): def _IgnoreExtraConfFile( filepath ):
BaseRequest.PostDataToHandler( { 'filepath': filepath }, BaseRequest().PostDataToHandler( { 'filepath': filepath },
'ignore_extra_conf_file' ) 'ignore_extra_conf_file' )

View File

@ -45,4 +45,4 @@ class YcmdKeepalive( object ):
while True: while True:
time.sleep( self._ping_interval_seconds ) time.sleep( self._ping_interval_seconds )
BaseRequest.GetDataFromHandler( 'healthy', display_message = False ) BaseRequest().GetDataFromHandler( 'healthy', display_message = False )

View File

@ -128,6 +128,6 @@ class OmniCompleter( Completer ):
'query': query 'query': query
} }
response = BaseRequest.PostDataToHandler( request_data, response = BaseRequest().PostDataToHandler( request_data,
'filter_and_sort_candidates' ) 'filter_and_sort_candidates' )
return response if response is not None else [] return response if response is not None else []

View File

@ -64,7 +64,7 @@ def MakeUserOptions( custom_options = {} ):
def _IsReady(): def _IsReady():
return BaseRequest.GetDataFromHandler( 'ready' ) return BaseRequest().GetDataFromHandler( 'ready' )
def WaitUntilReady( timeout = 5 ): def WaitUntilReady( timeout = 5 ):

View File

@ -125,6 +125,8 @@ def EventNotification_FileReadyToParse_NonDiagnostic_Error_test(
call( ERROR_TEXT, truncate = True ) call( ERROR_TEXT, truncate = True )
] ) ] )
ok_( not ycm.ShouldResendFileParseRequest() )
# But it does if a subsequent event raises again # But it does if a subsequent event raises again
ycm.OnFileReadyToParse() ycm.OnFileReadyToParse()
ok_( ycm.FileParseRequestReady() ) ok_( ycm.FileParseRequestReady() )
@ -134,6 +136,8 @@ def EventNotification_FileReadyToParse_NonDiagnostic_Error_test(
call( ERROR_TEXT, truncate = True ) call( ERROR_TEXT, truncate = True )
] ) ] )
ok_( not ycm.ShouldResendFileParseRequest() )
@YouCompleteMeInstance() @YouCompleteMeInstance()
def EventNotification_FileReadyToParse_NonDiagnostic_Error_NonNative_test( def EventNotification_FileReadyToParse_NonDiagnostic_Error_NonNative_test(
@ -154,20 +158,16 @@ def EventNotification_FileReadyToParse_NonDiagnostic_Error_NonNative_test(
test_utils.VIM_SIGNS, test_utils.VIM_SIGNS,
contains() contains()
) )
ok_( not ycm.ShouldResendFileParseRequest() )
@patch( 'ycm.client.base_request._LoadExtraConfFile',
new_callable = ExtendedMock )
@patch( 'ycm.client.base_request._IgnoreExtraConfFile',
new_callable = ExtendedMock )
@YouCompleteMeInstance() @YouCompleteMeInstance()
def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test( def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
ycm, ignore_extra_conf, load_extra_conf ): ycm ):
# This test validates the behaviour of YouCompleteMe.HandleFileParseRequest # This test validates the behaviour of YouCompleteMe.HandleFileParseRequest
# in combination with YouCompleteMe.OnFileReadyToParse when the completer # in combination with YouCompleteMe.OnFileReadyToParse when the completer
# raises the (special) UnknownExtraConf exception # raises the (special) UnknownExtraConf exception
FILE_NAME = 'a_file' FILE_NAME = 'a_file'
MESSAGE = ( 'Found ' + FILE_NAME + '. Load? \n\n(Question can be ' MESSAGE = ( 'Found ' + FILE_NAME + '. Load? \n\n(Question can be '
'turned off with options, see YCM docs)' ) 'turned off with options, see YCM docs)' )
@ -175,6 +175,8 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
def UnknownExtraConfResponse( *args ): def UnknownExtraConfResponse( *args ):
raise UnknownExtraConf( FILE_NAME ) raise UnknownExtraConf( FILE_NAME )
with patch( 'ycm.client.base_request.BaseRequest.PostDataToHandler',
new_callable = ExtendedMock ) as post_data_to_handler:
with MockArbitraryBuffer( 'javascript' ): with MockArbitraryBuffer( 'javascript' ):
with MockEventNotification( UnknownExtraConfResponse ): with MockEventNotification( UnknownExtraConfResponse ):
@ -189,8 +191,8 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
present_dialog.assert_has_exact_calls( [ present_dialog.assert_has_exact_calls( [
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
] ) ] )
load_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'load_extra_conf_file' )
] ) ] )
# Subsequent calls don't re-raise the warning # Subsequent calls don't re-raise the warning
@ -199,10 +201,12 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
present_dialog.assert_has_exact_calls( [ present_dialog.assert_has_exact_calls( [
PresentDialog_Confirm_Call( MESSAGE ) PresentDialog_Confirm_Call( MESSAGE )
] ) ] )
load_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'load_extra_conf_file' )
] ) ] )
ok_( ycm.ShouldResendFileParseRequest() )
# But it does if a subsequent event raises again # But it does if a subsequent event raises again
ycm.OnFileReadyToParse() ycm.OnFileReadyToParse()
ok_( ycm.FileParseRequestReady() ) ok_( ycm.FileParseRequestReady() )
@ -212,11 +216,15 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
] ) ] )
load_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'load_extra_conf_file' ),
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'load_extra_conf_file' )
] ) ] )
ok_( ycm.ShouldResendFileParseRequest() )
post_data_to_handler.reset_mock()
# When the user rejects the extra conf, we reject it # When the user rejects the extra conf, we reject it
with patch( 'ycm.vimsupport.PresentDialog', with patch( 'ycm.vimsupport.PresentDialog',
return_value = 1, return_value = 1,
@ -228,8 +236,8 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
present_dialog.assert_has_exact_calls( [ present_dialog.assert_has_exact_calls( [
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
] ) ] )
ignore_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'ignore_extra_conf_file' )
] ) ] )
# Subsequent calls don't re-raise the warning # Subsequent calls don't re-raise the warning
@ -238,10 +246,12 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
present_dialog.assert_has_exact_calls( [ present_dialog.assert_has_exact_calls( [
PresentDialog_Confirm_Call( MESSAGE ) PresentDialog_Confirm_Call( MESSAGE )
] ) ] )
ignore_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'ignore_extra_conf_file' )
] ) ] )
ok_( ycm.ShouldResendFileParseRequest() )
# But it does if a subsequent event raises again # But it does if a subsequent event raises again
ycm.OnFileReadyToParse() ycm.OnFileReadyToParse()
ok_( ycm.FileParseRequestReady() ) ok_( ycm.FileParseRequestReady() )
@ -251,11 +261,13 @@ def EventNotification_FileReadyToParse_NonDiagnostic_ConfirmExtraConf_test(
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
PresentDialog_Confirm_Call( MESSAGE ), PresentDialog_Confirm_Call( MESSAGE ),
] ) ] )
ignore_extra_conf.assert_has_exact_calls( [ post_data_to_handler.assert_has_exact_calls( [
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'ignore_extra_conf_file' ),
call( FILE_NAME ), call( { 'filepath': FILE_NAME }, 'ignore_extra_conf_file' )
] ) ] )
ok_( ycm.ShouldResendFileParseRequest() )
@YouCompleteMeInstance() @YouCompleteMeInstance()
def EventNotification_FileReadyToParse_Diagnostic_Error_Native_test( ycm ): def EventNotification_FileReadyToParse_Diagnostic_Error_Native_test( ycm ):
@ -302,6 +314,8 @@ def _Check_FileReadyToParse_Diagnostic_Error( ycm ):
eq_( ycm.GetErrorCount(), 1 ) eq_( ycm.GetErrorCount(), 1 )
eq_( ycm.GetWarningCount(), 0 ) eq_( ycm.GetWarningCount(), 0 )
ok_( not ycm.ShouldResendFileParseRequest() )
# New identical requests should result in the same diagnostics. # New identical requests should result in the same diagnostics.
ycm.OnFileReadyToParse() ycm.OnFileReadyToParse()
ok_( ycm.FileParseRequestReady() ) ok_( ycm.FileParseRequestReady() )
@ -315,6 +329,8 @@ def _Check_FileReadyToParse_Diagnostic_Error( ycm ):
eq_( ycm.GetErrorCount(), 1 ) eq_( ycm.GetErrorCount(), 1 )
eq_( ycm.GetWarningCount(), 0 ) eq_( ycm.GetWarningCount(), 0 )
ok_( not ycm.ShouldResendFileParseRequest() )
def _Check_FileReadyToParse_Diagnostic_Warning( ycm ): def _Check_FileReadyToParse_Diagnostic_Warning( ycm ):
# Tests Vim sign placement/unplacement and error/warning count python API # Tests Vim sign placement/unplacement and error/warning count python API
@ -353,6 +369,8 @@ def _Check_FileReadyToParse_Diagnostic_Warning( ycm ):
eq_( ycm.GetErrorCount(), 0 ) eq_( ycm.GetErrorCount(), 0 )
eq_( ycm.GetWarningCount(), 1 ) eq_( ycm.GetWarningCount(), 1 )
ok_( not ycm.ShouldResendFileParseRequest() )
def _Check_FileReadyToParse_Diagnostic_Clean( ycm ): def _Check_FileReadyToParse_Diagnostic_Clean( ycm ):
# Tests Vim sign unplacement and error/warning count python API # Tests Vim sign unplacement and error/warning count python API
@ -368,6 +386,7 @@ def _Check_FileReadyToParse_Diagnostic_Clean( ycm ):
) )
eq_( ycm.GetErrorCount(), 0 ) eq_( ycm.GetErrorCount(), 0 )
eq_( ycm.GetWarningCount(), 0 ) eq_( ycm.GetWarningCount(), 0 )
ok_( not ycm.ShouldResendFileParseRequest() )
@patch( 'ycm.youcompleteme.YouCompleteMe._AddUltiSnipsDataIfNeeded' ) @patch( 'ycm.youcompleteme.YouCompleteMe._AddUltiSnipsDataIfNeeded' )

View File

@ -228,7 +228,7 @@ class YouCompleteMe( object ):
def CheckIfServerIsReady( self ): def CheckIfServerIsReady( self ):
if not self._server_is_ready_with_cache and self.IsServerAlive(): if not self._server_is_ready_with_cache and self.IsServerAlive():
self._server_is_ready_with_cache = BaseRequest.GetDataFromHandler( self._server_is_ready_with_cache = BaseRequest().GetDataFromHandler(
'ready', display_message = False ) 'ready', display_message = False )
return self._server_is_ready_with_cache return self._server_is_ready_with_cache
@ -329,7 +329,7 @@ class YouCompleteMe( object ):
def GetDefinedSubcommands( self ): def GetDefinedSubcommands( self ):
subcommands = BaseRequest.PostDataToHandler( BuildRequestData(), subcommands = BaseRequest().PostDataToHandler( BuildRequestData(),
'defined_subcommands' ) 'defined_subcommands' )
return subcommands if subcommands else [] return subcommands if subcommands else []
@ -555,6 +555,10 @@ class YouCompleteMe( object ):
current_buffer.MarkResponseHandled() current_buffer.MarkResponseHandled()
def ShouldResendFileParseRequest( self ):
return self.CurrentBuffer().ShouldResendParseRequest()
def DebugInfo( self ): def DebugInfo( self ):
debug_info = '' debug_info = ''
if self._client_logfile: if self._client_logfile:
@ -644,7 +648,7 @@ class YouCompleteMe( object ):
def ShowDetailedDiagnostic( self ): def ShowDetailedDiagnostic( self ):
detailed_diagnostic = BaseRequest.PostDataToHandler( detailed_diagnostic = BaseRequest().PostDataToHandler(
BuildRequestData(), 'detailed_diagnostic' ) BuildRequestData(), 'detailed_diagnostic' )
if 'message' in detailed_diagnostic: if 'message' in detailed_diagnostic: