From e96ef7ce38958f16bca4fed0247d3e7eb0a1541d Mon Sep 17 00:00:00 2001 From: micbou Date: Mon, 26 Mar 2018 16:51:07 +0200 Subject: [PATCH] Do not disable omnifunc when filetype completion is disabled Allow users to still trigger Vim's omnifunc through C-Space when the g:ycm_filetype_specific_completion_to_disable option is set for the current filetype. --- python/ycm/omni_completer.py | 6 +- python/ycm/tests/omni_completer_test.py | 106 ++++++++++++++++++++++++ python/ycm/vimsupport.py | 9 ++ python/ycm/youcompleteme.py | 17 +--- 4 files changed, 124 insertions(+), 14 deletions(-) diff --git a/python/ycm/omni_completer.py b/python/ycm/omni_completer.py index 25ec3459..d45c3082 100644 --- a/python/ycm/omni_completer.py +++ b/python/ycm/omni_completer.py @@ -57,8 +57,12 @@ class OmniCompleter( Completer ): def ShouldUseNowInner( self, request_data ): - if request_data.get( 'force_semantic', False ): + if request_data[ 'force_semantic' ]: return True + disabled_filetypes = self.user_options[ + 'filetype_specific_completion_to_disable' ] + if not vimsupport.CurrentFiletypesEnabled( disabled_filetypes ): + return False return super( OmniCompleter, self ).ShouldUseNowInner( request_data ) diff --git a/python/ycm/tests/omni_completer_test.py b/python/ycm/tests/omni_completer_test.py index 27fbc5c6..4ae2b859 100644 --- a/python/ycm/tests/omni_completer_test.py +++ b/python/ycm/tests/omni_completer_test.py @@ -675,3 +675,109 @@ def OmniCompleter_GetCompletions_NoCache_ForceSemantic_test( ycm ): 'completion_start_column': 1 } ) ) + + +@YouCompleteMeInstance( { + 'cache_omnifunc': 0, + 'filetype_specific_completion_to_disable': { FILETYPE: 1 }, + 'semantic_triggers': TRIGGERS } ) +def OmniCompleter_GetCompletions_FiletypeDisabled_SemanticTrigger_test( ycm ): + def Omnifunc( findstart, base ): + if findstart: + return 5 + return [ 'a', 'b', 'cdef' ] + + current_buffer = VimBuffer( 'buffer', + contents = [ 'test.' ], + filetype = FILETYPE, + omnifunc = Omnifunc ) + + with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ): + ycm.SendCompletionRequest() + assert_that( + ycm.GetCompletionResponse(), + has_entries( { + 'completions': empty(), + 'completion_start_column': 6 + } ) + ) + + +@YouCompleteMeInstance( { + 'cache_omnifunc': 0, + 'filetype_specific_completion_to_disable': { '*': 1 }, + 'semantic_triggers': TRIGGERS } ) +def OmniCompleter_GetCompletions_AllFiletypesDisabled_SemanticTrigger_test( + ycm ): + + def Omnifunc( findstart, base ): + if findstart: + return 5 + return [ 'a', 'b', 'cdef' ] + + current_buffer = VimBuffer( 'buffer', + contents = [ 'test.' ], + filetype = FILETYPE, + omnifunc = Omnifunc ) + + with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ): + ycm.SendCompletionRequest() + assert_that( + ycm.GetCompletionResponse(), + has_entries( { + 'completions': empty(), + 'completion_start_column': 6 + } ) + ) + + +@YouCompleteMeInstance( { + 'cache_omnifunc': 0, + 'filetype_specific_completion_to_disable': { FILETYPE: 1 }, + 'semantic_triggers': TRIGGERS } ) +def OmniCompleter_GetCompletions_FiletypeDisabled_ForceSemantic_test( ycm ): + def Omnifunc( findstart, base ): + if findstart: + return 5 + return [ 'a', 'b', 'cdef' ] + + current_buffer = VimBuffer( 'buffer', + contents = [ 'test.' ], + filetype = FILETYPE, + omnifunc = Omnifunc ) + + with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ): + ycm.SendCompletionRequest( force_semantic = True ) + assert_that( + ycm.GetCompletionResponse(), + has_entries( { + 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ), + 'completion_start_column': 6 + } ) + ) + + +@YouCompleteMeInstance( { + 'cache_omnifunc': 0, + 'filetype_specific_completion_to_disable': { '*': 1 }, + 'semantic_triggers': TRIGGERS } ) +def OmniCompleter_GetCompletions_AllFiletypesDisabled_ForceSemantic_test( ycm ): + def Omnifunc( findstart, base ): + if findstart: + return 5 + return [ 'a', 'b', 'cdef' ] + + current_buffer = VimBuffer( 'buffer', + contents = [ 'test.' ], + filetype = FILETYPE, + omnifunc = Omnifunc ) + + with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ): + ycm.SendCompletionRequest( force_semantic = True ) + assert_that( + ycm.GetCompletionResponse(), + has_entries( { + 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ), + 'completion_start_column': 6 + } ) + ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 427f0b0f..b974da06 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -664,6 +664,15 @@ def CurrentFiletypes(): return ToUnicode( vim.eval( "&filetype" ) ).split( '.' ) +def CurrentFiletypesEnabled( disabled_filetypes ): + """Return False if one of the current filetypes is disabled, True otherwise. + |disabled_filetypes| must be a dictionary where keys are the disabled + filetypes and values are unimportant. The special key '*' matches all + filetypes.""" + return ( '*' not in disabled_filetypes and + not any( [ x in disabled_filetypes for x in CurrentFiletypes() ] ) ) + + def GetBufferFiletypes( bufnr ): command = 'getbufvar({0}, "&ft")'.format( bufnr ) return ToUnicode( vim.eval( command ) ).split( '.' ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 89d76481..301c71ee 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -301,8 +301,7 @@ class YouCompleteMe( object ): def SendCompletionRequest( self, force_semantic = False ): request_data = BuildRequestData() request_data[ 'force_semantic' ] = force_semantic - if ( not self.NativeFiletypeCompletionAvailable() and - self.CurrentFiletypeCompletionEnabled() ): + if not self.NativeFiletypeCompletionUsable(): wrapped_request_data = RequestWrap( request_data ) if self._omnicomp.ShouldUseNow( wrapped_request_data ): self._latest_completion_request = OmniCompletionRequest( @@ -380,7 +379,9 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionUsable( self ): - return ( self.CurrentFiletypeCompletionEnabled() and + disabled_filetypes = self._user_options[ + 'filetype_specific_completion_to_disable' ] + return ( vimsupport.CurrentFiletypesEnabled( disabled_filetypes ) and self.NativeFiletypeCompletionAvailable() ) @@ -808,16 +809,6 @@ class YouCompleteMe( object ): self._CloseLogfile( logfile ) - def CurrentFiletypeCompletionEnabled( self ): - filetypes = vimsupport.CurrentFiletypes() - filetype_to_disable = self._user_options[ - 'filetype_specific_completion_to_disable' ] - if '*' in filetype_to_disable: - return False - else: - return not any( [ x in filetype_to_disable for x in filetypes ] ) - - def ShowDetailedDiagnostic( self ): with HandleServerException(): detailed_diagnostic = BaseRequest.PostDataToHandler(