From 22256f361d99d8c38e55dafbe5b31fc89a0f9593 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 1 Sep 2013 20:17:37 -0700 Subject: [PATCH 001/149] ShouldUseNow now takes a current_line param This is now used instead of examining the vim.current.line property. --- .../completers/all/identifier_completer.py | 2 +- python/ycm/completers/all/omni_completer.py | 12 +++++----- python/ycm/completers/completer.py | 22 +++++++++---------- python/ycm/completers/cpp/clang_completer.py | 4 ++-- .../completers/general/filename_completer.py | 4 ++-- .../general/general_completer_store.py | 7 +++--- .../completers/general/ultisnips_completer.py | 2 +- python/ycm/youcompleteme.py | 4 ++-- 8 files changed, 30 insertions(+), 27 deletions(-) diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index 932b4406..cf376d7a 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -43,7 +43,7 @@ class IdentifierCompleter( GeneralCompleter ): self.filetypes_with_keywords_loaded = set() - def ShouldUseNow( self, start_column ): + def ShouldUseNow( self, start_column, unused_current_line ): return self.QueryLengthAboveMinThreshold( start_column ) diff --git a/python/ycm/completers/all/omni_completer.py b/python/ycm/completers/all/omni_completer.py index 66b6fff6..d45f7fa7 100644 --- a/python/ycm/completers/all/omni_completer.py +++ b/python/ycm/completers/all/omni_completer.py @@ -40,16 +40,18 @@ class OmniCompleter( Completer ): return vimsupport.GetBoolValue( "g:ycm_cache_omnifunc" ) - def ShouldUseNow( self, start_column ): + def ShouldUseNow( self, start_column, current_line ): if self.ShouldUseCache(): - return super( OmniCompleter, self ).ShouldUseNow( start_column ) - return self.ShouldUseNowInner( start_column ) + return super( OmniCompleter, self ).ShouldUseNow( start_column, + current_line ) + return self.ShouldUseNowInner( start_column, current_line ) - def ShouldUseNowInner( self, start_column ): + def ShouldUseNowInner( self, start_column, current_line ): if not self.omnifunc: return False - return super( OmniCompleter, self ).ShouldUseNowInner( start_column ) + return super( OmniCompleter, self ).ShouldUseNowInner( start_column, + current_line ) def CandidatesForQueryAsync( self, query, unused_start_column ): diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index dee07e5f..6e2576d3 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -18,7 +18,6 @@ # along with YouCompleteMe. If not, see . import abc -import vim import ycm_core from ycm import vimsupport from ycm.completers.completer_utils import TriggersForFiletype @@ -36,10 +35,11 @@ class Completer( object ): calling on your Completer: ShouldUseNow() is called with the start column of where a potential completion - string should start. For instance, if the user's input is 'foo.bar' and the - cursor is on the 'r' in 'bar', start_column will be the 0-based index of 'b' - in the line. Your implementation of ShouldUseNow() should return True if your - semantic completer should be used and False otherwise. + string should start and the current line (string) the cursor is on. For + instance, if the user's input is 'foo.bar' and the cursor is on the 'r' in + 'bar', start_column will be the 0-based index of 'b' in the line. Your + implementation of ShouldUseNow() should return True if your semantic completer + should be used and False otherwise. This is important to get right. You want to return False if you can't provide completions because then the identifier completer will kick in, and that's @@ -125,8 +125,8 @@ class Completer( object ): # It's highly likely you DON'T want to override this function but the *Inner # version of it. - def ShouldUseNow( self, start_column ): - inner_says_yes = self.ShouldUseNowInner( start_column ) + def ShouldUseNow( self, start_column, current_line ): + inner_says_yes = self.ShouldUseNowInner( start_column, current_line ) if not inner_says_yes: self.completions_cache = None @@ -137,9 +137,8 @@ class Completer( object ): return inner_says_yes and not previous_results_were_empty - def ShouldUseNowInner( self, start_column ): - line = vim.current.line - line_length = len( line ) + def ShouldUseNowInner( self, start_column, current_line ): + line_length = len( current_line ) if not line_length or start_column - 1 >= line_length: return False @@ -151,7 +150,7 @@ class Completer( object ): trigger_length = len( trigger ) while True: line_index = start_column + index - if line_index < 0 or line[ line_index ] != trigger[ index ]: + if line_index < 0 or current_line[ line_index ] != trigger[ index ]: break if abs( index ) == trigger_length: @@ -164,6 +163,7 @@ class Completer( object ): query_length = vimsupport.CurrentColumn() - start_column return query_length >= MIN_NUM_CHARS + # It's highly likely you DON'T want to override this function but the *Inner # version of it. def CandidatesForQueryAsync( self, query, start_column ): diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index dfbb1ab9..51a97b3a 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -301,9 +301,9 @@ class ClangCompleter( Completer ): vimsupport.EchoText( closest_diagnostic.long_formatted_text_ ) - def ShouldUseNow( self, start_column ): + def ShouldUseNow( self, start_column, current_line ): # We don't want to use the Completer API cache, we use one in the C++ code. - return self.ShouldUseNowInner( start_column ) + return self.ShouldUseNowInner( start_column, current_line ) def DebugInfo( self ): diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index 8dd1f181..d6dda640 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -63,8 +63,8 @@ class FilenameCompleter( ThreadedCompleter ): vim.current.line[ :start_column ] ) ) - def ShouldUseNowInner( self, start_column ): - return ( start_column and ( vim.current.line[ start_column - 1 ] == '/' or + def ShouldUseNowInner( self, start_column, current_line ): + return ( start_column and ( current_line[ start_column - 1 ] == '/' or self.AtIncludeStatementStart( start_column ) ) ) diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index c04972ae..20fec343 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -58,17 +58,18 @@ class GeneralCompleterStore( Completer ): return set() - def ShouldUseNow( self, start_column ): + def ShouldUseNow( self, start_column, current_line ): self._current_query_completers = [] - if self._filename_completer.ShouldUseNow( start_column ): + if self._filename_completer.ShouldUseNow( start_column, current_line ): self._current_query_completers = [ self._filename_completer ] return True should_use_now = False for completer in self._non_filename_completers: - should_use_this_completer = completer.ShouldUseNow( start_column ) + should_use_this_completer = completer.ShouldUseNow( start_column, + current_line ) should_use_now = should_use_now or should_use_this_completer if should_use_this_completer: diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index 32710a10..1f7834ee 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -33,7 +33,7 @@ class UltiSnipsCompleter( GeneralCompleter ): self._filtered_candidates = None - def ShouldUseNowInner( self, start_column ): + def ShouldUseNowInner( self, start_column, unused_current_line ): return self.QueryLengthAboveMinThreshold( start_column ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index ee689168..bca51821 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -87,13 +87,13 @@ class YouCompleteMe( object ): def ShouldUseGeneralCompleter( self, start_column ): - return self.gencomp.ShouldUseNow( start_column ) + return self.gencomp.ShouldUseNow( start_column, vim.current.line ) def ShouldUseFiletypeCompleter( self, start_column ): if self.FiletypeCompletionUsable(): return self.GetFiletypeCompleter().ShouldUseNow( - start_column ) + start_column, vim.current.line ) return False From cb741193535715c95824848118e0da15f51c665b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 1 Sep 2013 20:19:46 -0700 Subject: [PATCH 002/149] Minor code style fixes --- python/ycm/youcompleteme.py | 38 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index bca51821..8100604f 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -32,17 +32,17 @@ FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval( class YouCompleteMe( object ): def __init__( self ): - self.gencomp = GeneralCompleterStore() - self.omnicomp = OmniCompleter() - self.filetype_completers = {} + self._gencomp = GeneralCompleterStore() + self._omnicomp = OmniCompleter() + self._filetype_completers = {} def GetGeneralCompleter( self ): - return self.gencomp + return self._gencomp def GetOmniCompleter( self ): - return self.omnicomp + return self._omnicomp def GetFiletypeCompleter( self ): @@ -56,7 +56,7 @@ class YouCompleteMe( object ): # Try to find a native completer first for completer in completers: - if completer and completer is not self.omnicomp: + if completer and completer is not self._omnicomp: return completer # Return the omni completer for the first filetype @@ -65,7 +65,7 @@ class YouCompleteMe( object ): def GetFiletypeCompleterForFiletype( self, filetype ): try: - return self.filetype_completers[ filetype ] + return self._filetype_completers[ filetype ] except KeyError: pass @@ -79,15 +79,15 @@ class YouCompleteMe( object ): if completer: supported_filetypes.extend( completer.SupportedFiletypes() ) else: - completer = self.omnicomp + completer = self._omnicomp for supported_filetype in supported_filetypes: - self.filetype_completers[ supported_filetype ] = completer + self._filetype_completers[ supported_filetype ] = completer return completer def ShouldUseGeneralCompleter( self, start_column ): - return self.gencomp.ShouldUseNow( start_column, vim.current.line ) + return self._gencomp.ShouldUseNow( start_column, vim.current.line ) def ShouldUseFiletypeCompleter( self, start_column ): @@ -99,7 +99,7 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): completer = self.GetFiletypeCompleter() - return bool( completer ) and completer is not self.omnicomp + return bool( completer ) and completer is not self._omnicomp def FiletypeCompletionAvailable( self ): @@ -117,35 +117,35 @@ class YouCompleteMe( object ): def OnFileReadyToParse( self ): - self.gencomp.OnFileReadyToParse() + self._gencomp.OnFileReadyToParse() if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnFileReadyToParse() def OnBufferUnload( self, deleted_buffer_file ): - self.gencomp.OnBufferUnload( deleted_buffer_file ) + self._gencomp.OnBufferUnload( deleted_buffer_file ) if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnBufferUnload( deleted_buffer_file ) def OnBufferVisit( self ): - self.gencomp.OnBufferVisit() + self._gencomp.OnBufferVisit() if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnBufferVisit() def OnInsertLeave( self ): - self.gencomp.OnInsertLeave() + self._gencomp.OnInsertLeave() if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnInsertLeave() def OnVimLeave( self ): - self.gencomp.OnVimLeave() + self._gencomp.OnVimLeave() if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnVimLeave() @@ -175,15 +175,15 @@ class YouCompleteMe( object ): def OnCurrentIdentifierFinished( self ): - self.gencomp.OnCurrentIdentifierFinished() + self._gencomp.OnCurrentIdentifierFinished() if self.FiletypeCompletionUsable(): self.GetFiletypeCompleter().OnCurrentIdentifierFinished() def DebugInfo( self ): - completers = set( self.filetype_completers.values() ) - completers.add( self.gencomp ) + completers = set( self._filetype_completers.values() ) + completers.add( self._gencomp ) output = [] for completer in completers: if not completer: From 1d224eb8b42faa3fa3405bb6e99b1d1e757cfc98 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 11:27:45 -0700 Subject: [PATCH 003/149] Indentation fix --- python/ycm/completers/all/identifier_completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index cf376d7a..a46e3023 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -44,7 +44,7 @@ class IdentifierCompleter( GeneralCompleter ): def ShouldUseNow( self, start_column, unused_current_line ): - return self.QueryLengthAboveMinThreshold( start_column ) + return self.QueryLengthAboveMinThreshold( start_column ) def CandidatesForQueryAsync( self, query, unused_start_column ): From 496fe8f7a3207c0cf330381679fa6c2cdac009ee Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 11:34:18 -0700 Subject: [PATCH 004/149] Reordering some YCM init logic --- autoload/youcompleteme.vim | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 51f6e1d1..d3254148 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -51,6 +51,19 @@ function! youcompleteme#Enable() return endif + call s:SetUpCpoptions() + call s:SetUpCompleteopt() + call s:SetUpKeyMappings() + call s:SetUpBackwardsCompatibility() + + if g:ycm_register_as_syntastic_checker + call s:ForceSyntasticCFamilyChecker() + endif + + if g:ycm_allow_changing_updatetime + set ut=2000 + endif + py from ycm.youcompleteme import YouCompleteMe py ycm_state = YouCompleteMe() @@ -71,19 +84,6 @@ function! youcompleteme#Enable() autocmd VimLeave * call s:OnVimLeave() augroup END - call s:SetUpCpoptions() - call s:SetUpCompleteopt() - call s:SetUpKeyMappings() - call s:SetUpBackwardsCompatibility() - - if g:ycm_register_as_syntastic_checker - call s:ForceSyntasticCFamilyChecker() - endif - - if g:ycm_allow_changing_updatetime - set ut=2000 - endif - " Calling this once solves the problem of BufRead/BufEnter not triggering for " the first loaded file. This should be the last command executed in this " function! From a26243092f9e27a3abc2aea92a5ebb19cfdab2e0 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 14:45:53 -0700 Subject: [PATCH 005/149] Now more explicit about accessing user options We don't inspect the Vim process anymore when we want an option; we parse the options on startup and then use that data structure. --- autoload/youcompleteme.vim | 13 ++++--- python/ycm/base.py | 26 ++++++++++++++ .../completers/all/identifier_completer.py | 32 ++++++++--------- python/ycm/completers/all/omni_completer.py | 6 ++-- python/ycm/completers/c/hook.py | 4 +-- python/ycm/completers/completer.py | 9 +++-- python/ycm/completers/cpp/clang_completer.py | 10 +++--- python/ycm/completers/cpp/hook.py | 4 +-- python/ycm/completers/cs/cs_completer.py | 11 +++--- python/ycm/completers/cs/hook.py | 4 +-- .../completers/general/filename_completer.py | 17 ++++------ .../general/general_completer_store.py | 10 +++--- .../completers/general/ultisnips_completer.py | 4 +-- python/ycm/completers/general_completer.py | 4 +-- python/ycm/completers/objc/hook.py | 4 +-- python/ycm/completers/objcpp/hook.py | 4 +-- python/ycm/completers/python/hook.py | 4 +-- .../ycm/completers/python/jedi_completer.py | 4 +-- python/ycm/completers/threaded_completer.py | 4 +-- python/ycm/extra_conf_store.py | 24 +++++++------ python/ycm/user_options_store.py | 34 +++++++++++++++++++ python/ycm/vimsupport.py | 4 +++ python/ycm/youcompleteme.py | 28 +++++++-------- 23 files changed, 163 insertions(+), 101 deletions(-) create mode 100644 python/ycm/user_options_store.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index d3254148..ae1ab173 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -37,12 +37,16 @@ function! youcompleteme#Enable() return endif + call s:SetUpBackwardsCompatibility() + py import sys py import vim exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )' + py from ycm import base + py from ycm import user_options_store + py user_options_store.SetAll( base.BuildServerConf() ) py from ycm import extra_conf_store py extra_conf_store.CallExtraConfYcmCorePreloadIfExists() - py from ycm import base if !pyeval( 'base.CompatibleWithYcmCore()') echohl WarningMsg | @@ -51,10 +55,12 @@ function! youcompleteme#Enable() return endif + py from ycm.youcompleteme import YouCompleteMe + py ycm_state = YouCompleteMe( user_options_store.GetAll() ) + call s:SetUpCpoptions() call s:SetUpCompleteopt() call s:SetUpKeyMappings() - call s:SetUpBackwardsCompatibility() if g:ycm_register_as_syntastic_checker call s:ForceSyntasticCFamilyChecker() @@ -64,9 +70,6 @@ function! youcompleteme#Enable() set ut=2000 endif - py from ycm.youcompleteme import YouCompleteMe - py ycm_state = YouCompleteMe() - augroup youcompleteme autocmd! autocmd CursorMovedI * call s:OnCursorMovedInsertMode() diff --git a/python/ycm/base.py b/python/ycm/base.py index 8082b0e8..4d58f686 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -23,6 +23,8 @@ import vim from ycm import vimsupport from ycm import utils +YCM_VAR_PREFIX = 'ycm_' + try: import ycm_core except ImportError as e: @@ -34,6 +36,30 @@ except ImportError as e: str( e ) ) ) +def BuildServerConf(): + """Builds a dictionary mapping YCM Vim user options to values. Option names + don't have the 'ycm_' prefix.""" + + try: + # vim.vars is fairly new so it might not exist + vim_globals = vim.vars + except: + vim_globals = vim.eval( 'g:' ) + + server_conf = {} + for key, value in vim_globals.items(): + if not key.startswith( YCM_VAR_PREFIX ): + continue + try: + new_value = int( value ) + except: + new_value = value + new_key = key[ len( YCM_VAR_PREFIX ): ] + server_conf[ new_key ] = new_value + + return server_conf + + def CompletionStartColumn(): """Returns the 0-based index where the completion string should start. So if the user enters: diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index a46e3023..70fcdbe3 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -27,16 +27,12 @@ from ycm import vimsupport from ycm import utils MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10 -MIN_NUM_COMPLETION_START_CHARS = int( vimsupport.GetVariableValue( - "g:ycm_min_num_of_chars_for_completion" ) ) -MIN_NUM_CANDIDATE_SIZE_CHARS = int( vimsupport.GetVariableValue( - "g:ycm_min_num_identifier_candidate_chars" ) ) SYNTAX_FILENAME = 'YCM_PLACEHOLDER_FOR_SYNTAX' class IdentifierCompleter( GeneralCompleter ): - def __init__( self ): - super( IdentifierCompleter, self ).__init__() + def __init__( self, user_options ): + super( IdentifierCompleter, self ).__init__( user_options ) self.completer = ycm_core.IdentifierCompleter() self.completer.EnableThreading() self.tags_file_last_mtime = defaultdict( int ) @@ -69,7 +65,8 @@ class IdentifierCompleter( GeneralCompleter ): def AddPreviousIdentifier( self ): - self.AddIdentifier( PreviousIdentifier() ) + self.AddIdentifier( _PreviousIdentifier( self.user_options[ + 'min_num_of_chars_for_completion' ] ) ) def AddIdentifierUnderCursor( self ): @@ -90,8 +87,8 @@ class IdentifierCompleter( GeneralCompleter ): # TODO: use vimsupport.GetFiletypes; also elsewhere in file filetype = vim.eval( "&filetype" ) filepath = vim.eval( "expand('%:p')" ) - collect_from_comments_and_strings = vimsupport.GetBoolValue( - "g:ycm_collect_identifiers_from_comments_and_strings" ) + collect_from_comments_and_strings = bool( self.user_options[ + 'collect_identifiers_from_comments_and_strings' ] ) if not filetype or not filepath: return @@ -153,10 +150,10 @@ class IdentifierCompleter( GeneralCompleter ): def OnFileReadyToParse( self ): self.AddBufferIdentifiers() - if vimsupport.GetBoolValue( 'g:ycm_collect_identifiers_from_tags_files' ): + if self.user_options[ 'collect_identifiers_from_tags_files' ]: self.AddIdentifiersFromTagFiles() - if vimsupport.GetBoolValue( 'g:ycm_seed_identifiers_with_syntax' ): + if self.user_options[ 'seed_identifiers_with_syntax' ]: self.AddIdentifiersFromSyntax() @@ -174,7 +171,8 @@ class IdentifierCompleter( GeneralCompleter ): completions = self.completions_future.GetResults()[ : MAX_IDENTIFIER_COMPLETIONS_RETURNED ] - completions = _RemoveSmallCandidates( completions ) + completions = _RemoveSmallCandidates( + completions, self.user_options[ 'min_num_identifier_candidate_chars' ] ) # We will never have duplicates in completions so with 'dup':1 we tell Vim # to add this candidate even if it's a duplicate of an existing one (which @@ -183,7 +181,7 @@ class IdentifierCompleter( GeneralCompleter ): return [ { 'word': x, 'dup': 1 } for x in completions ] -def PreviousIdentifier(): +def _PreviousIdentifier( min_num_completion_start_chars ): line_num, column_num = vimsupport.CurrentLineAndColumn() buffer = vim.current.buffer line = buffer[ line_num ] @@ -208,15 +206,15 @@ def PreviousIdentifier(): while start_column > 0 and utils.IsIdentifierChar( line[ start_column - 1 ] ): start_column -= 1 - if end_column - start_column < MIN_NUM_COMPLETION_START_CHARS: + if end_column - start_column < min_num_completion_start_chars: return "" return line[ start_column : end_column ] -def _RemoveSmallCandidates( candidates ): - if MIN_NUM_CANDIDATE_SIZE_CHARS == 0: +def _RemoveSmallCandidates( candidates, min_num_candidate_size_chars ): + if min_num_candidate_size_chars == 0: return candidates - return [ x for x in candidates if len( x ) >= MIN_NUM_CANDIDATE_SIZE_CHARS ] + return [ x for x in candidates if len( x ) >= min_num_candidate_size_chars ] diff --git a/python/ycm/completers/all/omni_completer.py b/python/ycm/completers/all/omni_completer.py index d45f7fa7..1c19452b 100644 --- a/python/ycm/completers/all/omni_completer.py +++ b/python/ycm/completers/all/omni_completer.py @@ -26,8 +26,8 @@ OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" ' ' list when expected.' ) class OmniCompleter( Completer ): - def __init__( self ): - super( OmniCompleter, self ).__init__() + def __init__( self, user_options ): + super( OmniCompleter, self ).__init__( user_options ) self.omnifunc = None self.stored_candidates = None @@ -37,7 +37,7 @@ class OmniCompleter( Completer ): def ShouldUseCache( self ): - return vimsupport.GetBoolValue( "g:ycm_cache_omnifunc" ) + return bool( self.user_options[ 'cache_omnifunc' ] ) def ShouldUseNow( self, start_column, current_line ): diff --git a/python/ycm/completers/c/hook.py b/python/ycm/completers/c/hook.py index a4b02e0b..7c6f20e5 100644 --- a/python/ycm/completers/c/hook.py +++ b/python/ycm/completers/c/hook.py @@ -20,9 +20,9 @@ import ycm_core from ycm.completers.cpp.clang_completer import ClangCompleter -def GetCompleter(): +def GetCompleter( user_options ): if ycm_core.HasClangSupport(): - return ClangCompleter() + return ClangCompleter( user_options ) else: return None diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 6e2576d3..bb2345b6 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -24,9 +24,6 @@ from ycm.completers.completer_utils import TriggersForFiletype NO_USER_COMMANDS = 'This completer does not define any commands.' -MIN_NUM_CHARS = int( vimsupport.GetVariableValue( - "g:ycm_min_num_of_chars_for_completion" ) ) - class Completer( object ): """A base class for all Completers in YCM. @@ -116,7 +113,9 @@ class Completer( object ): __metaclass__ = abc.ABCMeta - def __init__( self ): + def __init__( self, user_options ): + self.user_options = user_options + self.min_num_chars = user_options[ 'min_num_of_chars_for_completion' ] self.triggers_for_filetype = TriggersForFiletype() self.completions_future = None self.completions_cache = None @@ -161,7 +160,7 @@ class Completer( object ): def QueryLengthAboveMinThreshold( self, start_column ): query_length = vimsupport.CurrentColumn() - start_column - return query_length >= MIN_NUM_CHARS + return query_length >= self.min_num_chars # It's highly likely you DON'T want to override this function but the *Inner diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 51a97b3a..35d82572 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -26,13 +26,13 @@ from ycm.completers.completer import Completer from ycm.completers.cpp.flags import Flags CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] ) -MAX_DIAGNOSTICS_TO_DISPLAY = int( vimsupport.GetVariableValue( - "g:ycm_max_diagnostics_to_display" ) ) class ClangCompleter( Completer ): - def __init__( self ): - super( ClangCompleter, self ).__init__() + def __init__( self, user_options ): + super( ClangCompleter, self ).__init__( user_options ) + self.max_diagnostics_to_display = user_options[ + 'max_diagnostics_to_display' ] self.completer = ycm_core.ClangCompleter() self.completer.EnableThreading() self.contents_holder = [] @@ -262,7 +262,7 @@ class ClangCompleter( Completer ): diagnostics = self.completer.DiagnosticsForFile( vim.current.buffer.name ) self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) self.last_prepared_diagnostics = [ DiagnosticToDict( x ) for x in - diagnostics[ : MAX_DIAGNOSTICS_TO_DISPLAY ] ] + diagnostics[ : self.max_diagnostics_to_display ] ] self.parse_future = None if self.extra_parse_desired: diff --git a/python/ycm/completers/cpp/hook.py b/python/ycm/completers/cpp/hook.py index a4b02e0b..7c6f20e5 100644 --- a/python/ycm/completers/cpp/hook.py +++ b/python/ycm/completers/cpp/hook.py @@ -20,9 +20,9 @@ import ycm_core from ycm.completers.cpp.clang_completer import ClangCompleter -def GetCompleter(): +def GetCompleter( user_options ): if ycm_core.HasClangSupport(): - return ClangCompleter() + return ClangCompleter( user_options ) else: return None diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index afabffaa..a83b3a4e 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -41,16 +41,16 @@ class CsharpCompleter( ThreadedCompleter ): A Completer that uses the Omnisharp server as completion engine. """ - def __init__( self ): - super( CsharpCompleter, self ).__init__() + def __init__( self, user_options ): + super( CsharpCompleter, self ).__init__( user_options ) self._omnisharp_port = None - if vimsupport.GetBoolValue( 'g:ycm_auto_start_csharp_server' ): + if self.user_options[ 'auto_start_csharp_server' ]: self._StartServer() def OnVimLeave( self ): - if ( vimsupport.GetBoolValue( 'g:ycm_auto_stop_csharp_server' ) and + if ( self.user_options[ 'auto_start_csharp_server' ] and self._ServerIsRunning() ): self._StopServer() @@ -199,8 +199,7 @@ class CsharpCompleter( ThreadedCompleter ): def _FindFreePort( self ): """ Find port without an OmniSharp server running on it """ - port = int( vimsupport.GetVariableValue( - 'g:ycm_csharp_server_port' ) ) + port = self.user_options[ 'csharp_server_port' ] while self._GetResponse( '/checkalivestatus', silent=True, port=port ) != None: diff --git a/python/ycm/completers/cs/hook.py b/python/ycm/completers/cs/hook.py index d082bbcc..d266191c 100644 --- a/python/ycm/completers/cs/hook.py +++ b/python/ycm/completers/cs/hook.py @@ -17,5 +17,5 @@ from ycm.completers.cs.cs_completer import CsharpCompleter -def GetCompleter(): - return CsharpCompleter() +def GetCompleter( user_options ): + return CsharpCompleter( user_options ) diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index d6dda640..abd22b41 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -20,22 +20,17 @@ import vim import os import re -from ycm import vimsupport from ycm.completers.threaded_completer import ThreadedCompleter from ycm.completers.cpp.clang_completer import InCFamilyFile from ycm.completers.cpp.flags import Flags -USE_WORKING_DIR = vimsupport.GetBoolValue( - 'g:ycm_filepath_completion_use_working_dir' ) - - class FilenameCompleter( ThreadedCompleter ): """ General completer that provides filename and filepath completions. """ - def __init__( self ): - super( FilenameCompleter, self ).__init__() + def __init__( self, user_options ): + super( FilenameCompleter, self ).__init__( user_options ) self._flags = Flags() self._path_regex = re.compile( """ @@ -88,7 +83,9 @@ class FilenameCompleter( ThreadedCompleter ): path_match = self._path_regex.search( line ) path_dir = os.path.expanduser( path_match.group() ) if path_match else '' - return _GenerateCandidatesForPaths( _GetPathsStandardCase( path_dir ) ) + return _GenerateCandidatesForPaths( + _GetPathsStandardCase( path_dir, self.user_options[ + 'filepath_completion_use_working_dir' ] ) ) def GetPathsIncludeCase( self, path_dir, include_current_file_dir ): @@ -110,8 +107,8 @@ class FilenameCompleter( ThreadedCompleter ): return sorted( set( paths ) ) -def _GetPathsStandardCase( path_dir ): - if not USE_WORKING_DIR and not path_dir.startswith( '/' ): +def _GetPathsStandardCase( path_dir, use_working_dir ): + if not use_working_dir and not path_dir.startswith( '/' ): path_dir = os.path.join( os.path.dirname( vim.current.buffer.name ), path_dir ) diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 20fec343..c173649a 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -38,11 +38,11 @@ class GeneralCompleterStore( Completer ): GeneralCompleterStore are passed to all general completers. """ - def __init__( self ): - super( GeneralCompleterStore, self ).__init__() - self._identifier_completer = IdentifierCompleter() - self._filename_completer = FilenameCompleter() - self._ultisnips_completer = ( UltiSnipsCompleter() + def __init__( self, user_options ): + super( GeneralCompleterStore, self ).__init__( user_options ) + self._identifier_completer = IdentifierCompleter( user_options ) + self._filename_completer = FilenameCompleter( user_options ) + self._ultisnips_completer = ( UltiSnipsCompleter( user_options ) if USE_ULTISNIPS_COMPLETER else None ) self._non_filename_completers = filter( lambda x: x, [ self._ultisnips_completer, diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index 1f7834ee..50895910 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -27,8 +27,8 @@ class UltiSnipsCompleter( GeneralCompleter ): General completer that provides UltiSnips snippet names in completions. """ - def __init__( self ): - super( UltiSnipsCompleter, self ).__init__() + def __init__( self, user_options ): + super( UltiSnipsCompleter, self ).__init__( user_options ) self._candidates = None self._filtered_candidates = None diff --git a/python/ycm/completers/general_completer.py b/python/ycm/completers/general_completer.py index dd59e1b6..71f013e0 100644 --- a/python/ycm/completers/general_completer.py +++ b/python/ycm/completers/general_completer.py @@ -29,8 +29,8 @@ class GeneralCompleter( Completer ): Subclass Completer directly. """ - def __init__( self ): - super( GeneralCompleter, self ).__init__() + def __init__( self, user_options ): + super( GeneralCompleter, self ).__init__( user_options ) def SupportedFiletypes( self ): diff --git a/python/ycm/completers/objc/hook.py b/python/ycm/completers/objc/hook.py index 737f795c..a26da7ed 100644 --- a/python/ycm/completers/objc/hook.py +++ b/python/ycm/completers/objc/hook.py @@ -20,8 +20,8 @@ import ycm_core from ycm.completers.cpp.clang_completer import ClangCompleter -def GetCompleter(): +def GetCompleter( user_options ): if ycm_core.HasClangSupport(): - return ClangCompleter() + return ClangCompleter( user_options ) else: return None diff --git a/python/ycm/completers/objcpp/hook.py b/python/ycm/completers/objcpp/hook.py index a4b02e0b..7c6f20e5 100644 --- a/python/ycm/completers/objcpp/hook.py +++ b/python/ycm/completers/objcpp/hook.py @@ -20,9 +20,9 @@ import ycm_core from ycm.completers.cpp.clang_completer import ClangCompleter -def GetCompleter(): +def GetCompleter( user_options ): if ycm_core.HasClangSupport(): - return ClangCompleter() + return ClangCompleter( user_options ) else: return None diff --git a/python/ycm/completers/python/hook.py b/python/ycm/completers/python/hook.py index db8ac883..690b7923 100644 --- a/python/ycm/completers/python/hook.py +++ b/python/ycm/completers/python/hook.py @@ -17,5 +17,5 @@ from ycm.completers.python.jedi_completer import JediCompleter -def GetCompleter(): - return JediCompleter() +def GetCompleter( user_options ): + return JediCompleter( user_options ) diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index 5d454a06..cde83b63 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -45,8 +45,8 @@ class JediCompleter( ThreadedCompleter ): https://jedi.readthedocs.org/en/latest/ """ - def __init__( self ): - super( JediCompleter, self ).__init__() + def __init__( self, user_options ): + super( JediCompleter, self ).__init__( user_options ) def SupportedFiletypes( self ): diff --git a/python/ycm/completers/threaded_completer.py b/python/ycm/completers/threaded_completer.py index fd6192aa..6d048676 100644 --- a/python/ycm/completers/threaded_completer.py +++ b/python/ycm/completers/threaded_completer.py @@ -38,8 +38,8 @@ class ThreadedCompleter( Completer ): python/completers/general/filename_completer.py """ - def __init__( self ): - super( ThreadedCompleter, self ).__init__() + def __init__( self, user_options ): + super( ThreadedCompleter, self ).__init__( user_options ) self._query_ready = Event() self._candidates_ready = Event() self._candidates = None diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 698dac2e..3248dc1d 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -26,15 +26,13 @@ import string import sys import vim from ycm import vimsupport +from ycm import user_options_store from fnmatch import fnmatch # Constants YCM_EXTRA_CONF_FILENAME = '.ycm_extra_conf.py' CONFIRM_CONF_FILE_MESSAGE = ('Found {0}. Load? \n\n(Question can be turned ' 'off with options, see YCM docs)') -GLOBAL_YCM_EXTRA_CONF_FILE = os.path.expanduser( - vimsupport.GetVariableValue( "g:ycm_global_ycm_extra_conf" ) -) # Singleton variables _module_for_module_file = {} @@ -86,11 +84,11 @@ def _ShouldLoad( module_file ): decide using a white-/blacklist and ask the user for confirmation as a fallback.""" - if ( module_file == GLOBAL_YCM_EXTRA_CONF_FILE or - not vimsupport.GetBoolValue( 'g:ycm_confirm_extra_conf' ) ): + if ( module_file == _GlobalYcmExtraConfFileLocation() or + not user_options_store.Value( 'confirm_extra_conf' ) ): return True - globlist = vimsupport.GetVariableValue( 'g:ycm_extra_conf_globlist' ) + globlist = user_options_store.Value( 'extra_conf_globlist' ) for glob in globlist: is_blacklisted = glob[0] == '!' if _MatchesGlobPattern( module_file, glob.lstrip('!') ): @@ -139,15 +137,16 @@ def _MatchesGlobPattern( filename, glob ): def _ExtraConfModuleSourceFilesForFile( filename ): """For a given filename, search all parent folders for YCM_EXTRA_CONF_FILENAME files that will compute the flags necessary to compile the file. - If GLOBAL_YCM_EXTRA_CONF_FILE exists it is returned as a fallback.""" + If _GlobalYcmExtraConfFileLocation() exists it is returned as a fallback.""" for folder in _PathsToAllParentFolders( filename ): candidate = os.path.join( folder, YCM_EXTRA_CONF_FILENAME ) if os.path.exists( candidate ): yield candidate - if ( GLOBAL_YCM_EXTRA_CONF_FILE - and os.path.exists( GLOBAL_YCM_EXTRA_CONF_FILE ) ): - yield GLOBAL_YCM_EXTRA_CONF_FILE + global_ycm_extra_conf = _GlobalYcmExtraConfFileLocation() + if ( global_ycm_extra_conf + and os.path.exists( global_ycm_extra_conf ) ): + yield global_ycm_extra_conf def _PathsToAllParentFolders( filename ): @@ -188,3 +187,8 @@ def _DirectoryOfThisScript(): def _RandomName(): """Generates a random module name.""" return ''.join( random.choice( string.ascii_lowercase ) for x in range( 15 ) ) + + +def _GlobalYcmExtraConfFileLocation(): + return os.path.expanduser( + user_options_store.Value( 'global_ycm_extra_conf' ) ) diff --git a/python/ycm/user_options_store.py b/python/ycm/user_options_store.py new file mode 100644 index 00000000..914a6750 --- /dev/null +++ b/python/ycm/user_options_store.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from frozendict import frozendict + +_USER_OPTIONS = {} + +def SetAll( new_options ): + global _USER_OPTIONS + _USER_OPTIONS = frozendict( new_options ) + + +def GetAll(): + return _USER_OPTIONS + + +def Value( key ): + return _USER_OPTIONS[ key ] diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index bd13041d..209319b7 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -145,3 +145,7 @@ def GetVariableValue( variable ): def GetBoolValue( variable ): return bool( int( vim.eval( variable ) ) ) + + +def GetIntValue( variable ): + return int( vim.eval( variable ) ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 8100604f..ec548ebe 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -26,14 +26,11 @@ from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general.general_completer_store import GeneralCompleterStore -FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval( - 'g:ycm_filetype_specific_completion_to_disable' ) - - class YouCompleteMe( object ): - def __init__( self ): - self._gencomp = GeneralCompleterStore() - self._omnicomp = OmniCompleter() + def __init__( self, user_options ): + self._user_options = user_options + self._gencomp = GeneralCompleterStore( user_options ) + self._omnicomp = OmniCompleter( user_options ) self._filetype_completers = {} @@ -60,7 +57,7 @@ class YouCompleteMe( object ): return completer # Return the omni completer for the first filetype - return completers[0] + return completers[ 0 ] def GetFiletypeCompleterForFiletype( self, filetype ): @@ -75,7 +72,7 @@ class YouCompleteMe( object ): supported_filetypes = [ filetype ] if os.path.exists( module_path ): module = imp.load_source( filetype, module_path ) - completer = module.GetCompleter() + completer = module.GetCompleter( self._user_options ) if completer: supported_filetypes.extend( completer.SupportedFiletypes() ) else: @@ -107,12 +104,12 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionUsable( self ): - return ( _CurrentFiletypeCompletionEnabled() and + return ( self.CurrentFiletypeCompletionEnabled() and self.NativeFiletypeCompletionAvailable() ) def FiletypeCompletionUsable( self ): - return ( _CurrentFiletypeCompletionEnabled() and + return ( self.CurrentFiletypeCompletionEnabled() and self.FiletypeCompletionAvailable() ) @@ -202,10 +199,11 @@ class YouCompleteMe( object ): return '\n'.join( output ) -def _CurrentFiletypeCompletionEnabled(): - filetypes = vimsupport.CurrentFiletypes() - return not all([ x in FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE - for x in filetypes ]) + def CurrentFiletypeCompletionEnabled( self ): + filetypes = vimsupport.CurrentFiletypes() + filetype_to_disable = self._user_options[ + 'filetype_specific_completion_to_disable' ] + return not all([ x in filetype_to_disable for x in filetypes ]) def _PathToCompletersFolder(): From 6d29f429bb0990457b9a14d44ac0b85f554d2389 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 14:50:28 -0700 Subject: [PATCH 006/149] Adding the frozendict module Upstream: https://github.com/slezica/python-frozendict --- python/ycm/frozendict.py | 54 ++++++++++++++++++++++++++++++++ python/ycm/user_options_store.py | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 python/ycm/frozendict.py diff --git a/python/ycm/frozendict.py b/python/ycm/frozendict.py new file mode 100644 index 00000000..ec0aa5ad --- /dev/null +++ b/python/ycm/frozendict.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +# Source: https://github.com/slezica/python-frozendict +# +# Copyright (c) 2012 Santiago Lezica +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import collections, operator + +class frozendict(collections.Mapping): + + def __init__(self, *args, **kwargs): + self.__dict = dict(*args, **kwargs) + self.__hash = None + + def __getitem__(self, key): + return self.__dict[key] + + def copy(self, **add_or_replace): + new = frozendict(self) + new.__dict.update(add_or_replace) # Feels like cheating + return new + + def __iter__(self): + return iter(self.__dict) + + def __len__(self): + return len(self.__dict) + + def __repr__(self): + return '' % repr(self.__dict) + + def __hash__(self): + if self.__hash is None: + self.__hash = reduce(operator.xor, map(hash, self.iteritems()), 0) + + return self.__hash diff --git a/python/ycm/user_options_store.py b/python/ycm/user_options_store.py index 914a6750..759e63b9 100644 --- a/python/ycm/user_options_store.py +++ b/python/ycm/user_options_store.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from frozendict import frozendict +from ycm.frozendict import frozendict _USER_OPTIONS = {} From 28c3d9648c46fcfc473f96389ba96f328c5392d2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 17:16:09 -0700 Subject: [PATCH 007/149] Deleting some dead code --- python/ycm/completers/completer.py | 12 ------------ .../completers/general/general_completer_store.py | 15 --------------- 2 files changed, 27 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index bb2345b6..b7cf7abb 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -253,14 +253,6 @@ class Completer( object ): pass - def OnCursorMovedInsertMode( self ): - pass - - - def OnCursorMovedNormalMode( self ): - pass - - def OnBufferVisit( self ): pass @@ -269,10 +261,6 @@ class Completer( object ): pass - def OnCursorHold( self ): - pass - - def OnInsertLeave( self ): pass diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index c173649a..8d0cd55b 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -101,16 +101,6 @@ class GeneralCompleterStore( Completer ): completer.OnFileReadyToParse() - def OnCursorMovedInsertMode( self ): - for completer in self._all_completers: - completer.OnCursorMovedInsertMode() - - - def OnCursorMovedNormalMode( self ): - for completer in self._all_completers: - completer.OnCursorMovedNormalMode() - - def OnBufferVisit( self ): for completer in self._all_completers: completer.OnBufferVisit() @@ -121,11 +111,6 @@ class GeneralCompleterStore( Completer ): completer.OnBufferUnload( deleted_buffer_file ) - def OnCursorHold( self ): - for completer in self._all_completers: - completer.OnCursorHold() - - def OnInsertLeave( self ): for completer in self._all_completers: completer.OnInsertLeave() From bd374a7096a58952186d493173e7bb5332c3de38 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 2 Sep 2013 19:46:30 -0700 Subject: [PATCH 008/149] Completer access now through CompletionRequest This will make it easier to put Completers in the server. --- autoload/youcompleteme.vim | 63 ++++++++++++++----------------------- python/ycm/youcompleteme.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index ae1ab173..075b0112 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -22,8 +22,6 @@ set cpo&vim " This needs to be called outside of a function let s:script_folder_path = escape( expand( ':p:h' ), '\' ) let s:searched_and_results_found = 0 -let s:should_use_filetype_completion = 0 -let s:completion_start_column = 0 let s:omnifunc_mode = 0 let s:old_cursor_position = [] @@ -497,29 +495,26 @@ function! s:InvokeCompletion() endfunction -function! s:CompletionsForQuery( query, use_filetype_completer, - \ completion_start_column ) - if a:use_filetype_completer - py completer = ycm_state.GetFiletypeCompleter() - else - py completer = ycm_state.GetGeneralCompleter() - endif - - py completer.CandidatesForQueryAsync( vim.eval( 'a:query' ), - \ int( vim.eval( 'a:completion_start_column' ) ) ) - - let l:results_ready = 0 - while !l:results_ready - let l:results_ready = pyeval( 'completer.AsyncCandidateRequestReady()' ) - if complete_check() - let s:searched_and_results_found = 0 +python << EOF +def GetCompletions( query ): + request = ycm_state.GetCurrentCompletionRequest() + request.Start( query ) + results_ready = False + while not results_ready: + results_ready = request.Done() + if bool( int( vim.eval( 'complete_check()' ) ) ): return { 'words' : [], 'refresh' : 'always'} - endif - endwhile - let l:results = pyeval( 'base.AdjustCandidateInsertionText( completer.CandidatesFromStoredRequest() )' ) - let s:searched_and_results_found = len( l:results ) != 0 - return { 'words' : l:results, 'refresh' : 'always' } + results = base.AdjustCandidateInsertionText( request.Results() ) + return { 'words' : results, 'refresh' : 'always' } +EOF + + +function! s:CompletionsForQuery( query ) + py results = GetCompletions( vim.eval( 'a:query' ) ) + let results = pyeval( 'results' ) + let s:searched_and_results_found = len( results.words ) != 0 + return results endfunction @@ -543,24 +538,15 @@ function! youcompleteme#Complete( findstart, base ) return -2 endif - - " TODO: make this a function-local variable instead of a script-local one - let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' ) - let s:should_use_filetype_completion = - \ pyeval( 'ycm_state.ShouldUseFiletypeCompleter(' . - \ s:completion_start_column . ')' ) - - if !s:should_use_filetype_completion && - \ !pyeval( 'ycm_state.ShouldUseGeneralCompleter(' . - \ s:completion_start_column . ')' ) + py request = ycm_state.CreateCompletionRequest() + if !pyeval( 'request.ShouldComplete()' ) " for vim, -2 means not found but don't trigger an error message " see :h complete-functions return -2 endif - return s:completion_start_column + return pyeval( 'request.CompletionStartColumn()' ) else - return s:CompletionsForQuery( a:base, s:should_use_filetype_completion, - \ s:completion_start_column ) + return s:CompletionsForQuery( a:base ) endif endfunction @@ -568,10 +554,9 @@ endfunction function! youcompleteme#OmniComplete( findstart, base ) if a:findstart let s:omnifunc_mode = 1 - let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' ) - return s:completion_start_column + return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' ) else - return s:CompletionsForQuery( a:base, 1, s:completion_start_column ) + return s:CompletionsForQuery( a:base ) endif endfunction diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index ec548ebe..80132729 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -22,16 +22,64 @@ import os import vim import ycm_core from ycm import vimsupport +from ycm import base from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general.general_completer_store import GeneralCompleterStore +class CompletionRequest( object ): + def __init__( self, ycm_state ): + self._completion_start_column = base.CompletionStartColumn() + self._ycm_state = ycm_state + self._do_filetype_completion = self._ycm_state.ShouldUseFiletypeCompleter( + self._completion_start_column ) + self._completer = ( self._ycm_state.GetFiletypeCompleter() if + self._do_filetype_completion else + self._ycm_state.GetGeneralCompleter() ) + + + def ShouldComplete( self ): + return ( self._do_filetype_completion or + self._ycm_state.ShouldUseGeneralCompleter( + self._completion_start_column ) ) + + + def CompletionStartColumn( self ): + return self._completion_start_column + + + def Start( self, query ): + self._completer.CandidatesForQueryAsync( query, + self._completion_start_column ) + + def Done( self ): + return self._completer.AsyncCandidateRequestReady() + + + def Results( self ): + return self._completer.CandidatesFromStoredRequest() + + + class YouCompleteMe( object ): def __init__( self, user_options ): self._user_options = user_options self._gencomp = GeneralCompleterStore( user_options ) self._omnicomp = OmniCompleter( user_options ) self._filetype_completers = {} + self._current_completion_request = None + + + def CreateCompletionRequest( self ): + # We have to store a reference to the newly created CompletionRequest + # because VimScript can't store a reference to a Python object across + # function calls... Thus we need to keep this request somewhere. + self._current_completion_request = CompletionRequest( self ) + return self._current_completion_request + + + def GetCurrentCompletionRequest( self ): + return self._current_completion_request def GetGeneralCompleter( self ): From 29bb90a6b434ec6b462398268fbe871924cbe8ed Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 5 Sep 2013 23:43:14 -0700 Subject: [PATCH 009/149] Decoupling completers from Vim; still WIP & broken Note to self: squash this commit before merging into master. --- autoload/youcompleteme.vim | 35 ++- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 42 +-- cpp/ycm/ClangCompleter/ClangCompleter.h | 45 +-- plugin/youcompleteme.vim | 4 +- .../completers/all/identifier_completer.py | 40 ++- python/ycm/completers/all/omni_completer.py | 24 +- python/ycm/completers/completer.py | 82 +++--- python/ycm/completers/completer_utils.py | 8 +- python/ycm/completers/cpp/clang_completer.py | 272 +++++++++--------- python/ycm/completers/cpp/flags.py | 11 +- python/ycm/completers/cs/cs_completer.py | 103 ++++--- .../completers/general/filename_completer.py | 51 ++-- .../general/general_completer_store.py | 15 +- .../completers/general/ultisnips_completer.py | 18 +- .../ycm/completers/python/jedi_completer.py | 104 ++++--- python/ycm/completers/threaded_completer.py | 10 +- python/ycm/extra_conf_store.py | 1 + python/ycm/server_responses.py | 76 +++++ python/ycm/vimsupport.py | 41 ++- python/ycm/youcompleteme.py | 137 ++++++++- 20 files changed, 665 insertions(+), 454 deletions(-) create mode 100644 python/ycm/server_responses.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 075b0112..d8e6e895 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -41,6 +41,7 @@ function! youcompleteme#Enable() py import vim exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )' py from ycm import base + py from ycm import vimsupport py from ycm import user_options_store py user_options_store.SetAll( base.BuildServerConf() ) py from ycm import extra_conf_store @@ -260,7 +261,7 @@ function! s:OnCursorHold() call s:SetUpCompleteopt() " Order is important here; we need to extract any done diagnostics before " reparsing the file again - call s:UpdateDiagnosticNotifications() + " call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -327,7 +328,7 @@ function! s:OnCursorMovedNormalMode() return endif - call s:UpdateDiagnosticNotifications() + " call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -338,7 +339,7 @@ function! s:OnInsertLeave() endif let s:omnifunc_mode = 0 - call s:UpdateDiagnosticNotifications() + " call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() py ycm_state.OnInsertLeave() if g:ycm_autoclose_preview_window_after_completion || @@ -572,7 +573,9 @@ command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic() " required (currently that's on buffer save) OR when the SyntasticCheck command " is invoked function! youcompleteme#CurrentFileDiagnostics() - return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' ) + " TODO: Make this work again. + " return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' ) + return [] endfunction @@ -595,28 +598,24 @@ function! s:CompleterCommand(...) " to select the omni completer or "ft=ycm:ident" to select the identifier " completer. The remaining arguments will passed to the completer. let arguments = copy(a:000) + let completer = '' if a:0 > 0 && strpart(a:1, 0, 3) == 'ft=' if a:1 == 'ft=ycm:omni' - py completer = ycm_state.GetOmniCompleter() + let completer = 'omni' elseif a:1 == 'ft=ycm:ident' - py completer = ycm_state.GetGeneralCompleter() - else - py completer = ycm_state.GetFiletypeCompleterForFiletype( - \ vim.eval('a:1').lstrip('ft=') ) + let completer = 'identifier' endif let arguments = arguments[1:] - elseif pyeval( 'ycm_state.NativeFiletypeCompletionAvailable()' ) - py completer = ycm_state.GetFiletypeCompleter() - else - echohl WarningMsg | - \ echomsg "No native completer found for current buffer." | - \ echomsg "Use ft=... as the first argument to specify a completer." | - \ echohl None - return endif - py completer.OnUserCommand( vim.eval( 'l:arguments' ) ) +py << EOF +response = ycm_state.SendCommandRequest( vim.eval( 'l:arguments' ), + vim.eval( 'l:completer' ) ) +if not response.Valid(): + vimsupport.PostVimMessage( 'No native completer found for current buffer. ' + + 'Use ft=... as the first argument to specify a completer.') +EOF endfunction diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index f822dbbd..961de1d7 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -103,7 +103,7 @@ void ClangCompleter::EnableThreading() { std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( - const std::string &filename ) { + std::string filename ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); if ( !unit ) @@ -127,9 +127,9 @@ bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) { void ClangCompleter::UpdateTranslationUnit( - const std::string &filename, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + std::string filename, + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ) { bool translation_unit_created; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, @@ -182,11 +182,11 @@ Future< void > ClangCompleter::UpdateTranslationUnitAsync( std::vector< CompletionData > ClangCompleter::CandidatesForLocationInFile( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -201,12 +201,12 @@ ClangCompleter::CandidatesForLocationInFile( Future< AsyncCompletions > ClangCompleter::CandidatesForQueryAndLocationInFileAsync( - const std::string &query, - const std::string &filename, + std::string query, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ) { // TODO: throw exception when threading is not enabled and this is called if ( !threading_enabled_ ) return Future< AsyncCompletions >(); @@ -238,7 +238,11 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync( CreateSortingTask( query, future ); if ( skip_clang_result_cache ) { - CreateClangTask( filename, line, column, unsaved_files, flags ); + CreateClangTask( boost::move( filename ), + line, + column, + boost::move( unsaved_files ), + boost::move( flags ) ); } return Future< AsyncCompletions >( boost::move( future ) ); @@ -246,11 +250,11 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync( Location ClangCompleter::GetDeclarationLocation( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -263,11 +267,11 @@ Location ClangCompleter::GetDeclarationLocation( Location ClangCompleter::GetDefinitionLocation( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -279,7 +283,7 @@ Location ClangCompleter::GetDefinitionLocation( } -void ClangCompleter::DeleteCachesForFileAsync( const std::string &filename ) { +void ClangCompleter::DeleteCachesForFileAsync( std::string filename ) { file_cache_delete_stack_.Push( filename ); } diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 266e3122..6ae7819c 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -59,18 +59,23 @@ public: void EnableThreading(); - std::vector< Diagnostic > DiagnosticsForFile( const std::string &filename ); + std::vector< Diagnostic > DiagnosticsForFile( std::string filename ); bool UpdatingTranslationUnit( const std::string &filename ); + // NOTE: params are taken by value on purpose! With a C++11 compiler we can + // avoid internal copies if params are taken by value (move ctors FTW), and we + // need to ensure we own the memory. + // TODO: Change some of these params back to const ref where possible after we + // get the server up. + // TODO: Remove the async methods and the threads when the server is ready. + // Public because of unit tests (gtest is not very thread-friendly) void UpdateTranslationUnit( - const std::string &filename, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + std::string filename, + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ); - // NOTE: params are taken by value on purpose! With a C++11 compiler we can - // avoid internal copies if params are taken by value (move ctors FTW) Future< void > UpdateTranslationUnitAsync( std::string filename, std::vector< UnsavedFile > unsaved_files, @@ -78,35 +83,35 @@ public: // Public because of unit tests (gtest is not very thread-friendly) std::vector< CompletionData > CandidatesForLocationInFile( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ); Future< AsyncCompletions > CandidatesForQueryAndLocationInFileAsync( - const std::string &query, - const std::string &filename, + std::string query, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ); Location GetDeclarationLocation( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ); Location GetDefinitionLocation( - const std::string &filename, + std::string filename, int line, int column, - const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + std::vector< UnsavedFile > unsaved_files, + std::vector< std::string > flags ); - void DeleteCachesForFileAsync( const std::string &filename ); + void DeleteCachesForFileAsync( std::string filename ); private: diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index d3c2ee3e..020db8b2 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -143,8 +143,8 @@ let g:ycm_extra_conf_globlist = let g:ycm_filepath_completion_use_working_dir = \ get( g:, 'ycm_filepath_completion_use_working_dir', 0 ) -" Default semantic triggers are in python/ycm/completers/completer.py, these -" just append new triggers to the default dict. +" Default semantic triggers are in python/ycm/completers/completer_utils.py +" these just append new triggers to the default dict. let g:ycm_semantic_triggers = \ get( g:, 'ycm_semantic_triggers', {} ) diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index 70fcdbe3..196ff85a 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -25,6 +25,7 @@ from ycm.completers.general_completer import GeneralCompleter from ycm.completers.general import syntax_parse from ycm import vimsupport from ycm import utils +from ycm import server_responses MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10 SYNTAX_FILENAME = 'YCM_PLACEHOLDER_FOR_SYNTAX' @@ -39,15 +40,14 @@ class IdentifierCompleter( GeneralCompleter ): self.filetypes_with_keywords_loaded = set() - def ShouldUseNow( self, start_column, unused_current_line ): - return self.QueryLengthAboveMinThreshold( start_column ) + def ShouldUseNow( self, request_data ): + return self.QueryLengthAboveMinThreshold( request_data ) - def CandidatesForQueryAsync( self, query, unused_start_column ): - filetype = vim.eval( "&filetype" ) + def CandidatesForQueryAsync( self, request_data ): self.completions_future = self.completer.CandidatesForQueryAndTypeAsync( - utils.SanitizeQuery( query ), - filetype ) + utils.SanitizeQuery( request_data[ 'query' ] ), + request_data[ 'filetypes' ][ 0 ] ) def AddIdentifier( self, identifier ): @@ -83,17 +83,16 @@ class IdentifierCompleter( GeneralCompleter ): self.AddIdentifier( stripped_cursor_identifier ) - def AddBufferIdentifiers( self ): - # TODO: use vimsupport.GetFiletypes; also elsewhere in file - filetype = vim.eval( "&filetype" ) - filepath = vim.eval( "expand('%:p')" ) + def AddBufferIdentifiers( self, request_data ): + filetype = request_data[ 'filetypes' ][ 0 ] + filepath = request_data[ 'filepath' ] collect_from_comments_and_strings = bool( self.user_options[ 'collect_identifiers_from_comments_and_strings' ] ) if not filetype or not filepath: return - text = "\n".join( vim.current.buffer ) + text = request_data[ 'file_data' ][ filepath ][ 'contents' ] self.completer.AddIdentifiersToDatabaseFromBufferAsync( text, filetype, @@ -147,14 +146,15 @@ class IdentifierCompleter( GeneralCompleter ): filepath ) - def OnFileReadyToParse( self ): - self.AddBufferIdentifiers() + def OnFileReadyToParse( self, request_data ): + self.AddBufferIdentifiers( request_data ) - if self.user_options[ 'collect_identifiers_from_tags_files' ]: - self.AddIdentifiersFromTagFiles() + # TODO: make these work again + # if self.user_options[ 'collect_identifiers_from_tags_files' ]: + # self.AddIdentifiersFromTagFiles() - if self.user_options[ 'seed_identifiers_with_syntax' ]: - self.AddIdentifiersFromSyntax() + # if self.user_options[ 'seed_identifiers_with_syntax' ]: + # self.AddIdentifiersFromSyntax() def OnInsertLeave( self ): @@ -174,11 +174,7 @@ class IdentifierCompleter( GeneralCompleter ): completions = _RemoveSmallCandidates( completions, self.user_options[ 'min_num_identifier_candidate_chars' ] ) - # We will never have duplicates in completions so with 'dup':1 we tell Vim - # to add this candidate even if it's a duplicate of an existing one (which - # will never happen). This saves us some expensive string matching - # operations in Vim. - return [ { 'word': x, 'dup': 1 } for x in completions ] + return [ server_responses.BuildCompletionData( x ) for x in completions ] def _PreviousIdentifier( min_num_completion_start_chars ): diff --git a/python/ycm/completers/all/omni_completer.py b/python/ycm/completers/all/omni_completer.py index 1c19452b..d0a51786 100644 --- a/python/ycm/completers/all/omni_completer.py +++ b/python/ycm/completers/all/omni_completer.py @@ -40,29 +40,27 @@ class OmniCompleter( Completer ): return bool( self.user_options[ 'cache_omnifunc' ] ) - def ShouldUseNow( self, start_column, current_line ): + def ShouldUseNow( self, request_data ): if self.ShouldUseCache(): - return super( OmniCompleter, self ).ShouldUseNow( start_column, - current_line ) - return self.ShouldUseNowInner( start_column, current_line ) + return super( OmniCompleter, self ).ShouldUseNow( request_data ) + return self.ShouldUseNowInner( request_data ) - def ShouldUseNowInner( self, start_column, current_line ): + def ShouldUseNowInner( self, request_data ): if not self.omnifunc: return False - return super( OmniCompleter, self ).ShouldUseNowInner( start_column, - current_line ) + return super( OmniCompleter, self ).ShouldUseNowInner( request_data ) - def CandidatesForQueryAsync( self, query, unused_start_column ): + def CandidatesForQueryAsync( self, request_data ): if self.ShouldUseCache(): return super( OmniCompleter, self ).CandidatesForQueryAsync( - query, unused_start_column ) + request_data ) else: - return self.CandidatesForQueryAsyncInner( query, unused_start_column ) + return self.CandidatesForQueryAsyncInner( request_data ) - def CandidatesForQueryAsyncInner( self, query, unused_start_column ): + def CandidatesForQueryAsyncInner( self, request_data ): if not self.omnifunc: self.stored_candidates = None return @@ -75,7 +73,7 @@ class OmniCompleter( Completer ): omnifunc_call = [ self.omnifunc, "(0,'", - vimsupport.EscapeForVim( query ), + vimsupport.EscapeForVim( request_data[ 'query' ] ), "')" ] items = vim.eval( ''.join( omnifunc_call ) ) @@ -98,7 +96,7 @@ class OmniCompleter( Completer ): return True - def OnFileReadyToParse( self ): + def OnFileReadyToParse( self, request_data ): self.omnifunc = vim.eval( '&omnifunc' ) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index b7cf7abb..60dcd6ec 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -19,7 +19,6 @@ import abc import ycm_core -from ycm import vimsupport from ycm.completers.completer_utils import TriggersForFiletype NO_USER_COMMANDS = 'This completer does not define any commands.' @@ -116,32 +115,35 @@ class Completer( object ): def __init__( self, user_options ): self.user_options = user_options self.min_num_chars = user_options[ 'min_num_of_chars_for_completion' ] - self.triggers_for_filetype = TriggersForFiletype() + self.triggers_for_filetype = TriggersForFiletype( + user_options[ 'semantic_triggers' ] ) self.completions_future = None self.completions_cache = None - self.completion_start_column = None # It's highly likely you DON'T want to override this function but the *Inner # version of it. - def ShouldUseNow( self, start_column, current_line ): - inner_says_yes = self.ShouldUseNowInner( start_column, current_line ) + def ShouldUseNow( self, request_data ): + inner_says_yes = self.ShouldUseNowInner( request_data ) if not inner_says_yes: self.completions_cache = None previous_results_were_empty = ( self.completions_cache and self.completions_cache.CacheValid( - start_column ) and + request_data[ 'line_num' ], + request_data[ 'start_column' ] ) and not self.completions_cache.raw_completions ) return inner_says_yes and not previous_results_were_empty - def ShouldUseNowInner( self, start_column, current_line ): + def ShouldUseNowInner( self, request_data ): + current_line = request_data[ 'line_value' ] + start_column = request_data[ 'start_column' ] line_length = len( current_line ) if not line_length or start_column - 1 >= line_length: return False - filetype = self._CurrentFiletype() + filetype = self._CurrentFiletype( request_data[ 'filetypes' ] ) triggers = self.triggers_for_filetype[ filetype ] for trigger in triggers: @@ -158,52 +160,61 @@ class Completer( object ): return False - def QueryLengthAboveMinThreshold( self, start_column ): - query_length = vimsupport.CurrentColumn() - start_column + def QueryLengthAboveMinThreshold( self, request_data ): + query_length = request_data[ 'column_num' ] - request_data[ 'start_column' ] return query_length >= self.min_num_chars # It's highly likely you DON'T want to override this function but the *Inner # version of it. - def CandidatesForQueryAsync( self, query, start_column ): - self.completion_start_column = start_column + def CandidatesForQueryAsync( self, request_data ): + self.request_data = request_data - if query and self.completions_cache and self.completions_cache.CacheValid( - start_column ): + if ( request_data[ 'query' ] and + self.completions_cache and + self.completions_cache.CacheValid( request_data[ 'line_num' ], + request_data[ 'start_column' ] ) ): self.completions_cache.filtered_completions = ( self.FilterAndSortCandidates( self.completions_cache.raw_completions, - query ) ) + request_data[ 'query' ] ) ) else: self.completions_cache = None - self.CandidatesForQueryAsyncInner( query, start_column ) + self.CandidatesForQueryAsyncInner( request_data ) def DefinedSubcommands( self ): return [] - def EchoUserCommandsHelpMessage( self ): + def UserCommandsHelpMessage( self ): subcommands = self.DefinedSubcommands() if subcommands: - vimsupport.EchoText( 'Supported commands are:\n' + - '\n'.join( subcommands ) + - '\nSee the docs for information on what they do.' ) + return ( 'Supported commands are:\n' + + '\n'.join( subcommands ) + + '\nSee the docs for information on what they do.' ) else: - vimsupport.EchoText( 'No supported subcommands' ) + return 'No supported subcommands' def FilterAndSortCandidates( self, candidates, query ): if not candidates: return [] - if hasattr( candidates, 'words' ): - candidates = candidates.words - items_are_objects = 'word' in candidates[ 0 ] + # We need to handle both an omni_completer style completer and a server + # style completer + if 'words' in candidates: + candidates = candidates[ 'words' ] + + sort_property = '' + if 'word' in candidates[ 0 ]: + sort_property = 'word' + elif 'insertion_text' in candidates[ 0 ]: + sort_property = 'insertion_text' matches = ycm_core.FilterAndSortCandidates( candidates, - 'word' if items_are_objects else '', + sort_property, query ) return matches @@ -238,8 +249,8 @@ class Completer( object ): else: self.completions_cache = CompletionsCache() self.completions_cache.raw_completions = self.CandidatesFromStoredRequestInner() - self.completions_cache.line, _ = vimsupport.CurrentLineAndColumn() - self.completions_cache.column = self.completion_start_column + self.completions_cache.line = self.request_data[ 'line_num' ] + self.completions_cache.column = self.request_data[ 'start_column' ] return self.completions_cache.raw_completions @@ -249,7 +260,7 @@ class Completer( object ): return self.completions_future.GetResults() - def OnFileReadyToParse( self ): + def OnFileReadyToParse( self, request_data ): pass @@ -269,8 +280,8 @@ class Completer( object ): pass - def OnUserCommand( self, arguments ): - vimsupport.PostVimMessage( NO_USER_COMMANDS ) + def OnUserCommand( self, arguments, request_data ): + raise NotImplementedError( NO_USER_COMMANDS ) def OnCurrentIdentifierFinished( self ): @@ -285,7 +296,7 @@ class Completer( object ): return [] - def ShowDetailedDiagnostic( self ): + def GetDetailedDiagnostic( self ): pass @@ -293,8 +304,7 @@ class Completer( object ): return False - def _CurrentFiletype( self ): - filetypes = vimsupport.CurrentFiletypes() + def _CurrentFiletype( self, filetypes ): supported = self.SupportedFiletypes() for filetype in filetypes: @@ -321,9 +331,7 @@ class CompletionsCache( object ): self.filtered_completions = [] - def CacheValid( self, start_column ): - completion_line, _ = vimsupport.CurrentLineAndColumn() - completion_column = start_column - return completion_line == self.line and completion_column == self.column + def CacheValid( self, current_line, start_column ): + return current_line == self.line and start_column == self.column diff --git a/python/ycm/completers/completer_utils.py b/python/ycm/completers/completer_utils.py index 1f4d0e50..4fbd9b20 100644 --- a/python/ycm/completers/completer_utils.py +++ b/python/ycm/completers/completer_utils.py @@ -19,7 +19,6 @@ from collections import defaultdict from copy import deepcopy -import vim DEFAULT_FILETYPE_TRIGGERS = { 'c' : ['->', '.'], @@ -58,12 +57,9 @@ def _FiletypeDictUnion( dict_one, dict_two ): return final_dict -def TriggersForFiletype(): - user_triggers = _FiletypeTriggerDictFromSpec( - vim.eval( 'g:ycm_semantic_triggers' ) ) - +def TriggersForFiletype( user_triggers ): default_triggers = _FiletypeTriggerDictFromSpec( DEFAULT_FILETYPE_TRIGGERS ) - return _FiletypeDictUnion( default_triggers, user_triggers ) + return _FiletypeDictUnion( default_triggers, dict( user_triggers ) ) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 35d82572..fc976ab1 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -18,14 +18,23 @@ # along with YouCompleteMe. If not, see . from collections import defaultdict -import vim import ycm_core -from ycm import vimsupport +import logging +from ycm import server_responses from ycm import extra_conf_store from ycm.completers.completer import Completer from ycm.completers.cpp.flags import Flags CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] ) +MIN_LINES_IN_FILE_TO_PARSE = 5 +PARSING_FILE_MESSAGE = 'Still parsing file, no completions yet.' +NO_COMPILE_FLAGS_MESSAGE = 'Still no compile flags, no completions yet.' +NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?' +INVALID_FILE_MESSAGE = 'File is invalid.' +FILE_TOO_SHORT_MESSAGE = ( + 'File is less than {} lines long; not compiling.'.format( + MIN_LINES_IN_FILE_TO_PARSE ) ) +NO_DIAGNOSTIC_MESSAGE = 'No diagnostic for current line!' class ClangCompleter( Completer ): @@ -35,12 +44,11 @@ class ClangCompleter( Completer ): 'max_diagnostics_to_display' ] self.completer = ycm_core.ClangCompleter() self.completer.EnableThreading() - self.contents_holder = [] - self.filename_holder = [] self.last_prepared_diagnostics = [] self.parse_future = None self.flags = Flags() self.diagnostic_store = None + self._logger = logging.getLogger( __name__ ) # We set this flag when a compilation request comes in while one is already # in progress. We use this to trigger the pending request after the previous @@ -53,63 +61,52 @@ class ClangCompleter( Completer ): return CLANG_FILETYPES - def GetUnsavedFilesVector( self ): - # CAREFUL HERE! For UnsavedFile filename and contents we are referring - # directly to Python-allocated and -managed memory since we are accepting - # pointers to data members of python objects. We need to ensure that those - # objects outlive our UnsavedFile objects. This is why we need the - # contents_holder and filename_holder lists, to make sure the string objects - # are still around when we call CandidatesForQueryAndLocationInFile. We do - # this to avoid an extra copy of the entire file contents. - + def GetUnsavedFilesVector( self, request_data ): files = ycm_core.UnsavedFileVec() - self.contents_holder = [] - self.filename_holder = [] - for buffer in vimsupport.GetUnsavedBuffers(): - if not ClangAvailableForBuffer( buffer ): + for filename, file_data in request_data[ 'file_data' ].iteritems(): + if not ClangAvailableForFiletypes( file_data[ 'filetypes' ] ): continue - contents = '\n'.join( buffer ) - name = buffer.name - if not contents or not name: + contents = file_data[ 'contents' ] + if not contents or not filename: continue - self.contents_holder.append( contents ) - self.filename_holder.append( name ) unsaved_file = ycm_core.UnsavedFile() - unsaved_file.contents_ = self.contents_holder[ -1 ] - unsaved_file.length_ = len( self.contents_holder[ -1 ] ) - unsaved_file.filename_ = self.filename_holder[ -1 ] + unsaved_file.contents_ = contents + unsaved_file.length_ = len( contents ) + unsaved_file.filename_ = filename files.append( unsaved_file ) - return files - def CandidatesForQueryAsync( self, query, start_column ): - filename = vim.current.buffer.name + def CandidatesForQueryAsync( self, request_data ): + filename = request_data[ 'filepath' ] if not filename: return if self.completer.UpdatingTranslationUnit( filename ): - vimsupport.PostVimMessage( 'Still parsing file, no completions yet.' ) self.completions_future = None - return + self._logger.info( PARSING_FILE_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + PARSING_FILE_MESSAGE ) flags = self.flags.FlagsForFile( filename ) if not flags: - vimsupport.PostVimMessage( 'Still no compile flags, no completions yet.' ) self.completions_future = None - return + self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + NO_COMPILE_FLAGS_MESSAGE ) # TODO: sanitize query, probably in C++ code files = ycm_core.UnsavedFileVec() + query = request_data[ 'query' ] if not query: - files = self.GetUnsavedFilesVector() + files = self.GetUnsavedFilesVector( request_data ) - line, _ = vim.current.window.cursor - column = start_column + 1 + line = request_data[ 'line_num' ] + 1 + column = request_data[ 'start_column' ] + 1 self.completions_future = ( self.completer.CandidatesForQueryAndLocationInFileAsync( query, @@ -123,10 +120,11 @@ class ClangCompleter( Completer ): def CandidatesFromStoredRequest( self ): if not self.completions_future: return [] - results = [ CompletionDataToDict( x ) for x in + results = [ ConvertCompletionData( x ) for x in self.completions_future.GetResults() ] if not results: - vimsupport.PostVimMessage( 'No completions found; errors in the file?' ) + self._logger.warning( NO_COMPLETIONS_MESSAGE ) + raise RuntimeError( NO_COMPLETIONS_MESSAGE ) return results @@ -137,37 +135,37 @@ class ClangCompleter( Completer ): 'ClearCompilationFlagCache'] - def OnUserCommand( self, arguments ): + def OnUserCommand( self, arguments, request_data ): if not arguments: - self.EchoUserCommandsHelpMessage() - return + raise ValueError( self.UserCommandsHelpMessage() ) command = arguments[ 0 ] if command == 'GoToDefinition': - self._GoToDefinition() + self._GoToDefinition( request_data ) elif command == 'GoToDeclaration': - self._GoToDeclaration() + self._GoToDeclaration( request_data ) elif command == 'GoToDefinitionElseDeclaration': - self._GoToDefinitionElseDeclaration() + self._GoToDefinitionElseDeclaration( request_data ) elif command == 'ClearCompilationFlagCache': - self._ClearCompilationFlagCache() + self._ClearCompilationFlagCache( request_data ) - def _LocationForGoTo( self, goto_function ): - filename = vim.current.buffer.name + def _LocationForGoTo( self, goto_function, request_data ): + filename = request_data[ 'filepath' ] if not filename: - return None + self._logger.warning( INVALID_FILE_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + INVALID_FILE_MESSAGE ) flags = self.flags.FlagsForFile( filename ) if not flags: - vimsupport.PostVimMessage( 'Still no compile flags, can\'t compile.' ) - return None + self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + NO_COMPILE_FLAGS_MESSAGE ) files = self.GetUnsavedFilesVector() - line, column = vimsupport.CurrentLineAndColumn() - # Making the line & column 1-based instead of 0-based - line += 1 - column += 1 + line = request_data[ 'line_num' ] + 1 + column = request_data[ 'start_column' ] + 1 return getattr( self.completer, goto_function )( filename, line, @@ -176,39 +174,37 @@ class ClangCompleter( Completer ): flags ) - def _GoToDefinition( self ): + def _GoToDefinition( self, request_data ): location = self._LocationForGoTo( 'GetDefinitionLocation' ) if not location or not location.IsValid(): - vimsupport.PostVimMessage( 'Can\'t jump to definition.' ) - return + raise RuntimeError( 'Can\'t jump to definition.' ) - vimsupport.JumpToLocation( location.filename_, - location.line_number_, - location.column_number_ ) + return server_responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) - def _GoToDeclaration( self ): + def _GoToDeclaration( self, request_data ): location = self._LocationForGoTo( 'GetDeclarationLocation' ) if not location or not location.IsValid(): - vimsupport.PostVimMessage( 'Can\'t jump to declaration.' ) - return + raise RuntimeError( 'Can\'t jump to declaration.' ) - vimsupport.JumpToLocation( location.filename_, - location.line_number_, - location.column_number_ ) + return server_responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) - def _GoToDefinitionElseDeclaration( self ): + def _GoToDefinitionElseDeclaration( self, request_data ): location = self._LocationForGoTo( 'GetDefinitionLocation' ) if not location or not location.IsValid(): location = self._LocationForGoTo( 'GetDeclarationLocation' ) if not location or not location.IsValid(): - vimsupport.PostVimMessage( 'Can\'t jump to definition or declaration.' ) - return + raise RuntimeError( 'Can\'t jump to definition or declaration.' ) + + return server_responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) - vimsupport.JumpToLocation( location.filename_, - location.line_number_, - location.column_number_ ) def _ClearCompilationFlagCache( self ): @@ -216,14 +212,18 @@ class ClangCompleter( Completer ): - def OnFileReadyToParse( self ): - if vimsupport.NumLinesInBuffer( vim.current.buffer ) < 5: + def OnFileReadyToParse( self, request_data ): + filename = request_data[ 'filepath' ] + contents = request_data[ 'file_data' ][ filename ][ 'contents' ] + if contents.count( '\n' ) < MIN_LINES_IN_FILE_TO_PARSE: self.parse_future = None - return + self._logger.warning( FILE_TOO_SHORT_MESSAGE ) + raise ValueError( FILE_TOO_SHORT_MESSAGE ) - filename = vim.current.buffer.name if not filename: - return + self._logger.warning( INVALID_FILE_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + INVALID_FILE_MESSAGE ) if self.completer.UpdatingTranslationUnit( filename ): self.extra_parse_desired = True @@ -232,11 +232,13 @@ class ClangCompleter( Completer ): flags = self.flags.FlagsForFile( filename ) if not flags: self.parse_future = None - return + self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) + return server_responses.BuildDisplayMessageResponse( + NO_COMPILE_FLAGS_MESSAGE ) self.parse_future = self.completer.UpdateTranslationUnitAsync( filename, - self.GetUnsavedFilesVector(), + self.GetUnsavedFilesVector( request_data ), flags ) self.extra_parse_desired = False @@ -253,16 +255,18 @@ class ClangCompleter( Completer ): return self.parse_future.ResultsReady() - def GettingCompletions( self ): - return self.completer.UpdatingTranslationUnit( vim.current.buffer.name ) + def GettingCompletions( self, request_data ): + return self.completer.UpdatingTranslationUnit( request_data[ 'filepath' ] ) - def GetDiagnosticsForCurrentFile( self ): + def GetDiagnosticsForCurrentFile( self, request_data ): + filename = request_data[ 'filepath' ] if self.DiagnosticsForCurrentFileReady(): - diagnostics = self.completer.DiagnosticsForFile( vim.current.buffer.name ) + diagnostics = self.completer.DiagnosticsForFile( filename ) self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) - self.last_prepared_diagnostics = [ DiagnosticToDict( x ) for x in - diagnostics[ : self.max_diagnostics_to_display ] ] + self.last_prepared_diagnostics = [ + server_responses.BuildDiagnosticData( x ) for x in + diagnostics[ : self.max_diagnostics_to_display ] ] self.parse_future = None if self.extra_parse_desired: @@ -271,23 +275,19 @@ class ClangCompleter( Completer ): return self.last_prepared_diagnostics - def ShowDetailedDiagnostic( self ): - current_line, current_column = vimsupport.CurrentLineAndColumn() - - # CurrentLineAndColumn() numbers are 0-based, clang numbers are 1-based - current_line += 1 - current_column += 1 - - current_file = vim.current.buffer.name + def GetDetailedDiagnostic( self, request_data ): + current_line = request_data[ 'line_num' ] + 1 + current_column = request_data[ 'column_num' ] + 1 + current_file = request_data[ 'filepath' ] if not self.diagnostic_store: - vimsupport.PostVimMessage( "No diagnostic for current line!" ) - return + return server_responses.BuildDisplayMessageResponse( + NO_DIAGNOSTIC_MESSAGE ) diagnostics = self.diagnostic_store[ current_file ][ current_line ] if not diagnostics: - vimsupport.PostVimMessage( "No diagnostic for current line!" ) - return + return server_responses.BuildDisplayMessageResponse( + NO_DIAGNOSTIC_MESSAGE ) closest_diagnostic = None distance_to_closest_diagnostic = 999 @@ -298,50 +298,61 @@ class ClangCompleter( Completer ): distance_to_closest_diagnostic = distance closest_diagnostic = diagnostic - vimsupport.EchoText( closest_diagnostic.long_formatted_text_ ) + return server_responses.BuildDisplayMessageResponse( + closest_diagnostic.long_formatted_text_ ) - def ShouldUseNow( self, start_column, current_line ): + def ShouldUseNow( self, request_data ): # We don't want to use the Completer API cache, we use one in the C++ code. - return self.ShouldUseNowInner( start_column, current_line ) + return self.ShouldUseNowInner( request_data ) - def DebugInfo( self ): - filename = vim.current.buffer.name + def DebugInfo( self, request_data ): + filename = request_data[ 'filepath' ] if not filename: return '' flags = self.flags.FlagsForFile( filename ) or [] source = extra_conf_store.ModuleFileForSourceFile( filename ) - return 'Flags for {0} loaded from {1}:\n{2}'.format( filename, - source, - list( flags ) ) + return server_responses.BuildDisplayMessageResponse( + 'Flags for {0} loaded from {1}:\n{2}'.format( filename, + source, + list( flags ) ) ) # TODO: make these functions module-local -def CompletionDataToDict( completion_data ): - # see :h complete-items for a description of the dictionary fields - return { - 'word' : completion_data.TextToInsertInBuffer(), - 'abbr' : completion_data.MainCompletionText(), - 'menu' : completion_data.ExtraMenuInfo(), - 'kind' : completion_data.kind_, - 'info' : completion_data.DetailedInfoForPreviewWindow(), - 'dup' : 1, - } +# def CompletionDataToDict( completion_data ): +# # see :h complete-items for a description of the dictionary fields +# return { +# 'word' : completion_data.TextToInsertInBuffer(), +# 'abbr' : completion_data.MainCompletionText(), +# 'menu' : completion_data.ExtraMenuInfo(), +# 'kind' : completion_data.kind_, +# 'info' : completion_data.DetailedInfoForPreviewWindow(), +# 'dup' : 1, +# } -def DiagnosticToDict( diagnostic ): - # see :h getqflist for a description of the dictionary fields - return { - # TODO: wrap the bufnr generation into a function - 'bufnr' : int( vim.eval( "bufnr('{0}', 1)".format( - diagnostic.filename_ ) ) ), - 'lnum' : diagnostic.line_number_, - 'col' : diagnostic.column_number_, - 'text' : diagnostic.text_, - 'type' : diagnostic.kind_, - 'valid' : 1 - } +# def DiagnosticToDict( diagnostic ): +# # see :h getqflist for a description of the dictionary fields +# return { +# # TODO: wrap the bufnr generation into a function +# 'bufnr' : int( vim.eval( "bufnr('{0}', 1)".format( +# diagnostic.filename_ ) ) ), +# 'lnum' : diagnostic.line_number_, +# 'col' : diagnostic.column_number_, +# 'text' : diagnostic.text_, +# 'type' : diagnostic.kind_, +# 'valid' : 1 +# } + + +def ConvertCompletionData( completion_data ): + return server_responses.BuildCompletionData( + insertion_text = completion_data.TextToInsertInBuffer(), + menu_text = completion_data.MainCompletionText(), + extra_menu_info = completion_data.ExtraMenuInfo(), + kind = completion_data.kind_, + detailed_info = completion_data.DetailedInfoForPreviewWindow() ) def DiagnosticsToDiagStructure( diagnostics ): @@ -352,12 +363,9 @@ def DiagnosticsToDiagStructure( diagnostics ): return structure -def ClangAvailableForBuffer( buffer_object ): - filetypes = vimsupport.FiletypesForBuffer( buffer_object ) +def ClangAvailableForFiletypes( filetypes ): return any( [ filetype in CLANG_FILETYPES for filetype in filetypes ] ) -def InCFamilyFile(): - return any( [ filetype in CLANG_FILETYPES for filetype in - vimsupport.CurrentFiletypes() ] ) - +def InCFamilyFile( filetypes ): + return ClangAvailableForFiletypes( filetypes ) diff --git a/python/ycm/completers/cpp/flags.py b/python/ycm/completers/cpp/flags.py index 61a241f9..17c4981a 100644 --- a/python/ycm/completers/cpp/flags.py +++ b/python/ycm/completers/cpp/flags.py @@ -19,12 +19,11 @@ import ycm_core import os -from ycm import vimsupport from ycm import extra_conf_store -NO_EXTRA_CONF_FILENAME_MESSAGE = ('No {0} file detected, so no compile flags ' +NO_EXTRA_CONF_FILENAME_MESSAGE = ( 'No {0} file detected, so no compile flags ' 'are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE ' - 'DOCS *NOW*, DON\'T file a bug report.').format( + 'DOCS *NOW*, DON\'T file a bug report.' ).format( extra_conf_store.YCM_EXTRA_CONF_FILENAME ) @@ -37,7 +36,6 @@ class Flags( object ): # It's caches all the way down... self.flags_for_file = {} self.special_clang_flags = _SpecialClangIncludes() - self.no_extra_conf_file_warning_posted = False def FlagsForFile( self, filename, add_special_clang_flags = True ): @@ -46,10 +44,7 @@ class Flags( object ): except KeyError: module = extra_conf_store.ModuleForSourceFile( filename ) if not module: - if not self.no_extra_conf_file_warning_posted: - vimsupport.PostVimMessage( NO_EXTRA_CONF_FILENAME_MESSAGE ) - self.no_extra_conf_file_warning_posted = True - return None + raise RuntimeError( NO_EXTRA_CONF_FILENAME_MESSAGE ) results = module.FlagsForFile( filename ) diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index a83b3a4e..f63b3b67 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -18,18 +18,18 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import vim import os from sys import platform import glob from ycm.completers.threaded_completer import ThreadedCompleter -from ycm import vimsupport +from ycm import server_responses import urllib2 import urllib import urlparse import json import subprocess import tempfile +import logging SERVER_NOT_FOUND_MSG = ( 'OmniSharp server binary not found at {0}. ' + @@ -44,9 +44,10 @@ class CsharpCompleter( ThreadedCompleter ): def __init__( self, user_options ): super( CsharpCompleter, self ).__init__( user_options ) self._omnisharp_port = None + self._logger = logging.getLogger(__name__) - if self.user_options[ 'auto_start_csharp_server' ]: - self._StartServer() + # if self.user_options[ 'auto_start_csharp_server' ]: + # self._StartServer() def OnVimLeave( self ): @@ -60,11 +61,12 @@ class CsharpCompleter( ThreadedCompleter ): return [ 'cs' ] - def ComputeCandidates( self, unused_query, unused_start_column ): - return [ { 'word': str( completion[ 'CompletionText' ] ), - 'menu': str( completion[ 'DisplayText' ] ), - 'info': str( completion[ 'Description' ] ) } - for completion in self._GetCompletions() ] + def ComputeCandidates( self, request_data ): + return [ server_responses.BuildCompletionData( + completion[ 'CompletionText' ], + completion[ 'DisplayText' ], + completion[ 'Description' ] ) + for completion in self._GetCompletions( request_data ) ] def DefinedSubcommands( self ): @@ -76,24 +78,23 @@ class CsharpCompleter( ThreadedCompleter ): 'GoToDefinitionElseDeclaration' ] - def OnUserCommand( self, arguments ): + def OnUserCommand( self, arguments, request_data ): if not arguments: - self.EchoUserCommandsHelpMessage() - return + raise ValueError( self.UserCommandsHelpMessage() ) command = arguments[ 0 ] if command == 'StartServer': - self._StartServer() + self._StartServer( request_data ) elif command == 'StopServer': self._StopServer() elif command == 'RestartServer': if self._ServerIsRunning(): self._StopServer() - self._StartServer() + self._StartServer( request_data ) elif command in [ 'GoToDefinition', 'GoToDeclaration', 'GoToDefinitionElseDeclaration' ]: - self._GoToDefinition() + return self._GoToDefinition( request_data ) def DebugInfo( self ): @@ -104,35 +105,27 @@ class CsharpCompleter( ThreadedCompleter ): return 'Server is not running' - def _StartServer( self ): + def _StartServer( self, request_data ): """ Start the OmniSharp server """ self._omnisharp_port = self._FindFreePort() - solutionfiles, folder = _FindSolutionFiles() + solutionfiles, folder = _FindSolutionFiles( request_data[ 'filepath' ] ) if len( solutionfiles ) == 0: - vimsupport.PostVimMessage( - 'Error starting OmniSharp server: no solutionfile found' ) - return + raise RuntimeError( + 'Error starting OmniSharp server: no solutionfile found' ) elif len( solutionfiles ) == 1: solutionfile = solutionfiles[ 0 ] else: - choice = vimsupport.PresentDialog( - 'Which solutionfile should be loaded?', - [ str( i ) + " " + solution for i, solution in - enumerate( solutionfiles ) ] ) - if choice == -1: - vimsupport.PostVimMessage( 'OmniSharp not started' ) - return - else: - solutionfile = solutionfiles[ choice ] + raise RuntimeError( + 'Found multiple solution files instead of one!\n{}'.format( + solutionfiles ) ) omnisharp = os.path.join( os.path.abspath( os.path.dirname( __file__ ) ), 'OmniSharpServer/OmniSharp/bin/Debug/OmniSharp.exe' ) if not os.path.isfile( omnisharp ): - vimsupport.PostVimMessage( SERVER_NOT_FOUND_MSG.format( omnisharp ) ) - return + raise RuntimeError( SERVER_NOT_FOUND_MSG.format( omnisharp ) ) if not platform.startswith( 'win' ): omnisharp = 'mono ' + omnisharp @@ -154,40 +147,44 @@ class CsharpCompleter( ThreadedCompleter ): with open( self._filename_stdout, 'w' ) as fstdout: subprocess.Popen( command, stdout=fstdout, stderr=fstderr, shell=True ) - vimsupport.PostVimMessage( 'Starting OmniSharp server' ) + self._logger.info( 'Starting OmniSharp server' ) def _StopServer( self ): """ Stop the OmniSharp server """ self._GetResponse( '/stopserver' ) self._omnisharp_port = None - vimsupport.PostVimMessage( 'Stopping OmniSharp server' ) + self._logger.info( 'Stopping OmniSharp server' ) - def _GetCompletions( self ): + def _GetCompletions( self, request_data ): """ Ask server for completions """ - completions = self._GetResponse( '/autocomplete', self._DefaultParameters() ) + completions = self._GetResponse( '/autocomplete', + self._DefaultParameters( request_data ) ) return completions if completions != None else [] - def _GoToDefinition( self ): + def _GoToDefinition( self, request_data ): """ Jump to definition of identifier under cursor """ - definition = self._GetResponse( '/gotodefinition', self._DefaultParameters() ) + definition = self._GetResponse( '/gotodefinition', + self._DefaultParameters( request_data ) ) if definition[ 'FileName' ] != None: - vimsupport.JumpToLocation( definition[ 'FileName' ], - definition[ 'Line' ], - definition[ 'Column' ] ) + return server_responses.BuildGoToResponse( definition[ 'FileName' ], + definition[ 'Line' ], + definition[ 'Column' ] ) else: - vimsupport.PostVimMessage( 'Can\'t jump to definition' ) + raise RuntimeError( 'Can\'t jump to definition' ) - def _DefaultParameters( self ): + def _DefaultParameters( self, request_data ): """ Some very common request parameters """ - line, column = vimsupport.CurrentLineAndColumn() parameters = {} - parameters[ 'line' ], parameters[ 'column' ] = line + 1, column + 1 - parameters[ 'buffer' ] = '\n'.join( vim.current.buffer ) - parameters[ 'filename' ] = vim.current.buffer.name + parameters[ 'line' ] = request_data[ 'line_num' ] + 1 + parameters[ 'column' ] = request_data[ 'column_num' ] + 1 + filepath = request_data[ 'filepath' ] + parameters[ 'buffer' ] = request_data[ 'file_data' ][ filepath ][ + 'contents' ] + parameters[ 'filename' ] = filepath return parameters @@ -215,20 +212,16 @@ class CsharpCompleter( ThreadedCompleter ): def _GetResponse( self, endPoint, parameters={}, silent=False, port=None ): """ Handle communication with server """ + # TODO: Replace usage of urllib with Requests target = urlparse.urljoin( self._PortToHost( port ), endPoint ) parameters = urllib.urlencode( parameters ) - try: - response = urllib2.urlopen( target, parameters ) - return json.loads( response.read() ) - except Exception: - # TODO: Add logging for this case. We can't post a Vim message because Vim - # crashes when that's done from a no-GUI thread. - return None + response = urllib2.urlopen( target, parameters ) + return json.loads( response.read() ) -def _FindSolutionFiles(): +def _FindSolutionFiles( filepath ): """ Find solution files by searching upwards in the file tree """ - folder = os.path.dirname( vim.current.buffer.name ) + folder = os.path.dirname( filepath ) solutionfiles = glob.glob1( folder, '*.sln' ) while not solutionfiles: lastfolder = folder diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index abd22b41..985bda04 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -16,13 +16,13 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import vim import os import re from ycm.completers.threaded_completer import ThreadedCompleter from ycm.completers.cpp.clang_completer import InCFamilyFile from ycm.completers.cpp.flags import Flags +from ycm import server_responses class FilenameCompleter( ThreadedCompleter ): """ @@ -52,23 +52,32 @@ class FilenameCompleter( ThreadedCompleter ): self._include_regex = re.compile( include_regex_common ) - def AtIncludeStatementStart( self, start_column ): - return ( InCFamilyFile() and + def AtIncludeStatementStart( self, request_data ): + start_column = request_data[ 'start_column' ] + current_line = request_data[ 'line_value' ] + filepath = request_data[ 'filepath' ] + filetypes = request_data[ 'file_data' ][ filepath ][ 'filetypes' ] + return ( InCFamilyFile( filetypes ) and self._include_start_regex.match( - vim.current.line[ :start_column ] ) ) + current_line[ :start_column ] ) ) - def ShouldUseNowInner( self, start_column, current_line ): + def ShouldUseNowInner( self, request_data ): + start_column = request_data[ 'start_column' ] + current_line = request_data[ 'line_value' ] return ( start_column and ( current_line[ start_column - 1 ] == '/' or - self.AtIncludeStatementStart( start_column ) ) ) + self.AtIncludeStatementStart( request_data ) ) ) def SupportedFiletypes( self ): return [] - def ComputeCandidates( self, unused_query, start_column ): - line = vim.current.line[ :start_column ] + def ComputeCandidates( self, request_data ): + current_line = request_data[ 'line_value' ] + start_column = request_data[ 'start_column' ] + filepath = request_data[ 'filepath' ] + line = current_line[ :start_column ] if InCFamilyFile(): include_match = self._include_regex.search( line ) @@ -78,22 +87,26 @@ class FilenameCompleter( ThreadedCompleter ): # http://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html include_current_file_dir = '<' not in include_match.group() return _GenerateCandidatesForPaths( - self.GetPathsIncludeCase( path_dir, include_current_file_dir ) ) + self.GetPathsIncludeCase( path_dir, + include_current_file_dir, + filepath ) ) path_match = self._path_regex.search( line ) path_dir = os.path.expanduser( path_match.group() ) if path_match else '' return _GenerateCandidatesForPaths( - _GetPathsStandardCase( path_dir, self.user_options[ - 'filepath_completion_use_working_dir' ] ) ) + _GetPathsStandardCase( + path_dir, + self.user_options[ 'filepath_completion_use_working_dir' ], + filepath ) ) - def GetPathsIncludeCase( self, path_dir, include_current_file_dir ): + def GetPathsIncludeCase( self, path_dir, include_current_file_dir, filepath ): paths = [] - include_paths = self._flags.UserIncludePaths( vim.current.buffer.name ) + include_paths = self._flags.UserIncludePaths( filepath ) if include_current_file_dir: - include_paths.append( os.path.dirname( vim.current.buffer.name ) ) + include_paths.append( os.path.dirname( filepath ) ) for include_path in include_paths: try: @@ -107,9 +120,9 @@ class FilenameCompleter( ThreadedCompleter ): return sorted( set( paths ) ) -def _GetPathsStandardCase( path_dir, use_working_dir ): +def _GetPathsStandardCase( path_dir, use_working_dir, filepath ): if not use_working_dir and not path_dir.startswith( '/' ): - path_dir = os.path.join( os.path.dirname( vim.current.buffer.name ), + path_dir = os.path.join( os.path.dirname( filepath ), path_dir ) try: @@ -132,8 +145,8 @@ def _GenerateCandidatesForPaths( absolute_paths ): seen_basenames.add( basename ) is_dir = os.path.isdir( absolute_path ) - completion_dicts.append( { 'word': basename, - 'dup': 1, - 'menu': '[Dir]' if is_dir else '[File]' } ) + completion_dicts.append( + server_responses.BuildCompletionData( basename, + '[Dir]' if is_dir else '[File]' ) ) return completion_dicts diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 8d0cd55b..fcfe5f76 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -58,18 +58,17 @@ class GeneralCompleterStore( Completer ): return set() - def ShouldUseNow( self, start_column, current_line ): + def ShouldUseNow( self, request_data ): self._current_query_completers = [] - if self._filename_completer.ShouldUseNow( start_column, current_line ): + if self._filename_completer.ShouldUseNow( request_data ): self._current_query_completers = [ self._filename_completer ] return True should_use_now = False for completer in self._non_filename_completers: - should_use_this_completer = completer.ShouldUseNow( start_column, - current_line ) + should_use_this_completer = completer.ShouldUseNow( request_data ) should_use_now = should_use_now or should_use_this_completer if should_use_this_completer: @@ -78,9 +77,9 @@ class GeneralCompleterStore( Completer ): return should_use_now - def CandidatesForQueryAsync( self, query, start_column ): + def CandidatesForQueryAsync( self, request_data ): for completer in self._current_query_completers: - completer.CandidatesForQueryAsync( query, start_column ) + completer.CandidatesForQueryAsync( request_data ) def AsyncCandidateRequestReady( self ): @@ -96,9 +95,9 @@ class GeneralCompleterStore( Completer ): return candidates - def OnFileReadyToParse( self ): + def OnFileReadyToParse( self, request_data ): for completer in self._all_completers: - completer.OnFileReadyToParse() + completer.OnFileReadyToParse( request_data ) def OnBufferVisit( self ): diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index 50895910..d16b174f 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -20,6 +20,7 @@ from ycm.completers.general_completer import GeneralCompleter from UltiSnips import UltiSnips_Manager +from ycm import server_responses class UltiSnipsCompleter( GeneralCompleter ): @@ -33,13 +34,13 @@ class UltiSnipsCompleter( GeneralCompleter ): self._filtered_candidates = None - def ShouldUseNowInner( self, start_column, unused_current_line ): - return self.QueryLengthAboveMinThreshold( start_column ) + def ShouldUseNowInner( self, request_data ): + return self.QueryLengthAboveMinThreshold( request_data ) - def CandidatesForQueryAsync( self, query, unused_start_column ): - self._filtered_candidates = self.FilterAndSortCandidates( self._candidates, - query ) + def CandidatesForQueryAsync( self, request_data ): + self._filtered_candidates = self.FilterAndSortCandidates( + self._candidates, request_data[ 'query' ] ) def AsyncCandidateRequestReady( self ): @@ -61,8 +62,9 @@ def _GetCandidates(): # UltiSnips_Manager._snips() returns a class instance where: # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) # class.description - description of the snippet - return [ { 'word': str( snip.trigger ), - 'menu': str( ' ' + snip.description.encode('utf-8') ) } - for snip in rawsnips ] + return [ server_responses.BuildCompletionData( + str( snip.trigger ), + str( ' ' + snip.description.encode( 'utf-8' ) ) ) + for snip in rawsnips ] except: return [] diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index cde83b63..7277d7bc 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -19,9 +19,8 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import vim from ycm.completers.threaded_completer import ThreadedCompleter -from ycm import vimsupport +from ycm import server_responses import sys from os.path import join, abspath, dirname @@ -33,7 +32,7 @@ sys.path.insert( 0, join( abspath( dirname( __file__ ) ), 'jedi' ) ) try: import jedi except ImportError: - vimsupport.PostVimMessage( + raise ImportError( 'Error importing jedi. Make sure the jedi submodule has been checked out. ' 'In the YouCompleteMe folder, run "git submodule update --init --recursive"') sys.path.pop( 0 ) @@ -54,113 +53,108 @@ class JediCompleter( ThreadedCompleter ): return [ 'python' ] - def _GetJediScript( self ): - contents = '\n'.join( vim.current.buffer ) - line, column = vimsupport.CurrentLineAndColumn() + def _GetJediScript( self, request_data ): + filename = request_data[ 'filepath' ] + contents = request_data[ 'file_data' ][ filename ][ 'contents' ] # Jedi expects lines to start at 1, not 0 - line += 1 - filename = vim.current.buffer.name + line = request_data[ 'line_num' ] + 1 + column = request_data[ 'column_num' ] + print contents return jedi.Script( contents, line, column, filename ) - def ComputeCandidates( self, unused_query, unused_start_column ): - script = self._GetJediScript() - - return [ { 'word': str( completion.name ), - 'menu': str( completion.description ), - 'info': str( completion.doc ) } + def ComputeCandidates( self, request_data ): + script = self._GetJediScript( request_data ) + return [ server_responses.BuildCompletionData( completion.name, + completion.description, + completion.doc ) for completion in script.completions() ] - def DefinedSubcommands( self ): return [ "GoToDefinition", "GoToDeclaration", "GoToDefinitionElseDeclaration" ] - def OnUserCommand( self, arguments ): + def OnUserCommand( self, arguments, request_data ): if not arguments: - self.EchoUserCommandsHelpMessage() - return + raise ValueError( self.UserCommandsHelpMessage() ) command = arguments[ 0 ] if command == 'GoToDefinition': - self._GoToDefinition() + return self._GoToDefinition( request_data ) elif command == 'GoToDeclaration': - self._GoToDeclaration() + return self._GoToDeclaration( request_data ) elif command == 'GoToDefinitionElseDeclaration': - self._GoToDefinitionElseDeclaration() + return self._GoToDefinitionElseDeclaration( request_data ) - def _GoToDefinition( self ): - definitions = self._GetDefinitionsList() + def _GoToDefinition( self, request_data ): + definitions = self._GetDefinitionsList( request_data ) if definitions: - self._JumpToLocation( definitions ) + return self._BuildGoToResponse( definitions ) else: - vimsupport.PostVimMessage( 'Can\'t jump to definition.' ) + raise RuntimeError( 'Can\'t jump to definition.' ) - def _GoToDeclaration( self ): - definitions = self._GetDefinitionsList( declaration = True ) + def _GoToDeclaration( self, request_data ): + definitions = self._GetDefinitionsList( request_data, declaration = True ) if definitions: - self._JumpToLocation( definitions ) + return self._BuildGoToResponse( definitions ) else: - vimsupport.PostVimMessage( 'Can\'t jump to declaration.' ) + raise RuntimeError( 'Can\'t jump to declaration.' ) - def _GoToDefinitionElseDeclaration( self ): + def _GoToDefinitionElseDeclaration( self, request_data ): definitions = self._GetDefinitionsList() or \ - self._GetDefinitionsList( declaration = True ) + self._GetDefinitionsList( request_data, declaration = True ) if definitions: - self._JumpToLocation( definitions ) + return self._BuildGoToResponse( definitions ) else: - vimsupport.PostVimMessage( 'Can\'t jump to definition or declaration.' ) + raise RuntimeError( 'Can\'t jump to definition or declaration.' ) - def _GetDefinitionsList( self, declaration = False ): + def _GetDefinitionsList( self, request_data, declaration = False ): definitions = [] - script = self._GetJediScript() + script = self._GetJediScript( request_data ) try: if declaration: definitions = script.goto_definitions() else: definitions = script.goto_assignments() except jedi.NotFoundError: - vimsupport.PostVimMessage( - "Cannot follow nothing. Put your cursor on a valid name." ) - except Exception as e: - vimsupport.PostVimMessage( - "Caught exception, aborting. Full error: " + str( e ) ) + raise RuntimeError( + 'Cannot follow nothing. Put your cursor on a valid name.' ) return definitions - def _JumpToLocation( self, definition_list ): + def _BuildGoToResponse( self, definition_list ): if len( definition_list ) == 1: definition = definition_list[ 0 ] if definition.in_builtin_module(): if definition.is_keyword: - vimsupport.PostVimMessage( - "Cannot get the definition of Python keywords." ) + raise RuntimeError( + 'Cannot get the definition of Python keywords.' ) else: - vimsupport.PostVimMessage( "Builtin modules cannot be displayed." ) + raise RuntimeError( 'Builtin modules cannot be displayed.' ) else: - vimsupport.JumpToLocation( definition.module_path, - definition.line, - definition.column + 1 ) + return server_responses.BuildGoToResponse( definition.module_path, + definition.line -1, + definition.column ) else: # multiple definitions defs = [] for definition in definition_list: if definition.in_builtin_module(): - defs.append( {'text': 'Builtin ' + \ - definition.description.encode( 'utf-8' ) } ) + defs.append( server_responses.BuildDescriptionOnlyGoToResponse( + 'Builting ' + definition.description ) ) else: - defs.append( {'filename': definition.module_path.encode( 'utf-8' ), - 'lnum': definition.line, - 'col': definition.column + 1, - 'text': definition.description.encode( 'utf-8' ) } ) + defs.append( + server_responses.BuildGoToResponse( definition.module_path, + definition.line -1, + definition.column, + definition.description ) ) + return defs - vim.eval( 'setqflist( %s )' % repr( defs ) ) - vim.eval( 'youcompleteme#OpenGoToList()' ) diff --git a/python/ycm/completers/threaded_completer.py b/python/ycm/completers/threaded_completer.py index 6d048676..cf1a1b13 100644 --- a/python/ycm/completers/threaded_completer.py +++ b/python/ycm/completers/threaded_completer.py @@ -52,11 +52,10 @@ class ThreadedCompleter( Completer ): self._completion_thread.start() - def CandidatesForQueryAsyncInner( self, query, start_column ): + def CandidatesForQueryAsyncInner( self, request_data ): self._candidates = None self._candidates_ready.clear() - self._query = query - self._start_column = start_column + self._request_data = request_data self._query_ready.set() @@ -69,7 +68,7 @@ class ThreadedCompleter( Completer ): @abc.abstractmethod - def ComputeCandidates( self, query, start_column ): + def ComputeCandidates( self, request_data ): """This function should compute the candidates to show to the user. The return value should be of the same type as that for CandidatesFromStoredRequest().""" @@ -80,8 +79,7 @@ class ThreadedCompleter( Completer ): while True: try: WaitAndClearIfSet( self._query_ready ) - self._candidates = self.ComputeCandidates( self._query, - self._start_column ) + self._candidates = self.ComputeCandidates( self._request_data ) except: self._query_ready.clear() self._candidates = [] diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 3248dc1d..5b4814d3 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -68,6 +68,7 @@ def CallExtraConfVimCloseIfExists(): def _CallExtraConfMethod( function_name ): vim_current_working_directory = vim.eval( 'getcwd()' ) path_to_dummy = os.path.join( vim_current_working_directory, 'DUMMY_FILE' ) + # The dummy file in the Vim CWD ensures we find the correct extra conf file module = ModuleForSourceFile( path_to_dummy ) if not module or not hasattr( module, function_name ): return diff --git a/python/ycm/server_responses.py b/python/ycm/server_responses.py new file mode 100644 index 00000000..f8414d9c --- /dev/null +++ b/python/ycm/server_responses.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + + +def BuildGoToResponse( filepath, line_num, column_num, description = None ): + response = { + 'filepath': filepath, + 'line_num': line_num, + 'column_num': column_num + } + + if description: + response[ 'description' ] = description + return response + + +def BuildDescriptionOnlyGoToResponse( text ): + return { + 'description': text, + } + + +def BuildDisplayMessageResponse( text ): + return { + 'message': text + } + + +def BuildCompletionData( insertion_text, + extra_menu_info = None, + detailed_info = None, + menu_text = None, + kind = None ): + completion_data = { + 'insertion_text': insertion_text + } + + if extra_menu_info: + completion_data[ 'extra_menu_info' ] = extra_menu_info + if menu_text: + completion_data[ 'menu_text' ] = menu_text + if detailed_info: + completion_data[ 'detailed_info' ] = detailed_info + if kind: + completion_data[ 'kind' ] = kind + return completion_data + + +def BuildDiagnosticData( filepath, + line_num, + column_num, + text, + kind ): + return { + 'filepath': filepath, + 'line_num': line_num, + 'column_num': column_num, + 'text': text, + 'kind': kind + } diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 209319b7..4ce05fb1 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -46,12 +46,33 @@ def TextAfterCursor(): return vim.current.line[ CurrentColumn(): ] -def GetUnsavedBuffers(): - def BufferModified( buffer_number ): - to_eval = 'getbufvar({0}, "&mod")'.format( buffer_number ) - return GetBoolValue( to_eval ) +# Note the difference between buffer OPTIONS and VARIABLES; the two are not +# the same. +def GetBufferOption( buffer_object, option ): + # The 'options' property is only available in recent (7.4+) Vim builds + if hasattr( buffer_object, 'options' ): + return buffer_object.options[ option ] - return ( x for x in vim.buffers if BufferModified( x.number ) ) + to_eval = 'getbufvar({0}, "&{1}")'.format( buffer.number, option ) + return GetVariableValue( to_eval ) + + +def GetUnsavedAndCurrentBufferData(): + def BufferModified( buffer_object ): + return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) + + buffers_data = {} + for buffer_object in vim.buffers: + if not ( BufferModified( buffer_object ) or + buffer_object == vim.current.buffer ): + continue + + buffers_data[ buffer_object.name ] = { + 'contents': '\n'.join( buffer_object ), + 'filetypes': FiletypesForBuffer( buffer_object ) + } + + return buffers_data # Both |line| and |column| need to be 1-based @@ -73,9 +94,9 @@ def JumpToLocation( filename, line, column ): vim.command( 'normal! zz' ) -def NumLinesInBuffer( buffer ): +def NumLinesInBuffer( buffer_object ): # This is actually less than obvious, that's why it's wrapped in a function - return len( buffer ) + return len( buffer_object ) def PostVimMessage( message ): @@ -128,15 +149,13 @@ def EscapeForVim( text ): def CurrentFiletypes(): - ft_string = vim.eval( "&filetype" ) - return ft_string.split( '.' ) + return vim.eval( "&filetype" ).split( '.' ) def FiletypesForBuffer( buffer_object ): # NOTE: Getting &ft for other buffers only works when the buffer has been # visited by the user at least once, which is true for modified buffers - ft_string = vim.eval( 'getbufvar({0}, "&ft")'.format( buffer_object.number ) ) - return ft_string.split( '.' ) + return GetBufferOption( buffer_object, 'ft' ).split( '.' ) def GetVariableValue( variable ): diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 80132729..1e8d8efb 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -19,20 +19,25 @@ import imp import os +import time import vim import ycm_core +import logging +import tempfile from ycm import vimsupport from ycm import base from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general.general_completer_store import GeneralCompleterStore +# TODO: Put the Request classes in separate files class CompletionRequest( object ): def __init__( self, ycm_state ): self._completion_start_column = base.CompletionStartColumn() self._ycm_state = ycm_state + self._request_data = _BuildRequestData( self._completion_start_column ) self._do_filetype_completion = self._ycm_state.ShouldUseFiletypeCompleter( - self._completion_start_column ) + self._request_data ) self._completer = ( self._ycm_state.GetFiletypeCompleter() if self._do_filetype_completion else self._ycm_state.GetGeneralCompleter() ) @@ -40,8 +45,7 @@ class CompletionRequest( object ): def ShouldComplete( self ): return ( self._do_filetype_completion or - self._ycm_state.ShouldUseGeneralCompleter( - self._completion_start_column ) ) + self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) ) def CompletionStartColumn( self ): @@ -49,20 +53,80 @@ class CompletionRequest( object ): def Start( self, query ): - self._completer.CandidatesForQueryAsync( query, - self._completion_start_column ) + self._request_data[ 'query' ] = query + self._completer.CandidatesForQueryAsync( self._request_data ) def Done( self ): return self._completer.AsyncCandidateRequestReady() def Results( self ): - return self._completer.CandidatesFromStoredRequest() + try: + return [ _ConvertCompletionDataToVimData( x ) + for x in self._completer.CandidatesFromStoredRequest() ] + except Exception as e: + vimsupport.PostVimMessage( str( e ) ) + return [] + + + +class CommandRequest( object ): + class ServerResponse( object ): + def __init__( self ): + pass + + def Valid( self ): + return True + + def __init__( self, ycm_state, arguments, completer_target = None ): + if not completer_target: + completer_target = 'filetpe_default' + + if completer_target == 'omni': + self._completer = ycm_state.GetOmniCompleter() + elif completer_target == 'identifier': + self._completer = ycm_state.GetGeneralCompleter() + else: + self._completer = ycm_state.GetFiletypeCompleter() + self._arguments = arguments + + + def Start( self ): + self._completer.OnUserCommand( self._arguments, + _BuildRequestData() ) + + def Done( self ): + return True + + + def Response( self ): + # TODO: Call vimsupport.JumpToLocation if the user called a GoTo command... + # we may want to have specific subclasses of CommandRequest so that a + # GoToRequest knows it needs to jump after the data comes back. + # + # Also need to run the following on GoTo data: + # CAREFUL about line/column number 0-based/1-based confusion! + # + # defs = [] + # defs.append( {'filename': definition.module_path.encode( 'utf-8' ), + # 'lnum': definition.line, + # 'col': definition.column + 1, + # 'text': definition.description.encode( 'utf-8' ) } ) + # vim.eval( 'setqflist( %s )' % repr( defs ) ) + # vim.eval( 'youcompleteme#OpenGoToList()' ) + return self.ServerResponse() class YouCompleteMe( object ): def __init__( self, user_options ): + # TODO: This should go into the server + # TODO: Use more logging like we do in cs_completer + self._logfile = tempfile.NamedTemporaryFile() + logging.basicConfig( format='%(asctime)s - %(levelname)s - %(message)s', + filename=self._logfile.name, + level=logging.DEBUG ) + self._user_options = user_options self._gencomp = GeneralCompleterStore( user_options ) self._omnicomp = OmniCompleter( user_options ) @@ -78,6 +142,16 @@ class YouCompleteMe( object ): return self._current_completion_request + def SendCommandRequest( self, arguments, completer ): + # TODO: This should be inside a method in a command_request module + request = CommandRequest( self, arguments, completer ) + request.Start() + while not request.Done(): + time.sleep( 0.1 ) + + return request.Response() + + def GetCurrentCompletionRequest( self ): return self._current_completion_request @@ -131,14 +205,13 @@ class YouCompleteMe( object ): return completer - def ShouldUseGeneralCompleter( self, start_column ): - return self._gencomp.ShouldUseNow( start_column, vim.current.line ) + def ShouldUseGeneralCompleter( self, request_data ): + return self._gencomp.ShouldUseNow( request_data ) - def ShouldUseFiletypeCompleter( self, start_column ): + def ShouldUseFiletypeCompleter( self, request_data ): if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().ShouldUseNow( - start_column, vim.current.line ) + return self.GetFiletypeCompleter().ShouldUseNow( request_data ) return False @@ -162,10 +235,10 @@ class YouCompleteMe( object ): def OnFileReadyToParse( self ): - self._gencomp.OnFileReadyToParse() + self._gencomp.OnFileReadyToParse( _BuildRequestData() ) if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnFileReadyToParse() + self.GetFiletypeCompleter().OnFileReadyToParse( _BuildRequestData() ) def OnBufferUnload( self, deleted_buffer_file ): @@ -208,9 +281,9 @@ class YouCompleteMe( object ): return [] - def ShowDetailedDiagnostic( self ): + def GetDetailedDiagnostic( self ): if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().ShowDetailedDiagnostic() + return self.GetFiletypeCompleter().GetDetailedDiagnostic() def GettingCompletions( self ): @@ -263,3 +336,37 @@ def _PathToFiletypeCompleterPluginLoader( filetype ): return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' ) +def _BuildRequestData( start_column = None, query = None ): + line, column = vimsupport.CurrentLineAndColumn() + request_data = { + 'filetypes': vimsupport.CurrentFiletypes(), + 'line_num': line, + 'column_num': column, + 'start_column': start_column, + 'line_value': vim.current.line, + 'filepath': vim.current.buffer.name, + 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() + } + + if query: + request_data[ 'query' ] = query + + return request_data + +def _ConvertCompletionDataToVimData( completion_data ): + # see :h complete-items for a description of the dictionary fields + vim_data = { + 'word' : completion_data[ 'insertion_text' ], + 'dup' : 1, + } + + if 'menu_text' in completion_data: + vim_data[ 'abbr' ] = completion_data[ 'menu_text' ] + if 'extra_menu_info' in completion_data: + vim_data[ 'menu' ] = completion_data[ 'extra_menu_info' ] + if 'kind' in completion_data: + vim_data[ 'kind' ] = completion_data[ 'kind' ] + if 'detailed_info' in completion_data: + vim_data[ 'info' ] = completion_data[ 'detailed_info' ] + + return vim_data From 3f0b7198461214dd42e874fec56b709354c8a82c Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 7 Sep 2013 11:58:42 -0700 Subject: [PATCH 010/149] Encoding data to utf8 if 'unicode' object ycm_core only deals with utf8 and the functions only accept python string objects. --- python/ycm/completers/completer.py | 3 ++- python/ycm/completers/cpp/clang_completer.py | 7 +++++-- python/ycm/utils.py | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 60dcd6ec..6e2f1486 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -19,6 +19,7 @@ import abc import ycm_core +from ycm.utils import ToUtf8IfNeeded from ycm.completers.completer_utils import TriggersForFiletype NO_USER_COMMANDS = 'This completer does not define any commands.' @@ -177,7 +178,7 @@ class Completer( object ): self.completions_cache.filtered_completions = ( self.FilterAndSortCandidates( self.completions_cache.raw_completions, - request_data[ 'query' ] ) ) + ToUtf8IfNeeded( request_data[ 'query' ] ) ) ) else: self.completions_cache = None self.CandidatesForQueryAsyncInner( request_data ) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index fc976ab1..73e90eef 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -22,6 +22,7 @@ import ycm_core import logging from ycm import server_responses from ycm import extra_conf_store +from ycm.utils import ToUtf8IfNeeded from ycm.completers.completer import Completer from ycm.completers.cpp.flags import Flags @@ -109,8 +110,8 @@ class ClangCompleter( Completer ): column = request_data[ 'start_column' ] + 1 self.completions_future = ( self.completer.CandidatesForQueryAndLocationInFileAsync( - query, - filename, + ToUtf8IfNeeded( query ), + ToUtf8IfNeeded( filename ), line, column, files, @@ -369,3 +370,5 @@ def ClangAvailableForFiletypes( filetypes ): def InCFamilyFile( filetypes ): return ClangAvailableForFiletypes( filetypes ) + + diff --git a/python/ycm/utils.py b/python/ycm/utils.py index d1fe5035..81e4bd77 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -24,3 +24,8 @@ def IsIdentifierChar( char ): def SanitizeQuery( query ): return query.strip() + +def ToUtf8IfNeeded( string_or_unicode ): + if isinstance( string_or_unicode, unicode ): + return string_or_unicode.encode( 'utf8' ) + return string_or_unicode From a7c609efd77fc6516aae79cdcb23fe6f268bfd23 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 7 Sep 2013 12:33:01 -0700 Subject: [PATCH 011/149] More python unicode object support in ycm_core --- cpp/ycm/PythonSupport.cpp | 12 ++++++++++-- python/ycm/completers/completer.py | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cpp/ycm/PythonSupport.cpp b/cpp/ycm/PythonSupport.cpp index 635ae5ed..7b94a7a3 100644 --- a/cpp/ycm/PythonSupport.cpp +++ b/cpp/ycm/PythonSupport.cpp @@ -27,6 +27,7 @@ using boost::algorithm::any_of; using boost::algorithm::is_upper; using boost::python::len; +using boost::python::str; using boost::python::extract; using boost::python::object; typedef boost::python::list pylist; @@ -35,6 +36,13 @@ namespace YouCompleteMe { namespace { +std::string GetUtf8String( const boost::python::object &string_or_unicode ) { + extract< std::string > to_string( string_or_unicode ); + if ( to_string.check() ) + return to_string(); + return extract< std::string >( str( string_or_unicode ).encode( "utf8" ) ); +} + std::vector< const Candidate * > CandidatesFromObjectList( const pylist &candidates, const std::string &candidate_property ) { @@ -44,10 +52,10 @@ std::vector< const Candidate * > CandidatesFromObjectList( for ( int i = 0; i < num_candidates; ++i ) { if ( candidate_property.empty() ) { - candidate_strings.push_back( extract< std::string >( candidates[ i ] ) ); + candidate_strings.push_back( GetUtf8String( candidates[ i ] ) ); } else { object holder = extract< object >( candidates[ i ] ); - candidate_strings.push_back( extract< std::string >( + candidate_strings.push_back( GetUtf8String( holder[ candidate_property.c_str() ] ) ); } } diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 6e2f1486..ec5732e5 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -178,7 +178,7 @@ class Completer( object ): self.completions_cache.filtered_completions = ( self.FilterAndSortCandidates( self.completions_cache.raw_completions, - ToUtf8IfNeeded( request_data[ 'query' ] ) ) ) + request_data[ 'query' ] ) ) else: self.completions_cache = None self.CandidatesForQueryAsyncInner( request_data ) @@ -216,7 +216,7 @@ class Completer( object ): matches = ycm_core.FilterAndSortCandidates( candidates, sort_property, - query ) + ToUtf8IfNeeded( query ) ) return matches From 6acc381262e08dddc065755bacfac84620e7a81d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 7 Sep 2013 17:39:52 -0700 Subject: [PATCH 012/149] Identifier completer now fully decoupled from Vim --- .../completers/all/identifier_completer.py | 139 +++++++++++------- .../all/tests/identifier_completer_test.py | 127 ++++++++++++++++ python/ycm/completers/completer.py | 10 +- python/ycm/completers/cpp/clang_completer.py | 6 +- .../general/general_completer_store.py | 20 +-- .../completers/general/ultisnips_completer.py | 4 +- python/ycm/youcompleteme.py | 99 +++++++++---- 7 files changed, 300 insertions(+), 105 deletions(-) create mode 100644 python/ycm/completers/all/tests/identifier_completer_test.py diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index 196ff85a..aa3d812b 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -18,12 +18,10 @@ # along with YouCompleteMe. If not, see . import os -import vim import ycm_core from collections import defaultdict from ycm.completers.general_completer import GeneralCompleter -from ycm.completers.general import syntax_parse -from ycm import vimsupport +# from ycm.completers.general import syntax_parse from ycm import utils from ycm import server_responses @@ -50,9 +48,9 @@ class IdentifierCompleter( GeneralCompleter ): request_data[ 'filetypes' ][ 0 ] ) - def AddIdentifier( self, identifier ): - filetype = vim.eval( "&filetype" ) - filepath = vim.eval( "expand('%:p')" ) + def AddIdentifier( self, identifier, request_data ): + filetype = request_data[ 'filetypes' ][ 0 ] + filepath = request_data[ 'filepath' ] if not filetype or not filepath or not identifier: return @@ -64,23 +62,20 @@ class IdentifierCompleter( GeneralCompleter ): filepath ) - def AddPreviousIdentifier( self ): - self.AddIdentifier( _PreviousIdentifier( self.user_options[ - 'min_num_of_chars_for_completion' ] ) ) + def AddPreviousIdentifier( self, request_data ): + self.AddIdentifier( + _PreviousIdentifier( + self.user_options[ 'min_num_of_chars_for_completion' ], + request_data ), + request_data ) - def AddIdentifierUnderCursor( self ): - cursor_identifier = vim.eval( 'expand("")' ) + def AddIdentifierUnderCursor( self, request_data ): + cursor_identifier = _GetCursorIdentifier( request_data ) if not cursor_identifier: return - stripped_cursor_identifier = ''.join( ( x for x in - cursor_identifier if - utils.IsIdentifierChar( x ) ) ) - if not stripped_cursor_identifier: - return - - self.AddIdentifier( stripped_cursor_identifier ) + self.AddIdentifier( cursor_identifier, request_data ) def AddBufferIdentifiers( self, request_data ): @@ -100,26 +95,22 @@ class IdentifierCompleter( GeneralCompleter ): collect_from_comments_and_strings ) - def AddIdentifiersFromTagFiles( self ): - tag_files = vim.eval( 'tagfiles()' ) - current_working_directory = os.getcwd() + def AddIdentifiersFromTagFiles( self, tag_files ): absolute_paths_to_tag_files = ycm_core.StringVec() for tag_file in tag_files: - absolute_tag_file = os.path.join( current_working_directory, - tag_file ) try: - current_mtime = os.path.getmtime( absolute_tag_file ) + current_mtime = os.path.getmtime( tag_file ) except: continue - last_mtime = self.tags_file_last_mtime[ absolute_tag_file ] + last_mtime = self.tags_file_last_mtime[ tag_file ] # We don't want to repeatedly process the same file over and over; we only # process if it's changed since the last time we looked at it if current_mtime <= last_mtime: continue - self.tags_file_last_mtime[ absolute_tag_file ] = current_mtime - absolute_paths_to_tag_files.append( absolute_tag_file ) + self.tags_file_last_mtime[ tag_file ] = current_mtime + absolute_paths_to_tag_files.append( tag_file ) if not absolute_paths_to_tag_files: return @@ -128,41 +119,37 @@ class IdentifierCompleter( GeneralCompleter ): absolute_paths_to_tag_files ) - def AddIdentifiersFromSyntax( self ): - filetype = vim.eval( "&filetype" ) - if filetype in self.filetypes_with_keywords_loaded: - return + # def AddIdentifiersFromSyntax( self ): + # filetype = vim.eval( "&filetype" ) + # if filetype in self.filetypes_with_keywords_loaded: + # return - self.filetypes_with_keywords_loaded.add( filetype ) + # self.filetypes_with_keywords_loaded.add( filetype ) - keyword_set = syntax_parse.SyntaxKeywordsForCurrentBuffer() - keywords = ycm_core.StringVec() - for keyword in keyword_set: - keywords.append( keyword ) + # keyword_set = syntax_parse.SyntaxKeywordsForCurrentBuffer() + # keywords = ycm_core.StringVec() + # for keyword in keyword_set: + # keywords.append( keyword ) - filepath = SYNTAX_FILENAME + filetype - self.completer.AddIdentifiersToDatabase( keywords, - filetype, - filepath ) + # filepath = SYNTAX_FILENAME + filetype + # self.completer.AddIdentifiersToDatabase( keywords, + # filetype, + # filepath ) def OnFileReadyToParse( self, request_data ): self.AddBufferIdentifiers( request_data ) - - # TODO: make these work again - # if self.user_options[ 'collect_identifiers_from_tags_files' ]: - # self.AddIdentifiersFromTagFiles() - - # if self.user_options[ 'seed_identifiers_with_syntax' ]: - # self.AddIdentifiersFromSyntax() + if 'tag_files' in request_data: + self.AddIdentifiersFromTagFiles( request_data[ 'tag_files' ] ) + #self.AddIdentifiersFromSyntax() - def OnInsertLeave( self ): - self.AddIdentifierUnderCursor() + def OnInsertLeave( self, request_data ): + self.AddIdentifierUnderCursor( request_data ) - def OnCurrentIdentifierFinished( self ): - self.AddPreviousIdentifier() + def OnCurrentIdentifierFinished( self, request_data ): + self.AddPreviousIdentifier( request_data ) def CandidatesFromStoredRequest( self ): @@ -177,10 +164,13 @@ class IdentifierCompleter( GeneralCompleter ): return [ server_responses.BuildCompletionData( x ) for x in completions ] -def _PreviousIdentifier( min_num_completion_start_chars ): - line_num, column_num = vimsupport.CurrentLineAndColumn() - buffer = vim.current.buffer - line = buffer[ line_num ] +def _PreviousIdentifier( min_num_completion_start_chars, request_data ): + line_num = request_data[ 'line_num' ] + column_num = request_data[ 'column_num' ] + filepath = request_data[ 'filepath' ] + contents_per_line = ( + request_data[ 'file_data' ][ filepath ][ 'contents' ].split( '\n' ) ) + line = contents_per_line[ line_num ] end_column = column_num @@ -190,7 +180,7 @@ def _PreviousIdentifier( min_num_completion_start_chars ): # Look at the previous line if we reached the end of the current one if end_column == 0: try: - line = buffer[ line_num - 1] + line = contents_per_line[ line_num - 1 ] except: return "" end_column = len( line ) @@ -214,3 +204,40 @@ def _RemoveSmallCandidates( candidates, min_num_candidate_size_chars ): return [ x for x in candidates if len( x ) >= min_num_candidate_size_chars ] + +# This is meant to behave like 'expand(" 0 and utils.IsIdentifierChar( line[ + identifier_start - 1 ] ): + identifier_start -= 1 + return identifier_start + + + def FindIdentifierEnd( line, valid_char_column ): + identifier_end = valid_char_column + while identifier_end < len( line ) - 1 and utils.IsIdentifierChar( line[ + identifier_end + 1 ] ): + identifier_end += 1 + return identifier_end + 1 + + column_num = request_data[ 'column_num' ] + line = request_data[ 'line_value' ] + + try: + valid_char_column = FindFirstValidChar( line, column_num ) + return line[ FindIdentifierStart( line, valid_char_column ) : + FindIdentifierEnd( line, valid_char_column ) ] + except: + return '' + diff --git a/python/ycm/completers/all/tests/identifier_completer_test.py b/python/ycm/completers/all/tests/identifier_completer_test.py new file mode 100644 index 00000000..532773cd --- /dev/null +++ b/python/ycm/completers/all/tests/identifier_completer_test.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from nose.tools import eq_ +from ycm.completers.all import identifier_completer + + +def GetCursorIdentifier_StartOfLine_test(): + eq_( 'foo', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 0, + 'line_value': 'foo' + } ) ) + + eq_( 'fooBar', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 0, + 'line_value': 'fooBar' + } ) ) + + +def GetCursorIdentifier_EndOfLine_test(): + eq_( 'foo', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 2, + 'line_value': 'foo' + } ) ) + + +def GetCursorIdentifier_PastEndOfLine_test(): + eq_( '', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 10, + 'line_value': 'foo' + } ) ) + + +def GetCursorIdentifier_NegativeColumn_test(): + eq_( '', + identifier_completer._GetCursorIdentifier( + { + 'column_num': -10, + 'line_value': 'foo' + } ) ) + + +def GetCursorIdentifier_StartOfLine_StopsAtNonIdentifierChar_test(): + eq_( 'foo', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 0, + 'line_value': 'foo(goo)' + } ) ) + + +def GetCursorIdentifier_AtNonIdentifier_test(): + eq_( 'goo', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 3, + 'line_value': 'foo(goo)' + } ) ) + + +def GetCursorIdentifier_WalksForwardForIdentifier_test(): + eq_( 'foo', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 0, + 'line_value': ' foo' + } ) ) + + +def GetCursorIdentifier_FindsNothingForward_test(): + eq_( '', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 4, + 'line_value': 'foo ()***()' + } ) ) + + +def GetCursorIdentifier_SingleCharIdentifier_test(): + eq_( 'f', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 0, + 'line_value': ' f ' + } ) ) + + +def GetCursorIdentifier_StartsInMiddleOfIdentifier_test(): + eq_( 'foobar', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 3, + 'line_value': 'foobar' + } ) ) + + +def GetCursorIdentifier_LineEmpty_test(): + eq_( '', + identifier_completer._GetCursorIdentifier( + { + 'column_num': 11, + 'line_value': '' + } ) ) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index ec5732e5..321c8e1c 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -265,19 +265,19 @@ class Completer( object ): pass - def OnBufferVisit( self ): + def OnBufferVisit( self, request_data ): pass - def OnBufferUnload( self, deleted_buffer_file ): + def OnBufferUnload( self, request_data ): pass - def OnInsertLeave( self ): + def OnInsertLeave( self, request_data ): pass - def OnVimLeave( self ): + def OnVimLeave( self, request_data ): pass @@ -285,7 +285,7 @@ class Completer( object ): raise NotImplementedError( NO_USER_COMMANDS ) - def OnCurrentIdentifierFinished( self ): + def OnCurrentIdentifierFinished( self, request_data ): pass diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 73e90eef..98d9f5a0 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -245,8 +245,8 @@ class ClangCompleter( Completer ): self.extra_parse_desired = False - def OnBufferUnload( self, deleted_buffer_file ): - self.completer.DeleteCachesForFileAsync( deleted_buffer_file ) + def OnBufferUnload( self, request_data ): + self.completer.DeleteCachesForFileAsync( request_data[ 'unloaded_buffer' ] ) def DiagnosticsForCurrentFileReady( self ): @@ -271,7 +271,7 @@ class ClangCompleter( Completer ): self.parse_future = None if self.extra_parse_desired: - self.OnFileReadyToParse() + self.OnFileReadyToParse( request_data ) return self.last_prepared_diagnostics diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index fcfe5f76..3269bc97 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -100,29 +100,29 @@ class GeneralCompleterStore( Completer ): completer.OnFileReadyToParse( request_data ) - def OnBufferVisit( self ): + def OnBufferVisit( self, request_data ): for completer in self._all_completers: - completer.OnBufferVisit() + completer.OnBufferVisit( request_data ) - def OnBufferUnload( self, deleted_buffer_file ): + def OnBufferUnload( self, request_data ): for completer in self._all_completers: - completer.OnBufferUnload( deleted_buffer_file ) + completer.OnBufferUnload( request_data ) - def OnInsertLeave( self ): + def OnInsertLeave( self, request_data ): for completer in self._all_completers: - completer.OnInsertLeave() + completer.OnInsertLeave( request_data ) - def OnVimLeave( self ): + def OnVimLeave( self, request_data ): for completer in self._all_completers: - completer.OnVimLeave() + completer.OnVimLeave( request_data ) - def OnCurrentIdentifierFinished( self ): + def OnCurrentIdentifierFinished( self, request_data ): for completer in self._all_completers: - completer.OnCurrentIdentifierFinished() + completer.OnCurrentIdentifierFinished( request_data ) def GettingCompletions( self ): diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index d16b174f..a66c6fdc 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -51,7 +51,9 @@ class UltiSnipsCompleter( GeneralCompleter ): return self._filtered_candidates if self._filtered_candidates else [] - def OnBufferVisit( self ): + def OnBufferVisit( self, request_data ): + # TODO: _GetCandidates should be called on the client and it should send + # the snippets to the server self._candidates = _GetCandidates() diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 1e8d8efb..a7f8e276 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -31,8 +31,27 @@ from ycm.completers.general.general_completer_store import GeneralCompleterStore # TODO: Put the Request classes in separate files -class CompletionRequest( object ): +class BaseRequest( object ): + def __init__( self ): + pass + + + def Start( self ): + pass + + + def Done( self ): + return True + + + def Response( self ): + return {} + + +class CompletionRequest( BaseRequest ): def __init__( self, ycm_state ): + super( CompletionRequest, self ).__init__() + self._completion_start_column = base.CompletionStartColumn() self._ycm_state = ycm_state self._request_data = _BuildRequestData( self._completion_start_column ) @@ -70,7 +89,7 @@ class CompletionRequest( object ): -class CommandRequest( object ): +class CommandRequest( BaseRequest ): class ServerResponse( object ): def __init__( self ): pass @@ -79,6 +98,8 @@ class CommandRequest( object ): return True def __init__( self, ycm_state, arguments, completer_target = None ): + super( CommandRequest, self ).__init__() + if not completer_target: completer_target = 'filetpe_default' @@ -95,9 +116,6 @@ class CommandRequest( object ): self._completer.OnUserCommand( self._arguments, _BuildRequestData() ) - def Done( self ): - return True - def Response( self ): # TODO: Call vimsupport.JumpToLocation if the user called a GoTo command... @@ -117,6 +135,31 @@ class CommandRequest( object ): return self.ServerResponse() +class EventNotification( BaseRequest ): + def __init__( self, event_name, ycm_state, extra_data = None ): + super( EventNotification, self ).__init__() + + self._ycm_state = ycm_state + self._event_name = event_name + self._request_data = _BuildRequestData() + if extra_data: + self._request_data.update( extra_data ) + + + def Start( self ): + getattr( self._ycm_state.GetGeneralCompleter(), + 'On' + self._event_name )( self._request_data ) + + if self._ycm_state.FiletypeCompletionUsable(): + getattr( self._ycm_state.GetFiletypeCompleter(), + 'On' + self._event_name )( self._request_data ) + + + +def SendEventNotificationAsync( event_name, ycm_state, extra_data = None ): + event = EventNotification( event_name, ycm_state, extra_data ) + event.Start() + class YouCompleteMe( object ): def __init__( self, user_options ): @@ -235,38 +278,36 @@ class YouCompleteMe( object ): def OnFileReadyToParse( self ): - self._gencomp.OnFileReadyToParse( _BuildRequestData() ) + extra_data = {} + if self._user_options[ 'collect_identifiers_from_tags_files' ]: + extra_data[ 'tag_files' ] = _GetTagFiles() - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnFileReadyToParse( _BuildRequestData() ) + # TODO: make this work again + # if self._user_options[ 'seed_identifiers_with_syntax' ]: + + SendEventNotificationAsync( 'FileReadyToParse', self, extra_data ) def OnBufferUnload( self, deleted_buffer_file ): - self._gencomp.OnBufferUnload( deleted_buffer_file ) - - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnBufferUnload( deleted_buffer_file ) + SendEventNotificationAsync( 'BufferUnload', + self, + { 'unloaded_buffer': deleted_buffer_file } ) def OnBufferVisit( self ): - self._gencomp.OnBufferVisit() - - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnBufferVisit() + SendEventNotificationAsync( 'BufferVisit', self ) def OnInsertLeave( self ): - self._gencomp.OnInsertLeave() - - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnInsertLeave() + SendEventNotificationAsync( 'InsertLeave', self ) def OnVimLeave( self ): - self._gencomp.OnVimLeave() + SendEventNotificationAsync( 'VimLeave', self ) - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnVimLeave() + + def OnCurrentIdentifierFinished( self ): + SendEventNotificationAsync( 'CurrentIdentifierFinished', self ) def DiagnosticsForCurrentFileReady( self ): @@ -292,13 +333,6 @@ class YouCompleteMe( object ): return False - def OnCurrentIdentifierFinished( self ): - self._gencomp.OnCurrentIdentifierFinished() - - if self.FiletypeCompletionUsable(): - self.GetFiletypeCompleter().OnCurrentIdentifierFinished() - - def DebugInfo( self ): completers = set( self._filetype_completers.values() ) completers.add( self._gencomp ) @@ -370,3 +404,8 @@ def _ConvertCompletionDataToVimData( completion_data ): vim_data[ 'info' ] = completion_data[ 'detailed_info' ] return vim_data + +def _GetTagFiles(): + tag_files = vim.eval( 'tagfiles()' ) + current_working_directory = os.getcwd() + return [ os.path.join( current_working_directory, x ) for x in tag_files ] From b6c311c4dde0662b137562d502810be36d7b773d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 13:55:41 -0700 Subject: [PATCH 013/149] Code typo fix --- python/ycm/vimsupport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 4ce05fb1..2314be72 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -53,7 +53,7 @@ def GetBufferOption( buffer_object, option ): if hasattr( buffer_object, 'options' ): return buffer_object.options[ option ] - to_eval = 'getbufvar({0}, "&{1}")'.format( buffer.number, option ) + to_eval = 'getbufvar({0}, "&{1}")'.format( buffer_object.number, option ) return GetVariableValue( to_eval ) From 08a9ff59b620eb9d76b89b558d4706af3f0b7b31 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 14:24:38 -0700 Subject: [PATCH 014/149] Using os.getcwd instead of getcwd from vimscript --- python/ycm/extra_conf_store.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 5b4814d3..9611d697 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -24,7 +24,6 @@ import imp import random import string import sys -import vim from ycm import vimsupport from ycm import user_options_store from fnmatch import fnmatch @@ -66,7 +65,7 @@ def CallExtraConfVimCloseIfExists(): def _CallExtraConfMethod( function_name ): - vim_current_working_directory = vim.eval( 'getcwd()' ) + vim_current_working_directory = os.getcwd() path_to_dummy = os.path.join( vim_current_working_directory, 'DUMMY_FILE' ) # The dummy file in the Vim CWD ensures we find the correct extra conf file module = ModuleForSourceFile( path_to_dummy ) From 9698bf8789f8213c6687e730c58f1a48483701f9 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 15:29:10 -0700 Subject: [PATCH 015/149] Bump ycm_core.so API version --- cpp/ycm/ycm_core.cpp | 2 +- python/ycm/base.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index 4c1ba0d9..ea353a91 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -46,7 +46,7 @@ int YcmCoreVersion() { // We increment this every time when we want to force users to recompile // ycm_core. - return 4; + return 5; } diff --git a/python/ycm/base.py b/python/ycm/base.py index 4d58f686..e87a09cc 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -147,7 +147,7 @@ def AdjustCandidateInsertionText( candidates ): return new_candidates -COMPATIBLE_WITH_CORE_VERSION = 4 +COMPATIBLE_WITH_CORE_VERSION = 5 def CompatibleWithYcmCore(): try: From f5ad981f67b0c0f020e99dc4dd330f23c77443b2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 17:40:36 -0700 Subject: [PATCH 016/149] Converting Jedi unicode data to strings --- python/ycm/completers/python/jedi_completer.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index 7277d7bc..a9a490de 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -66,9 +66,10 @@ class JediCompleter( ThreadedCompleter ): def ComputeCandidates( self, request_data ): script = self._GetJediScript( request_data ) - return [ server_responses.BuildCompletionData( completion.name, - completion.description, - completion.doc ) + return [ server_responses.BuildCompletionData( + str( completion.name ), + str( completion.description ), + str( completion.doc ) ) for completion in script.completions() ] def DefinedSubcommands( self ): From 1d29a9a3bd7b7148c0eaf77da256c849618a3c86 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 17:47:30 -0700 Subject: [PATCH 017/149] Updating Jedi to v0.7.0 --- python/ycm/completers/python/jedi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/completers/python/jedi b/python/ycm/completers/python/jedi index d5d12716..78f1ae5e 160000 --- a/python/ycm/completers/python/jedi +++ b/python/ycm/completers/python/jedi @@ -1 +1 @@ -Subproject commit d5d12716b1d67df9cbaa4d3ea0c90e47c0023208 +Subproject commit 78f1ae5e7163bda737433f1d551b3180cf03e1d1 From fba4477ca3845bcb9ed6219da5ea29ce549e7a3d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 16 Sep 2013 17:53:19 -0700 Subject: [PATCH 018/149] Deleting a stray 'print' statement This was put in for debugging at some point. --- python/ycm/completers/python/jedi_completer.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index a9a490de..fdfa95d6 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -59,7 +59,6 @@ class JediCompleter( ThreadedCompleter ): # Jedi expects lines to start at 1, not 0 line = request_data[ 'line_num' ] + 1 column = request_data[ 'column_num' ] - print contents return jedi.Script( contents, line, column, filename ) From 02b88dccf1bde70ad44b7f031562adfe981075a1 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 17 Sep 2013 16:34:02 -0700 Subject: [PATCH 019/149] extra conf store now vim-free --- autoload/youcompleteme.vim | 1 - python/ycm/extra_conf_store.py | 11 ++++++++--- python/ycm/youcompleteme.py | 9 +++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index d8e6e895..32731bbd 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -223,7 +223,6 @@ endfunction function! s:OnVimLeave() py ycm_state.OnVimLeave() - py extra_conf_store.CallExtraConfVimCloseIfExists() endfunction diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 9611d697..15a3f087 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -24,7 +24,6 @@ import imp import random import string import sys -from ycm import vimsupport from ycm import user_options_store from fnmatch import fnmatch @@ -37,6 +36,12 @@ CONFIRM_CONF_FILE_MESSAGE = ('Found {0}. Load? \n\n(Question can be turned ' _module_for_module_file = {} _module_file_for_source_file = {} +class UnknownExtraConf( Exception ): + def __init__( self, extra_conf_file ): + message = CONFIRM_CONF_FILE_MESSAGE.format( extra_conf_file ) + super( UnknownExtraConf, self ).__init__( message ) + self.extra_conf_file = extra_conf_file + def ModuleForSourceFile( filename ): return _Load( ModuleFileForSourceFile( filename ) ) @@ -60,7 +65,7 @@ def CallExtraConfYcmCorePreloadIfExists(): _CallExtraConfMethod( 'YcmCorePreload' ) -def CallExtraConfVimCloseIfExists(): +def OnVimLeave( request_data ): _CallExtraConfMethod( 'VimClose' ) @@ -94,7 +99,7 @@ def _ShouldLoad( module_file ): if _MatchesGlobPattern( module_file, glob.lstrip('!') ): return not is_blacklisted - return vimsupport.Confirm( CONFIRM_CONF_FILE_MESSAGE.format( module_file ) ) + raise UnknownExtraConf( module_file ) def _Load( module_file, force = False ): diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index a7f8e276..ea600f44 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -26,6 +26,7 @@ import logging import tempfile from ycm import vimsupport from ycm import base +from ycm import extra_conf_store from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general.general_completer_store import GeneralCompleterStore @@ -147,12 +148,16 @@ class EventNotification( BaseRequest ): def Start( self ): + event_handler = 'On' + self._event_name getattr( self._ycm_state.GetGeneralCompleter(), - 'On' + self._event_name )( self._request_data ) + event_handler )( self._request_data ) if self._ycm_state.FiletypeCompletionUsable(): getattr( self._ycm_state.GetFiletypeCompleter(), - 'On' + self._event_name )( self._request_data ) + event_handler )( self._request_data ) + + if hasattr( extra_conf_store, event_handler ): + getattr( extra_conf_store, event_handler )( self._request_data ) From 1730660555e6c6216a76b0fdff197a8ac4030973 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 20 Sep 2013 17:24:34 -0700 Subject: [PATCH 020/149] A (barely) working version of ycmd + client Still a lot of work to do. --- autoload/youcompleteme.vim | 22 +- cpp/ycm/tests/IdentifierCompleter_test.cpp | 12 + python/ycm/client/__init__.py | 0 python/ycm/client/base_request.py | 77 ++++ python/ycm/client/command_request.py | 67 ++++ python/ycm/client/completion_request.py | 74 ++++ python/ycm/client/event_notification.py | 42 +++ .../completers/all/identifier_completer.py | 27 +- python/ycm/completers/cpp/clang_completer.py | 76 ++-- python/ycm/completers/cs/cs_completer.py | 18 +- .../completers/general/filename_completer.py | 6 +- .../completers/general/ultisnips_completer.py | 4 +- .../ycm/completers/python/jedi_completer.py | 20 +- python/ycm/server/__init__.py | 0 python/ycm/server/default_settings.json | 1 + .../responses.py} | 2 + python/ycm/server/server.py | 168 +++++++++ python/ycm/server/server_state.py | 108 ++++++ python/ycm/server/tests/__init__.py | 0 python/ycm/server/tests/basic_test.py | 74 ++++ python/ycm/user_options_store.py | 15 + python/ycm/utils.py | 7 + python/ycm/youcompleteme.py | 332 ++++-------------- 23 files changed, 798 insertions(+), 354 deletions(-) create mode 100644 python/ycm/client/__init__.py create mode 100644 python/ycm/client/base_request.py create mode 100644 python/ycm/client/command_request.py create mode 100644 python/ycm/client/completion_request.py create mode 100644 python/ycm/client/event_notification.py create mode 100644 python/ycm/server/__init__.py create mode 100644 python/ycm/server/default_settings.json rename python/ycm/{server_responses.py => server/responses.py} (96%) create mode 100755 python/ycm/server/server.py create mode 100644 python/ycm/server/server_state.py create mode 100644 python/ycm/server/tests/__init__.py create mode 100644 python/ycm/server/tests/basic_test.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 32731bbd..bb6b46aa 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -539,11 +539,6 @@ function! youcompleteme#Complete( findstart, base ) endif py request = ycm_state.CreateCompletionRequest() - if !pyeval( 'request.ShouldComplete()' ) - " for vim, -2 means not found but don't trigger an error message - " see :h complete-functions - return -2 - endif return pyeval( 'request.CompletionStartColumn()' ) else return s:CompletionsForQuery( a:base ) @@ -628,14 +623,15 @@ function! youcompleteme#OpenGoToList() endfunction -command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete - \ YcmCompleter call s:CompleterCommand() - - -function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) - return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ), - \ "\n") -endfunction +" TODO: Make this work again +" command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete +" \ YcmCompleter call s:CompleterCommand() +" +" +" function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) +" return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ), +" \ "\n") +" endfunction function! s:ForceCompile() diff --git a/cpp/ycm/tests/IdentifierCompleter_test.cpp b/cpp/ycm/tests/IdentifierCompleter_test.cpp index 959b8f53..d3c4d0dc 100644 --- a/cpp/ycm/tests/IdentifierCompleter_test.cpp +++ b/cpp/ycm/tests/IdentifierCompleter_test.cpp @@ -217,6 +217,18 @@ TEST( IdentifierCompleterTest, ShorterAndLowercaseWins ) { "STDIN_FILENO" ) ); } +TEST( IdentifierCompleterTest, AddIdentifiersToDatabaseFromBufferWorks ) { + IdentifierCompleter completer; + completer.AddIdentifiersToDatabaseFromBuffer( "foo foogoo ba", + "foo", + "/foo/bar", + false ); + + EXPECT_THAT( completer.CandidatesForQueryAndType( "oo", "foo" ), + ElementsAre( "foo", + "foogoo" ) ); +} + TEST( IdentifierCompleterTest, TagsEndToEndWorks ) { IdentifierCompleter completer; std::vector< std::string > tag_files; diff --git a/python/ycm/client/__init__.py b/python/ycm/client/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py new file mode 100644 index 00000000..650e512b --- /dev/null +++ b/python/ycm/client/base_request.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import vim +import json +import requests +from ycm import vimsupport + +HEADERS = {'content-type': 'application/json'} + +class BaseRequest( object ): + def __init__( self ): + pass + + + def Start( self ): + pass + + + def Done( self ): + return True + + + def Response( self ): + return {} + + + def PostDataToHandler( self, data, handler ): + response = requests.post( _BuildUri( handler ), + data = json.dumps( data ), + headers = HEADERS ) + response.raise_for_status() + if response.text: + return response.json() + return None + + server_location = 'http://localhost:6666' + + +def BuildRequestData( start_column = None, query = None ): + line, column = vimsupport.CurrentLineAndColumn() + request_data = { + 'filetypes': vimsupport.CurrentFiletypes(), + 'line_num': line, + 'column_num': column, + 'start_column': start_column, + 'line_value': vim.current.line, + 'filepath': vim.current.buffer.name, + 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() + } + + if query: + request_data[ 'query' ] = query + + return request_data + + +def _BuildUri( handler ): + return ''.join( [ BaseRequest.server_location, '/', handler ] ) + + diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py new file mode 100644 index 00000000..77af8602 --- /dev/null +++ b/python/ycm/client/command_request.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from ycm.client.base_request import BaseRequest, BuildRequestData + + +class CommandRequest( BaseRequest ): + class ServerResponse( object ): + def __init__( self ): + pass + + def Valid( self ): + return True + + def __init__( self, arguments, completer_target = None ): + super( CommandRequest, self ).__init__() + self._arguments = arguments + self._completer_target = ( completer_target if completer_target + else 'filetype_default' ) + # TODO: Handle this case. + # if completer_target == 'omni': + # completer = SERVER_STATE.GetOmniCompleter() + + def Start( self ): + request_data = BuildRequestData() + request_data.update( { + 'completer_target': self._completer_target, + 'command_arguments': self._arguments + } ) + self._response = self.PostDataToHandler( request_data, + 'run_completer_command' ) + + + def Response( self ): + # TODO: Call vimsupport.JumpToLocation if the user called a GoTo command... + # we may want to have specific subclasses of CommandRequest so that a + # GoToRequest knows it needs to jump after the data comes back. + # + # Also need to run the following on GoTo data: + # + # CAREFUL about line/column number 0-based/1-based confusion! + # + # defs = [] + # defs.append( {'filename': definition.module_path.encode( 'utf-8' ), + # 'lnum': definition.line, + # 'col': definition.column + 1, + # 'text': definition.description.encode( 'utf-8' ) } ) + # vim.eval( 'setqflist( %s )' % repr( defs ) ) + # vim.eval( 'youcompleteme#OpenGoToList()' ) + return self.ServerResponse() + diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py new file mode 100644 index 00000000..b3ff943f --- /dev/null +++ b/python/ycm/client/completion_request.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from ycm import base +from ycm import vimsupport +from ycm.client.base_request import BaseRequest, BuildRequestData + + +class CompletionRequest( BaseRequest ): + def __init__( self ): + super( CompletionRequest, self ).__init__() + + self._completion_start_column = base.CompletionStartColumn() + self._request_data = BuildRequestData( self._completion_start_column ) + + + # def ShouldComplete( self ): + # return ( self._do_filetype_completion or + # self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) ) + + + def CompletionStartColumn( self ): + return self._completion_start_column + + + def Start( self, query ): + self._request_data[ 'query' ] = query + self._response = self.PostDataToHandler( self._request_data, + 'get_completions' ) + + + def Results( self ): + if not self._response: + return [] + try: + return [ _ConvertCompletionDataToVimData( x ) for x in self._response ] + except Exception as e: + vimsupport.PostVimMessage( str( e ) ) + return [] + + +def _ConvertCompletionDataToVimData( completion_data ): + # see :h complete-items for a description of the dictionary fields + vim_data = { + 'word' : completion_data[ 'insertion_text' ], + 'dup' : 1, + } + + if 'menu_text' in completion_data: + vim_data[ 'abbr' ] = completion_data[ 'menu_text' ] + if 'extra_menu_info' in completion_data: + vim_data[ 'menu' ] = completion_data[ 'extra_menu_info' ] + if 'kind' in completion_data: + vim_data[ 'kind' ] = completion_data[ 'kind' ] + if 'detailed_info' in completion_data: + vim_data[ 'info' ] = completion_data[ 'detailed_info' ] + + return vim_data diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py new file mode 100644 index 00000000..c190e6e2 --- /dev/null +++ b/python/ycm/client/event_notification.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from ycm.client.base_request import BaseRequest, BuildRequestData + + +class EventNotification( BaseRequest ): + def __init__( self, event_name, extra_data = None ): + super( EventNotification, self ).__init__() + self._event_name = event_name + self._extra_data = extra_data + + + def Start( self ): + request_data = BuildRequestData() + if self._extra_data: + request_data.update( self._extra_data ) + request_data[ 'event_name' ] = self._event_name + + self.PostDataToHandler( request_data, 'event_notification' ) + + +def SendEventNotificationAsync( event_name, extra_data = None ): + event = EventNotification( event_name, extra_data ) + event.Start() + diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index aa3d812b..d1b8359d 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -18,12 +18,14 @@ # along with YouCompleteMe. If not, see . import os +import logging import ycm_core from collections import defaultdict from ycm.completers.general_completer import GeneralCompleter # from ycm.completers.general import syntax_parse from ycm import utils -from ycm import server_responses +from ycm.utils import ToUtf8IfNeeded +from ycm.server import responses MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10 SYNTAX_FILENAME = 'YCM_PLACEHOLDER_FOR_SYNTAX' @@ -36,6 +38,7 @@ class IdentifierCompleter( GeneralCompleter ): self.completer.EnableThreading() self.tags_file_last_mtime = defaultdict( int ) self.filetypes_with_keywords_loaded = set() + self._logger = logging.getLogger( __name__ ) def ShouldUseNow( self, request_data ): @@ -44,8 +47,8 @@ class IdentifierCompleter( GeneralCompleter ): def CandidatesForQueryAsync( self, request_data ): self.completions_future = self.completer.CandidatesForQueryAndTypeAsync( - utils.SanitizeQuery( request_data[ 'query' ] ), - request_data[ 'filetypes' ][ 0 ] ) + ToUtf8IfNeeded( utils.SanitizeQuery( request_data[ 'query' ] ) ), + ToUtf8IfNeeded( request_data[ 'filetypes' ][ 0 ] ) ) def AddIdentifier( self, identifier, request_data ): @@ -56,10 +59,11 @@ class IdentifierCompleter( GeneralCompleter ): return vector = ycm_core.StringVec() - vector.append( identifier ) + vector.append( ToUtf8IfNeeded( identifier ) ) + self._logger.info( 'Adding ONE buffer identifier for file: %s', filepath ) self.completer.AddIdentifiersToDatabase( vector, - filetype, - filepath ) + ToUtf8IfNeeded( filetype ), + ToUtf8IfNeeded( filepath ) ) def AddPreviousIdentifier( self, request_data ): @@ -88,10 +92,11 @@ class IdentifierCompleter( GeneralCompleter ): return text = request_data[ 'file_data' ][ filepath ][ 'contents' ] + self._logger.info( 'Adding buffer identifiers for file: %s', filepath ) self.completer.AddIdentifiersToDatabaseFromBufferAsync( - text, - filetype, - filepath, + ToUtf8IfNeeded( text ), + ToUtf8IfNeeded( filetype ), + ToUtf8IfNeeded( filepath ), collect_from_comments_and_strings ) @@ -110,7 +115,7 @@ class IdentifierCompleter( GeneralCompleter ): continue self.tags_file_last_mtime[ tag_file ] = current_mtime - absolute_paths_to_tag_files.append( tag_file ) + absolute_paths_to_tag_files.append( ToUtf8IfNeeded( tag_file ) ) if not absolute_paths_to_tag_files: return @@ -161,7 +166,7 @@ class IdentifierCompleter( GeneralCompleter ): completions = _RemoveSmallCandidates( completions, self.user_options[ 'min_num_identifier_candidate_chars' ] ) - return [ server_responses.BuildCompletionData( x ) for x in completions ] + return [ responses.BuildCompletionData( x ) for x in completions ] def _PreviousIdentifier( min_num_completion_start_chars, request_data ): diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 98d9f5a0..4fbed782 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -20,7 +20,7 @@ from collections import defaultdict import ycm_core import logging -from ycm import server_responses +from ycm.server import responses from ycm import extra_conf_store from ycm.utils import ToUtf8IfNeeded from ycm.completers.completer import Completer @@ -72,9 +72,10 @@ class ClangCompleter( Completer ): continue unsaved_file = ycm_core.UnsavedFile() - unsaved_file.contents_ = contents - unsaved_file.length_ = len( contents ) - unsaved_file.filename_ = filename + utf8_contents = ToUtf8IfNeeded( contents ) + unsaved_file.contents_ = utf8_contents + unsaved_file.length_ = len( utf8_contents ) + unsaved_file.filename_ = ToUtf8IfNeeded( filename ) files.append( unsaved_file ) return files @@ -86,17 +87,17 @@ class ClangCompleter( Completer ): if not filename: return - if self.completer.UpdatingTranslationUnit( filename ): + if self.completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): self.completions_future = None self._logger.info( PARSING_FILE_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( PARSING_FILE_MESSAGE ) flags = self.flags.FlagsForFile( filename ) if not flags: self.completions_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( NO_COMPILE_FLAGS_MESSAGE ) # TODO: sanitize query, probably in C++ code @@ -142,11 +143,11 @@ class ClangCompleter( Completer ): command = arguments[ 0 ] if command == 'GoToDefinition': - self._GoToDefinition( request_data ) + return self._GoToDefinition( request_data ) elif command == 'GoToDeclaration': - self._GoToDeclaration( request_data ) + return self._GoToDeclaration( request_data ) elif command == 'GoToDefinitionElseDeclaration': - self._GoToDefinitionElseDeclaration( request_data ) + return self._GoToDefinitionElseDeclaration( request_data ) elif command == 'ClearCompilationFlagCache': self._ClearCompilationFlagCache( request_data ) @@ -155,20 +156,20 @@ class ClangCompleter( Completer ): filename = request_data[ 'filepath' ] if not filename: self._logger.warning( INVALID_FILE_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( INVALID_FILE_MESSAGE ) flags = self.flags.FlagsForFile( filename ) if not flags: self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( NO_COMPILE_FLAGS_MESSAGE ) - files = self.GetUnsavedFilesVector() + files = self.GetUnsavedFilesVector( request_data ) line = request_data[ 'line_num' ] + 1 column = request_data[ 'start_column' ] + 1 return getattr( self.completer, goto_function )( - filename, + ToUtf8IfNeeded( filename ), line, column, files, @@ -180,9 +181,9 @@ class ClangCompleter( Completer ): if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition.' ) - return server_responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + return responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) def _GoToDeclaration( self, request_data ): @@ -190,9 +191,9 @@ class ClangCompleter( Completer ): if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to declaration.' ) - return server_responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + return responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) def _GoToDefinitionElseDeclaration( self, request_data ): @@ -202,9 +203,9 @@ class ClangCompleter( Completer ): if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition or declaration.' ) - return server_responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + return responses.BuildGoToResponse( location.filename_, + location.line_number_, + location.column_number_ ) @@ -223,10 +224,10 @@ class ClangCompleter( Completer ): if not filename: self._logger.warning( INVALID_FILE_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( INVALID_FILE_MESSAGE ) - if self.completer.UpdatingTranslationUnit( filename ): + if self.completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): self.extra_parse_desired = True return @@ -234,11 +235,11 @@ class ClangCompleter( Completer ): if not flags: self.parse_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( NO_COMPILE_FLAGS_MESSAGE ) self.parse_future = self.completer.UpdateTranslationUnitAsync( - filename, + ToUtf8IfNeeded( filename ), self.GetUnsavedFilesVector( request_data ), flags ) @@ -246,7 +247,8 @@ class ClangCompleter( Completer ): def OnBufferUnload( self, request_data ): - self.completer.DeleteCachesForFileAsync( request_data[ 'unloaded_buffer' ] ) + self.completer.DeleteCachesForFileAsync( + ToUtf8IfNeeded( request_data[ 'unloaded_buffer' ] ) ) def DiagnosticsForCurrentFileReady( self ): @@ -257,16 +259,18 @@ class ClangCompleter( Completer ): def GettingCompletions( self, request_data ): - return self.completer.UpdatingTranslationUnit( request_data[ 'filepath' ] ) + return self.completer.UpdatingTranslationUnit( + ToUtf8IfNeeded( request_data[ 'filepath' ] ) ) def GetDiagnosticsForCurrentFile( self, request_data ): filename = request_data[ 'filepath' ] if self.DiagnosticsForCurrentFileReady(): - diagnostics = self.completer.DiagnosticsForFile( filename ) + diagnostics = self.completer.DiagnosticsForFile( + ToUtf8IfNeeded( filename ) ) self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) self.last_prepared_diagnostics = [ - server_responses.BuildDiagnosticData( x ) for x in + responses.BuildDiagnosticData( x ) for x in diagnostics[ : self.max_diagnostics_to_display ] ] self.parse_future = None @@ -282,12 +286,12 @@ class ClangCompleter( Completer ): current_file = request_data[ 'filepath' ] if not self.diagnostic_store: - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( NO_DIAGNOSTIC_MESSAGE ) diagnostics = self.diagnostic_store[ current_file ][ current_line ] if not diagnostics: - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( NO_DIAGNOSTIC_MESSAGE ) closest_diagnostic = None @@ -299,7 +303,7 @@ class ClangCompleter( Completer ): distance_to_closest_diagnostic = distance closest_diagnostic = diagnostic - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( closest_diagnostic.long_formatted_text_ ) @@ -314,7 +318,7 @@ class ClangCompleter( Completer ): return '' flags = self.flags.FlagsForFile( filename ) or [] source = extra_conf_store.ModuleFileForSourceFile( filename ) - return server_responses.BuildDisplayMessageResponse( + return responses.BuildDisplayMessageResponse( 'Flags for {0} loaded from {1}:\n{2}'.format( filename, source, list( flags ) ) ) @@ -348,7 +352,7 @@ class ClangCompleter( Completer ): def ConvertCompletionData( completion_data ): - return server_responses.BuildCompletionData( + return responses.BuildCompletionData( insertion_text = completion_data.TextToInsertInBuffer(), menu_text = completion_data.MainCompletionText(), extra_menu_info = completion_data.ExtraMenuInfo(), diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index f63b3b67..20240ee8 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -22,13 +22,13 @@ import os from sys import platform import glob from ycm.completers.threaded_completer import ThreadedCompleter -from ycm import server_responses +from ycm.server import responses +from ycm import utils import urllib2 import urllib import urlparse import json import subprocess -import tempfile import logging @@ -44,7 +44,7 @@ class CsharpCompleter( ThreadedCompleter ): def __init__( self, user_options ): super( CsharpCompleter, self ).__init__( user_options ) self._omnisharp_port = None - self._logger = logging.getLogger(__name__) + self._logger = logging.getLogger( __name__ ) # if self.user_options[ 'auto_start_csharp_server' ]: # self._StartServer() @@ -62,7 +62,7 @@ class CsharpCompleter( ThreadedCompleter ): def ComputeCandidates( self, request_data ): - return [ server_responses.BuildCompletionData( + return [ responses.BuildCompletionData( completion[ 'CompletionText' ], completion[ 'DisplayText' ], completion[ 'Description' ] ) @@ -135,8 +135,8 @@ class CsharpCompleter( ThreadedCompleter ): command = [ omnisharp + ' -p ' + str( self._omnisharp_port ) + ' -s ' + path_to_solutionfile ] - filename_format = ( tempfile.gettempdir() + - '/omnisharp_{port}_{sln}_{std}.log' ) + filename_format = os.path.join( utils.PathToTempDir(), + 'omnisharp_{port}_{sln}_{std}.log' ) self._filename_stdout = filename_format.format( port=self._omnisharp_port, sln=solutionfile, std='stdout' ) @@ -169,9 +169,9 @@ class CsharpCompleter( ThreadedCompleter ): definition = self._GetResponse( '/gotodefinition', self._DefaultParameters( request_data ) ) if definition[ 'FileName' ] != None: - return server_responses.BuildGoToResponse( definition[ 'FileName' ], - definition[ 'Line' ], - definition[ 'Column' ] ) + return responses.BuildGoToResponse( definition[ 'FileName' ], + definition[ 'Line' ], + definition[ 'Column' ] ) else: raise RuntimeError( 'Can\'t jump to definition' ) diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index 985bda04..5d9eb0da 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -22,7 +22,7 @@ import re from ycm.completers.threaded_completer import ThreadedCompleter from ycm.completers.cpp.clang_completer import InCFamilyFile from ycm.completers.cpp.flags import Flags -from ycm import server_responses +from ycm.server import responses class FilenameCompleter( ThreadedCompleter ): """ @@ -146,7 +146,7 @@ def _GenerateCandidatesForPaths( absolute_paths ): is_dir = os.path.isdir( absolute_path ) completion_dicts.append( - server_responses.BuildCompletionData( basename, - '[Dir]' if is_dir else '[File]' ) ) + responses.BuildCompletionData( basename, + '[Dir]' if is_dir else '[File]' ) ) return completion_dicts diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index a66c6fdc..dfc37cd1 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -20,7 +20,7 @@ from ycm.completers.general_completer import GeneralCompleter from UltiSnips import UltiSnips_Manager -from ycm import server_responses +from ycm.server import responses class UltiSnipsCompleter( GeneralCompleter ): @@ -64,7 +64,7 @@ def _GetCandidates(): # UltiSnips_Manager._snips() returns a class instance where: # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) # class.description - description of the snippet - return [ server_responses.BuildCompletionData( + return [ responses.BuildCompletionData( str( snip.trigger ), str( ' ' + snip.description.encode( 'utf-8' ) ) ) for snip in rawsnips ] diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index fdfa95d6..b64a95e4 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -20,7 +20,7 @@ # along with YouCompleteMe. If not, see . from ycm.completers.threaded_completer import ThreadedCompleter -from ycm import server_responses +from ycm.server import responses import sys from os.path import join, abspath, dirname @@ -65,7 +65,7 @@ class JediCompleter( ThreadedCompleter ): def ComputeCandidates( self, request_data ): script = self._GetJediScript( request_data ) - return [ server_responses.BuildCompletionData( + return [ responses.BuildCompletionData( str( completion.name ), str( completion.description ), str( completion.doc ) ) @@ -140,21 +140,21 @@ class JediCompleter( ThreadedCompleter ): else: raise RuntimeError( 'Builtin modules cannot be displayed.' ) else: - return server_responses.BuildGoToResponse( definition.module_path, - definition.line -1, - definition.column ) + return responses.BuildGoToResponse( definition.module_path, + definition.line -1, + definition.column ) else: # multiple definitions defs = [] for definition in definition_list: if definition.in_builtin_module(): - defs.append( server_responses.BuildDescriptionOnlyGoToResponse( + defs.append( responses.BuildDescriptionOnlyGoToResponse( 'Builting ' + definition.description ) ) else: defs.append( - server_responses.BuildGoToResponse( definition.module_path, - definition.line -1, - definition.column, - definition.description ) ) + responses.BuildGoToResponse( definition.module_path, + definition.line -1, + definition.column, + definition.description ) ) return defs diff --git a/python/ycm/server/__init__.py b/python/ycm/server/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json new file mode 100644 index 00000000..f6030fa1 --- /dev/null +++ b/python/ycm/server/default_settings.json @@ -0,0 +1 @@ +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [ "~\/repos\/*", "\/home\/strahinja\/googrepos\/*", "~\/local_googrepos\/*", "~\/.ycm_extra_conf.py" ], "global_ycm_extra_conf": "\/usr\/lib\/youcompleteme\/ycm_extra_conf.py", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file diff --git a/python/ycm/server_responses.py b/python/ycm/server/responses.py similarity index 96% rename from python/ycm/server_responses.py rename to python/ycm/server/responses.py index f8414d9c..a6544d4b 100644 --- a/python/ycm/server_responses.py +++ b/python/ycm/server/responses.py @@ -18,6 +18,8 @@ # along with YouCompleteMe. If not, see . +# TODO: Move this file under server/ and rename it responses.py + def BuildGoToResponse( filepath, line_num, column_num, description = None ): response = { 'filepath': filepath, diff --git a/python/ycm/server/server.py b/python/ycm/server/server.py new file mode 100755 index 00000000..a9289427 --- /dev/null +++ b/python/ycm/server/server.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import sys +import os + +# We want to have the YouCompleteMe/python directory on the Python PATH because +# all the code already assumes that it's there. This is a relic from before the +# client/server architecture. +# TODO: Fix things so that this is not needed anymore when we split ycmd into a +# separate repository. +sys.path.insert( 0, os.path.join( + os.path.dirname( os.path.abspath( __file__ ) ), + '../..' ) ) + +import logging +import time +import httplib +import json +import bottle +from bottle import run, request, response +import server_state +from ycm import extra_conf_store +from ycm import user_options_store +import argparse + +# num bytes for the request body buffer; request.json only works if the request +# size is less than this +bottle.Request.MEMFILE_MAX = 300 * 1024 + +user_options_store.LoadDefaults() +SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) + +LOGGER = logging.getLogger( __name__ ) +app = bottle.Bottle() + + +@app.post( '/event_notification' ) +def EventNotification(): + LOGGER.info( 'Received event notification') + request_data = request.json + event_name = request_data[ 'event_name' ] + LOGGER.debug( 'Event name: %s', event_name ) + + event_handler = 'On' + event_name + getattr( SERVER_STATE.GetGeneralCompleter(), event_handler )( request_data ) + + filetypes = request_data[ 'filetypes' ] + if SERVER_STATE.FiletypeCompletionUsable( filetypes ): + getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), + event_handler )( request_data ) + + if hasattr( extra_conf_store, event_handler ): + getattr( extra_conf_store, event_handler )( request_data ) + + # TODO: shut down the server on VimClose + + +@app.post( '/run_completer_command' ) +def RunCompleterCommand(): + LOGGER.info( 'Received command request') + request_data = request.json + completer_target = request_data[ 'completer_target' ] + + if completer_target == 'identifier': + completer = SERVER_STATE.GetGeneralCompleter() + else: + completer = SERVER_STATE.GetFiletypeCompleter() + + return _JsonResponse( + completer.OnUserCommand( request_data[ 'command_arguments' ], + request_data ) ) + + +@app.post( '/get_completions' ) +def GetCompletions(): + LOGGER.info( 'Received completion request') + request_data = request.json + do_filetype_completion = SERVER_STATE.ShouldUseFiletypeCompleter( + request_data ) + LOGGER.debug( 'Using filetype completion: %s', do_filetype_completion ) + filetypes = request_data[ 'filetypes' ] + completer = ( SERVER_STATE.GetFiletypeCompleter( filetypes ) if + do_filetype_completion else + SERVER_STATE.GetGeneralCompleter() ) + + # This is necessary so that general_completer_store fills up + # _current_query_completers. + # TODO: Fix this. + completer.ShouldUseNow( request_data ) + + # TODO: This should not be async anymore, server is multi-threaded + completer.CandidatesForQueryAsync( request_data ) + while not completer.AsyncCandidateRequestReady(): + time.sleep( 0.03 ) + return _JsonResponse( completer.CandidatesFromStoredRequest() ) + + +@app.route( '/user_options' ) +def UserOptions(): + global SERVER_STATE + + if request.method == 'GET': + LOGGER.info( 'Received user options GET request') + return SERVER_STATE.user_options + elif request.method == 'POST': + LOGGER.info( 'Received user options POST request') + data = request.json + SERVER_STATE = server_state.ServerState( data ) + user_options_store.SetAll( data ) + else: + response.status = httplib.BAD_REQUEST + + +@app.post( '/filetype_completion_available') +def FiletypeCompletionAvailable(): + LOGGER.info( 'Received filetype completion available request') + return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable( + request.json[ 'filetypes' ] ) ) + + +def _JsonResponse( data ): + response.set_header( 'Content-Type', 'application/json' ) + return json.dumps( data ) + + +def Main(): + global LOGGER + parser = argparse.ArgumentParser() + parser.add_argument( '--host', type = str, default = 'localhost', + help='server hostname') + parser.add_argument( '--port', type = int, default = 6666, + help='server port') + parser.add_argument( '--log', type = str, default = 'info', + help='log level, one of ' + '[debug|info|warning|error|critical]') + args = parser.parse_args() + + numeric_level = getattr( logging, args.log.upper(), None ) + if not isinstance( numeric_level, int ): + raise ValueError( 'Invalid log level: %s' % args.log ) + + logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s', + level = numeric_level ) + + LOGGER = logging.getLogger( __name__ ) + run( app = app, host = args.host, port = args.port, server='cherrypy' ) + + +if __name__ == "__main__": + Main() + diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py new file mode 100644 index 00000000..a7f3eaa0 --- /dev/null +++ b/python/ycm/server/server_state.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import imp +import os +from ycm.completers.general.general_completer_store import GeneralCompleterStore + + +class ServerState( object ): + def __init__( self, user_options ): + self._user_options = user_options + self._filetype_completers = {} + self._gencomp = GeneralCompleterStore( self._user_options ) + + + @property + def user_options( self ): + return self._user_options + + + def _GetFiletypeCompleterForFiletype( self, filetype ): + try: + return self._filetype_completers[ filetype ] + except KeyError: + pass + + module_path = _PathToFiletypeCompleterPluginLoader( filetype ) + + completer = None + supported_filetypes = [ filetype ] + if os.path.exists( module_path ): + module = imp.load_source( filetype, module_path ) + completer = module.GetCompleter( self._user_options ) + if completer: + supported_filetypes.extend( completer.SupportedFiletypes() ) + + for supported_filetype in supported_filetypes: + self._filetype_completers[ supported_filetype ] = completer + return completer + + + def GetFiletypeCompleter( self, current_filetypes ): + completers = [ self._GetFiletypeCompleterForFiletype( filetype ) + for filetype in current_filetypes ] + + for completer in completers: + if completer: + return completer + + return None + + + def FiletypeCompletionAvailable( self, filetypes ): + return bool( self.GetFiletypeCompleter( filetypes ) ) + + + def FiletypeCompletionUsable( self, filetypes ): + return ( self.CurrentFiletypeCompletionEnabled( filetypes ) and + self.FiletypeCompletionAvailable( filetypes ) ) + + + def ShouldUseGeneralCompleter( self, request_data ): + return self._gencomp.ShouldUseNow( request_data ) + + + def ShouldUseFiletypeCompleter( self, request_data ): + filetypes = request_data[ 'filetypes' ] + if self.FiletypeCompletionUsable( filetypes ): + return self.GetFiletypeCompleter( filetypes ).ShouldUseNow( request_data ) + return False + + + def GetGeneralCompleter( self ): + return self._gencomp + + + def CurrentFiletypeCompletionEnabled( self, current_filetypes ): + filetype_to_disable = self._user_options[ + 'filetype_specific_completion_to_disable' ] + return not all([ x in filetype_to_disable for x in current_filetypes ]) + + + +def _PathToCompletersFolder(): + dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) + return os.path.join( dir_of_current_script, '..', 'completers' ) + + +def _PathToFiletypeCompleterPluginLoader( filetype ): + return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' ) + + diff --git a/python/ycm/server/tests/__init__.py b/python/ycm/server/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py new file mode 100644 index 00000000..b7184fac --- /dev/null +++ b/python/ycm/server/tests/basic_test.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from webtest import TestApp +from .. import server +from ..responses import BuildCompletionData +from nose.tools import ok_, eq_ +import bottle + +bottle.debug( True ) + + +def GetCompletions_IdentifierCompleterWorks_test(): + app = TestApp( server.app ) + event_data = { + 'event_name': 'FileReadyToParse', + 'filetypes': ['foo'], + 'filepath': '/foo/bar', + 'file_data': { + '/foo/bar': { + 'contents': 'foo foogoo ba', + 'filetypes': ['foo'] + } + } + } + + app.post_json( '/event_notification', event_data ) + + line_value = 'oo foo foogoo ba'; + completion_data = { + 'query': 'oo', + 'filetypes': ['foo'], + 'filepath': '/foo/bar', + 'line_num': 0, + 'column_num': 2, + 'start_column': 0, + 'line_value': line_value, + 'file_data': { + '/foo/bar': { + 'contents': line_value, + 'filetypes': ['foo'] + } + } + } + + eq_( [ BuildCompletionData( 'foo' ), + BuildCompletionData( 'foogoo' ) ], + app.post_json( '/get_completions', completion_data ).json ) + + +def FiletypeCompletionAvailable_Works_test(): + app = TestApp( server.app ) + request_data = { + 'filetypes': ['cpp'] + } + + ok_( app.post_json( '/filetype_completion_available', + request_data ).json ) diff --git a/python/ycm/user_options_store.py b/python/ycm/user_options_store.py index 759e63b9..b453f01e 100644 --- a/python/ycm/user_options_store.py +++ b/python/ycm/user_options_store.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import json +import os from ycm.frozendict import frozendict _USER_OPTIONS = {} @@ -32,3 +34,16 @@ def GetAll(): def Value( key ): return _USER_OPTIONS[ key ] + + +def LoadDefaults(): + SetAll( _DefaultOptions() ) + + +def _DefaultOptions(): + settings_path = os.path.join( + os.path.dirname( os.path.abspath( __file__ ) ), + 'server/default_settings.json' ) + with open( settings_path ) as f: + return json.loads( f.read() ) + diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 81e4bd77..d66a4fef 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -17,6 +17,9 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import tempfile +import os + def IsIdentifierChar( char ): return char.isalnum() or char == '_' @@ -29,3 +32,7 @@ def ToUtf8IfNeeded( string_or_unicode ): if isinstance( string_or_unicode, unicode ): return string_or_unicode.encode( 'utf8' ) return string_or_unicode + + +def PathToTempDir(): + return os.path.join( tempfile.gettempdir(), 'ycm_temp' ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index ea600f44..b0b95773 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -17,176 +17,53 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import imp import os import time import vim import ycm_core -import logging -import tempfile +import subprocess from ycm import vimsupport -from ycm import base -from ycm import extra_conf_store +from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter -from ycm.completers.general.general_completer_store import GeneralCompleterStore - - -# TODO: Put the Request classes in separate files -class BaseRequest( object ): - def __init__( self ): - pass - - - def Start( self ): - pass - - - def Done( self ): - return True - - - def Response( self ): - return {} - - -class CompletionRequest( BaseRequest ): - def __init__( self, ycm_state ): - super( CompletionRequest, self ).__init__() - - self._completion_start_column = base.CompletionStartColumn() - self._ycm_state = ycm_state - self._request_data = _BuildRequestData( self._completion_start_column ) - self._do_filetype_completion = self._ycm_state.ShouldUseFiletypeCompleter( - self._request_data ) - self._completer = ( self._ycm_state.GetFiletypeCompleter() if - self._do_filetype_completion else - self._ycm_state.GetGeneralCompleter() ) - - - def ShouldComplete( self ): - return ( self._do_filetype_completion or - self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) ) - - - def CompletionStartColumn( self ): - return self._completion_start_column - - - def Start( self, query ): - self._request_data[ 'query' ] = query - self._completer.CandidatesForQueryAsync( self._request_data ) - - def Done( self ): - return self._completer.AsyncCandidateRequestReady() - - - def Results( self ): - try: - return [ _ConvertCompletionDataToVimData( x ) - for x in self._completer.CandidatesFromStoredRequest() ] - except Exception as e: - vimsupport.PostVimMessage( str( e ) ) - return [] - - - -class CommandRequest( BaseRequest ): - class ServerResponse( object ): - def __init__( self ): - pass - - def Valid( self ): - return True - - def __init__( self, ycm_state, arguments, completer_target = None ): - super( CommandRequest, self ).__init__() - - if not completer_target: - completer_target = 'filetpe_default' - - if completer_target == 'omni': - self._completer = ycm_state.GetOmniCompleter() - elif completer_target == 'identifier': - self._completer = ycm_state.GetGeneralCompleter() - else: - self._completer = ycm_state.GetFiletypeCompleter() - self._arguments = arguments - - - def Start( self ): - self._completer.OnUserCommand( self._arguments, - _BuildRequestData() ) - - - def Response( self ): - # TODO: Call vimsupport.JumpToLocation if the user called a GoTo command... - # we may want to have specific subclasses of CommandRequest so that a - # GoToRequest knows it needs to jump after the data comes back. - # - # Also need to run the following on GoTo data: - # CAREFUL about line/column number 0-based/1-based confusion! - # - # defs = [] - # defs.append( {'filename': definition.module_path.encode( 'utf-8' ), - # 'lnum': definition.line, - # 'col': definition.column + 1, - # 'text': definition.description.encode( 'utf-8' ) } ) - # vim.eval( 'setqflist( %s )' % repr( defs ) ) - # vim.eval( 'youcompleteme#OpenGoToList()' ) - return self.ServerResponse() - - -class EventNotification( BaseRequest ): - def __init__( self, event_name, ycm_state, extra_data = None ): - super( EventNotification, self ).__init__() - - self._ycm_state = ycm_state - self._event_name = event_name - self._request_data = _BuildRequestData() - if extra_data: - self._request_data.update( extra_data ) - - - def Start( self ): - event_handler = 'On' + self._event_name - getattr( self._ycm_state.GetGeneralCompleter(), - event_handler )( self._request_data ) - - if self._ycm_state.FiletypeCompletionUsable(): - getattr( self._ycm_state.GetFiletypeCompleter(), - event_handler )( self._request_data ) - - if hasattr( extra_conf_store, event_handler ): - getattr( extra_conf_store, event_handler )( self._request_data ) - - - -def SendEventNotificationAsync( event_name, ycm_state, extra_data = None ): - event = EventNotification( event_name, ycm_state, extra_data ) - event.Start() +from ycm.client.base_request import BaseRequest +from ycm.client.command_request import CommandRequest +from ycm.client.completion_request import CompletionRequest +from ycm.client.event_notification import SendEventNotificationAsync +SERVER_PORT_RANGE_START = 10000 class YouCompleteMe( object ): def __init__( self, user_options ): - # TODO: This should go into the server - # TODO: Use more logging like we do in cs_completer - self._logfile = tempfile.NamedTemporaryFile() - logging.basicConfig( format='%(asctime)s - %(levelname)s - %(message)s', - filename=self._logfile.name, - level=logging.DEBUG ) - self._user_options = user_options - self._gencomp = GeneralCompleterStore( user_options ) self._omnicomp = OmniCompleter( user_options ) - self._filetype_completers = {} self._current_completion_request = None + server_port = SERVER_PORT_RANGE_START + os.getpid() + command = ''.join( [ 'python ', + _PathToServerScript(), + ' --port=', + str( server_port ) ] ) + + BaseRequest.server_location = 'http://localhost:' + str( server_port ) + + filename_format = os.path.join( utils.PathToTempDir(), + 'server_{port}_{std}.log' ) + + self._server_stdout = filename_format.format( port=server_port, + std='stdout' ) + self._server_stderr = filename_format.format( port=server_port, + std='stderr' ) + + with open( self._server_stderr, 'w' ) as fstderr: + with open( self._server_stdout, 'w' ) as fstdout: + subprocess.Popen( command, stdout=fstdout, stderr=fstderr, shell=True ) + def CreateCompletionRequest( self ): # We have to store a reference to the newly created CompletionRequest # because VimScript can't store a reference to a Python object across # function calls... Thus we need to keep this request somewhere. - self._current_completion_request = CompletionRequest( self ) + self._current_completion_request = CompletionRequest() return self._current_completion_request @@ -204,72 +81,19 @@ class YouCompleteMe( object ): return self._current_completion_request - def GetGeneralCompleter( self ): - return self._gencomp - - def GetOmniCompleter( self ): return self._omnicomp - def GetFiletypeCompleter( self ): - filetypes = vimsupport.CurrentFiletypes() - - completers = [ self.GetFiletypeCompleterForFiletype( filetype ) - for filetype in filetypes ] - - if not completers: - return None - - # Try to find a native completer first - for completer in completers: - if completer and completer is not self._omnicomp: - return completer - - # Return the omni completer for the first filetype - return completers[ 0 ] - - - def GetFiletypeCompleterForFiletype( self, filetype ): - try: - return self._filetype_completers[ filetype ] - except KeyError: - pass - - module_path = _PathToFiletypeCompleterPluginLoader( filetype ) - - completer = None - supported_filetypes = [ filetype ] - if os.path.exists( module_path ): - module = imp.load_source( filetype, module_path ) - completer = module.GetCompleter( self._user_options ) - if completer: - supported_filetypes.extend( completer.SupportedFiletypes() ) - else: - completer = self._omnicomp - - for supported_filetype in supported_filetypes: - self._filetype_completers[ supported_filetype ] = completer - return completer - - - def ShouldUseGeneralCompleter( self, request_data ): - return self._gencomp.ShouldUseNow( request_data ) - - - def ShouldUseFiletypeCompleter( self, request_data ): - if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().ShouldUseNow( request_data ) + def NativeFiletypeCompletionAvailable( self ): + # TODO: Talk to server about this. return False - def NativeFiletypeCompletionAvailable( self ): - completer = self.GetFiletypeCompleter() - return bool( completer ) and completer is not self._omnicomp - - - def FiletypeCompletionAvailable( self ): - return bool( self.GetFiletypeCompleter() ) + # TODO: This may not be needed at all when the server is ready. Evaluate this + # later. + # def FiletypeCompletionAvailable( self ): + # return bool( self.GetFiletypeCompleter() ) def NativeFiletypeCompletionUsable( self ): @@ -277,9 +101,11 @@ class YouCompleteMe( object ): self.NativeFiletypeCompletionAvailable() ) - def FiletypeCompletionUsable( self ): - return ( self.CurrentFiletypeCompletionEnabled() and - self.FiletypeCompletionAvailable() ) + # TODO: This may not be needed at all when the server is ready. Evaluate this + # later. + # def FiletypeCompletionUsable( self ): + # return ( self.CurrentFiletypeCompletionEnabled() and + # self.FiletypeCompletionAvailable() ) def OnFileReadyToParse( self ): @@ -290,51 +116,55 @@ class YouCompleteMe( object ): # TODO: make this work again # if self._user_options[ 'seed_identifiers_with_syntax' ]: - SendEventNotificationAsync( 'FileReadyToParse', self, extra_data ) + SendEventNotificationAsync( 'FileReadyToParse', extra_data ) def OnBufferUnload( self, deleted_buffer_file ): SendEventNotificationAsync( 'BufferUnload', - self, { 'unloaded_buffer': deleted_buffer_file } ) def OnBufferVisit( self ): - SendEventNotificationAsync( 'BufferVisit', self ) + SendEventNotificationAsync( 'BufferVisit' ) def OnInsertLeave( self ): - SendEventNotificationAsync( 'InsertLeave', self ) + SendEventNotificationAsync( 'InsertLeave' ) def OnVimLeave( self ): - SendEventNotificationAsync( 'VimLeave', self ) + SendEventNotificationAsync( 'VimLeave' ) def OnCurrentIdentifierFinished( self ): - SendEventNotificationAsync( 'CurrentIdentifierFinished', self ) + SendEventNotificationAsync( 'CurrentIdentifierFinished' ) + # TODO: Make this work again. def DiagnosticsForCurrentFileReady( self ): - if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady() + # if self.FiletypeCompletionUsable(): + # return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady() return False + # TODO: Make this work again. def GetDiagnosticsForCurrentFile( self ): - if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile() + # if self.FiletypeCompletionUsable(): + # return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile() return [] + # TODO: Make this work again. def GetDetailedDiagnostic( self ): - if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().GetDetailedDiagnostic() + # if self.FiletypeCompletionUsable(): + # return self.GetFiletypeCompleter().GetDetailedDiagnostic() + pass + # TODO: Make this work again. def GettingCompletions( self ): - if self.FiletypeCompletionUsable(): - return self.GetFiletypeCompleter().GettingCompletions() + # if self.FiletypeCompletionUsable(): + # return self.GetFiletypeCompleter().GettingCompletions() return False @@ -366,51 +196,13 @@ class YouCompleteMe( object ): return not all([ x in filetype_to_disable for x in filetypes ]) -def _PathToCompletersFolder(): - dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) - return os.path.join( dir_of_current_script, 'completers' ) - - -def _PathToFiletypeCompleterPluginLoader( filetype ): - return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' ) - - -def _BuildRequestData( start_column = None, query = None ): - line, column = vimsupport.CurrentLineAndColumn() - request_data = { - 'filetypes': vimsupport.CurrentFiletypes(), - 'line_num': line, - 'column_num': column, - 'start_column': start_column, - 'line_value': vim.current.line, - 'filepath': vim.current.buffer.name, - 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() - } - - if query: - request_data[ 'query' ] = query - - return request_data - -def _ConvertCompletionDataToVimData( completion_data ): - # see :h complete-items for a description of the dictionary fields - vim_data = { - 'word' : completion_data[ 'insertion_text' ], - 'dup' : 1, - } - - if 'menu_text' in completion_data: - vim_data[ 'abbr' ] = completion_data[ 'menu_text' ] - if 'extra_menu_info' in completion_data: - vim_data[ 'menu' ] = completion_data[ 'extra_menu_info' ] - if 'kind' in completion_data: - vim_data[ 'kind' ] = completion_data[ 'kind' ] - if 'detailed_info' in completion_data: - vim_data[ 'info' ] = completion_data[ 'detailed_info' ] - - return vim_data def _GetTagFiles(): tag_files = vim.eval( 'tagfiles()' ) current_working_directory = os.getcwd() return [ os.path.join( current_working_directory, x ) for x in tag_files ] + + +def _PathToServerScript(): + dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) + return os.path.join( dir_of_current_script, 'server/server.py' ) From f51a6872972f3a17b01628205cc5701c9a849a29 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 13:27:32 -0700 Subject: [PATCH 021/149] Server now shuts down cleanly on VimClose --- plugin/youcompleteme.vim | 6 ++++++ python/ycm/server/server.py | 26 +++++++++++++++++++++++--- python/ycm/utils.py | 16 ++++++++++++++++ python/ycm/youcompleteme.py | 33 +++++++++++++++++++++++---------- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 020db8b2..939d2dea 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -160,6 +160,12 @@ let g:ycm_auto_stop_csharp_server = let g:ycm_csharp_server_port = \ get( g:, 'ycm_csharp_server_port', 2000 ) +let g:ycm_server_use_vim_stdout = + \ get( g:, 'ycm_server_use_vim_stdout', 0 ) + +let g:ycm_server_log_level = + \ get( g:, 'ycm_server_log_level', 'info' ) + " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. augroup youcompletemeStart diff --git a/python/ycm/server/server.py b/python/ycm/server/server.py index a9289427..5c6cfc8e 100755 --- a/python/ycm/server/server.py +++ b/python/ycm/server/server.py @@ -19,6 +19,7 @@ import sys import os +import threading # We want to have the YouCompleteMe/python directory on the Python PATH because # all the code already assumes that it's there. This is a relic from before the @@ -38,6 +39,7 @@ from bottle import run, request, response import server_state from ycm import extra_conf_store from ycm import user_options_store +from ycm import utils import argparse # num bytes for the request body buffer; request.json only works if the request @@ -66,10 +68,15 @@ def EventNotification(): getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), event_handler )( request_data ) - if hasattr( extra_conf_store, event_handler ): - getattr( extra_conf_store, event_handler )( request_data ) + try: + if hasattr( extra_conf_store, event_handler ): + getattr( extra_conf_store, event_handler )( request_data ) + except OSError as e: + LOGGER.exception( e ) + + if event_name == 'VimLeave': + _ScheduleServerShutdown() - # TODO: shut down the server on VimClose @app.post( '/run_completer_command' ) @@ -140,6 +147,19 @@ def _JsonResponse( data ): return json.dumps( data ) +def _ScheduleServerShutdown(): + # The reason why we want to schedule a shutdown in the near future instead of + # just shutting down right now is because we want the current request (the one + # that made us want to shutdown) to complete successfully first. + + def Shutdown(): + # sys.exit() doesn't work because we're not in the main thread. + utils.TerminateProcess( os.getpid() ) + + killer_thread = threading.Timer( 2, Shutdown ) + killer_thread.start() + + def Main(): global LOGGER parser = argparse.ArgumentParser() diff --git a/python/ycm/utils.py b/python/ycm/utils.py index d66a4fef..9f1ae775 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -19,6 +19,8 @@ import tempfile import os +import sys +import signal def IsIdentifierChar( char ): return char.isalnum() or char == '_' @@ -36,3 +38,17 @@ def ToUtf8IfNeeded( string_or_unicode ): def PathToTempDir(): return os.path.join( tempfile.gettempdir(), 'ycm_temp' ) + + +# From here: http://stackoverflow.com/a/8536476/1672783 +def TerminateProcess( pid ): + if sys.platform == 'win32': + import ctypes + PROCESS_TERMINATE = 1 + handle = ctypes.windll.kernel32.OpenProcess( PROCESS_TERMINATE, + False, + pid ) + ctypes.windll.kernel32.TerminateProcess( handle, -1 ) + ctypes.windll.kernel32.CloseHandle( handle ) + else: + os.kill( pid, signal.SIGTERM ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index b0b95773..c8b574c2 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -37,26 +37,39 @@ class YouCompleteMe( object ): self._user_options = user_options self._omnicomp = OmniCompleter( user_options ) self._current_completion_request = None + self._server_stdout = None + self._server_stderr = None + self._SetupServer() + + def _SetupServer( self ): server_port = SERVER_PORT_RANGE_START + os.getpid() command = ''.join( [ 'python ', _PathToServerScript(), ' --port=', - str( server_port ) ] ) + str( server_port ), + ' --log=', + self._user_options[ 'server_log_level' ] ] ) BaseRequest.server_location = 'http://localhost:' + str( server_port ) - filename_format = os.path.join( utils.PathToTempDir(), - 'server_{port}_{std}.log' ) + if self._user_options[ 'server_use_vim_stdout' ]: + subprocess.Popen( command, shell = True ) + else: + filename_format = os.path.join( utils.PathToTempDir(), + 'server_{port}_{std}.log' ) - self._server_stdout = filename_format.format( port=server_port, - std='stdout' ) - self._server_stderr = filename_format.format( port=server_port, - std='stderr' ) + self._server_stdout = filename_format.format( port = server_port, + std = 'stdout' ) + self._server_stderr = filename_format.format( port = server_port, + std = 'stderr' ) - with open( self._server_stderr, 'w' ) as fstderr: - with open( self._server_stdout, 'w' ) as fstdout: - subprocess.Popen( command, stdout=fstdout, stderr=fstderr, shell=True ) + with open( self._server_stderr, 'w' ) as fstderr: + with open( self._server_stdout, 'w' ) as fstdout: + subprocess.Popen( command, + stdout = fstdout, + stderr = fstderr, + shell = True ) def CreateCompletionRequest( self ): From 088eb4d0d2c826f1b294530b91dc6ea1a5071496 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 14:33:14 -0700 Subject: [PATCH 022/149] Cleaner server shutdown Listening for VimLeave was sub-optimal. popen.terminate() is much cleaner. --- python/ycm/completers/completer.py | 13 +++++---- python/ycm/completers/cs/cs_completer.py | 6 ++-- .../general/general_completer_store.py | 12 ++++---- python/ycm/extra_conf_store.py | 5 +++- python/ycm/server/server.py | 28 +++---------------- python/ycm/server/server_state.py | 11 ++++++++ python/ycm/youcompleteme.py | 13 +++++---- 7 files changed, 44 insertions(+), 44 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 321c8e1c..84411d95 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -109,7 +109,10 @@ class Completer( object ): configuration. When the command is called with no arguments you should print a short summary of the supported commands or point the user to the help section where this - information can be found.""" + information can be found. + + Override the Shutdown() member function if your Completer subclass needs to do + custom cleanup logic on server shutdown.""" __metaclass__ = abc.ABCMeta @@ -277,10 +280,6 @@ class Completer( object ): pass - def OnVimLeave( self, request_data ): - pass - - def OnUserCommand( self, arguments, request_data ): raise NotImplementedError( NO_USER_COMMANDS ) @@ -324,6 +323,10 @@ class Completer( object ): return '' + def Shutdown( self ): + pass + + class CompletionsCache( object ): def __init__( self ): self.line = -1 diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 20240ee8..0f902c27 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -46,11 +46,11 @@ class CsharpCompleter( ThreadedCompleter ): self._omnisharp_port = None self._logger = logging.getLogger( __name__ ) - # if self.user_options[ 'auto_start_csharp_server' ]: - # self._StartServer() + if self.user_options[ 'auto_start_csharp_server' ]: + self._StartServer() - def OnVimLeave( self ): + def Shutdown( self ): if ( self.user_options[ 'auto_start_csharp_server' ] and self._ServerIsRunning() ): self._StopServer() diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 3269bc97..4effcdd4 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -115,11 +115,6 @@ class GeneralCompleterStore( Completer ): completer.OnInsertLeave( request_data ) - def OnVimLeave( self, request_data ): - for completer in self._all_completers: - completer.OnVimLeave( request_data ) - - def OnCurrentIdentifierFinished( self, request_data ): for completer in self._all_completers: completer.OnCurrentIdentifierFinished( request_data ) @@ -128,3 +123,10 @@ class GeneralCompleterStore( Completer ): def GettingCompletions( self ): for completer in self._all_completers: completer.GettingCompletions() + + + def Shutdown( self ): + for completer in self._all_completers: + completer.Shutdown() + + diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 15a3f087..277b9d61 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -65,8 +65,11 @@ def CallExtraConfYcmCorePreloadIfExists(): _CallExtraConfMethod( 'YcmCorePreload' ) -def OnVimLeave( request_data ): +def Shutdown(): + # VimClose is for the sake of backwards compatibility; it's a no-op when it + # doesn't exist. _CallExtraConfMethod( 'VimClose' ) + _CallExtraConfMethod( 'Shutdown' ) def _CallExtraConfMethod( function_name ): diff --git a/python/ycm/server/server.py b/python/ycm/server/server.py index 5c6cfc8e..df3d8507 100755 --- a/python/ycm/server/server.py +++ b/python/ycm/server/server.py @@ -19,7 +19,7 @@ import sys import os -import threading +import atexit # We want to have the YouCompleteMe/python directory on the Python PATH because # all the code already assumes that it's there. This is a relic from before the @@ -37,9 +37,7 @@ import json import bottle from bottle import run, request, response import server_state -from ycm import extra_conf_store from ycm import user_options_store -from ycm import utils import argparse # num bytes for the request body buffer; request.json only works if the request @@ -68,16 +66,6 @@ def EventNotification(): getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), event_handler )( request_data ) - try: - if hasattr( extra_conf_store, event_handler ): - getattr( extra_conf_store, event_handler )( request_data ) - except OSError as e: - LOGGER.exception( e ) - - if event_name == 'VimLeave': - _ScheduleServerShutdown() - - @app.post( '/run_completer_command' ) def RunCompleterCommand(): @@ -147,17 +135,9 @@ def _JsonResponse( data ): return json.dumps( data ) -def _ScheduleServerShutdown(): - # The reason why we want to schedule a shutdown in the near future instead of - # just shutting down right now is because we want the current request (the one - # that made us want to shutdown) to complete successfully first. - - def Shutdown(): - # sys.exit() doesn't work because we're not in the main thread. - utils.TerminateProcess( os.getpid() ) - - killer_thread = threading.Timer( 2, Shutdown ) - killer_thread.start() +@atexit.register +def _ServerShutdown(): + SERVER_STATE.Shutdown() def Main(): diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index a7f3eaa0..4824732c 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -19,6 +19,7 @@ import imp import os +from ycm import extra_conf_store from ycm.completers.general.general_completer_store import GeneralCompleterStore @@ -27,6 +28,7 @@ class ServerState( object ): self._user_options = user_options self._filetype_completers = {} self._gencomp = GeneralCompleterStore( self._user_options ) + extra_conf_store.CallExtraConfYcmCorePreloadIfExists() @property @@ -34,6 +36,15 @@ class ServerState( object ): return self._user_options + def Shutdown( self ): + for completer in self._filetype_completers.itervalues(): + completer.Shutdown() + + self._gencomp.Shutdown() + extra_conf_store.Shutdown() + + + def _GetFiletypeCompleterForFiletype( self, filetype ): try: return self._filetype_completers[ filetype ] diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index c8b574c2..b2e0f7ab 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -39,6 +39,7 @@ class YouCompleteMe( object ): self._current_completion_request = None self._server_stdout = None self._server_stderr = None + self._server_popen = None self._SetupServer() @@ -54,7 +55,7 @@ class YouCompleteMe( object ): BaseRequest.server_location = 'http://localhost:' + str( server_port ) if self._user_options[ 'server_use_vim_stdout' ]: - subprocess.Popen( command, shell = True ) + self._server_popen = subprocess.Popen( command, shell = True ) else: filename_format = os.path.join( utils.PathToTempDir(), 'server_{port}_{std}.log' ) @@ -66,10 +67,10 @@ class YouCompleteMe( object ): with open( self._server_stderr, 'w' ) as fstderr: with open( self._server_stdout, 'w' ) as fstdout: - subprocess.Popen( command, - stdout = fstdout, - stderr = fstderr, - shell = True ) + self._server_popen = subprocess.Popen( command, + stdout = fstdout, + stderr = fstderr, + shell = True ) def CreateCompletionRequest( self ): @@ -146,7 +147,7 @@ class YouCompleteMe( object ): def OnVimLeave( self ): - SendEventNotificationAsync( 'VimLeave' ) + self._server_popen.terminate() def OnCurrentIdentifierFinished( self ): From 17716ff51f60129953f0bd1a25618f08b5fcdb20 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 14:34:26 -0700 Subject: [PATCH 023/149] Whitespace fix --- python/ycm/client/base_request.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 650e512b..f5f4840c 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -43,8 +43,9 @@ class BaseRequest( object ): def PostDataToHandler( self, data, handler ): response = requests.post( _BuildUri( handler ), - data = json.dumps( data ), - headers = HEADERS ) + data = json.dumps( data ), + headers = HEADERS ) + response.raise_for_status() if response.text: return response.json() From 9a4707f2c6e0e78b74009e5ef80e09394444736a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 14:34:38 -0700 Subject: [PATCH 024/149] Ignoring failed event notifications These happen rarely and are not a big deal when they do. We still log them to the Vim message area, but we don't annoy the user with the default, in-your-face Python traceback. --- python/ycm/client/event_notification.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index c190e6e2..4aa7825a 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import traceback +from ycm import vimsupport from ycm.client.base_request import BaseRequest, BuildRequestData @@ -33,7 +35,15 @@ class EventNotification( BaseRequest ): request_data.update( self._extra_data ) request_data[ 'event_name' ] = self._event_name - self.PostDataToHandler( request_data, 'event_notification' ) + # On occasion, Vim tries to send event notifications to the server before + # it's up. This causes intrusive exception messages to interrupt the user. + # While we do want to log these exceptions just in case, we post them + # quietly to the Vim message log because nothing bad will happen if the + # server misses some events and we don't want to annoy the user. + try: + self.PostDataToHandler( request_data, 'event_notification' ) + except: + vimsupport.EchoText( traceback.format_exc() ) def SendEventNotificationAsync( event_name, extra_data = None ): From c29dc44b388898e61d19d9a302a0bf77e7bd938a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 14:37:17 -0700 Subject: [PATCH 025/149] Whitespace fix --- python/ycm/server/server_state.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 4824732c..c81e0307 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -44,7 +44,6 @@ class ServerState( object ): extra_conf_store.Shutdown() - def _GetFiletypeCompleterForFiletype( self, filetype ): try: return self._filetype_completers[ filetype ] From c527cda436d11910bf300154a35e3f81c352ad13 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 15:23:33 -0700 Subject: [PATCH 026/149] Only calling Shutdown on valid Completers --- python/ycm/server/server_state.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index c81e0307..1d1c1572 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -38,7 +38,8 @@ class ServerState( object ): def Shutdown( self ): for completer in self._filetype_completers.itervalues(): - completer.Shutdown() + if completer: + completer.Shutdown() self._gencomp.Shutdown() extra_conf_store.Shutdown() From 3b9b9ed0364acc4e7a7df4517cd1ba324a667f1c Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 23 Sep 2013 15:31:11 -0700 Subject: [PATCH 027/149] Ident completer loads syntax keywords again --- .../completers/all/identifier_completer.py | 29 +++++-------- python/ycm/server/tests/basic_test.py | 41 ++++++++++++++++++- python/ycm/youcompleteme.py | 16 +++++++- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index d1b8359d..5fb0b77a 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -37,7 +37,6 @@ class IdentifierCompleter( GeneralCompleter ): self.completer = ycm_core.IdentifierCompleter() self.completer.EnableThreading() self.tags_file_last_mtime = defaultdict( int ) - self.filetypes_with_keywords_loaded = set() self._logger = logging.getLogger( __name__ ) @@ -124,29 +123,23 @@ class IdentifierCompleter( GeneralCompleter ): absolute_paths_to_tag_files ) - # def AddIdentifiersFromSyntax( self ): - # filetype = vim.eval( "&filetype" ) - # if filetype in self.filetypes_with_keywords_loaded: - # return - - # self.filetypes_with_keywords_loaded.add( filetype ) - - # keyword_set = syntax_parse.SyntaxKeywordsForCurrentBuffer() - # keywords = ycm_core.StringVec() - # for keyword in keyword_set: - # keywords.append( keyword ) - - # filepath = SYNTAX_FILENAME + filetype - # self.completer.AddIdentifiersToDatabase( keywords, - # filetype, - # filepath ) + def AddIdentifiersFromSyntax( self, keyword_list, filetypes ): + keyword_vector = ycm_core.StringVec() + for keyword in keyword_list: + keyword_vector.append( ToUtf8IfNeeded( keyword ) ) + filepath = SYNTAX_FILENAME + filetypes[ 0 ] + self.completer.AddIdentifiersToDatabase( keyword_vector, + ToUtf8IfNeeded( filetypes[ 0 ] ), + ToUtf8IfNeeded( filepath ) ) def OnFileReadyToParse( self, request_data ): self.AddBufferIdentifiers( request_data ) if 'tag_files' in request_data: self.AddIdentifiersFromTagFiles( request_data[ 'tag_files' ] ) - #self.AddIdentifiersFromSyntax() + if 'syntax_keywords' in request_data: + self.AddIdentifiersFromSyntax( request_data[ 'syntax_keywords' ], + request_data[ 'filetypes' ] ) def OnInsertLeave( self, request_data ): diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index b7184fac..95fb7c03 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -42,7 +42,7 @@ def GetCompletions_IdentifierCompleterWorks_test(): app.post_json( '/event_notification', event_data ) - line_value = 'oo foo foogoo ba'; + line_value = 'oo foo foogoo ba' completion_data = { 'query': 'oo', 'filetypes': ['foo'], @@ -64,6 +64,45 @@ def GetCompletions_IdentifierCompleterWorks_test(): app.post_json( '/get_completions', completion_data ).json ) +def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): + app = TestApp( server.app ) + event_data = { + 'event_name': 'FileReadyToParse', + 'filetypes': ['foo'], + 'filepath': '/foo/bar', + 'file_data': { + '/foo/bar': { + 'contents': '', + 'filetypes': ['foo'] + } + }, + 'syntax_keywords': ['foo', 'bar', 'zoo'] + } + + app.post_json( '/event_notification', event_data ) + + line_value = 'oo ' + completion_data = { + 'query': 'oo', + 'filetypes': ['foo'], + 'filepath': '/foo/bar', + 'line_num': 0, + 'column_num': 2, + 'start_column': 0, + 'line_value': line_value, + 'file_data': { + '/foo/bar': { + 'contents': line_value, + 'filetypes': ['foo'] + } + } + } + + eq_( [ BuildCompletionData( 'foo' ), + BuildCompletionData( 'zoo' ) ], + app.post_json( '/get_completions', completion_data ).json ) + + def FiletypeCompletionAvailable_Works_test(): app = TestApp( server.app ) request_data = { diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index b2e0f7ab..6eccdc6e 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -25,6 +25,7 @@ import subprocess from ycm import vimsupport from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter +from ycm.completers.general import syntax_parse from ycm.client.base_request import BaseRequest from ycm.client.command_request import CommandRequest from ycm.client.completion_request import CompletionRequest @@ -40,6 +41,7 @@ class YouCompleteMe( object ): self._server_stdout = None self._server_stderr = None self._server_popen = None + self._filetypes_with_keywords_loaded = set() self._SetupServer() @@ -127,8 +129,8 @@ class YouCompleteMe( object ): if self._user_options[ 'collect_identifiers_from_tags_files' ]: extra_data[ 'tag_files' ] = _GetTagFiles() - # TODO: make this work again - # if self._user_options[ 'seed_identifiers_with_syntax' ]: + if self._user_options[ 'seed_identifiers_with_syntax' ]: + self._AddSyntaxDataIfNeeded( extra_data ) SendEventNotificationAsync( 'FileReadyToParse', extra_data ) @@ -210,6 +212,15 @@ class YouCompleteMe( object ): return not all([ x in filetype_to_disable for x in filetypes ]) + def _AddSyntaxDataIfNeeded( self, extra_data ): + filetype = vimsupport.CurrentFiletypes()[ 0 ] + if filetype in self._filetypes_with_keywords_loaded: + return + + self._filetypes_with_keywords_loaded.add( filetype ) + extra_data[ 'syntax_keywords' ] = list( + syntax_parse.SyntaxKeywordsForCurrentBuffer() ) + def _GetTagFiles(): tag_files = vim.eval( 'tagfiles()' ) @@ -220,3 +231,4 @@ def _GetTagFiles(): def _PathToServerScript(): dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) return os.path.join( dir_of_current_script, 'server/server.py' ) + From c01bc0481acd581a8c23ff2954c7e58a735974b1 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 10:04:22 -0700 Subject: [PATCH 028/149] Refactoring the server tests to use a helper func This makes the tests smaller, less repetitive and easier to maintain. --- .../completers/all/identifier_completer.py | 1 + python/ycm/server/tests/basic_test.py | 72 ++++++++----------- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index 5fb0b77a..b1f75db3 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -133,6 +133,7 @@ class IdentifierCompleter( GeneralCompleter ): ToUtf8IfNeeded( filetypes[ 0 ] ), ToUtf8IfNeeded( filepath ) ) + def OnFileReadyToParse( self, request_data ): self.AddBufferIdentifiers( request_data ) if 'tag_files' in request_data: diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 95fb7c03..e6ab628d 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -25,39 +25,38 @@ import bottle bottle.debug( True ) +# 'contents' should be just one line of text +def RequestDataForFileWithContents( filename, contents ): + return { + 'filetypes': ['foo'], + 'filepath': filename, + 'line_value': contents, + 'file_data': { + filename: { + 'contents': contents, + 'filetypes': ['foo'] + } + } + } + def GetCompletions_IdentifierCompleterWorks_test(): app = TestApp( server.app ) - event_data = { + event_data = RequestDataForFileWithContents( '/foo/bar', 'foo foogoo ba' ) + event_data.update( { 'event_name': 'FileReadyToParse', - 'filetypes': ['foo'], - 'filepath': '/foo/bar', - 'file_data': { - '/foo/bar': { - 'contents': 'foo foogoo ba', - 'filetypes': ['foo'] - } - } - } + } ) app.post_json( '/event_notification', event_data ) - line_value = 'oo foo foogoo ba' - completion_data = { + completion_data = RequestDataForFileWithContents( '/foo/bar', + 'oo foo foogoo ba' ) + completion_data.update( { 'query': 'oo', - 'filetypes': ['foo'], - 'filepath': '/foo/bar', 'line_num': 0, 'column_num': 2, 'start_column': 0, - 'line_value': line_value, - 'file_data': { - '/foo/bar': { - 'contents': line_value, - 'filetypes': ['foo'] - } - } - } + } ) eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'foogoo' ) ], @@ -66,37 +65,22 @@ def GetCompletions_IdentifierCompleterWorks_test(): def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( server.app ) - event_data = { + event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data.update( { 'event_name': 'FileReadyToParse', - 'filetypes': ['foo'], - 'filepath': '/foo/bar', - 'file_data': { - '/foo/bar': { - 'contents': '', - 'filetypes': ['foo'] - } - }, 'syntax_keywords': ['foo', 'bar', 'zoo'] - } + } ) app.post_json( '/event_notification', event_data ) - line_value = 'oo ' - completion_data = { + completion_data = RequestDataForFileWithContents( '/foo/bar', + 'oo ' ) + completion_data.update( { 'query': 'oo', - 'filetypes': ['foo'], - 'filepath': '/foo/bar', 'line_num': 0, 'column_num': 2, 'start_column': 0, - 'line_value': line_value, - 'file_data': { - '/foo/bar': { - 'contents': line_value, - 'filetypes': ['foo'] - } - } - } + } ) eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'zoo' ) ], From 387621d957ff262174e1f9fdeb452b405f21211c Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 10:17:38 -0700 Subject: [PATCH 029/149] Moving SendCommandRequest into appropriate module --- python/ycm/client/command_request.py | 10 ++++++++++ python/ycm/youcompleteme.py | 11 ++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py index 77af8602..7e830c68 100644 --- a/python/ycm/client/command_request.py +++ b/python/ycm/client/command_request.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import time from ycm.client.base_request import BaseRequest, BuildRequestData @@ -65,3 +66,12 @@ class CommandRequest( BaseRequest ): # vim.eval( 'youcompleteme#OpenGoToList()' ) return self.ServerResponse() + +def SendCommandRequest( self, arguments, completer ): + request = CommandRequest( self, arguments, completer ) + request.Start() + while not request.Done(): + time.sleep( 0.1 ) + + return request.Response() + diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 6eccdc6e..b585cf9c 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -18,7 +18,6 @@ # along with YouCompleteMe. If not, see . import os -import time import vim import ycm_core import subprocess @@ -27,7 +26,7 @@ from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general import syntax_parse from ycm.client.base_request import BaseRequest -from ycm.client.command_request import CommandRequest +from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest from ycm.client.event_notification import SendEventNotificationAsync @@ -84,13 +83,7 @@ class YouCompleteMe( object ): def SendCommandRequest( self, arguments, completer ): - # TODO: This should be inside a method in a command_request module - request = CommandRequest( self, arguments, completer ) - request.Start() - while not request.Done(): - time.sleep( 0.1 ) - - return request.Response() + return SendCommandRequest( arguments, completer ) def GetCurrentCompletionRequest( self ): From 10469d318d95b74e6acf322c54bfed7302ac3408 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 12:53:44 -0700 Subject: [PATCH 030/149] Passing user options to server now --- python/ycm/client/base_request.py | 3 +- python/ycm/retries.py | 80 +++++++++++++++++++++++++ python/ycm/server/default_settings.json | 2 +- python/ycm/server/server.py | 25 ++++---- python/ycm/server/tests/basic_test.py | 12 ++++ python/ycm/youcompleteme.py | 22 +++++++ 6 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 python/ycm/retries.py diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index f5f4840c..1b922973 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -41,7 +41,8 @@ class BaseRequest( object ): return {} - def PostDataToHandler( self, data, handler ): + @staticmethod + def PostDataToHandler( data, handler ): response = requests.post( _BuildUri( handler ), data = json.dumps( data ), headers = HEADERS ) diff --git a/python/ycm/retries.py b/python/ycm/retries.py new file mode 100644 index 00000000..1d7131d5 --- /dev/null +++ b/python/ycm/retries.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# +# Copyright 2012 by Jeff Laughlin Consulting LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import sys +from time import sleep + +# Source: https://gist.github.com/n1ywb/2570004 + +def example_exc_handler(tries_remaining, exception, delay): + """Example exception handler; prints a warning to stderr. + + tries_remaining: The number of tries remaining. + exception: The exception instance which was raised. + """ + print >> sys.stderr, "Caught '%s', %d tries remaining, sleeping for %s seconds" % (exception, tries_remaining, delay) + + +def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None): + """Function decorator implementing retrying logic. + + delay: Sleep this many seconds * backoff * try number after failure + backoff: Multiply delay by this factor after each failure + exceptions: A tuple of exception classes; default (Exception,) + hook: A function with the signature myhook(tries_remaining, exception); + default None + + The decorator will call the function up to max_tries times if it raises + an exception. + + By default it catches instances of the Exception class and subclasses. + This will recover after all but the most fatal errors. You may specify a + custom tuple of exception classes with the 'exceptions' argument; the + function will only be retried if it raises one of the specified + exceptions. + + Additionally you may specify a hook function which will be called prior + to retrying with the number of remaining tries and the exception instance; + see given example. This is primarily intended to give the opportunity to + log the failure. Hook is not called after failure if no retries remain. + """ + def dec(func): + def f2(*args, **kwargs): + mydelay = delay + tries = range(max_tries) + tries.reverse() + for tries_remaining in tries: + try: + return func(*args, **kwargs) + except exceptions as e: + if tries_remaining > 0: + if hook is not None: + hook(tries_remaining, e, mydelay) + sleep(mydelay) + mydelay = mydelay * backoff + else: + raise + else: + break + return f2 + return dec diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index f6030fa1..b3822b6d 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [ "~\/repos\/*", "\/home\/strahinja\/googrepos\/*", "~\/local_googrepos\/*", "~\/.ycm_extra_conf.py" ], "global_ycm_extra_conf": "\/usr\/lib\/youcompleteme\/ycm_extra_conf.py", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [ "~\/repos\/*", "\/home\/strahinja\/googrepos\/*", "~\/local_googrepos\/*", "~\/.ycm_extra_conf.py" ], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file diff --git a/python/ycm/server/server.py b/python/ycm/server/server.py index df3d8507..1b8387dd 100755 --- a/python/ycm/server/server.py +++ b/python/ycm/server/server.py @@ -32,7 +32,6 @@ sys.path.insert( 0, os.path.join( import logging import time -import httplib import json import bottle from bottle import run, request, response @@ -107,20 +106,20 @@ def GetCompletions(): return _JsonResponse( completer.CandidatesFromStoredRequest() ) -@app.route( '/user_options' ) -def UserOptions(): +@app.get( '/user_options' ) +def GetUserOptions(): + LOGGER.info( 'Received user options GET request') + return _JsonResponse( dict( SERVER_STATE.user_options ) ) + + +@app.post( '/user_options' ) +def SetUserOptions(): global SERVER_STATE - if request.method == 'GET': - LOGGER.info( 'Received user options GET request') - return SERVER_STATE.user_options - elif request.method == 'POST': - LOGGER.info( 'Received user options POST request') - data = request.json - SERVER_STATE = server_state.ServerState( data ) - user_options_store.SetAll( data ) - else: - response.status = httplib.BAD_REQUEST + LOGGER.info( 'Received user options POST request') + data = request.json + SERVER_STATE = server_state.ServerState( data ) + user_options_store.SetAll( data ) @app.post( '/filetype_completion_available') diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index e6ab628d..8f8160fa 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -95,3 +95,15 @@ def FiletypeCompletionAvailable_Works_test(): ok_( app.post_json( '/filetype_completion_available', request_data ).json ) + + +def UserOptions_Works_test(): + app = TestApp( server.app ) + options = app.get( '/user_options' ).json + ok_( len( options ) ) + + options[ 'foobar' ] = 'zoo' + + app.post_json( '/user_options', options ) + eq_( options, app.get( '/user_options' ).json ) + diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index b585cf9c..3d3ca2be 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -21,6 +21,8 @@ import os import vim import ycm_core import subprocess +import threading +from ycm.retries import retries from ycm import vimsupport from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter @@ -41,6 +43,7 @@ class YouCompleteMe( object ): self._server_stderr = None self._server_popen = None self._filetypes_with_keywords_loaded = set() + self._options_thread = None self._SetupServer() @@ -73,6 +76,25 @@ class YouCompleteMe( object ): stderr = fstderr, shell = True ) + self._StartOptionsThread() + + + def _StartOptionsThread( self ): + def OptionsThreadMain( options ): + @retries( 5, delay = 0.5 ) + def PostOptionsToServer(): + BaseRequest.PostDataToHandler( options, 'user_options' ) + + PostOptionsToServer() + + self._options_thread = threading.Thread( + target = OptionsThreadMain, + args = ( dict( self._user_options ), ) ) + + self._options_thread.daemon = True + self._options_thread.start() + + def CreateCompletionRequest( self ): # We have to store a reference to the newly created CompletionRequest From dd2445db063176c0db9f9f7408c0fdc3217bb12e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 14:00:27 -0700 Subject: [PATCH 031/149] Passing user options as file to server This is a much better idea than starting the server in a default state, and then resetting the state with a POST request. --- python/ycm/retries.py | 80 ------------------------------------- python/ycm/server/server.py | 18 ++++++--- python/ycm/youcompleteme.py | 75 ++++++++++++++-------------------- 3 files changed, 44 insertions(+), 129 deletions(-) delete mode 100644 python/ycm/retries.py diff --git a/python/ycm/retries.py b/python/ycm/retries.py deleted file mode 100644 index 1d7131d5..00000000 --- a/python/ycm/retries.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python -# -# Copyright 2012 by Jeff Laughlin Consulting LLC -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - - -import sys -from time import sleep - -# Source: https://gist.github.com/n1ywb/2570004 - -def example_exc_handler(tries_remaining, exception, delay): - """Example exception handler; prints a warning to stderr. - - tries_remaining: The number of tries remaining. - exception: The exception instance which was raised. - """ - print >> sys.stderr, "Caught '%s', %d tries remaining, sleeping for %s seconds" % (exception, tries_remaining, delay) - - -def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None): - """Function decorator implementing retrying logic. - - delay: Sleep this many seconds * backoff * try number after failure - backoff: Multiply delay by this factor after each failure - exceptions: A tuple of exception classes; default (Exception,) - hook: A function with the signature myhook(tries_remaining, exception); - default None - - The decorator will call the function up to max_tries times if it raises - an exception. - - By default it catches instances of the Exception class and subclasses. - This will recover after all but the most fatal errors. You may specify a - custom tuple of exception classes with the 'exceptions' argument; the - function will only be retried if it raises one of the specified - exceptions. - - Additionally you may specify a hook function which will be called prior - to retrying with the number of remaining tries and the exception instance; - see given example. This is primarily intended to give the opportunity to - log the failure. Hook is not called after failure if no retries remain. - """ - def dec(func): - def f2(*args, **kwargs): - mydelay = delay - tries = range(max_tries) - tries.reverse() - for tries_remaining in tries: - try: - return func(*args, **kwargs) - except exceptions as e: - if tries_remaining > 0: - if hook is not None: - hook(tries_remaining, e, mydelay) - sleep(mydelay) - mydelay = mydelay * backoff - else: - raise - else: - break - return f2 - return dec diff --git a/python/ycm/server/server.py b/python/ycm/server/server.py index 1b8387dd..85520ca6 100755 --- a/python/ycm/server/server.py +++ b/python/ycm/server/server.py @@ -114,12 +114,8 @@ def GetUserOptions(): @app.post( '/user_options' ) def SetUserOptions(): - global SERVER_STATE - LOGGER.info( 'Received user options POST request') - data = request.json - SERVER_STATE = server_state.ServerState( data ) - user_options_store.SetAll( data ) + _SetUserOptions( request.json ) @app.post( '/filetype_completion_available') @@ -139,6 +135,13 @@ def _ServerShutdown(): SERVER_STATE.Shutdown() +def _SetUserOptions( options ): + global SERVER_STATE + + SERVER_STATE = server_state.ServerState( options ) + user_options_store.SetAll( options ) + + def Main(): global LOGGER parser = argparse.ArgumentParser() @@ -149,8 +152,13 @@ def Main(): parser.add_argument( '--log', type = str, default = 'info', help='log level, one of ' '[debug|info|warning|error|critical]') + parser.add_argument( '--options_file', type = str, default = '', + help='file with user options, in JSON format') args = parser.parse_args() + if args.options_file: + _SetUserOptions( json.load( open( args.options_file, 'r' ) ) ) + numeric_level = getattr( logging, args.log.upper(), None ) if not isinstance( numeric_level, int ): raise ValueError( 'Invalid log level: %s' % args.log ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 3d3ca2be..2a5b48b7 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -21,8 +21,8 @@ import os import vim import ycm_core import subprocess -import threading -from ycm.retries import retries +import tempfile +import json from ycm import vimsupport from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter @@ -43,57 +43,43 @@ class YouCompleteMe( object ): self._server_stderr = None self._server_popen = None self._filetypes_with_keywords_loaded = set() - self._options_thread = None + self._temp_options_filename = None self._SetupServer() def _SetupServer( self ): server_port = SERVER_PORT_RANGE_START + os.getpid() - command = ''.join( [ 'python ', - _PathToServerScript(), - ' --port=', - str( server_port ), - ' --log=', - self._user_options[ 'server_log_level' ] ] ) + with tempfile.NamedTemporaryFile( delete = False ) as options_file: + self._temp_options_filename = options_file.name + json.dump( dict( self._user_options ), options_file ) + command = ''.join( [ 'python ', + _PathToServerScript(), + ' --port=', + str( server_port ), + ' --options_file=', + options_file.name, + ' --log=', + self._user_options[ 'server_log_level' ] ] ) - BaseRequest.server_location = 'http://localhost:' + str( server_port ) + BaseRequest.server_location = 'http://localhost:' + str( server_port ) - if self._user_options[ 'server_use_vim_stdout' ]: - self._server_popen = subprocess.Popen( command, shell = True ) - else: - filename_format = os.path.join( utils.PathToTempDir(), - 'server_{port}_{std}.log' ) + if self._user_options[ 'server_use_vim_stdout' ]: + self._server_popen = subprocess.Popen( command, shell = True ) + else: + filename_format = os.path.join( utils.PathToTempDir(), + 'server_{port}_{std}.log' ) - self._server_stdout = filename_format.format( port = server_port, - std = 'stdout' ) - self._server_stderr = filename_format.format( port = server_port, - std = 'stderr' ) - - with open( self._server_stderr, 'w' ) as fstderr: - with open( self._server_stdout, 'w' ) as fstdout: - self._server_popen = subprocess.Popen( command, - stdout = fstdout, - stderr = fstderr, - shell = True ) - - self._StartOptionsThread() - - - def _StartOptionsThread( self ): - def OptionsThreadMain( options ): - @retries( 5, delay = 0.5 ) - def PostOptionsToServer(): - BaseRequest.PostDataToHandler( options, 'user_options' ) - - PostOptionsToServer() - - self._options_thread = threading.Thread( - target = OptionsThreadMain, - args = ( dict( self._user_options ), ) ) - - self._options_thread.daemon = True - self._options_thread.start() + self._server_stdout = filename_format.format( port = server_port, + std = 'stdout' ) + self._server_stderr = filename_format.format( port = server_port, + std = 'stderr' ) + with open( self._server_stderr, 'w' ) as fstderr: + with open( self._server_stdout, 'w' ) as fstdout: + self._server_popen = subprocess.Popen( command, + stdout = fstdout, + stderr = fstderr, + shell = False ) def CreateCompletionRequest( self ): @@ -165,6 +151,7 @@ class YouCompleteMe( object ): def OnVimLeave( self ): self._server_popen.terminate() + os.remove( self._temp_options_filename ) def OnCurrentIdentifierFinished( self ): From 4ac5466d80dbb133e73cb58c874fdc626303d812 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 14:04:08 -0700 Subject: [PATCH 032/149] Renaming server.py to ycmd.py --- python/ycm/server/tests/basic_test.py | 10 +++++----- python/ycm/server/{server.py => ycmd.py} | 0 python/ycm/youcompleteme.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) rename python/ycm/server/{server.py => ycmd.py} (100%) diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 8f8160fa..e012c856 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -18,7 +18,7 @@ # along with YouCompleteMe. If not, see . from webtest import TestApp -from .. import server +from .. import ycmd from ..responses import BuildCompletionData from nose.tools import ok_, eq_ import bottle @@ -41,7 +41,7 @@ def RequestDataForFileWithContents( filename, contents ): def GetCompletions_IdentifierCompleterWorks_test(): - app = TestApp( server.app ) + app = TestApp( ycmd.app ) event_data = RequestDataForFileWithContents( '/foo/bar', 'foo foogoo ba' ) event_data.update( { 'event_name': 'FileReadyToParse', @@ -64,7 +64,7 @@ def GetCompletions_IdentifierCompleterWorks_test(): def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): - app = TestApp( server.app ) + app = TestApp( ycmd.app ) event_data = RequestDataForFileWithContents( '/foo/bar', '' ) event_data.update( { 'event_name': 'FileReadyToParse', @@ -88,7 +88,7 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): def FiletypeCompletionAvailable_Works_test(): - app = TestApp( server.app ) + app = TestApp( ycmd.app ) request_data = { 'filetypes': ['cpp'] } @@ -98,7 +98,7 @@ def FiletypeCompletionAvailable_Works_test(): def UserOptions_Works_test(): - app = TestApp( server.app ) + app = TestApp( ycmd.app ) options = app.get( '/user_options' ).json ok_( len( options ) ) diff --git a/python/ycm/server/server.py b/python/ycm/server/ycmd.py similarity index 100% rename from python/ycm/server/server.py rename to python/ycm/server/ycmd.py diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 2a5b48b7..b30653c2 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -232,5 +232,5 @@ def _GetTagFiles(): def _PathToServerScript(): dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) - return os.path.join( dir_of_current_script, 'server/server.py' ) + return os.path.join( dir_of_current_script, 'server/ycmd.py' ) From bdd769d043774d0150e52030bac1dd732d094916 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 14:11:46 -0700 Subject: [PATCH 033/149] default_settings.json now actually has defaults --- python/ycm/server/default_settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index b3822b6d..4aeaa025 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [ "~\/repos\/*", "\/home\/strahinja\/googrepos\/*", "~\/local_googrepos\/*", "~\/.ycm_extra_conf.py" ], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file From 967a4dd519d9a3586f9b11ffa5cf4bbad3f8fcfd Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 14:33:11 -0700 Subject: [PATCH 034/149] Adding a script that prints all YCM TODOs --- print_todos.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 print_todos.sh diff --git a/print_todos.sh b/print_todos.sh new file mode 100755 index 00000000..66e0a456 --- /dev/null +++ b/print_todos.sh @@ -0,0 +1,8 @@ +#!/bin/bash +ag \ +--ignore gmock \ +--ignore jedi/ \ +--ignore OmniSharpServer \ +--ignore testdata \ +TODO \ +cpp/ycm python autoload plugin From bf5708c4225d1680772cc0b6e850362ff2cc923b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 24 Sep 2013 14:48:14 -0700 Subject: [PATCH 035/149] NativeFiletypeCompletionAvailable works again --- python/ycm/youcompleteme.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index b30653c2..7a8b58d2 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -27,7 +27,7 @@ from ycm import vimsupport from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general import syntax_parse -from ycm.client.base_request import BaseRequest +from ycm.client.base_request import BaseRequest, BuildRequestData from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest from ycm.client.event_notification import SendEventNotificationAsync @@ -103,8 +103,11 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): - # TODO: Talk to server about this. - return False + try: + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'filetype_completion_available') + except: + return False # TODO: This may not be needed at all when the server is ready. Evaluate this From 4c6a69b43294ab162d8b4c9c39148beee7582552 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 25 Sep 2013 10:56:46 -0700 Subject: [PATCH 036/149] The UltiSnips completer works again --- autoload/youcompleteme.vim | 2 + cpp/ycm/Result.h | 1 + python/ycm/completers/cpp/clang_completer.py | 4 +- .../general/general_completer_store.py | 11 +----- .../completers/general/ultisnips_completer.py | 24 +++--------- python/ycm/server/tests/basic_test.py | 39 ++++++++++++++++++- python/ycm/server/ycmd.py | 16 +++++--- python/ycm/youcompleteme.py | 28 ++++++++++++- 8 files changed, 89 insertions(+), 36 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index bb6b46aa..3ce1fbd2 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -44,6 +44,7 @@ function! youcompleteme#Enable() py from ycm import vimsupport py from ycm import user_options_store py user_options_store.SetAll( base.BuildServerConf() ) + " TODO: Remove the call to YcmPreload py from ycm import extra_conf_store py extra_conf_store.CallExtraConfYcmCorePreloadIfExists() @@ -549,6 +550,7 @@ endfunction function! youcompleteme#OmniComplete( findstart, base ) if a:findstart let s:omnifunc_mode = 1 + " TODO: Force semantic mode here ( needs to work) return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' ) else return s:CompletionsForQuery( a:base ) diff --git a/cpp/ycm/Result.h b/cpp/ycm/Result.h index a9c6cab4..2aaf8c4d 100644 --- a/cpp/ycm/Result.h +++ b/cpp/ycm/Result.h @@ -88,6 +88,7 @@ private: template< class T > struct ResultAnd { + // TODO: Swap the order of these parameters ResultAnd( T extra_object, const Result &result ) : extra_object_( extra_object ), result_( result ) {} diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 4fbed782..d2fd8b17 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -55,6 +55,9 @@ class ClangCompleter( Completer ): # in progress. We use this to trigger the pending request after the previous # one completes (from GetDiagnosticsForCurrentFile because that's the only # method that knows when the compilation has finished). + # TODO: Remove this now that we have multiple threads in the server; the + # subsequent requests that want to parse will just block until the current + # parse is done and will then proceed. self.extra_parse_desired = False @@ -213,7 +216,6 @@ class ClangCompleter( Completer ): self.flags.Clear() - def OnFileReadyToParse( self, request_data ): filename = request_data[ 'filepath' ] contents = request_data[ 'file_data' ][ filename ][ 'contents' ] diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 4effcdd4..3c80a7b7 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -21,13 +21,7 @@ from ycm.completers.completer import Completer from ycm.completers.all.identifier_completer import IdentifierCompleter from ycm.completers.general.filename_completer import FilenameCompleter - -try: - from ycm.completers.general.ultisnips_completer import UltiSnipsCompleter - USE_ULTISNIPS_COMPLETER = True -except ImportError: - USE_ULTISNIPS_COMPLETER = False - +from ycm.completers.general.ultisnips_completer import UltiSnipsCompleter class GeneralCompleterStore( Completer ): @@ -42,8 +36,7 @@ class GeneralCompleterStore( Completer ): super( GeneralCompleterStore, self ).__init__( user_options ) self._identifier_completer = IdentifierCompleter( user_options ) self._filename_completer = FilenameCompleter( user_options ) - self._ultisnips_completer = ( UltiSnipsCompleter( user_options ) - if USE_ULTISNIPS_COMPLETER else None ) + self._ultisnips_completer = UltiSnipsCompleter( user_options ) self._non_filename_completers = filter( lambda x: x, [ self._ultisnips_completer, self._identifier_completer ] ) diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index dfc37cd1..15858585 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -19,7 +19,6 @@ # along with YouCompleteMe. If not, see . from ycm.completers.general_completer import GeneralCompleter -from UltiSnips import UltiSnips_Manager from ycm.server import responses @@ -52,21 +51,10 @@ class UltiSnipsCompleter( GeneralCompleter ): def OnBufferVisit( self, request_data ): - # TODO: _GetCandidates should be called on the client and it should send - # the snippets to the server - self._candidates = _GetCandidates() + raw_candidates = request_data[ 'ultisnips_snippets' ] + self._candidates = [ + responses.BuildCompletionData( + str( snip[ 'trigger' ] ), + str( ' ' + snip[ 'description' ].encode( 'utf-8' ) ) ) + for snip in raw_candidates ] - -def _GetCandidates(): - try: - rawsnips = UltiSnips_Manager._snips( '', 1 ) - - # UltiSnips_Manager._snips() returns a class instance where: - # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) - # class.description - description of the snippet - return [ responses.BuildCompletionData( - str( snip.trigger ), - str( ' ' + snip.description.encode( 'utf-8' ) ) ) - for snip in rawsnips ] - except: - return [] diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index e012c856..db0bbfed 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -20,7 +20,7 @@ from webtest import TestApp from .. import ycmd from ..responses import BuildCompletionData -from nose.tools import ok_, eq_ +from nose.tools import ok_, eq_, with_setup import bottle bottle.debug( True ) @@ -40,7 +40,12 @@ def RequestDataForFileWithContents( filename, contents ): } -def GetCompletions_IdentifierCompleterWorks_test(): +def Setup(): + ycmd.SetServerStateToDefaults() + + +@with_setup( Setup ) +def GetCompletions_IdentifierCompleter_Works_test(): app = TestApp( ycmd.app ) event_data = RequestDataForFileWithContents( '/foo/bar', 'foo foogoo ba' ) event_data.update( { @@ -63,6 +68,7 @@ def GetCompletions_IdentifierCompleterWorks_test(): app.post_json( '/get_completions', completion_data ).json ) +@with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) event_data = RequestDataForFileWithContents( '/foo/bar', '' ) @@ -87,6 +93,34 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app.post_json( '/get_completions', completion_data ).json ) +@with_setup( Setup ) +def GetCompletions_UltiSnipsCompleter_Works_test(): + app = TestApp( ycmd.app ) + event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data.update( { + 'event_name': 'BufferVisit', + 'ultisnips_snippets': [ + {'trigger': 'foo', 'description': 'bar'}, + {'trigger': 'zoo', 'description': 'goo'}, + ] + } ) + + app.post_json( '/event_notification', event_data ) + + completion_data = RequestDataForFileWithContents( '/foo/bar', 'oo ' ) + completion_data.update( { + 'query': 'oo', + 'line_num': 0, + 'column_num': 2, + 'start_column': 0, + } ) + + eq_( [ BuildCompletionData( 'foo', ' bar' ), + BuildCompletionData( 'zoo', ' goo' ) ], + app.post_json( '/get_completions', completion_data ).json ) + + +@with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) request_data = { @@ -97,6 +131,7 @@ def FiletypeCompletionAvailable_Works_test(): request_data ).json ) +@with_setup( Setup ) def UserOptions_Works_test(): app = TestApp( ycmd.app ) options = app.get( '/user_options' ).json diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 85520ca6..ae2a314f 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -43,9 +43,8 @@ import argparse # size is less than this bottle.Request.MEMFILE_MAX = 300 * 1024 -user_options_store.LoadDefaults() -SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) - +SERVER_STATE = None +# TODO: is init needed here? LOGGER = logging.getLogger( __name__ ) app = bottle.Bottle() @@ -132,14 +131,21 @@ def _JsonResponse( data ): @atexit.register def _ServerShutdown(): - SERVER_STATE.Shutdown() + if SERVER_STATE: + SERVER_STATE.Shutdown() def _SetUserOptions( options ): global SERVER_STATE - SERVER_STATE = server_state.ServerState( options ) user_options_store.SetAll( options ) + SERVER_STATE = server_state.ServerState( options ) + + +def SetServerStateToDefaults(): + global SERVER_STATE + user_options_store.LoadDefaults() + SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) def Main(): diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 7a8b58d2..a6bdc490 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -32,6 +32,12 @@ from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest from ycm.client.event_notification import SendEventNotificationAsync +try: + from UltiSnips import UltiSnips_Manager + USE_ULTISNIPS_DATA = True +except ImportError: + USE_ULTISNIPS_DATA = False + SERVER_PORT_RANGE_START = 10000 class YouCompleteMe( object ): @@ -145,7 +151,9 @@ class YouCompleteMe( object ): def OnBufferVisit( self ): - SendEventNotificationAsync( 'BufferVisit' ) + extra_data = {} + _AddUltiSnipsDataIfNeeded( extra_data ) + SendEventNotificationAsync( 'BufferVisit', extra_data ) def OnInsertLeave( self ): @@ -237,3 +245,21 @@ def _PathToServerScript(): dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) return os.path.join( dir_of_current_script, 'server/ycmd.py' ) + +def _AddUltiSnipsDataIfNeeded( extra_data ): + if not USE_ULTISNIPS_DATA: + return + + try: + rawsnips = UltiSnips_Manager._snips( '', 1 ) + except: + return + + # UltiSnips_Manager._snips() returns a class instance where: + # class.trigger - name of snippet trigger word ( e.g. defn or testcase ) + # class.description - description of the snippet + extra_data[ 'ultisnips_snippets' ] = [ { 'trigger': x.trigger, + 'description': x.description + } for x in rawsnips ] + + From 5b4b3ed28151b916dfdb9d1440c275791812ff7b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 25 Sep 2013 11:07:25 -0700 Subject: [PATCH 037/149] LOGGER now set to None by default Init happens in test setup func or Main --- python/ycm/server/ycmd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index ae2a314f..028682f5 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -44,8 +44,7 @@ import argparse bottle.Request.MEMFILE_MAX = 300 * 1024 SERVER_STATE = None -# TODO: is init needed here? -LOGGER = logging.getLogger( __name__ ) +LOGGER = None app = bottle.Bottle() @@ -143,7 +142,8 @@ def _SetUserOptions( options ): def SetServerStateToDefaults(): - global SERVER_STATE + global SERVER_STATE, LOGGER + LOGGER = logging.getLogger( __name__ ) user_options_store.LoadDefaults() SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) From 13b86563f027389848350e326e87f564c5216d1d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 25 Sep 2013 11:12:34 -0700 Subject: [PATCH 038/149] Minor code formatting changes --- python/ycm/server/ycmd.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 028682f5..2b6df431 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -152,14 +152,14 @@ def Main(): global LOGGER parser = argparse.ArgumentParser() parser.add_argument( '--host', type = str, default = 'localhost', - help='server hostname') + help = 'server hostname') parser.add_argument( '--port', type = int, default = 6666, - help='server port') + help = 'server port') parser.add_argument( '--log', type = str, default = 'info', - help='log level, one of ' - '[debug|info|warning|error|critical]') + help = 'log level, one of ' + '[debug|info|warning|error|critical]' ) parser.add_argument( '--options_file', type = str, default = '', - help='file with user options, in JSON format') + help = 'file with user options, in JSON format' ) args = parser.parse_args() if args.options_file: From fe0c0a1607f91127ad44134f54a4ac2e795fecd7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 13:52:04 -0700 Subject: [PATCH 039/149] GoTo commands work again --- autoload/youcompleteme.vim | 22 +++--- python/ycm/client/base_request.py | 10 +++ python/ycm/client/command_request.py | 76 +++++++++++-------- python/ycm/client/completion_request.py | 1 + python/ycm/completers/completer.py | 2 +- python/ycm/completers/cpp/clang_completer.py | 28 ++----- python/ycm/completers/cs/cs_completer.py | 1 + .../general/general_completer_store.py | 4 + .../ycm/completers/python/jedi_completer.py | 11 +-- python/ycm/server/responses.py | 8 ++ python/ycm/server/server_state.py | 10 ++- python/ycm/server/tests/basic_test.py | 35 +++++++++ python/ycm/server/ycmd.py | 18 +++-- python/ycm/vimsupport.py | 2 +- 14 files changed, 149 insertions(+), 79 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 3ce1fbd2..ed1577a3 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -585,33 +585,27 @@ endfunction command! YcmDebugInfo call s:DebugInfo() + function! s:CompleterCommand(...) " CompleterCommand will call the OnUserCommand function of a completer. " If the first arguments is of the form "ft=..." it can be used to specify the " completer to use (for example "ft=cpp"). Else the native filetype completer " of the current buffer is used. If no native filetype completer is found and - " no completer was specified this throws an error. You can use "ft=ycm:omni" - " to select the omni completer or "ft=ycm:ident" to select the identifier - " completer. The remaining arguments will passed to the completer. + " no completer was specified this throws an error. You can use + " "ft=ycm:ident" to select the identifier completer. + " The remaining arguments will be passed to the completer. let arguments = copy(a:000) let completer = '' if a:0 > 0 && strpart(a:1, 0, 3) == 'ft=' - if a:1 == 'ft=ycm:omni' - let completer = 'omni' - elseif a:1 == 'ft=ycm:ident' + if a:1 == 'ft=ycm:ident' let completer = 'identifier' endif let arguments = arguments[1:] endif -py << EOF -response = ycm_state.SendCommandRequest( vim.eval( 'l:arguments' ), - vim.eval( 'l:completer' ) ) -if not response.Valid(): - vimsupport.PostVimMessage( 'No native completer found for current buffer. ' + - 'Use ft=... as the first argument to specify a completer.') -EOF + py ycm_state.SendCommandRequest( vim.eval( 'l:arguments' ), + \ vim.eval( 'l:completer' ) ) endfunction @@ -625,6 +619,8 @@ function! youcompleteme#OpenGoToList() endfunction +command! -nargs=* YcmCompleter call s:CompleterCommand() + " TODO: Make this work again " command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete " \ YcmCompleter call s:CompleterCommand() diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 1b922973..84b95e81 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -24,6 +24,11 @@ from ycm import vimsupport HEADERS = {'content-type': 'application/json'} +class ServerError( Exception ): + def __init__( self, message ): + super( ServerError, self ).__init__( message ) + + class BaseRequest( object ): def __init__( self ): pass @@ -46,8 +51,13 @@ class BaseRequest( object ): response = requests.post( _BuildUri( handler ), data = json.dumps( data ), headers = HEADERS ) + if response.status_code == requests.codes.server_error: + raise ServerError( response.json()[ 'message' ] ) + # We let Requests handle the other status types, we only handle the 500 + # error code. response.raise_for_status() + if response.text: return response.json() return None diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py index 7e830c68..4c2630fc 100644 --- a/python/ycm/client/command_request.py +++ b/python/ycm/client/command_request.py @@ -17,26 +17,23 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import vim import time -from ycm.client.base_request import BaseRequest, BuildRequestData +from ycm.client.base_request import BaseRequest, BuildRequestData, ServerError +from ycm import vimsupport +from ycm.utils import ToUtf8IfNeeded class CommandRequest( BaseRequest ): - class ServerResponse( object ): - def __init__( self ): - pass - - def Valid( self ): - return True - def __init__( self, arguments, completer_target = None ): super( CommandRequest, self ).__init__() self._arguments = arguments self._completer_target = ( completer_target if completer_target else 'filetype_default' ) - # TODO: Handle this case. - # if completer_target == 'omni': - # completer = SERVER_STATE.GetOmniCompleter() + self._is_goto_command = ( + True if arguments and arguments[ 0 ].startswith( 'GoTo' ) else False ) + self._response = None + def Start( self ): request_data = BuildRequestData() @@ -44,34 +41,51 @@ class CommandRequest( BaseRequest ): 'completer_target': self._completer_target, 'command_arguments': self._arguments } ) - self._response = self.PostDataToHandler( request_data, - 'run_completer_command' ) + try: + self._response = self.PostDataToHandler( request_data, + 'run_completer_command' ) + except ServerError as e: + vimsupport.PostVimMessage( e ) def Response( self ): - # TODO: Call vimsupport.JumpToLocation if the user called a GoTo command... - # we may want to have specific subclasses of CommandRequest so that a - # GoToRequest knows it needs to jump after the data comes back. - # - # Also need to run the following on GoTo data: - # - # CAREFUL about line/column number 0-based/1-based confusion! - # - # defs = [] - # defs.append( {'filename': definition.module_path.encode( 'utf-8' ), - # 'lnum': definition.line, - # 'col': definition.column + 1, - # 'text': definition.description.encode( 'utf-8' ) } ) - # vim.eval( 'setqflist( %s )' % repr( defs ) ) - # vim.eval( 'youcompleteme#OpenGoToList()' ) - return self.ServerResponse() + return self._response -def SendCommandRequest( self, arguments, completer ): - request = CommandRequest( self, arguments, completer ) + def RunPostCommandActionsIfNeeded( self ): + if not self._is_goto_command or not self.Done() or not self._response: + return + + if isinstance( self._response, list ): + defs = [ _BuildQfListItem( x ) for x in self._response ] + vim.eval( 'setqflist( %s )' % repr( defs ) ) + vim.eval( 'youcompleteme#OpenGoToList()' ) + else: + vimsupport.JumpToLocation( self._response[ 'filepath' ], + self._response[ 'line_num' ] + 1, + self._response[ 'column_num' ] ) + + + + +def SendCommandRequest( arguments, completer ): + request = CommandRequest( arguments, completer ) request.Start() while not request.Done(): time.sleep( 0.1 ) + request.RunPostCommandActionsIfNeeded() return request.Response() + +def _BuildQfListItem( goto_data_item ): + qf_item = {} + if 'filepath' in goto_data_item: + qf_item[ 'filename' ] = ToUtf8IfNeeded( goto_data_item[ 'filepath' ] ) + if 'description' in goto_data_item: + qf_item[ 'text' ] = ToUtf8IfNeeded( goto_data_item[ 'description' ] ) + if 'line_num' in goto_data_item: + qf_item[ 'lnum' ] = goto_data_item[ 'line_num' ] + 1 + if 'column_num' in goto_data_item: + qf_item[ 'col' ] = goto_data_item[ 'column_num' ] + return qf_item diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index b3ff943f..076f26ac 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -30,6 +30,7 @@ class CompletionRequest( BaseRequest ): self._request_data = BuildRequestData( self._completion_start_column ) + # TODO: Do we need this anymore? # def ShouldComplete( self ): # return ( self._do_filetype_completion or # self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) ) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 84411d95..c66b7564 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -198,7 +198,7 @@ class Completer( object ): '\n'.join( subcommands ) + '\nSee the docs for information on what they do.' ) else: - return 'No supported subcommands' + return 'This Completer has no supported subcommands.' def FilterAndSortCandidates( self, candidates, query ): diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index d2fd8b17..a4028f5a 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -152,7 +152,8 @@ class ClangCompleter( Completer ): elif command == 'GoToDefinitionElseDeclaration': return self._GoToDefinitionElseDeclaration( request_data ) elif command == 'ClearCompilationFlagCache': - self._ClearCompilationFlagCache( request_data ) + return self._ClearCompilationFlagCache( request_data ) + raise ValueError( self.UserCommandsHelpMessage() ) def _LocationForGoTo( self, goto_function, request_data ): @@ -170,7 +171,7 @@ class ClangCompleter( Completer ): files = self.GetUnsavedFilesVector( request_data ) line = request_data[ 'line_num' ] + 1 - column = request_data[ 'start_column' ] + 1 + column = request_data[ 'column_num' ] + 1 return getattr( self.completer, goto_function )( ToUtf8IfNeeded( filename ), line, @@ -180,7 +181,7 @@ class ClangCompleter( Completer ): def _GoToDefinition( self, request_data ): - location = self._LocationForGoTo( 'GetDefinitionLocation' ) + location = self._LocationForGoTo( 'GetDefinitionLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition.' ) @@ -190,7 +191,7 @@ class ClangCompleter( Completer ): def _GoToDeclaration( self, request_data ): - location = self._LocationForGoTo( 'GetDeclarationLocation' ) + location = self._LocationForGoTo( 'GetDeclarationLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to declaration.' ) @@ -200,9 +201,9 @@ class ClangCompleter( Completer ): def _GoToDefinitionElseDeclaration( self, request_data ): - location = self._LocationForGoTo( 'GetDefinitionLocation' ) + location = self._LocationForGoTo( 'GetDefinitionLocation', request_data ) if not location or not location.IsValid(): - location = self._LocationForGoTo( 'GetDeclarationLocation' ) + location = self._LocationForGoTo( 'GetDeclarationLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition or declaration.' ) @@ -325,20 +326,7 @@ class ClangCompleter( Completer ): source, list( flags ) ) ) - -# TODO: make these functions module-local -# def CompletionDataToDict( completion_data ): -# # see :h complete-items for a description of the dictionary fields -# return { -# 'word' : completion_data.TextToInsertInBuffer(), -# 'abbr' : completion_data.MainCompletionText(), -# 'menu' : completion_data.ExtraMenuInfo(), -# 'kind' : completion_data.kind_, -# 'info' : completion_data.DetailedInfoForPreviewWindow(), -# 'dup' : 1, -# } - - +# TODO: Make this work again # def DiagnosticToDict( diagnostic ): # # see :h getqflist for a description of the dictionary fields # return { diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 0f902c27..4943ccae 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -95,6 +95,7 @@ class CsharpCompleter( ThreadedCompleter ): 'GoToDeclaration', 'GoToDefinitionElseDeclaration' ]: return self._GoToDefinition( request_data ) + raise ValueError( self.UserCommandsHelpMessage() ) def DebugInfo( self ): diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 3c80a7b7..850b39ed 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -51,6 +51,10 @@ class GeneralCompleterStore( Completer ): return set() + def GetIdentifierCompleter( self ): + return self._identifier_completer + + def ShouldUseNow( self, request_data ): self._current_query_completers = [] diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index b64a95e4..ea9113a0 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -88,6 +88,7 @@ class JediCompleter( ThreadedCompleter ): return self._GoToDeclaration( request_data ) elif command == 'GoToDefinitionElseDeclaration': return self._GoToDefinitionElseDeclaration( request_data ) + raise ValueError( self.UserCommandsHelpMessage() ) def _GoToDefinition( self, request_data ): @@ -107,8 +108,8 @@ class JediCompleter( ThreadedCompleter ): def _GoToDefinitionElseDeclaration( self, request_data ): - definitions = self._GetDefinitionsList() or \ - self._GetDefinitionsList( request_data, declaration = True ) + definitions = ( self._GetDefinitionsList( request_data ) or + self._GetDefinitionsList( request_data, declaration = True ) ) if definitions: return self._BuildGoToResponse( definitions ) else: @@ -141,7 +142,7 @@ class JediCompleter( ThreadedCompleter ): raise RuntimeError( 'Builtin modules cannot be displayed.' ) else: return responses.BuildGoToResponse( definition.module_path, - definition.line -1, + definition.line - 1, definition.column ) else: # multiple definitions @@ -149,11 +150,11 @@ class JediCompleter( ThreadedCompleter ): for definition in definition_list: if definition.in_builtin_module(): defs.append( responses.BuildDescriptionOnlyGoToResponse( - 'Builting ' + definition.description ) ) + 'Builtin ' + definition.description ) ) else: defs.append( responses.BuildGoToResponse( definition.module_path, - definition.line -1, + definition.line - 1, definition.column, definition.description ) ) return defs diff --git a/python/ycm/server/responses.py b/python/ycm/server/responses.py index a6544d4b..6be83ce5 100644 --- a/python/ycm/server/responses.py +++ b/python/ycm/server/responses.py @@ -76,3 +76,11 @@ def BuildDiagnosticData( filepath, 'text': text, 'kind': kind } + + +def BuildExceptionResponse( error_message, traceback ): + return { + 'message': error_message, + 'traceback': traceback + } + diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 1d1c1572..afcf6c32 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -74,12 +74,16 @@ class ServerState( object ): if completer: return completer - return None + raise ValueError( 'No semantic completer exists for filetypes: {}'.format( + current_filetypes ) ) def FiletypeCompletionAvailable( self, filetypes ): - return bool( self.GetFiletypeCompleter( filetypes ) ) - + try: + self.GetFiletypeCompleter( filetypes ) + return True + except: + return False def FiletypeCompletionUsable( self, filetypes ): return ( self.CurrentFiletypeCompletionEnabled( filetypes ) and diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index db0bbfed..3f2b6214 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -120,6 +120,41 @@ def GetCompletions_UltiSnipsCompleter_Works_test(): app.post_json( '/get_completions', completion_data ).json ) +@with_setup( Setup ) +def RunCompleterCommand_GoTo_Jedi_ZeroBasedLineAndColumn_test(): + app = TestApp( ycmd.app ) + contents = """ +def foo(): + pass + +foo() +""" + + goto_data = { + 'completer_target': 'filetype_default', + 'command_arguments': ['GoToDefinition'], + 'line_num': 4, + 'column_num': 0, + 'filetypes': ['python'], + 'filepath': '/foo.py', + 'line_value': contents, + 'file_data': { + '/foo.py': { + 'contents': contents, + 'filetypes': ['python'] + } + } + } + + # 0-based line and column! + eq_( { + 'filepath': '/foo.py', + 'line_num': 1, + 'column_num': 4 + }, + app.post_json( '/run_completer_command', goto_data ).json ) + + @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 2b6df431..c0284c15 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -37,6 +37,7 @@ import bottle from bottle import run, request, response import server_state from ycm import user_options_store +from ycm.server.responses import BuildExceptionResponse import argparse # num bytes for the request body buffer; request.json only works if the request @@ -71,13 +72,13 @@ def RunCompleterCommand(): completer_target = request_data[ 'completer_target' ] if completer_target == 'identifier': - completer = SERVER_STATE.GetGeneralCompleter() + completer = SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() else: - completer = SERVER_STATE.GetFiletypeCompleter() + completer = SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) - return _JsonResponse( - completer.OnUserCommand( request_data[ 'command_arguments' ], - request_data ) ) + return _JsonResponse( completer.OnUserCommand( + request_data[ 'command_arguments' ], + request_data ) ) @app.post( '/get_completions' ) @@ -123,6 +124,13 @@ def FiletypeCompletionAvailable(): request.json[ 'filetypes' ] ) ) +# The type of the param is Bottle.HTTPError +@app.error( 500 ) +def ErrorHandler( httperror ): + return _JsonResponse( BuildExceptionResponse( str( httperror.exception ), + httperror.traceback ) ) + + def _JsonResponse( data ): response.set_header( 'Content-Type', 'application/json' ) return json.dumps( data ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 2314be72..bb2e20ec 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -105,7 +105,7 @@ def PostVimMessage( message ): # non-GUI thread which *sometimes* crashes Vim because Vim is not thread-safe. # A consistent crash should force us to notice the error. vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None" - .format( EscapeForVim( message ) ) ) + .format( EscapeForVim( str( message ) ) ) ) def PresentDialog( message, choices, default_choice_index = 0 ): From 4a95c2fc7cde2afb81331932afca5802e3e4e8f8 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 14:59:00 -0700 Subject: [PATCH 040/149] GoTo commands for clang completer work again --- .../ClangCompleter/ClangCompleter_test.cpp | 17 +++++++ cpp/ycm/tests/testdata/basic.cpp | 2 +- python/ycm/client/command_request.py | 2 +- python/ycm/completers/cpp/clang_completer.py | 35 ++++++++------- python/ycm/completers/cpp/flags.py | 7 +-- python/ycm/server/tests/basic_test.py | 44 ++++++++++++++++++- 6 files changed, 86 insertions(+), 21 deletions(-) diff --git a/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp b/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp index c40aa1ee..e70f365a 100644 --- a/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp +++ b/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp @@ -61,4 +61,21 @@ TEST( ClangCompleterTest, CandidatesForQueryAndLocationInFileAsync ) { EXPECT_TRUE( !completions_future.GetResults()->empty() ); } +TEST( ClangCompleterTest, GetDefinitionLocation ) { + ClangCompleter completer; + std::string filename = PathToTestFile( "basic.cpp" ).string(); + + // Clang operates on the reasonable assumption that line and column numbers + // are 1-based. + Location actual_location = + completer.GetDefinitionLocation( + filename, + 9, + 3, + std::vector< UnsavedFile >(), + std::vector< std::string >() ); + + EXPECT_EQ( Location( filename, 1, 8 ), actual_location ); +} + } // namespace YouCompleteMe diff --git a/cpp/ycm/tests/testdata/basic.cpp b/cpp/ycm/tests/testdata/basic.cpp index b5bdf7f6..722d6bc3 100644 --- a/cpp/ycm/tests/testdata/basic.cpp +++ b/cpp/ycm/tests/testdata/basic.cpp @@ -2,7 +2,7 @@ struct Foo { int x; int y; char c; -} +}; int main() { diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py index 4c2630fc..d3fe5ad3 100644 --- a/python/ycm/client/command_request.py +++ b/python/ycm/client/command_request.py @@ -63,7 +63,7 @@ class CommandRequest( BaseRequest ): else: vimsupport.JumpToLocation( self._response[ 'filepath' ], self._response[ 'line_num' ] + 1, - self._response[ 'column_num' ] ) + self._response[ 'column_num' ] + 1) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index a4028f5a..3e2e318a 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -24,7 +24,7 @@ from ycm.server import responses from ycm import extra_conf_store from ycm.utils import ToUtf8IfNeeded from ycm.completers.completer import Completer -from ycm.completers.cpp.flags import Flags +from ycm.completers.cpp.flags import Flags, PrepareFlagsForClang CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] ) MIN_LINES_IN_FILE_TO_PARSE = 5 @@ -96,7 +96,7 @@ class ClangCompleter( Completer ): return responses.BuildDisplayMessageResponse( PARSING_FILE_MESSAGE ) - flags = self.flags.FlagsForFile( filename ) + flags = self._FlagsForRequest( request_data ) if not flags: self.completions_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) @@ -160,14 +160,12 @@ class ClangCompleter( Completer ): filename = request_data[ 'filepath' ] if not filename: self._logger.warning( INVALID_FILE_MESSAGE ) - return responses.BuildDisplayMessageResponse( - INVALID_FILE_MESSAGE ) + raise ValueError( INVALID_FILE_MESSAGE ) - flags = self.flags.FlagsForFile( filename ) + flags = self._FlagsForRequest( request_data ) if not flags: self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return responses.BuildDisplayMessageResponse( - NO_COMPILE_FLAGS_MESSAGE ) + raise ValueError( NO_COMPILE_FLAGS_MESSAGE ) files = self.GetUnsavedFilesVector( request_data ) line = request_data[ 'line_num' ] + 1 @@ -186,8 +184,8 @@ class ClangCompleter( Completer ): raise RuntimeError( 'Can\'t jump to definition.' ) return responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + location.line_number_ - 1, + location.column_number_ - 1) def _GoToDeclaration( self, request_data ): @@ -196,8 +194,8 @@ class ClangCompleter( Completer ): raise RuntimeError( 'Can\'t jump to declaration.' ) return responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + location.line_number_ - 1, + location.column_number_ - 1) def _GoToDefinitionElseDeclaration( self, request_data ): @@ -208,8 +206,8 @@ class ClangCompleter( Completer ): raise RuntimeError( 'Can\'t jump to definition or declaration.' ) return responses.BuildGoToResponse( location.filename_, - location.line_number_, - location.column_number_ ) + location.line_number_ - 1, + location.column_number_ - 1) @@ -234,7 +232,7 @@ class ClangCompleter( Completer ): self.extra_parse_desired = True return - flags = self.flags.FlagsForFile( filename ) + flags = self._FlagsForRequest( request_data ) if not flags: self.parse_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) @@ -319,13 +317,20 @@ class ClangCompleter( Completer ): filename = request_data[ 'filepath' ] if not filename: return '' - flags = self.flags.FlagsForFile( filename ) or [] + flags = self._FlagsForRequest( request_data ) or [] source = extra_conf_store.ModuleFileForSourceFile( filename ) return responses.BuildDisplayMessageResponse( 'Flags for {0} loaded from {1}:\n{2}'.format( filename, source, list( flags ) ) ) + def _FlagsForRequest( self, request_data ): + filename = request_data[ 'filepath' ] + if 'compilation_flags' in request_data: + return PrepareFlagsForClang( request_data[ 'compilation_flags' ], + filename ) + return self.flags.FlagsForFile( filename ) + # TODO: Make this work again # def DiagnosticToDict( diagnostic ): # # see :h getqflist for a description of the dictionary fields diff --git a/python/ycm/completers/cpp/flags.py b/python/ycm/completers/cpp/flags.py index 17c4981a..a896986f 100644 --- a/python/ycm/completers/cpp/flags.py +++ b/python/ycm/completers/cpp/flags.py @@ -20,6 +20,7 @@ import ycm_core import os from ycm import extra_conf_store +from ycm.utils import ToUtf8IfNeeded NO_EXTRA_CONF_FILENAME_MESSAGE = ( 'No {0} file detected, so no compile flags ' 'are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE ' @@ -54,7 +55,7 @@ class Flags( object ): flags = list( results[ 'flags' ] ) if add_special_clang_flags: flags += self.special_clang_flags - sanitized_flags = _PrepareFlagsForClang( flags, filename ) + sanitized_flags = PrepareFlagsForClang( flags, filename ) if results[ 'do_cache' ]: self.flags_for_file[ filename ] = sanitized_flags @@ -90,7 +91,7 @@ class Flags( object ): self.flags_for_file.clear() -def _PrepareFlagsForClang( flags, filename ): +def PrepareFlagsForClang( flags, filename ): flags = _RemoveUnusedFlags( flags, filename ) flags = _SanitizeFlags( flags ) return flags @@ -116,7 +117,7 @@ def _SanitizeFlags( flags ): vector = ycm_core.StringVec() for flag in sanitized_flags: - vector.append( flag ) + vector.append( ToUtf8IfNeeded( flag ) ) return vector diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 3f2b6214..632e9d40 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -137,7 +137,6 @@ foo() 'column_num': 0, 'filetypes': ['python'], 'filepath': '/foo.py', - 'line_value': contents, 'file_data': { '/foo.py': { 'contents': contents, @@ -155,6 +154,49 @@ foo() app.post_json( '/run_completer_command', goto_data ).json ) +@with_setup( Setup ) +def RunCompleterCommand_GoTo_Clang_ZeroBasedLineAndColumn_test(): + app = TestApp( ycmd.app ) + contents = """ +struct Foo { + int x; + int y; + char c; +}; + +int main() +{ + Foo foo; + return 0; +} +""" + + filename = '/foo.cpp' + goto_data = { + 'compilation_flags': ['-x', 'c++'], + 'completer_target': 'filetype_default', + 'command_arguments': ['GoToDefinition'], + 'line_num': 9, + 'column_num': 2, + 'filetypes': ['cpp'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': contents, + 'filetypes': ['cpp'] + } + } + } + + # 0-based line and column! + eq_( { + 'filepath': '/foo.cpp', + 'line_num': 1, + 'column_num': 7 + }, + app.post_json( '/run_completer_command', goto_data ).json ) + + @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) From a6a364de41d59199be7b78e974b939a7fa3b7f04 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:16:55 -0700 Subject: [PATCH 041/149] Fix bug in cache clear clang completer subcommand --- python/ycm/completers/cpp/clang_completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 3e2e318a..5aebae8e 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -152,7 +152,7 @@ class ClangCompleter( Completer ): elif command == 'GoToDefinitionElseDeclaration': return self._GoToDefinitionElseDeclaration( request_data ) elif command == 'ClearCompilationFlagCache': - return self._ClearCompilationFlagCache( request_data ) + return self._ClearCompilationFlagCache() raise ValueError( self.UserCommandsHelpMessage() ) From 718e9974b761f18c0f57b50c15f75e69d9dbdca7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:18:04 -0700 Subject: [PATCH 042/149] Resolving symlinks in GoTo target filepath --- python/ycm/server/responses.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/ycm/server/responses.py b/python/ycm/server/responses.py index 6be83ce5..37a2afd4 100644 --- a/python/ycm/server/responses.py +++ b/python/ycm/server/responses.py @@ -17,12 +17,12 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import os -# TODO: Move this file under server/ and rename it responses.py def BuildGoToResponse( filepath, line_num, column_num, description = None ): response = { - 'filepath': filepath, + 'filepath': os.path.realpath( filepath ), 'line_num': line_num, 'column_num': column_num } From 446d02f66edcce63852b601e92db6865d18193d4 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:20:35 -0700 Subject: [PATCH 043/149] Subcommand name completion works again --- autoload/youcompleteme.vim | 16 ++++------ python/ycm/server/tests/basic_test.py | 46 ++++++++++++++++++++++++--- python/ycm/server/ycmd.py | 26 +++++++++++---- python/ycm/youcompleteme.py | 5 +++ 4 files changed, 72 insertions(+), 21 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index ed1577a3..c60c207a 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -619,17 +619,13 @@ function! youcompleteme#OpenGoToList() endfunction -command! -nargs=* YcmCompleter call s:CompleterCommand() +command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete + \ YcmCompleter call s:CompleterCommand() -" TODO: Make this work again -" command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete -" \ YcmCompleter call s:CompleterCommand() -" -" -" function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) -" return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ), -" \ "\n") -" endfunction +function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) + return join( pyeval( 'ycm_state.GetDefinedSubcommands()' ), + \ "\n") +endfunction function! s:ForceCompile() diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 632e9d40..ad8b52a8 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -26,14 +26,15 @@ import bottle bottle.debug( True ) # 'contents' should be just one line of text -def RequestDataForFileWithContents( filename, contents ): +def RequestDataForFileWithContents( filename, contents = None ): + real_contents = contents if contents else '' return { 'filetypes': ['foo'], 'filepath': filename, - 'line_value': contents, + 'line_value': real_contents, 'file_data': { filename: { - 'contents': contents, + 'contents': real_contents, 'filetypes': ['foo'] } } @@ -71,7 +72,7 @@ def GetCompletions_IdentifierCompleter_Works_test(): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data.update( { 'event_name': 'FileReadyToParse', 'syntax_keywords': ['foo', 'bar', 'zoo'] @@ -96,7 +97,7 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): @with_setup( Setup ) def GetCompletions_UltiSnipsCompleter_Works_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data.update( { 'event_name': 'BufferVisit', 'ultisnips_snippets': [ @@ -197,6 +198,41 @@ int main() app.post_json( '/run_completer_command', goto_data ).json ) +@with_setup( Setup ) +def DefinedSubcommands_Works_test(): + app = TestApp( ycmd.app ) + subcommands_data = RequestDataForFileWithContents( '/foo/bar' ) + subcommands_data.update( { + 'completer_target': 'python', + } ) + + eq_( [ 'GoToDefinition', + 'GoToDeclaration', + 'GoToDefinitionElseDeclaration' ], + app.post_json( '/defined_subcommands', subcommands_data ).json ) + + +@with_setup( Setup ) +def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): + app = TestApp( ycmd.app ) + filename = 'foo.py' + subcommands_data = { + 'filetypes': ['python'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': '', + 'filetypes': ['python'] + } + } + } + + eq_( [ 'GoToDefinition', + 'GoToDeclaration', + 'GoToDefinitionElseDeclaration' ], + app.post_json( '/defined_subcommands', subcommands_data ).json ) + + @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index c0284c15..9a628761 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -69,12 +69,7 @@ def EventNotification(): def RunCompleterCommand(): LOGGER.info( 'Received command request') request_data = request.json - completer_target = request_data[ 'completer_target' ] - - if completer_target == 'identifier': - completer = SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() - else: - completer = SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) + completer = _GetCompleterForRequestData( request_data ) return _JsonResponse( completer.OnUserCommand( request_data[ 'command_arguments' ], @@ -124,6 +119,14 @@ def FiletypeCompletionAvailable(): request.json[ 'filetypes' ] ) ) +@app.post( '/defined_subcommands') +def DefinedSubcommands(): + LOGGER.info( 'Received defined subcommands request') + completer = _GetCompleterForRequestData( request.json ) + + return _JsonResponse( completer.DefinedSubcommands() ) + + # The type of the param is Bottle.HTTPError @app.error( 500 ) def ErrorHandler( httperror ): @@ -136,6 +139,17 @@ def _JsonResponse( data ): return json.dumps( data ) +def _GetCompleterForRequestData( request_data ): + completer_target = request_data.get( 'completer_target', None ) + + if completer_target == 'identifier': + return SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() + elif completer_target == 'filetype_default' or not completer_target: + return SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) + else: + return SERVER_STATE.GetFiletypeCompleter( [ completer_target ] ) + + @atexit.register def _ServerShutdown(): if SERVER_STATE: diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index a6bdc490..0e0009d6 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -100,6 +100,11 @@ class YouCompleteMe( object ): return SendCommandRequest( arguments, completer ) + def GetDefinedSubcommands( self ): + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'defined_subcommands' ) + + def GetCurrentCompletionRequest( self ): return self._current_completion_request From 1027de73fb5db0e898e24873c333237a3da034b2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:24:42 -0700 Subject: [PATCH 044/149] Using a constant for server error instead of 500 --- python/ycm/server/ycmd.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 9a628761..4b27dd63 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -39,6 +39,7 @@ import server_state from ycm import user_options_store from ycm.server.responses import BuildExceptionResponse import argparse +import httplib # num bytes for the request body buffer; request.json only works if the request # size is less than this @@ -128,7 +129,7 @@ def DefinedSubcommands(): # The type of the param is Bottle.HTTPError -@app.error( 500 ) +@app.error( httplib.INTERNAL_SERVER_ERROR ) def ErrorHandler( httperror ): return _JsonResponse( BuildExceptionResponse( str( httperror.exception ), httperror.traceback ) ) From c9b4e9ed3a34c1098074fc8afdf44b4d5c80eb88 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:50:49 -0700 Subject: [PATCH 045/149] Minor code style changes --- python/ycm/completers/python/jedi_completer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index ea9113a0..b2149076 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -72,9 +72,9 @@ class JediCompleter( ThreadedCompleter ): for completion in script.completions() ] def DefinedSubcommands( self ): - return [ "GoToDefinition", - "GoToDeclaration", - "GoToDefinitionElseDeclaration" ] + return [ 'GoToDefinition', + 'GoToDeclaration', + 'GoToDefinitionElseDeclaration' ] def OnUserCommand( self, arguments, request_data ): From 3ca758a581ea8f20d506ef0248cbfb8525143f17 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 17:13:43 -0700 Subject: [PATCH 046/149] Getting debug info works again --- python/ycm/completers/completer.py | 2 +- python/ycm/completers/cpp/clang_completer.py | 7 +++---- python/ycm/server/responses.py | 2 ++ python/ycm/server/ycmd.py | 20 +++++++++++++++++++ python/ycm/youcompleteme.py | 21 ++------------------ 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index c66b7564..7bf055f9 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -319,7 +319,7 @@ class Completer( object ): pass - def DebugInfo( self ): + def DebugInfo( self, request_data ): return '' diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 5aebae8e..339379ce 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -319,10 +319,9 @@ class ClangCompleter( Completer ): return '' flags = self._FlagsForRequest( request_data ) or [] source = extra_conf_store.ModuleFileForSourceFile( filename ) - return responses.BuildDisplayMessageResponse( - 'Flags for {0} loaded from {1}:\n{2}'.format( filename, - source, - list( flags ) ) ) + return 'Flags for {0} loaded from {1}:\n{2}'.format( filename, + source, + list( flags ) ) def _FlagsForRequest( self, request_data ): filename = request_data[ 'filepath' ] diff --git a/python/ycm/server/responses.py b/python/ycm/server/responses.py index 37a2afd4..450106d9 100644 --- a/python/ycm/server/responses.py +++ b/python/ycm/server/responses.py @@ -38,6 +38,8 @@ def BuildDescriptionOnlyGoToResponse( text ): } +# TODO: Look at all the callers and ensure they are not using this instead of an +# exception. def BuildDisplayMessageResponse( text ): return { 'message': text diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 4b27dd63..609243d7 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -128,6 +128,26 @@ def DefinedSubcommands(): return _JsonResponse( completer.DefinedSubcommands() ) +@app.post( '/debug_info') +def DebugInfo(): + # This can't be at the top level because of possible extra conf preload + import ycm_core + LOGGER.info( 'Received debug info request') + + output = [] + has_clang_support = ycm_core.HasClangSupport() + output.append( 'Server has Clang support compiled in: {0}'.format( + has_clang_support ) ) + + if has_clang_support: + output.append( ycm_core.ClangVersion() ) + + request_data = request.json + output.append( + _GetCompleterForRequestData( request_data ).DebugInfo( request_data) ) + return _JsonResponse( '\n'.join( output ) ) + + # The type of the param is Bottle.HTTPError @app.error( httplib.INTERNAL_SERVER_ERROR ) def ErrorHandler( httperror ): diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 0e0009d6..44146ec1 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -19,7 +19,6 @@ import os import vim -import ycm_core import subprocess import tempfile import json @@ -203,24 +202,8 @@ class YouCompleteMe( object ): def DebugInfo( self ): - completers = set( self._filetype_completers.values() ) - completers.add( self._gencomp ) - output = [] - for completer in completers: - if not completer: - continue - debug = completer.DebugInfo() - if debug: - output.append( debug ) - - has_clang_support = ycm_core.HasClangSupport() - output.append( 'Has Clang support compiled in: {0}'.format( - has_clang_support ) ) - - if has_clang_support: - output.append( ycm_core.ClangVersion() ) - - return '\n'.join( output ) + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'debug_info' ) def CurrentFiletypeCompletionEnabled( self ): From e38d145a47bb692bfd89d5d80e0f71e3ec68f9a6 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 17:26:14 -0700 Subject: [PATCH 047/149] Added debug info about the server Location where running + logfiles location --- python/ycm/server/ycmd.py | 7 +++++-- python/ycm/youcompleteme.py | 14 +++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 609243d7..2730e56f 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -143,8 +143,11 @@ def DebugInfo(): output.append( ycm_core.ClangVersion() ) request_data = request.json - output.append( - _GetCompleterForRequestData( request_data ).DebugInfo( request_data) ) + try: + output.append( + _GetCompleterForRequestData( request_data ).DebugInfo( request_data) ) + except: + pass return _JsonResponse( '\n'.join( output ) ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 44146ec1..78ee83cf 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -84,7 +84,7 @@ class YouCompleteMe( object ): self._server_popen = subprocess.Popen( command, stdout = fstdout, stderr = fstderr, - shell = False ) + shell = True ) def CreateCompletionRequest( self ): @@ -202,8 +202,16 @@ class YouCompleteMe( object ): def DebugInfo( self ): - return BaseRequest.PostDataToHandler( BuildRequestData(), - 'debug_info' ) + debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), + 'debug_info' ) + debug_info += '\nServer running at: {}'.format( + BaseRequest.server_location ) + if self._server_stderr or self._server_stdout: + debug_info += '\nServer logfiles:\n {}\n {}'.format( + self._server_stdout, + self._server_stderr ) + + return debug_info def CurrentFiletypeCompletionEnabled( self ): From 6c53bad58f2c9d67167ca49ff536f381f2088653 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 30 Sep 2013 13:40:25 -0700 Subject: [PATCH 048/149] No more threading in completers! The server is multi-threaded and will spawn a new thread for each new request. Thus, the completers need not manage their own threads or even provide async APIs; we _want_ them to block because now were implementing the request-response networking API. The client gets the async API through the network (i.e., it can do something else while the request is pending). --- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 345 +----------------- cpp/ycm/ClangCompleter/ClangCompleter.h | 106 +----- cpp/ycm/ClangCompleter/ClangResultsCache.cpp | 43 --- cpp/ycm/ClangCompleter/ClangResultsCache.h | 84 ----- .../ClangCompleter/TranslationUnitStore.cpp | 7 + cpp/ycm/IdentifierCompleter.cpp | 152 +------- cpp/ycm/IdentifierCompleter.h | 43 --- .../ClangCompleter/ClangCompleter_test.cpp | 18 - cpp/ycm/ycm_core.cpp | 37 +- python/test_requirements.txt | 2 + .../completers/all/identifier_completer.py | 42 +-- python/ycm/completers/completer.py | 119 ++---- python/ycm/completers/cpp/clang_completer.py | 118 ++---- python/ycm/completers/cs/cs_completer.py | 6 +- .../completers/general/filename_completer.py | 6 +- .../general/general_completer_store.py | 15 +- .../completers/general/ultisnips_completer.py | 16 +- .../ycm/completers/python/jedi_completer.py | 6 +- python/ycm/completers/threaded_completer.py | 100 ----- python/ycm/server/tests/basic_test.py | 48 +++ python/ycm/server/ycmd.py | 12 +- 21 files changed, 189 insertions(+), 1136 deletions(-) delete mode 100644 cpp/ycm/ClangCompleter/ClangResultsCache.cpp delete mode 100644 cpp/ycm/ClangCompleter/ClangResultsCache.h delete mode 100644 python/ycm/completers/threaded_completer.py diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index 961de1d7..141af8ac 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic +// Copyright (C) 2011-2013 Strahinja Val Markovic // // This file is part of YouCompleteMe. // @@ -28,43 +28,17 @@ #include "ClangUtils.h" #include -#include -#include -#include +#include -using boost::algorithm::any_of; -using boost::algorithm::is_upper; -using boost::bind; -using boost::cref; -using boost::function; -using boost::lock_guard; -using boost::make_shared; -using boost::mutex; -using boost::packaged_task; -using boost::ref; -using boost::shared_lock; -using boost::shared_mutex; using boost::shared_ptr; -using boost::thread; -using boost::thread_interrupted; -using boost::try_to_lock_t; -using boost::unique_future; -using boost::unique_lock; using boost::unordered_map; namespace YouCompleteMe { -extern const unsigned int MAX_ASYNC_THREADS; -extern const unsigned int MIN_ASYNC_THREADS; - ClangCompleter::ClangCompleter() : clang_index_( clang_createIndex( 0, 0 ) ), - translation_unit_store_( clang_index_ ), - candidate_repository_( CandidateRepository::Instance() ), - threading_enabled_( false ), - time_to_die_( false ), - clang_data_ready_( false ) { + translation_unit_store_( clang_index_ ) { // The libclang docs don't say what is the default value for crash recovery. // I'm pretty sure it's turned on by default, but I'm not going to take any // chances. @@ -73,35 +47,18 @@ ClangCompleter::ClangCompleter() ClangCompleter::~ClangCompleter() { - { - unique_lock< shared_mutex > lock( time_to_die_mutex_ ); - time_to_die_ = true; - } - - sorting_threads_.interrupt_all(); - sorting_threads_.join_all(); - - if ( clang_thread_ ) { - clang_thread_->interrupt(); - clang_thread_->join(); - } - // We need to destroy all TUs before calling clang_disposeIndex because // the translation units need to be destroyed before the index is destroyed. + // Technically, a thread could still be holding onto a shared_ptr object + // when we destroy the clang index, but since we're shutting down, we don't + // really care. + // In practice, this situation shouldn't happen because the server threads are + // Python deamon threads and will all be killed before the main thread exits. translation_unit_store_.RemoveAll(); clang_disposeIndex( clang_index_ ); } -// We need this mostly so that we can not use it in tests. Apparently the -// GoogleTest framework goes apeshit on us (on some platforms, in some -// occasions) if we enable threads by default. -void ClangCompleter::EnableThreading() { - threading_enabled_ = true; - InitThreads(); -} - - std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( std::string filename ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); @@ -156,30 +113,6 @@ void ClangCompleter::UpdateTranslationUnit( } -Future< void > ClangCompleter::UpdateTranslationUnitAsync( - std::string filename, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { - function< void() > functor = - boost::bind( &ClangCompleter::UpdateTranslationUnit, - boost::ref( *this ), - boost::move( filename ), - boost::move( unsaved_files ), - boost::move( flags ) ); - - shared_ptr< ClangPackagedTask > clang_packaged_task = - make_shared< ClangPackagedTask >(); - - clang_packaged_task->parsing_task_ = packaged_task< void >( - boost::move( functor ) ); - unique_future< void > future = - clang_packaged_task->parsing_task_.get_future(); - clang_task_.Set( clang_packaged_task ); - - return Future< void >( boost::move( future ) ); -} - - std::vector< CompletionData > ClangCompleter::CandidatesForLocationInFile( std::string filename, @@ -199,56 +132,6 @@ ClangCompleter::CandidatesForLocationInFile( } -Future< AsyncCompletions > -ClangCompleter::CandidatesForQueryAndLocationInFileAsync( - std::string query, - std::string filename, - int line, - int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { - // TODO: throw exception when threading is not enabled and this is called - if ( !threading_enabled_ ) - return Future< AsyncCompletions >(); - - bool skip_clang_result_cache = ShouldSkipClangResultCache( query, - line, - column ); - - if ( skip_clang_result_cache ) { - // The clang thread is busy, return nothing - if ( UpdatingTranslationUnit( filename ) ) - return Future< AsyncCompletions >(); - - { - lock_guard< mutex > lock( clang_data_ready_mutex_ ); - clang_data_ready_ = false; - } - - // Needed to "reset" the sorting threads to the start of their loop. This - // way any threads blocking on a read in sorting_task_.Get() are reset to - // wait on the clang_data_ready_condition_variable_. - sorting_threads_.interrupt_all(); - } - - // the sorting task needs to be set before the clang task (if any) just in - // case the clang task finishes (and therefore notifies a sorting thread to - // consume a sorting task) before the sorting task is set - unique_future< AsyncCompletions > future; - CreateSortingTask( query, future ); - - if ( skip_clang_result_cache ) { - CreateClangTask( boost::move( filename ), - line, - column, - boost::move( unsaved_files ), - boost::move( flags ) ); - } - - return Future< AsyncCompletions >( boost::move( future ) ); -} - - Location ClangCompleter::GetDeclarationLocation( std::string filename, int line, @@ -283,216 +166,8 @@ Location ClangCompleter::GetDefinitionLocation( } -void ClangCompleter::DeleteCachesForFileAsync( std::string filename ) { - file_cache_delete_stack_.Push( filename ); -} - - -void ClangCompleter::DeleteCaches() { - std::vector< std::string > filenames; - - if ( !file_cache_delete_stack_.PopAllNoWait( filenames ) ) - return; - - foreach( const std::string & filename, filenames ) { - translation_unit_store_.Remove( filename ); - } -} - - -bool ClangCompleter::ShouldSkipClangResultCache( const std::string &query, - int line, - int column ) { - // We need query.empty() in addition to the second check because if we don't - // have it, then we have a problem in the following situation: - // The user has a variable 'foo' of type 'A' and a variable 'bar' of type 'B'. - // He then types in 'foo.' and we store the clang results and also return - // them. The user then deletes that code and types in 'bar.'. Without the - // query.empty() check we would return the results from the cache here (it's - // the same line and column!) which would be incorrect. - return query.empty() || latest_clang_results_ - .NewPositionDifferentFromStoredPosition( line, column ); -} - - -// Copy-ctor for unique_future is private in C++03 mode so we need to take it as -// an out param -void ClangCompleter::CreateSortingTask( - const std::string &query, - unique_future< AsyncCompletions > &future ) { - // Careful! The code in this function may burn your eyes. - - function< CompletionDatas( const CompletionDatas & ) > - sort_candidates_for_query_functor = - boost::bind( &ClangCompleter::SortCandidatesForQuery, - boost::ref( *this ), - query, - _1 ); - - function< CompletionDatas() > operate_on_completion_data_functor = - boost::bind( &ClangResultsCache::OperateOnCompletionDatas< CompletionDatas >, - boost::cref( latest_clang_results_ ), - boost::move( sort_candidates_for_query_functor ) ); - - shared_ptr< packaged_task< AsyncCompletions > > task = - boost::make_shared< packaged_task< AsyncCompletions > >( - boost::bind( ReturnValueAsShared< std::vector< CompletionData > >, - boost::move( operate_on_completion_data_functor ) ) ); - - future = task->get_future(); - sorting_task_.Set( task ); -} - - -void ClangCompleter::CreateClangTask( - std::string filename, - int line, - int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { - latest_clang_results_.ResetWithNewLineAndColumn( line, column ); - - function< CompletionDatas() > candidates_for_location_functor = - boost::bind( &ClangCompleter::CandidatesForLocationInFile, - boost::ref( *this ), - boost::move( filename ), - line, - column, - boost::move( unsaved_files ), - boost::move( flags ) ); - - shared_ptr< ClangPackagedTask > clang_packaged_task = - make_shared< ClangPackagedTask >(); - - clang_packaged_task->completions_task_ = - packaged_task< AsyncCompletions >( - boost::bind( ReturnValueAsShared< std::vector< CompletionData > >, - boost::move( candidates_for_location_functor ) ) ); - - clang_task_.Set( clang_packaged_task ); -} - - - -std::vector< CompletionData > ClangCompleter::SortCandidatesForQuery( - const std::string &query, - const std::vector< CompletionData > &completion_datas ) { - Bitset query_bitset = LetterBitsetFromString( query ); - bool query_has_uppercase_letters = any_of( query, is_upper() ); - - std::vector< const Candidate * > repository_candidates = - candidate_repository_.GetCandidatesForStrings( completion_datas ); - - std::vector< ResultAnd< CompletionData * > > data_and_results; - - for ( uint i = 0; i < repository_candidates.size(); ++i ) { - const Candidate *candidate = repository_candidates[ i ]; - - if ( !candidate->MatchesQueryBitset( query_bitset ) ) - continue; - - Result result = candidate->QueryMatchResult( query, - query_has_uppercase_letters ); - - if ( result.IsSubsequence() ) { - ResultAnd< CompletionData * > data_and_result( &completion_datas[ i ], - result ); - data_and_results.push_back( boost::move( data_and_result ) ); - } - } - - std::sort( data_and_results.begin(), data_and_results.end() ); - - std::vector< CompletionData > sorted_completion_datas; - sorted_completion_datas.reserve( data_and_results.size() ); - - foreach ( const ResultAnd< CompletionData * > &data_and_result, - data_and_results ) { - sorted_completion_datas.push_back( *data_and_result.extra_object_ ); - } - - return sorted_completion_datas; -} - - -void ClangCompleter::InitThreads() { - int threads_to_create = - std::max( MIN_ASYNC_THREADS, - std::min( MAX_ASYNC_THREADS, thread::hardware_concurrency() ) ); - - for ( int i = 0; i < threads_to_create; ++i ) { - sorting_threads_.create_thread( bind( &ClangCompleter::SortingThreadMain, - boost::ref( *this ) ) ); - } - - clang_thread_.reset( new thread( &ClangCompleter::ClangThreadMain, - boost::ref( *this ) ) ); -} - - -void ClangCompleter::ClangThreadMain() { - while ( true ) { - try { - shared_ptr< ClangPackagedTask > task = clang_task_.Get(); - - bool has_completions_task = task->completions_task_.valid(); - - if ( has_completions_task ) - task->completions_task_(); - else - task->parsing_task_(); - - if ( !has_completions_task ) { - DeleteCaches(); - continue; - } - - unique_future< AsyncCompletions > future = - task->completions_task_.get_future(); - latest_clang_results_.SetCompletionDatas( *future.get() ); - - { - lock_guard< mutex > lock( clang_data_ready_mutex_ ); - clang_data_ready_ = true; - } - - clang_data_ready_condition_variable_.notify_all(); - } catch ( thread_interrupted & ) { - shared_lock< shared_mutex > lock( time_to_die_mutex_ ); - - if ( time_to_die_ ) - return; - - // else do nothing and re-enter the loop - } - } -} - - -void ClangCompleter::SortingThreadMain() { - while ( true ) { - try { - { - unique_lock< mutex > lock( clang_data_ready_mutex_ ); - - while ( !clang_data_ready_ ) { - clang_data_ready_condition_variable_.wait( lock ); - } - } - - shared_ptr< packaged_task< AsyncCompletions > > task = - sorting_task_.Get(); - - ( *task )(); - } catch ( thread_interrupted & ) { - shared_lock< shared_mutex > lock( time_to_die_mutex_ ); - - if ( time_to_die_ ) - return; - - // else do nothing and re-enter the loop - } - } +void ClangCompleter::DeleteCachesForFile( std::string filename ) { + translation_unit_store_.Remove( filename ); } diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 6ae7819c..46281a4c 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -23,13 +23,9 @@ #include "Future.h" #include "UnsavedFile.h" #include "Diagnostic.h" -#include "ClangResultsCache.h" #include "TranslationUnitStore.h" #include -#include -#include -#include #include @@ -37,19 +33,12 @@ typedef struct CXTranslationUnitImpl *CXTranslationUnit; namespace YouCompleteMe { -class CandidateRepository; class TranslationUnit; struct CompletionData; struct Location; typedef std::vector< CompletionData > CompletionDatas; -typedef boost::shared_ptr< CompletionDatas > AsyncCompletions; - -typedef boost::unordered_map < std::string, - boost::shared_ptr < - std::vector< std::string > > > FlagsForFile; - // TODO: document that all filename parameters must be absolute paths class ClangCompleter : boost::noncopyable { @@ -57,8 +46,6 @@ public: ClangCompleter(); ~ClangCompleter(); - void EnableThreading(); - std::vector< Diagnostic > DiagnosticsForFile( std::string filename ); bool UpdatingTranslationUnit( const std::string &filename ); @@ -68,20 +55,11 @@ public: // need to ensure we own the memory. // TODO: Change some of these params back to const ref where possible after we // get the server up. - // TODO: Remove the async methods and the threads when the server is ready. - - // Public because of unit tests (gtest is not very thread-friendly) void UpdateTranslationUnit( std::string filename, std::vector< UnsavedFile > unsaved_files, std::vector< std::string > flags ); - Future< void > UpdateTranslationUnitAsync( - std::string filename, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); - - // Public because of unit tests (gtest is not very thread-friendly) std::vector< CompletionData > CandidatesForLocationInFile( std::string filename, int line, @@ -89,14 +67,6 @@ public: std::vector< UnsavedFile > unsaved_files, std::vector< std::string > flags ); - Future< AsyncCompletions > CandidatesForQueryAndLocationInFileAsync( - std::string query, - std::string filename, - int line, - int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); - Location GetDeclarationLocation( std::string filename, int line, @@ -111,55 +81,10 @@ public: std::vector< UnsavedFile > unsaved_files, std::vector< std::string > flags ); - void DeleteCachesForFileAsync( std::string filename ); + void DeleteCachesForFile( std::string filename ); private: - void DeleteCaches(); - - // This is basically a union. Only one of the two tasks is set to something - // valid, the other task is invalid. Which one is valid depends on the caller. - struct ClangPackagedTask { - boost::packaged_task< AsyncCompletions > completions_task_; - boost::packaged_task< void > parsing_task_; - }; - - typedef ConcurrentLatestValue < - boost::shared_ptr < - boost::packaged_task< AsyncCompletions > > > LatestSortingTask; - - typedef ConcurrentLatestValue < - boost::shared_ptr< ClangPackagedTask > > LatestClangTask; - - typedef ConcurrentStack< std::string > FileCacheDeleteStack; - - bool ShouldSkipClangResultCache( const std::string &query, - int line, - int column ); - - void CreateSortingTask( const std::string &query, - boost::unique_future< AsyncCompletions > &future ); - - // NOTE: params are taken by value on purpose! With a C++11 compiler we can - // avoid internal copies if params are taken by value (move ctors FTW) - void CreateClangTask( - std::string filename, - int line, - int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); - - std::vector< CompletionData > SortCandidatesForQuery( - const std::string &query, - const std::vector< CompletionData > &completion_datas ); - - void InitThreads(); - - void ClangThreadMain(); - - void SortingThreadMain(); - - ///////////////////////////// // PRIVATE MEMBER VARIABLES ///////////////////////////// @@ -167,35 +92,6 @@ private: CXIndex clang_index_; TranslationUnitStore translation_unit_store_; - - CandidateRepository &candidate_repository_; - - bool threading_enabled_; - - // TODO: use boost.atomic for time_to_die_ - bool time_to_die_; - boost::shared_mutex time_to_die_mutex_; - - // TODO: use boost.atomic for clang_data_ready_ - bool clang_data_ready_; - boost::mutex clang_data_ready_mutex_; - boost::condition_variable clang_data_ready_condition_variable_; - - ClangResultsCache latest_clang_results_; - - FileCacheDeleteStack file_cache_delete_stack_; - - // Unfortunately clang is not thread-safe so we need to be careful when we - // access it. Only one thread at a time is allowed to access any single - // translation unit. Currently we only use one thread to access clang and that - // is the thread represented by clang_thread_. - boost::scoped_ptr< boost::thread > clang_thread_; - - boost::thread_group sorting_threads_; - - mutable LatestClangTask clang_task_; - - mutable LatestSortingTask sorting_task_; }; } // namespace YouCompleteMe diff --git a/cpp/ycm/ClangCompleter/ClangResultsCache.cpp b/cpp/ycm/ClangCompleter/ClangResultsCache.cpp deleted file mode 100644 index e11856e3..00000000 --- a/cpp/ycm/ClangCompleter/ClangResultsCache.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic -// -// This file is part of YouCompleteMe. -// -// YouCompleteMe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// YouCompleteMe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with YouCompleteMe. If not, see . - -#include "ClangResultsCache.h" -#include "standard.h" - -using boost::shared_mutex; -using boost::shared_lock; -using boost::unique_lock; - -namespace YouCompleteMe { - -bool ClangResultsCache::NewPositionDifferentFromStoredPosition( int new_line, - int new_colum ) -const { - shared_lock< shared_mutex > reader_lock( access_mutex_ ); - return line_ != new_line || column_ != new_colum; -} - -void ClangResultsCache::ResetWithNewLineAndColumn( int new_line, - int new_colum ) { - unique_lock< shared_mutex > reader_lock( access_mutex_ ); - - line_ = new_line; - column_ = new_colum; - completion_datas_.clear(); -} - -} // namespace YouCompleteMe diff --git a/cpp/ycm/ClangCompleter/ClangResultsCache.h b/cpp/ycm/ClangCompleter/ClangResultsCache.h deleted file mode 100644 index cae4f5fc..00000000 --- a/cpp/ycm/ClangCompleter/ClangResultsCache.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic -// -// This file is part of YouCompleteMe. -// -// YouCompleteMe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// YouCompleteMe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with YouCompleteMe. If not, see . - -#ifndef CLANGRESULTSCACHE_H_REUWM3RU -#define CLANGRESULTSCACHE_H_REUWM3RU - -#include "CompletionData.h" - -#include -#include -#include -#include -#include -#include - -namespace YouCompleteMe { - -struct CompletionData; - -class ClangResultsCache : boost::noncopyable { -public: - - ClangResultsCache() : line_( -1 ), column_( -1 ) {} - - bool NewPositionDifferentFromStoredPosition( int new_line, int new_colum ) - const; - - void ResetWithNewLineAndColumn( int new_line, int new_colum ); - - void SetCompletionDatas( - const std::vector< CompletionData > new_completions ) { - completion_datas_ = new_completions; - } - -#ifndef BOOST_NO_RVALUE_REFERENCES -# ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wc++98-compat" -# endif //#ifdef __clang__ - - void SetCompletionDatas( std::vector< CompletionData > &&new_completions ) { - completion_datas_ = new_completions; - } - -# ifdef __clang__ -# pragma clang diagnostic pop -# endif //#ifdef __clang__ -#endif //#ifndef BOOST_NO_RVALUE_REFERENCES - - template< typename T > - T OperateOnCompletionDatas( - boost::function< T( const std::vector< CompletionData >& ) > operation ) - const { - boost::shared_lock< boost::shared_mutex > reader_lock( access_mutex_ ); - return operation( completion_datas_ ); - } - -private: - - int line_; - int column_; - std::vector< CompletionData > completion_datas_; - - mutable boost::shared_mutex access_mutex_; -}; - -} // namespace YouCompleteMe - -#endif /* end of include guard: CLANGRESULTSCACHE_H_REUWM3RU */ - diff --git a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp index 26320024..7c5c0b9e 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp @@ -38,14 +38,17 @@ std::size_t HashForFlags( const std::vector< std::string > &flags ) { } // unnamed namespace + TranslationUnitStore::TranslationUnitStore( CXIndex clang_index ) : clang_index_( clang_index ) { } + TranslationUnitStore::~TranslationUnitStore() { RemoveAll(); } + shared_ptr< TranslationUnit > TranslationUnitStore::GetOrCreate( const std::string &filename, const std::vector< UnsavedFile > &unsaved_files, @@ -104,24 +107,28 @@ shared_ptr< TranslationUnit > TranslationUnitStore::GetOrCreate( return unit; } + shared_ptr< TranslationUnit > TranslationUnitStore::Get( const std::string &filename ) { lock_guard< mutex > lock( filename_to_translation_unit_and_flags_mutex_ ); return GetNoLock( filename ); } + bool TranslationUnitStore::Remove( const std::string &filename ) { lock_guard< mutex > lock( filename_to_translation_unit_and_flags_mutex_ ); Erase( filename_to_flags_hash_, filename ); return Erase( filename_to_translation_unit_, filename ); } + void TranslationUnitStore::RemoveAll() { lock_guard< mutex > lock( filename_to_translation_unit_and_flags_mutex_ ); filename_to_translation_unit_.clear(); filename_to_flags_hash_.clear(); } + shared_ptr< TranslationUnit > TranslationUnitStore::GetNoLock( const std::string &filename ) { return FindWithDefault( filename_to_translation_unit_, diff --git a/cpp/ycm/IdentifierCompleter.cpp b/cpp/ycm/IdentifierCompleter.cpp index 88294ae1..36ca2436 100644 --- a/cpp/ycm/IdentifierCompleter.cpp +++ b/cpp/ycm/IdentifierCompleter.cpp @@ -23,62 +23,16 @@ #include "Result.h" #include "Utils.h" -#include -#include #include -using boost::packaged_task; -using boost::unique_future; -using boost::shared_ptr; -using boost::thread; - namespace YouCompleteMe { -typedef boost::function< std::vector< std::string >() > -FunctionReturnsStringVector; - -extern const unsigned int MAX_ASYNC_THREADS = 4; -extern const unsigned int MIN_ASYNC_THREADS = 2; - -namespace { - -void QueryThreadMain( - IdentifierCompleter::LatestQueryTask &latest_query_task ) { - while ( true ) { - try { - ( *latest_query_task.Get() )(); - } catch ( boost::thread_interrupted & ) { - return; - } - } - -} - -void BufferIdentifiersThreadMain( - IdentifierCompleter::BufferIdentifiersTaskStack - &buffer_identifiers_task_stack ) { - while ( true ) { - try { - ( *buffer_identifiers_task_stack.Pop() )(); - } catch ( boost::thread_interrupted & ) { - return; - } - } -} - - -} // unnamed namespace - - -IdentifierCompleter::IdentifierCompleter() - : threading_enabled_( false ) { -} +IdentifierCompleter::IdentifierCompleter() {} IdentifierCompleter::IdentifierCompleter( - const std::vector< std::string > &candidates ) - : threading_enabled_( false ) { + const std::vector< std::string > &candidates ) { identifier_database_.AddIdentifiers( candidates, "", "" ); } @@ -86,31 +40,11 @@ IdentifierCompleter::IdentifierCompleter( IdentifierCompleter::IdentifierCompleter( const std::vector< std::string > &candidates, const std::string &filetype, - const std::string &filepath ) - : threading_enabled_( false ) { + const std::string &filepath ) { identifier_database_.AddIdentifiers( candidates, filetype, filepath ); } -IdentifierCompleter::~IdentifierCompleter() { - query_threads_.interrupt_all(); - query_threads_.join_all(); - - if ( buffer_identifiers_thread_ ) { - buffer_identifiers_thread_->interrupt(); - buffer_identifiers_thread_->join(); - } -} - - -// We need this mostly so that we can not use it in tests. Apparently the -// GoogleTest framework goes apeshit on us if we enable threads by default. -void IdentifierCompleter::EnableThreading() { - threading_enabled_ = true; - InitThreads(); -} - - void IdentifierCompleter::AddIdentifiersToDatabase( const std::vector< std::string > &new_candidates, const std::string &filetype, @@ -130,22 +64,6 @@ void IdentifierCompleter::AddIdentifiersToDatabaseFromTagFiles( } -void IdentifierCompleter::AddIdentifiersToDatabaseFromTagFilesAsync( - std::vector< std::string > absolute_paths_to_tag_files ) { - // TODO: throw exception when threading is not enabled and this is called - if ( !threading_enabled_ ) - return; - - boost::function< void() > functor = - boost::bind( &IdentifierCompleter::AddIdentifiersToDatabaseFromTagFiles, - boost::ref( *this ), - boost::move( absolute_paths_to_tag_files ) ); - - buffer_identifiers_task_stack_.Push( - boost::make_shared< packaged_task< void > >( boost::move( functor ) ) ); -} - - void IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer( const std::string &buffer_contents, const std::string &filetype, @@ -165,28 +83,6 @@ void IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer( } -void IdentifierCompleter::AddIdentifiersToDatabaseFromBufferAsync( - std::string buffer_contents, - std::string filetype, - std::string filepath, - bool collect_from_comments_and_strings ) { - // TODO: throw exception when threading is not enabled and this is called - if ( !threading_enabled_ ) - return; - - boost::function< void() > functor = - boost::bind( &IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer, - boost::ref( *this ), - boost::move( buffer_contents ), - boost::move( filetype ), - boost::move( filepath ), - collect_from_comments_and_strings ); - - buffer_identifiers_task_stack_.Push( - boost::make_shared< packaged_task< void > >( boost::move( functor ) ) ); -} - - std::vector< std::string > IdentifierCompleter::CandidatesForQuery( const std::string &query ) const { return CandidatesForQueryAndType( query, "" ); @@ -209,46 +105,4 @@ std::vector< std::string > IdentifierCompleter::CandidatesForQueryAndType( } -Future< AsyncResults > IdentifierCompleter::CandidatesForQueryAndTypeAsync( - const std::string &query, - const std::string &filetype ) const { - // TODO: throw exception when threading is not enabled and this is called - if ( !threading_enabled_ ) - return Future< AsyncResults >(); - - FunctionReturnsStringVector functor = - boost::bind( &IdentifierCompleter::CandidatesForQueryAndType, - boost::cref( *this ), - query, - filetype ); - - QueryTask task = - boost::make_shared< packaged_task< AsyncResults > >( - boost::bind( ReturnValueAsShared< std::vector< std::string > >, - boost::move( functor ) ) ); - - unique_future< AsyncResults > future = task->get_future(); - - latest_query_task_.Set( task ); - return Future< AsyncResults >( boost::move( future ) ); -} - - -void IdentifierCompleter::InitThreads() { - int query_threads_to_create = - std::max( MIN_ASYNC_THREADS, - std::min( MAX_ASYNC_THREADS, thread::hardware_concurrency() ) ); - - for ( int i = 0; i < query_threads_to_create; ++i ) { - query_threads_.create_thread( - boost::bind( QueryThreadMain, - boost::ref( latest_query_task_ ) ) ); - } - - buffer_identifiers_thread_.reset( - new boost::thread( BufferIdentifiersThreadMain, - boost::ref( buffer_identifiers_task_stack_ ) ) ); -} - - } // namespace YouCompleteMe diff --git a/cpp/ycm/IdentifierCompleter.h b/cpp/ycm/IdentifierCompleter.h index bc7dd5fd..7eb1edcf 100644 --- a/cpp/ycm/IdentifierCompleter.h +++ b/cpp/ycm/IdentifierCompleter.h @@ -36,8 +36,6 @@ namespace YouCompleteMe { class Candidate; -typedef boost::shared_ptr< std::vector< std::string > > AsyncResults; - class IdentifierCompleter : boost::noncopyable { public: @@ -47,10 +45,6 @@ public: const std::string &filetype, const std::string &filepath ); - ~IdentifierCompleter(); - - void EnableThreading(); - void AddIdentifiersToDatabase( const std::vector< std::string > &new_candidates, const std::string &filetype, @@ -59,25 +53,12 @@ public: void AddIdentifiersToDatabaseFromTagFiles( const std::vector< std::string > &absolute_paths_to_tag_files ); - // NOTE: params are taken by value on purpose! - void AddIdentifiersToDatabaseFromTagFilesAsync( - std::vector< std::string > absolute_paths_to_tag_files ); - void AddIdentifiersToDatabaseFromBuffer( const std::string &buffer_contents, const std::string &filetype, const std::string &filepath, bool collect_from_comments_and_strings ); - // NOTE: params are taken by value on purpose! With a C++11 compiler we can - // avoid an expensive copy of buffer_contents if the param is taken by value - // (move ctors FTW) - void AddIdentifiersToDatabaseFromBufferAsync( - std::string buffer_contents, - std::string filetype, - std::string filepath, - bool collect_from_comments_and_strings ); - // Only provided for tests! std::vector< std::string > CandidatesForQuery( const std::string &query ) const; @@ -86,37 +67,13 @@ public: const std::string &query, const std::string &filetype ) const; - Future< AsyncResults > CandidatesForQueryAndTypeAsync( - const std::string &query, - const std::string &filetype ) const; - - typedef boost::shared_ptr < - boost::packaged_task< AsyncResults > > QueryTask; - - typedef ConcurrentLatestValue< QueryTask > LatestQueryTask; - - typedef ConcurrentStack< VoidTask > BufferIdentifiersTaskStack; - private: - void InitThreads(); - - ///////////////////////////// // PRIVATE MEMBER VARIABLES ///////////////////////////// IdentifierDatabase identifier_database_; - - bool threading_enabled_; - - boost::thread_group query_threads_; - - boost::scoped_ptr< boost::thread > buffer_identifiers_thread_; - - mutable LatestQueryTask latest_query_task_; - - BufferIdentifiersTaskStack buffer_identifiers_task_stack_; }; } // namespace YouCompleteMe diff --git a/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp b/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp index e70f365a..18be50d2 100644 --- a/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp +++ b/cpp/ycm/tests/ClangCompleter/ClangCompleter_test.cpp @@ -43,24 +43,6 @@ TEST( ClangCompleterTest, CandidatesForLocationInFile ) { } -TEST( ClangCompleterTest, CandidatesForQueryAndLocationInFileAsync ) { - ClangCompleter completer; - completer.EnableThreading(); - - Future< AsyncCompletions > completions_future = - completer.CandidatesForQueryAndLocationInFileAsync( - "", - PathToTestFile( "basic.cpp" ).string(), - 11, - 7, - std::vector< UnsavedFile >(), - std::vector< std::string >() ); - - completions_future.Wait(); - - EXPECT_TRUE( !completions_future.GetResults()->empty() ); -} - TEST( ClangCompleterTest, GetDefinitionLocation ) { ClangCompleter completer; std::string filename = PathToTestFile( "basic.cpp" ).string(); diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index ea353a91..5bb2214f 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -60,36 +60,24 @@ BOOST_PYTHON_MODULE(ycm_core) def( "YcmCoreVersion", YcmCoreVersion ); class_< IdentifierCompleter, boost::noncopyable >( "IdentifierCompleter" ) - .def( "EnableThreading", &IdentifierCompleter::EnableThreading ) .def( "AddIdentifiersToDatabase", &IdentifierCompleter::AddIdentifiersToDatabase ) - .def( "AddIdentifiersToDatabaseFromTagFilesAsync", - &IdentifierCompleter::AddIdentifiersToDatabaseFromTagFilesAsync ) - .def( "AddIdentifiersToDatabaseFromBufferAsync", - &IdentifierCompleter::AddIdentifiersToDatabaseFromBufferAsync ) - .def( "CandidatesForQueryAndTypeAsync", - &IdentifierCompleter::CandidatesForQueryAndTypeAsync ); + .def( "AddIdentifiersToDatabaseFromTagFiles", + &IdentifierCompleter::AddIdentifiersToDatabaseFromTagFiles ) + .def( "AddIdentifiersToDatabaseFromBuffer", + &IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer ) + .def( "CandidatesForQueryAndType", + &IdentifierCompleter::CandidatesForQueryAndType ); // TODO: rename these *Vec classes to *Vector; don't forget the python file class_< std::vector< std::string >, boost::shared_ptr< std::vector< std::string > > >( "StringVec" ) .def( vector_indexing_suite< std::vector< std::string > >() ); - class_< Future< AsyncResults > >( "FutureResults" ) - .def( "ResultsReady", &Future< AsyncResults >::ResultsReady ) - .def( "GetResults", &Future< AsyncResults >::GetResults ); - - class_< Future< void > >( "FutureVoid" ) - .def( "ResultsReady", &Future< void >::ResultsReady ) - .def( "GetResults", &Future< void >::GetResults ); - #ifdef USE_CLANG_COMPLETER def( "ClangVersion", ClangVersion ); - class_< Future< AsyncCompletions > >( "FutureCompletions" ) - .def( "ResultsReady", &Future< AsyncCompletions >::ResultsReady ) - .def( "GetResults", &Future< AsyncCompletions >::GetResults ); - + // TODO: We may not need this at all anymore. Look into it. class_< Future< AsyncCompilationInfoForFile > >( "FutureCompilationInfoForFile" ) .def( "ResultsReady", @@ -116,17 +104,14 @@ BOOST_PYTHON_MODULE(ycm_core) .def( vector_indexing_suite< std::vector< UnsavedFile > >() ); class_< ClangCompleter, boost::noncopyable >( "ClangCompleter" ) - .def( "EnableThreading", &ClangCompleter::EnableThreading ) .def( "DiagnosticsForFile", &ClangCompleter::DiagnosticsForFile ) .def( "GetDeclarationLocation", &ClangCompleter::GetDeclarationLocation ) .def( "GetDefinitionLocation", &ClangCompleter::GetDefinitionLocation ) - .def( "DeleteCachesForFileAsync", - &ClangCompleter::DeleteCachesForFileAsync ) + .def( "DeleteCachesForFile", &ClangCompleter::DeleteCachesForFile ) .def( "UpdatingTranslationUnit", &ClangCompleter::UpdatingTranslationUnit ) - .def( "UpdateTranslationUnitAsync", - &ClangCompleter::UpdateTranslationUnitAsync ) - .def( "CandidatesForQueryAndLocationInFileAsync", - &ClangCompleter::CandidatesForQueryAndLocationInFileAsync ); + .def( "UpdateTranslationUnit", &ClangCompleter::UpdateTranslationUnit ) + .def( "CandidatesForLocationInFile", + &ClangCompleter::CandidatesForLocationInFile ); class_< CompletionData >( "CompletionData" ) .def( "TextToInsertInBuffer", &CompletionData::TextToInsertInBuffer ) diff --git a/python/test_requirements.txt b/python/test_requirements.txt index 65164842..d9d5206d 100644 --- a/python/test_requirements.txt +++ b/python/test_requirements.txt @@ -1,3 +1,5 @@ flake8>=2.0 mock>=1.0.1 nose>=1.3.0 +PyHamcrest>=1.7.2 + diff --git a/python/ycm/completers/all/identifier_completer.py b/python/ycm/completers/all/identifier_completer.py index b1f75db3..c3e27bfb 100644 --- a/python/ycm/completers/all/identifier_completer.py +++ b/python/ycm/completers/all/identifier_completer.py @@ -34,9 +34,8 @@ SYNTAX_FILENAME = 'YCM_PLACEHOLDER_FOR_SYNTAX' class IdentifierCompleter( GeneralCompleter ): def __init__( self, user_options ): super( IdentifierCompleter, self ).__init__( user_options ) - self.completer = ycm_core.IdentifierCompleter() - self.completer.EnableThreading() - self.tags_file_last_mtime = defaultdict( int ) + self._completer = ycm_core.IdentifierCompleter() + self._tags_file_last_mtime = defaultdict( int ) self._logger = logging.getLogger( __name__ ) @@ -44,11 +43,20 @@ class IdentifierCompleter( GeneralCompleter ): return self.QueryLengthAboveMinThreshold( request_data ) - def CandidatesForQueryAsync( self, request_data ): - self.completions_future = self.completer.CandidatesForQueryAndTypeAsync( + def ComputeCandidates( self, request_data ): + if not self.ShouldUseNow( request_data ): + return [] + + completions = self._completer.CandidatesForQueryAndType( ToUtf8IfNeeded( utils.SanitizeQuery( request_data[ 'query' ] ) ), ToUtf8IfNeeded( request_data[ 'filetypes' ][ 0 ] ) ) + completions = completions[ : MAX_IDENTIFIER_COMPLETIONS_RETURNED ] + completions = _RemoveSmallCandidates( + completions, self.user_options[ 'min_num_identifier_candidate_chars' ] ) + + return [ responses.BuildCompletionData( x ) for x in completions ] + def AddIdentifier( self, identifier, request_data ): filetype = request_data[ 'filetypes' ][ 0 ] @@ -60,7 +68,7 @@ class IdentifierCompleter( GeneralCompleter ): vector = ycm_core.StringVec() vector.append( ToUtf8IfNeeded( identifier ) ) self._logger.info( 'Adding ONE buffer identifier for file: %s', filepath ) - self.completer.AddIdentifiersToDatabase( vector, + self._completer.AddIdentifiersToDatabase( vector, ToUtf8IfNeeded( filetype ), ToUtf8IfNeeded( filepath ) ) @@ -92,7 +100,7 @@ class IdentifierCompleter( GeneralCompleter ): text = request_data[ 'file_data' ][ filepath ][ 'contents' ] self._logger.info( 'Adding buffer identifiers for file: %s', filepath ) - self.completer.AddIdentifiersToDatabaseFromBufferAsync( + self._completer.AddIdentifiersToDatabaseFromBuffer( ToUtf8IfNeeded( text ), ToUtf8IfNeeded( filetype ), ToUtf8IfNeeded( filepath ), @@ -106,20 +114,20 @@ class IdentifierCompleter( GeneralCompleter ): current_mtime = os.path.getmtime( tag_file ) except: continue - last_mtime = self.tags_file_last_mtime[ tag_file ] + last_mtime = self._tags_file_last_mtime[ tag_file ] # We don't want to repeatedly process the same file over and over; we only # process if it's changed since the last time we looked at it if current_mtime <= last_mtime: continue - self.tags_file_last_mtime[ tag_file ] = current_mtime + self._tags_file_last_mtime[ tag_file ] = current_mtime absolute_paths_to_tag_files.append( ToUtf8IfNeeded( tag_file ) ) if not absolute_paths_to_tag_files: return - self.completer.AddIdentifiersToDatabaseFromTagFilesAsync( + self._completer.AddIdentifiersToDatabaseFromTagFiles( absolute_paths_to_tag_files ) @@ -129,7 +137,7 @@ class IdentifierCompleter( GeneralCompleter ): keyword_vector.append( ToUtf8IfNeeded( keyword ) ) filepath = SYNTAX_FILENAME + filetypes[ 0 ] - self.completer.AddIdentifiersToDatabase( keyword_vector, + self._completer.AddIdentifiersToDatabase( keyword_vector, ToUtf8IfNeeded( filetypes[ 0 ] ), ToUtf8IfNeeded( filepath ) ) @@ -151,18 +159,6 @@ class IdentifierCompleter( GeneralCompleter ): self.AddPreviousIdentifier( request_data ) - def CandidatesFromStoredRequest( self ): - if not self.completions_future: - return [] - completions = self.completions_future.GetResults()[ - : MAX_IDENTIFIER_COMPLETIONS_RETURNED ] - - completions = _RemoveSmallCandidates( - completions, self.user_options[ 'min_num_identifier_candidate_chars' ] ) - - return [ responses.BuildCompletionData( x ) for x in completions ] - - def _PreviousIdentifier( min_num_completion_start_chars, request_data ): line_num = request_data[ 'line_num' ] column_num = request_data[ 'column_num' ] diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 7bf055f9..c44b5572 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -58,44 +58,21 @@ class Completer( object ): and will NOT re-query your completer but will in fact provide fuzzy-search on the candidate strings that were stored in the cache. - CandidatesForQueryAsync() is the main entry point when the user types. For + ComputeCandidates() is the main entry point when the user types. For "foo.bar", the user query is "bar" and completions matching this string should - be shown. The job of CandidatesForQueryAsync() is to merely initiate this - request, which will hopefully be processed in a background thread. You may - want to subclass ThreadedCompleter instead of Completer directly. + be shown. It should return the list of candidates. The format of the result + can be a list of strings or a more complicated list of dictionaries. Use + ycm.server.responses.BuildCompletionData to build the detailed response. See + clang_completer.py to see how its used in practice. - AsyncCandidateRequestReady() is the function that is repeatedly polled until - it returns True. If CandidatesForQueryAsync() started a background task of - collecting the required completions, AsyncCandidateRequestReady() would check - the state of that task and return False until it was completed. - - CandidatesFromStoredRequest() should return the list of candidates. This is - what YCM calls after AsyncCandidateRequestReady() returns True. The format of - the result can be a list of strings or a more complicated list of - dictionaries. See ':h complete-items' for the format, and clang_completer.py - to see how its used in practice. + Again, you probably want to override ComputeCandidatesInner(). You also need to implement the SupportedFiletypes() function which should return a list of strings, where the strings are Vim filetypes your completer supports. - clang_completer.py is a good example of a "complicated" completer that - maintains its own internal cache and therefore directly overrides the "main" - functions in the API instead of the *Inner versions. A good example of a - simple completer that does not do this is omni_completer.py. - - If you're confident your completer doesn't need a background task (think - again, you probably do) because you can "certainly" furnish a response in - under 10ms, then you can perform your backend processing in a synchronous - fashion. You may also need to do this because of technical restrictions (much - like omni_completer.py has to do it because accessing Vim internals is not - thread-safe). But even if you're certain, still try to do the processing in a - background thread. Your completer is unlikely to be merged if it does not, - because synchronous processing will block Vim's GUI thread and that's a very, - VERY bad thing (so try not to do it!). Again, you may want to subclass - ThreadedCompleter instead of Completer directly; ThreadedCompleter will - abstract away the use of a background thread for you. See - threaded_completer.py. + clang_completer.py is a good example of a "complicated" completer that A good + example of a simple completer is ultisnips_completer.py. The On* functions are provided for your convenience. They are called when their specific events occur. For instance, the identifier completer collects @@ -122,7 +99,7 @@ class Completer( object ): self.triggers_for_filetype = TriggersForFiletype( user_options[ 'semantic_triggers' ] ) self.completions_future = None - self.completions_cache = None + self._completions_cache = None # It's highly likely you DON'T want to override this function but the *Inner @@ -130,13 +107,13 @@ class Completer( object ): def ShouldUseNow( self, request_data ): inner_says_yes = self.ShouldUseNowInner( request_data ) if not inner_says_yes: - self.completions_cache = None + self._completions_cache = None - previous_results_were_empty = ( self.completions_cache and - self.completions_cache.CacheValid( + previous_results_were_empty = ( self._completions_cache and + self._completions_cache.CacheValid( request_data[ 'line_num' ], request_data[ 'start_column' ] ) and - not self.completions_cache.raw_completions ) + not self._completions_cache.raw_completions ) return inner_says_yes and not previous_results_were_empty @@ -171,20 +148,28 @@ class Completer( object ): # It's highly likely you DON'T want to override this function but the *Inner # version of it. - def CandidatesForQueryAsync( self, request_data ): - self.request_data = request_data + def ComputeCandidates( self, request_data ): + if not self.ShouldUseNow( request_data ): + return [] if ( request_data[ 'query' ] and - self.completions_cache and - self.completions_cache.CacheValid( request_data[ 'line_num' ], - request_data[ 'start_column' ] ) ): - self.completions_cache.filtered_completions = ( - self.FilterAndSortCandidates( - self.completions_cache.raw_completions, - request_data[ 'query' ] ) ) + self._completions_cache and + self._completions_cache.CacheValid( request_data[ 'line_num' ], + request_data[ 'start_column' ] ) ): + return self.FilterAndSortCandidates( + self._completions_cache.raw_completions, + request_data[ 'query' ] ) else: - self.completions_cache = None - self.CandidatesForQueryAsyncInner( request_data ) + self._completions_cache = CompletionsCache() + self._completions_cache.raw_completions = self.ComputeCandidatesInner( + request_data ) + self._completions_cache.line = request_data[ 'line_num' ] + self._completions_cache.column = request_data[ 'start_column' ] + return self._completions_cache.raw_completions + + + def ComputeCandidatesInner( self, request_data ): + pass def DefinedSubcommands( self ): @@ -224,46 +209,6 @@ class Completer( object ): return matches - def CandidatesForQueryAsyncInner( self, query, start_column ): - pass - - - # It's highly likely you DON'T want to override this function but the *Inner - # version of it. - def AsyncCandidateRequestReady( self ): - if self.completions_cache: - return True - else: - return self.AsyncCandidateRequestReadyInner() - - - def AsyncCandidateRequestReadyInner( self ): - if not self.completions_future: - # We return True so that the caller can extract the default value from the - # future - return True - return self.completions_future.ResultsReady() - - - # It's highly likely you DON'T want to override this function but the *Inner - # version of it. - def CandidatesFromStoredRequest( self ): - if self.completions_cache: - return self.completions_cache.filtered_completions - else: - self.completions_cache = CompletionsCache() - self.completions_cache.raw_completions = self.CandidatesFromStoredRequestInner() - self.completions_cache.line = self.request_data[ 'line_num' ] - self.completions_cache.column = self.request_data[ 'start_column' ] - return self.completions_cache.raw_completions - - - def CandidatesFromStoredRequestInner( self ): - if not self.completions_future: - return [] - return self.completions_future.GetResults() - - def OnFileReadyToParse( self, request_data ): pass diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 339379ce..31a0bf10 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -30,8 +30,8 @@ CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] ) MIN_LINES_IN_FILE_TO_PARSE = 5 PARSING_FILE_MESSAGE = 'Still parsing file, no completions yet.' NO_COMPILE_FLAGS_MESSAGE = 'Still no compile flags, no completions yet.' -NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?' INVALID_FILE_MESSAGE = 'File is invalid.' +NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?' FILE_TOO_SHORT_MESSAGE = ( 'File is less than {} lines long; not compiling.'.format( MIN_LINES_IN_FILE_TO_PARSE ) ) @@ -41,25 +41,14 @@ NO_DIAGNOSTIC_MESSAGE = 'No diagnostic for current line!' class ClangCompleter( Completer ): def __init__( self, user_options ): super( ClangCompleter, self ).__init__( user_options ) - self.max_diagnostics_to_display = user_options[ + self._max_diagnostics_to_display = user_options[ 'max_diagnostics_to_display' ] - self.completer = ycm_core.ClangCompleter() - self.completer.EnableThreading() - self.last_prepared_diagnostics = [] - self.parse_future = None - self.flags = Flags() - self.diagnostic_store = None + self._completer = ycm_core.ClangCompleter() + self._last_prepared_diagnostics = [] + self._flags = Flags() + self._diagnostic_store = None self._logger = logging.getLogger( __name__ ) - # We set this flag when a compilation request comes in while one is already - # in progress. We use this to trigger the pending request after the previous - # one completes (from GetDiagnosticsForCurrentFile because that's the only - # method that knows when the compilation has finished). - # TODO: Remove this now that we have multiple threads in the server; the - # subsequent requests that want to parse will just block until the current - # parse is done and will then proceed. - self.extra_parse_desired = False - def SupportedFiletypes( self ): return CLANG_FILETYPES @@ -84,53 +73,35 @@ class ClangCompleter( Completer ): return files - def CandidatesForQueryAsync( self, request_data ): + def ComputeCandidatesInner( self, request_data ): filename = request_data[ 'filepath' ] - if not filename: return - if self.completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): - self.completions_future = None + if self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): self._logger.info( PARSING_FILE_MESSAGE ) - return responses.BuildDisplayMessageResponse( - PARSING_FILE_MESSAGE ) + raise RuntimeError( PARSING_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) if not flags: - self.completions_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return responses.BuildDisplayMessageResponse( - NO_COMPILE_FLAGS_MESSAGE ) - - # TODO: sanitize query, probably in C++ code - - files = ycm_core.UnsavedFileVec() - query = request_data[ 'query' ] - if not query: - files = self.GetUnsavedFilesVector( request_data ) + raise RuntimeError( NO_COMPILE_FLAGS_MESSAGE ) + files = self.GetUnsavedFilesVector( request_data ) line = request_data[ 'line_num' ] + 1 column = request_data[ 'start_column' ] + 1 - self.completions_future = ( - self.completer.CandidatesForQueryAndLocationInFileAsync( - ToUtf8IfNeeded( query ), + results = self._completer.CandidatesForLocationInFile( ToUtf8IfNeeded( filename ), line, column, files, - flags ) ) + flags ) - - def CandidatesFromStoredRequest( self ): - if not self.completions_future: - return [] - results = [ ConvertCompletionData( x ) for x in - self.completions_future.GetResults() ] if not results: self._logger.warning( NO_COMPLETIONS_MESSAGE ) raise RuntimeError( NO_COMPLETIONS_MESSAGE ) - return results + + return [ ConvertCompletionData( x ) for x in results ] def DefinedSubcommands( self ): @@ -170,7 +141,7 @@ class ClangCompleter( Completer ): files = self.GetUnsavedFilesVector( request_data ) line = request_data[ 'line_num' ] + 1 column = request_data[ 'column_num' ] + 1 - return getattr( self.completer, goto_function )( + return getattr( self._completer, goto_function )( ToUtf8IfNeeded( filename ), line, column, @@ -212,73 +183,63 @@ class ClangCompleter( Completer ): def _ClearCompilationFlagCache( self ): - self.flags.Clear() + self._flags.Clear() def OnFileReadyToParse( self, request_data ): filename = request_data[ 'filepath' ] contents = request_data[ 'file_data' ][ filename ][ 'contents' ] if contents.count( '\n' ) < MIN_LINES_IN_FILE_TO_PARSE: - self.parse_future = None self._logger.warning( FILE_TOO_SHORT_MESSAGE ) raise ValueError( FILE_TOO_SHORT_MESSAGE ) if not filename: self._logger.warning( INVALID_FILE_MESSAGE ) - return responses.BuildDisplayMessageResponse( - INVALID_FILE_MESSAGE ) - - if self.completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): - self.extra_parse_desired = True - return + raise ValueError( INVALID_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) if not flags: - self.parse_future = None self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) - return responses.BuildDisplayMessageResponse( - NO_COMPILE_FLAGS_MESSAGE ) + raise ValueError( NO_COMPILE_FLAGS_MESSAGE ) - self.parse_future = self.completer.UpdateTranslationUnitAsync( + self._completer.UpdateTranslationUnit( ToUtf8IfNeeded( filename ), self.GetUnsavedFilesVector( request_data ), flags ) - self.extra_parse_desired = False - def OnBufferUnload( self, request_data ): - self.completer.DeleteCachesForFileAsync( + self._completer.DeleteCachesForFile( ToUtf8IfNeeded( request_data[ 'unloaded_buffer' ] ) ) def DiagnosticsForCurrentFileReady( self ): - if not self.parse_future: - return False - - return self.parse_future.ResultsReady() + # if not self.parse_future: + # return False + # return self.parse_future.ResultsReady() + pass def GettingCompletions( self, request_data ): - return self.completer.UpdatingTranslationUnit( + return self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( request_data[ 'filepath' ] ) ) def GetDiagnosticsForCurrentFile( self, request_data ): filename = request_data[ 'filepath' ] if self.DiagnosticsForCurrentFileReady(): - diagnostics = self.completer.DiagnosticsForFile( + diagnostics = self._completer.DiagnosticsForFile( ToUtf8IfNeeded( filename ) ) - self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) - self.last_prepared_diagnostics = [ + self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) + self._last_prepared_diagnostics = [ responses.BuildDiagnosticData( x ) for x in - diagnostics[ : self.max_diagnostics_to_display ] ] - self.parse_future = None + diagnostics[ : self._max_diagnostics_to_display ] ] + # self.parse_future = None - if self.extra_parse_desired: - self.OnFileReadyToParse( request_data ) + # if self.extra_parse_desired: + # self.OnFileReadyToParse( request_data ) - return self.last_prepared_diagnostics + return self._last_prepared_diagnostics def GetDetailedDiagnostic( self, request_data ): @@ -286,11 +247,11 @@ class ClangCompleter( Completer ): current_column = request_data[ 'column_num' ] + 1 current_file = request_data[ 'filepath' ] - if not self.diagnostic_store: + if not self._diagnostic_store: return responses.BuildDisplayMessageResponse( NO_DIAGNOSTIC_MESSAGE ) - diagnostics = self.diagnostic_store[ current_file ][ current_line ] + diagnostics = self._diagnostic_store[ current_file ][ current_line ] if not diagnostics: return responses.BuildDisplayMessageResponse( NO_DIAGNOSTIC_MESSAGE ) @@ -308,11 +269,6 @@ class ClangCompleter( Completer ): closest_diagnostic.long_formatted_text_ ) - def ShouldUseNow( self, request_data ): - # We don't want to use the Completer API cache, we use one in the C++ code. - return self.ShouldUseNowInner( request_data ) - - def DebugInfo( self, request_data ): filename = request_data[ 'filepath' ] if not filename: @@ -328,7 +284,7 @@ class ClangCompleter( Completer ): if 'compilation_flags' in request_data: return PrepareFlagsForClang( request_data[ 'compilation_flags' ], filename ) - return self.flags.FlagsForFile( filename ) + return self._flags.FlagsForFile( filename ) # TODO: Make this work again # def DiagnosticToDict( diagnostic ): diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 4943ccae..5af69592 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -21,7 +21,7 @@ import os from sys import platform import glob -from ycm.completers.threaded_completer import ThreadedCompleter +from ycm.completers.completer import Completer from ycm.server import responses from ycm import utils import urllib2 @@ -36,7 +36,7 @@ SERVER_NOT_FOUND_MSG = ( 'OmniSharp server binary not found at {0}. ' + 'Did you compile it? You can do so by running ' + '"./install.sh --omnisharp-completer".' ) -class CsharpCompleter( ThreadedCompleter ): +class CsharpCompleter( Completer ): """ A Completer that uses the Omnisharp server as completion engine. """ @@ -61,7 +61,7 @@ class CsharpCompleter( ThreadedCompleter ): return [ 'cs' ] - def ComputeCandidates( self, request_data ): + def ComputeCandidatesInner( self, request_data ): return [ responses.BuildCompletionData( completion[ 'CompletionText' ], completion[ 'DisplayText' ], diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index 5d9eb0da..b6549b64 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -19,12 +19,12 @@ import os import re -from ycm.completers.threaded_completer import ThreadedCompleter +from ycm.completers.completer import Completer from ycm.completers.cpp.clang_completer import InCFamilyFile from ycm.completers.cpp.flags import Flags from ycm.server import responses -class FilenameCompleter( ThreadedCompleter ): +class FilenameCompleter( Completer ): """ General completer that provides filename and filepath completions. """ @@ -73,7 +73,7 @@ class FilenameCompleter( ThreadedCompleter ): return [] - def ComputeCandidates( self, request_data ): + def ComputeCandidatesInner( self, request_data ): current_line = request_data[ 'line_value' ] start_column = request_data[ 'start_column' ] filepath = request_data[ 'filepath' ] diff --git a/python/ycm/completers/general/general_completer_store.py b/python/ycm/completers/general/general_completer_store.py index 850b39ed..f47dff8d 100644 --- a/python/ycm/completers/general/general_completer_store.py +++ b/python/ycm/completers/general/general_completer_store.py @@ -74,20 +74,13 @@ class GeneralCompleterStore( Completer ): return should_use_now - def CandidatesForQueryAsync( self, request_data ): - for completer in self._current_query_completers: - completer.CandidatesForQueryAsync( request_data ) + def ComputeCandidates( self, request_data ): + if not self.ShouldUseNow( request_data ): + return [] - - def AsyncCandidateRequestReady( self ): - return all( x.AsyncCandidateRequestReady() for x in - self._current_query_completers ) - - - def CandidatesFromStoredRequest( self ): candidates = [] for completer in self._current_query_completers: - candidates += completer.CandidatesFromStoredRequest() + candidates += completer.ComputeCandidates( request_data ) return candidates diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index 15858585..7a364f4d 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -33,23 +33,17 @@ class UltiSnipsCompleter( GeneralCompleter ): self._filtered_candidates = None - def ShouldUseNowInner( self, request_data ): + def ShouldUseNow( self, request_data ): return self.QueryLengthAboveMinThreshold( request_data ) - def CandidatesForQueryAsync( self, request_data ): - self._filtered_candidates = self.FilterAndSortCandidates( + def ComputeCandidates( self, request_data ): + if not self.ShouldUseNow( request_data ): + return [] + return self.FilterAndSortCandidates( self._candidates, request_data[ 'query' ] ) - def AsyncCandidateRequestReady( self ): - return True - - - def CandidatesFromStoredRequest( self ): - return self._filtered_candidates if self._filtered_candidates else [] - - def OnBufferVisit( self, request_data ): raw_candidates = request_data[ 'ultisnips_snippets' ] self._candidates = [ diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index b2149076..f1599276 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -19,7 +19,7 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from ycm.completers.threaded_completer import ThreadedCompleter +from ycm.completers.completer import Completer from ycm.server import responses import sys @@ -38,7 +38,7 @@ except ImportError: sys.path.pop( 0 ) -class JediCompleter( ThreadedCompleter ): +class JediCompleter( Completer ): """ A Completer that uses the Jedi completion engine. https://jedi.readthedocs.org/en/latest/ @@ -63,7 +63,7 @@ class JediCompleter( ThreadedCompleter ): return jedi.Script( contents, line, column, filename ) - def ComputeCandidates( self, request_data ): + def ComputeCandidatesInner( self, request_data ): script = self._GetJediScript( request_data ) return [ responses.BuildCompletionData( str( completion.name ), diff --git a/python/ycm/completers/threaded_completer.py b/python/ycm/completers/threaded_completer.py deleted file mode 100644 index cf1a1b13..00000000 --- a/python/ycm/completers/threaded_completer.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (C) 2011, 2012 Strahinja Val Markovic -# -# This file is part of YouCompleteMe. -# -# YouCompleteMe is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# YouCompleteMe is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with YouCompleteMe. If not, see . - -import abc -from threading import Thread, Event -from ycm.completers.completer import Completer - -class ThreadedCompleter( Completer ): - """A subclass of Completer that abstracts away the use of a background thread. - - This is a great class to subclass for your custom completer. It will provide - you with async computation of candidates when you implement the - ComputeCandidates() method; no need to worry about threads, locks, events or - similar. - - If you use this class as the base class for your Completer, you DON'T need to - provide implementations for the CandidatesForQueryAsync(), - AsyncCandidateRequestReady() and CandidatesFromStoredRequest() functions. Just - implement ComputeCandidates(). - - For examples of subclasses of this class, see the following files: - python/completers/general/filename_completer.py - """ - - def __init__( self, user_options ): - super( ThreadedCompleter, self ).__init__( user_options ) - self._query_ready = Event() - self._candidates_ready = Event() - self._candidates = None - self._start_completion_thread() - - - def _start_completion_thread( self ): - self._completion_thread = Thread( target=self.SetCandidates ) - self._completion_thread.daemon = True - self._completion_thread.start() - - - def CandidatesForQueryAsyncInner( self, request_data ): - self._candidates = None - self._candidates_ready.clear() - self._request_data = request_data - self._query_ready.set() - - - def AsyncCandidateRequestReadyInner( self ): - return WaitAndClearIfSet( self._candidates_ready, timeout=0.005 ) - - - def CandidatesFromStoredRequestInner( self ): - return self._candidates or [] - - - @abc.abstractmethod - def ComputeCandidates( self, request_data ): - """This function should compute the candidates to show to the user. - The return value should be of the same type as that for - CandidatesFromStoredRequest().""" - pass - - - def SetCandidates( self ): - while True: - try: - WaitAndClearIfSet( self._query_ready ) - self._candidates = self.ComputeCandidates( self._request_data ) - except: - self._query_ready.clear() - self._candidates = [] - self._candidates_ready.set() - - -def WaitAndClearIfSet( event, timeout=None ): - """Given an |event| and a |timeout|, waits for the event a maximum of timeout - seconds. After waiting, clears the event if it's set and returns the state of - the event before it was cleared.""" - - # We can't just do flag_is_set = event.wait( timeout ) because that breaks on - # Python 2.6 - event.wait( timeout ) - flag_is_set = event.is_set() - if flag_is_set: - event.clear() - return flag_is_set diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index ad8b52a8..74b24e13 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -21,6 +21,7 @@ from webtest import TestApp from .. import ycmd from ..responses import BuildCompletionData from nose.tools import ok_, eq_, with_setup +from hamcrest import assert_that, has_items, has_entry import bottle bottle.debug( True ) @@ -41,6 +42,11 @@ def RequestDataForFileWithContents( filename, contents = None ): } +# TODO: Make the other tests use this helper too instead of BuildCompletionData +def CompletionEntryMatcher( insertion_text ): + return has_entry( 'insertion_text', insertion_text ) + + def Setup(): ycmd.SetServerStateToDefaults() @@ -69,6 +75,48 @@ def GetCompletions_IdentifierCompleter_Works_test(): app.post_json( '/get_completions', completion_data ).json ) +@with_setup( Setup ) +def GetCompletions_ClangCompleter_Works_test(): + app = TestApp( ycmd.app ) + contents = """ +struct Foo { + int x; + int y; + char c; +}; + +int main() +{ + Foo foo; + foo. +} +""" + + filename = '/foo.cpp' + completion_data = { + 'compilation_flags': ['-x', 'c++'], + # 0-based line and column! + 'query': '', + 'line_num': 10, + 'column_num': 6, + 'start_column': 6, + 'line_value': ' foo.', + 'filetypes': ['cpp'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': contents, + 'filetypes': ['cpp'] + } + } + } + + results = app.post_json( '/get_completions', completion_data ).json + assert_that( results, has_items( CompletionEntryMatcher( 'c' ), + CompletionEntryMatcher( 'x' ), + CompletionEntryMatcher( 'y' ) ) ) + + @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 2730e56f..098e95d1 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -31,7 +31,6 @@ sys.path.insert( 0, os.path.join( '../..' ) ) import logging -import time import json import bottle from bottle import run, request, response @@ -89,16 +88,7 @@ def GetCompletions(): do_filetype_completion else SERVER_STATE.GetGeneralCompleter() ) - # This is necessary so that general_completer_store fills up - # _current_query_completers. - # TODO: Fix this. - completer.ShouldUseNow( request_data ) - - # TODO: This should not be async anymore, server is multi-threaded - completer.CandidatesForQueryAsync( request_data ) - while not completer.AsyncCandidateRequestReady(): - time.sleep( 0.03 ) - return _JsonResponse( completer.CandidatesFromStoredRequest() ) + return _JsonResponse( completer.ComputeCandidates( request_data ) ) @app.get( '/user_options' ) From 46360219f8e085a9a0a5933c0cbfb3668e96ff29 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 30 Sep 2013 17:30:46 -0700 Subject: [PATCH 049/149] Removed threads & async API in CompilationDatabase This is not needed anymore; the server request merely blocks when waiting for flags. --- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 1 - cpp/ycm/ClangCompleter/ClangCompleter.h | 2 - .../ClangCompleter/CompilationDatabase.cpp | 64 ++----------------- cpp/ycm/ClangCompleter/CompilationDatabase.h | 20 +----- cpp/ycm/ClangCompleter/TranslationUnit.h | 1 - cpp/ycm/ycm_core.cpp | 14 +--- 6 files changed, 9 insertions(+), 93 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index 141af8ac..d03684a7 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -23,7 +23,6 @@ #include "standard.h" #include "CandidateRepository.h" #include "CompletionData.h" -#include "ConcurrentLatestValue.h" #include "Utils.h" #include "ClangUtils.h" diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 46281a4c..b69910be 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -18,8 +18,6 @@ #ifndef CLANGCOMPLETE_H_WLKDU0ZV #define CLANGCOMPLETE_H_WLKDU0ZV -#include "ConcurrentLatestValue.h" -#include "ConcurrentStack.h" #include "Future.h" #include "UnsavedFile.h" #include "Diagnostic.h" diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp index 35444878..7a3b5f43 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp @@ -20,18 +20,14 @@ #include "standard.h" #include -#include #include #include +#include -using boost::bind; -using boost::make_shared; -using boost::packaged_task; +using boost::lock_guard; using boost::remove_pointer; using boost::shared_ptr; -using boost::thread; -using boost::unique_future; -using boost::function; +using boost::mutex; namespace YouCompleteMe { @@ -39,22 +35,9 @@ typedef shared_ptr < remove_pointer< CXCompileCommands >::type > CompileCommandsWrap; -void QueryThreadMain( CompilationDatabase::InfoTaskStack &info_task_stack ) { - while ( true ) { - try { - ( *info_task_stack.Pop() )(); - } catch ( boost::thread_interrupted & ) { - return; - } - } - -} - - CompilationDatabase::CompilationDatabase( const std::string &path_to_directory ) - : threading_enabled_( false ), - is_loaded_( false ) { + : is_loaded_( false ) { CXCompilationDatabase_Error status; compilation_database_ = clang_CompilationDatabase_fromDirectory( path_to_directory.c_str(), @@ -68,14 +51,6 @@ CompilationDatabase::~CompilationDatabase() { } -// We need this mostly so that we can not use it in tests. Apparently the -// GoogleTest framework goes apeshit on us if we enable threads by default. -void CompilationDatabase::EnableThreading() { - threading_enabled_ = true; - InitThreads(); -} - - bool CompilationDatabase::DatabaseSuccessfullyLoaded() { return is_loaded_; } @@ -88,7 +63,7 @@ CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile( if ( !is_loaded_ ) return info; - // TODO: mutex protect calls to getCompileCommands and getDirectory + lock_guard< mutex > lock( compilation_database_mutex_ ); CompileCommandsWrap commands( clang_CompilationDatabase_getCompileCommands( @@ -120,34 +95,5 @@ CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile( return info; } - -Future< AsyncCompilationInfoForFile > -CompilationDatabase::GetCompilationInfoForFileAsync( - const std::string &path_to_file ) { - // TODO: throw exception when threading is not enabled and this is called - if ( !threading_enabled_ ) - return Future< AsyncCompilationInfoForFile >(); - - function< CompilationInfoForFile() > functor = - boost::bind( &CompilationDatabase::GetCompilationInfoForFile, - boost::ref( *this ), - path_to_file ); - - InfoTask task = - make_shared< packaged_task< AsyncCompilationInfoForFile > >( - bind( ReturnValueAsShared< CompilationInfoForFile >, - functor ) ); - - unique_future< AsyncCompilationInfoForFile > future = task->get_future(); - info_task_stack_.Push( task ); - return Future< AsyncCompilationInfoForFile >( boost::move( future ) ); -} - - -void CompilationDatabase::InitThreads() { - info_thread_ = boost::thread( QueryThreadMain, - boost::ref( info_task_stack_ ) ); -} - } // namespace YouCompleteMe diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.h b/cpp/ycm/ClangCompleter/CompilationDatabase.h index 1205f9ce..e39bb6cd 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.h +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.h @@ -25,8 +25,10 @@ #include #include #include +#include #include + namespace YouCompleteMe { struct CompilationInfoForFile { @@ -34,8 +36,6 @@ struct CompilationInfoForFile { std::string compiler_working_dir_; }; -typedef boost::shared_ptr< CompilationInfoForFile > -AsyncCompilationInfoForFile; class CompilationDatabase : boost::noncopyable { public: @@ -44,28 +44,14 @@ public: bool DatabaseSuccessfullyLoaded(); - void EnableThreading(); - CompilationInfoForFile GetCompilationInfoForFile( const std::string &path_to_file ); - Future< AsyncCompilationInfoForFile > GetCompilationInfoForFileAsync( - const std::string &path_to_file ); - - typedef boost::shared_ptr < - boost::packaged_task< AsyncCompilationInfoForFile > > InfoTask; - - typedef ConcurrentStack< InfoTask > InfoTaskStack; - private: - void InitThreads(); - bool threading_enabled_; bool is_loaded_; CXCompilationDatabase compilation_database_; - - boost::thread info_thread_; - InfoTaskStack info_task_stack_; + boost::mutex compilation_database_mutex_; }; } // namespace YouCompleteMe diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.h b/cpp/ycm/ClangCompleter/TranslationUnit.h index 53baf989..3fe08671 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.h +++ b/cpp/ycm/ClangCompleter/TranslationUnit.h @@ -18,7 +18,6 @@ #ifndef TRANSLATIONUNIT_H_XQ7I6SVA #define TRANSLATIONUNIT_H_XQ7I6SVA -#include "ConcurrentLatestValue.h" #include "Future.h" #include "UnsavedFile.h" #include "Diagnostic.h" diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index 5bb2214f..80626d63 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -17,7 +17,6 @@ #include "IdentifierCompleter.h" #include "PythonSupport.h" -#include "Future.h" #ifdef USE_CLANG_COMPLETER # include "ClangCompleter.h" @@ -77,14 +76,6 @@ BOOST_PYTHON_MODULE(ycm_core) #ifdef USE_CLANG_COMPLETER def( "ClangVersion", ClangVersion ); - // TODO: We may not need this at all anymore. Look into it. - class_< Future< AsyncCompilationInfoForFile > >( - "FutureCompilationInfoForFile" ) - .def( "ResultsReady", - &Future< AsyncCompilationInfoForFile >::ResultsReady ) - .def( "GetResults", - &Future< AsyncCompilationInfoForFile >::GetResults ); - // CAREFUL HERE! For filename and contents we are referring directly to // Python-allocated and -managed memory since we are accepting pointers to // data members of python objects. We need to ensure that those objects @@ -145,13 +136,10 @@ BOOST_PYTHON_MODULE(ycm_core) class_< CompilationDatabase, boost::noncopyable >( "CompilationDatabase", init< std::string >() ) - .def( "EnableThreading", &CompilationDatabase::EnableThreading ) .def( "DatabaseSuccessfullyLoaded", &CompilationDatabase::DatabaseSuccessfullyLoaded ) .def( "GetCompilationInfoForFile", - &CompilationDatabase::GetCompilationInfoForFile ) - .def( "GetCompilationInfoForFileAsync", - &CompilationDatabase::GetCompilationInfoForFileAsync ); + &CompilationDatabase::GetCompilationInfoForFile ); class_< CompilationInfoForFile, boost::shared_ptr< CompilationInfoForFile > >( From 531e564a88a8769dc3dbe7ecd716a722d5eed682 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 10:01:01 -0700 Subject: [PATCH 050/149] Removing Concurrent* & Future classes They're not needed anymore. --- cpp/ycm/ClangCompleter/ClangCompleter.h | 1 - cpp/ycm/ClangCompleter/CompilationDatabase.h | 3 - cpp/ycm/ClangCompleter/TranslationUnit.h | 1 - .../ClangCompleter/TranslationUnitStore.cpp | 1 + cpp/ycm/ConcurrentLatestValue.h | 79 ---------------- cpp/ycm/ConcurrentStack.h | 89 ------------------- cpp/ycm/Future.h | 78 ---------------- cpp/ycm/IdentifierCompleter.h | 3 - 8 files changed, 1 insertion(+), 254 deletions(-) delete mode 100644 cpp/ycm/ConcurrentLatestValue.h delete mode 100644 cpp/ycm/ConcurrentStack.h delete mode 100644 cpp/ycm/Future.h diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index b69910be..5218f4e5 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -18,7 +18,6 @@ #ifndef CLANGCOMPLETE_H_WLKDU0ZV #define CLANGCOMPLETE_H_WLKDU0ZV -#include "Future.h" #include "UnsavedFile.h" #include "Diagnostic.h" #include "TranslationUnitStore.h" diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.h b/cpp/ycm/ClangCompleter/CompilationDatabase.h index e39bb6cd..346c4299 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.h +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.h @@ -18,9 +18,6 @@ #ifndef COMPILATIONDATABASE_H_ZT7MQXPG #define COMPILATIONDATABASE_H_ZT7MQXPG -#include "Future.h" -#include "ConcurrentStack.h" - #include #include #include diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.h b/cpp/ycm/ClangCompleter/TranslationUnit.h index 3fe08671..16110b40 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.h +++ b/cpp/ycm/ClangCompleter/TranslationUnit.h @@ -18,7 +18,6 @@ #ifndef TRANSLATIONUNIT_H_XQ7I6SVA #define TRANSLATIONUNIT_H_XQ7I6SVA -#include "Future.h" #include "UnsavedFile.h" #include "Diagnostic.h" #include "Location.h" diff --git a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp index 7c5c0b9e..a93d5e7e 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnitStore.cpp @@ -21,6 +21,7 @@ #include "exceptions.h" #include +#include #include using boost::lock_guard; diff --git a/cpp/ycm/ConcurrentLatestValue.h b/cpp/ycm/ConcurrentLatestValue.h deleted file mode 100644 index 97a63d76..00000000 --- a/cpp/ycm/ConcurrentLatestValue.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic -// -// This file is part of YouCompleteMe. -// -// YouCompleteMe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// YouCompleteMe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with YouCompleteMe. If not, see . - -#ifndef CONCURRENTLATESTVALUE_H_SYF1JPPG -#define CONCURRENTLATESTVALUE_H_SYF1JPPG - -#include -#include - -namespace YouCompleteMe { - -// This is is basically a multi-consumer single-producer queue, only with the -// twist that we only care about the latest value set. So the GUI thread is the -// setter, and the worker threads are the workers. The workers wait in line on -// the condition variable and when the setter sets a value, a worker is chosen -// to consume it. -// -// The point is that we always want to have one "fresh" worker thread ready to -// work on our latest value. If a newer value is set, then we don't care what -// happens to the old values. -// -// This implementation is mutex-based and is not lock-free. Normally using a -// lock-free data structure makes more sense, but since the GUI thread goes -// through VimL and Python on every keystroke, there's really no point. Those 5 -// nanoseconds it takes to lock a mutex are laughably negligible compared to the -// VimL/Python overhead. -template -class ConcurrentLatestValue : boost::noncopyable { -public: - - ConcurrentLatestValue() : empty_( true ) {} - - void Set( const T &data ) { - { - boost::unique_lock< boost::mutex > lock( mutex_ ); - latest_ = data; - empty_ = false; - } - - condition_variable_.notify_one(); - } - - T Get() { - boost::unique_lock< boost::mutex > lock( mutex_ ); - - while ( empty_ ) { - condition_variable_.wait( lock ); - } - - empty_ = true; - return latest_; - } - - -private: - T latest_; - bool empty_; - boost::mutex mutex_; - boost::condition_variable condition_variable_; - -}; - -} // namespace YouCompleteMe - -#endif /* end of include guard: CONCURRENTLATESTVALUE_H_SYF1JPPG */ diff --git a/cpp/ycm/ConcurrentStack.h b/cpp/ycm/ConcurrentStack.h deleted file mode 100644 index 5d88bd0a..00000000 --- a/cpp/ycm/ConcurrentStack.h +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic -// -// This file is part of YouCompleteMe. -// -// YouCompleteMe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// YouCompleteMe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with YouCompleteMe. If not, see . - -#ifndef CONCURRENTSTACK_H_TGI0GOR6 -#define CONCURRENTSTACK_H_TGI0GOR6 - -#include -#include -#include - -namespace YouCompleteMe { - -template -class ConcurrentStack : boost::noncopyable { -public: - - void Push( const T &data ) { - { - boost::unique_lock< boost::mutex > lock( mutex_ ); - stack_.push( data ); - } - - condition_variable_.notify_one(); - } - - - T Pop() { - boost::unique_lock< boost::mutex > lock( mutex_ ); - - while ( stack_.empty() ) { - condition_variable_.wait( lock ); - } - - T top = stack_.top(); - stack_.pop(); - return top; - } - - - // Gets all the items from the stack and appends them to the input vector. - // Does not wait for the stack to get items; if the stack is empty, returns - // false and does not touch the input vector. - bool PopAllNoWait( std::vector< T > &items ) { - boost::unique_lock< boost::mutex > lock( mutex_ ); - - if ( stack_.empty() ) - return false; - - int num_items = stack_.size(); - items.reserve( num_items + items.size() ); - - for ( int i = 0; i < num_items; ++i ) { - items.push_back( stack_.top() ); - stack_.pop(); - } - - return true; - } - - - bool Empty() { - boost::unique_lock< boost::mutex > lock( mutex_ ); - return stack_.empty(); - } - -private: - std::stack< T > stack_; - boost::mutex mutex_; - boost::condition_variable condition_variable_; - -}; - -} // namespace YouCompleteMe - -#endif /* end of include guard: CONCURRENTSTACK_H_TGI0GOR6 */ diff --git a/cpp/ycm/Future.h b/cpp/ycm/Future.h deleted file mode 100644 index 235eea24..00000000 --- a/cpp/ycm/Future.h +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (C) 2011, 2012 Strahinja Val Markovic -// -// This file is part of YouCompleteMe. -// -// YouCompleteMe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// YouCompleteMe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with YouCompleteMe. If not, see . - -#ifndef FUTURE_H_NR1U6MZS -#define FUTURE_H_NR1U6MZS - -#include -#include -#include -#include - -namespace YouCompleteMe { - -class Result; -typedef boost::shared_ptr< boost::packaged_task< void > > VoidTask; - -template< typename T > -boost::shared_ptr< T > ReturnValueAsShared( - boost::function< T() > func ) { - return boost::make_shared< T >( func() ); -} - - -template< typename T > -class Future { -public: - Future() {}; - Future( boost::shared_future< T > future ) - : future_( boost::move( future ) ) {} - - bool ResultsReady() { - // It's OK to return true since GetResults will just return a - // default-constructed value if the future_ is uninitialized. If we don't - // return true for this case, any loop waiting on ResultsReady will wait - // forever. - if ( future_.get_state() == boost::future_state::uninitialized ) - return true; - - return future_.is_ready(); - } - - - void Wait() { - future_.wait(); - } - - - T GetResults() { - try { - return future_.get(); - } catch ( boost::future_uninitialized & ) { - // Do nothing and return a T() - } - - return T(); - } - -private: - boost::shared_future< T > future_; -}; - -} // namespace YouCompleteMe - -#endif /* end of include guard: FUTURE_H_NR1U6MZS */ diff --git a/cpp/ycm/IdentifierCompleter.h b/cpp/ycm/IdentifierCompleter.h index 7eb1edcf..5b14420b 100644 --- a/cpp/ycm/IdentifierCompleter.h +++ b/cpp/ycm/IdentifierCompleter.h @@ -18,10 +18,7 @@ #ifndef COMPLETER_H_7AR4UGXE #define COMPLETER_H_7AR4UGXE -#include "ConcurrentLatestValue.h" -#include "ConcurrentStack.h" #include "IdentifierDatabase.h" -#include "Future.h" #include #include From d63843ea4c91d02b7540b34f59b410f7053107e3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 10:12:50 -0700 Subject: [PATCH 051/149] Clang completer method params are const ref again They were temporarily all passed by-value until server-side threading issues were resolved. --- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 28 +++++++++--------- cpp/ycm/ClangCompleter/ClangCompleter.h | 35 ++++++++++------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index d03684a7..c7239c44 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -59,7 +59,7 @@ ClangCompleter::~ClangCompleter() { std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( - std::string filename ) { + const std::string &filename ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); if ( !unit ) @@ -83,9 +83,9 @@ bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) { void ClangCompleter::UpdateTranslationUnit( - std::string filename, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { + const std::string &filename, + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ) { bool translation_unit_created; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, @@ -114,11 +114,11 @@ void ClangCompleter::UpdateTranslationUnit( std::vector< CompletionData > ClangCompleter::CandidatesForLocationInFile( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -132,11 +132,11 @@ ClangCompleter::CandidatesForLocationInFile( Location ClangCompleter::GetDeclarationLocation( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -149,11 +149,11 @@ Location ClangCompleter::GetDeclarationLocation( Location ClangCompleter::GetDefinitionLocation( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ) { + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -165,7 +165,7 @@ Location ClangCompleter::GetDefinitionLocation( } -void ClangCompleter::DeleteCachesForFile( std::string filename ) { +void ClangCompleter::DeleteCachesForFile( const std::string &filename ) { translation_unit_store_.Remove( filename ); } diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 5218f4e5..70fb3252 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -37,48 +37,43 @@ struct Location; typedef std::vector< CompletionData > CompletionDatas; -// TODO: document that all filename parameters must be absolute paths +// All filename parameters must be absolute paths. class ClangCompleter : boost::noncopyable { public: ClangCompleter(); ~ClangCompleter(); - std::vector< Diagnostic > DiagnosticsForFile( std::string filename ); + std::vector< Diagnostic > DiagnosticsForFile( const std::string &filename ); bool UpdatingTranslationUnit( const std::string &filename ); - // NOTE: params are taken by value on purpose! With a C++11 compiler we can - // avoid internal copies if params are taken by value (move ctors FTW), and we - // need to ensure we own the memory. - // TODO: Change some of these params back to const ref where possible after we - // get the server up. void UpdateTranslationUnit( - std::string filename, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); + const std::string &filename, + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ); std::vector< CompletionData > CandidatesForLocationInFile( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ); Location GetDeclarationLocation( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ); Location GetDefinitionLocation( - std::string filename, + const std::string &filename, int line, int column, - std::vector< UnsavedFile > unsaved_files, - std::vector< std::string > flags ); + const std::vector< UnsavedFile > &unsaved_files, + const std::vector< std::string > &flags ); - void DeleteCachesForFile( std::string filename ); + void DeleteCachesForFile( const std::string &filename ); private: From ec920b2758f4f54ca53e6085940f1168ec70b6f2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 10:41:02 -0700 Subject: [PATCH 052/149] Adding WebTest to pip test dependencies --- python/test_requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/python/test_requirements.txt b/python/test_requirements.txt index d9d5206d..1170bb4d 100644 --- a/python/test_requirements.txt +++ b/python/test_requirements.txt @@ -2,4 +2,5 @@ flake8>=2.0 mock>=1.0.1 nose>=1.3.0 PyHamcrest>=1.7.2 +WebTest>=2.0.9 From fe94ed6b1c3586c3d61acab505c7e7b3af65172c Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 10:41:34 -0700 Subject: [PATCH 053/149] Removing an outdate TODO --- python/ycm/vimsupport.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index bb2e20ec..78047003 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -99,11 +99,10 @@ def NumLinesInBuffer( buffer_object ): return len( buffer_object ) +# Calling this function from the non-GUI thread will sometimes crash Vim. At the +# time of writing, YCM only uses the GUI thread inside Vim (this used to not be +# the case). def PostVimMessage( message ): - # TODO: Check are we on the main thread or not, and if not, force a crash - # here. This should make it impossible to accidentally call this from a - # non-GUI thread which *sometimes* crashes Vim because Vim is not thread-safe. - # A consistent crash should force us to notice the error. vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None" .format( EscapeForVim( str( message ) ) ) ) From f56ced6374eb50a03696190d185b8776c138023b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 11:03:55 -0700 Subject: [PATCH 054/149] Using add_definitions, workaround cmake warning We haven't been building LLVM in-tree for many months now so we can use this now just fine. --- cpp/CMakeLists.txt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 62873efb..5dbc4a2d 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -138,19 +138,18 @@ endif() # the compiler to output a warning during linking: # clang: warning: argument unused during compilation: '-std=c++0x' # This is caused by cmake passing this flag to the linking stage which it -# shouldn't do. It's ignored so it does no harm, but the warning is annoying and -# there's no way around the problem (the flag is correctly used during the -# compilation stage). We could use add_definitions(-std=c++0x), but this will -# break the llvm build since the flag will then be used when compiling C code -# too. Sadly there's no way around the warning. +# shouldn't do. It's ignored so it does no harm, but the warning is annoying. +# +# Putting the flag in add_definitions() works around the issue, even though it +# shouldn't in theory go there. if ( CPP11_AVAILABLE ) message( "Your C++ compiler supports C++11, compiling in that mode." ) # Cygwin needs its hand held a bit; see issue #473 if ( CYGWIN AND CMAKE_COMPILER_IS_GNUCXX ) - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x" ) + add_definitions( -std=gnu++0x ) else() - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x" ) + add_definitions( -std=c++0x ) endif() else() message( From 5dd6782970f8a231f4db489351c4762e69b1792e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 14:25:08 -0700 Subject: [PATCH 055/149] Vim doesn't execute extra conf preload This is executed inside the server now, as it should be. --- autoload/youcompleteme.vim | 3 --- 1 file changed, 3 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index c60c207a..9290a0b7 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -44,9 +44,6 @@ function! youcompleteme#Enable() py from ycm import vimsupport py from ycm import user_options_store py user_options_store.SetAll( base.BuildServerConf() ) - " TODO: Remove the call to YcmPreload - py from ycm import extra_conf_store - py extra_conf_store.CallExtraConfYcmCorePreloadIfExists() if !pyeval( 'base.CompatibleWithYcmCore()') echohl WarningMsg | From a2657f5af8f82d6127905babbe91dd4854d64fdd Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 15:03:41 -0700 Subject: [PATCH 056/149] Adding requests-futures subrepo to third_party --- .gitmodules | 3 +++ third_party/requests-futures | 1 + 2 files changed, 4 insertions(+) create mode 160000 third_party/requests-futures diff --git a/.gitmodules b/.gitmodules index 7d13a22b..68dde940 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "python/ycm/completers/cs/OmniSharpServer"] path = python/ycm/completers/cs/OmniSharpServer url = https://github.com/nosami/OmniSharpServer.git +[submodule "third_party/requests-futures"] + path = third_party/requests-futures + url = https://github.com/ross/requests-futures diff --git a/third_party/requests-futures b/third_party/requests-futures new file mode 160000 index 00000000..79e52563 --- /dev/null +++ b/third_party/requests-futures @@ -0,0 +1 @@ +Subproject commit 79e525634e33d86cb5f3e62dcba88f98fe4f4cbc From e08dd4ab333c60ba2352e6a5c52fe6b5ab6dd690 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 15:11:35 -0700 Subject: [PATCH 057/149] Adding Requests as submodule to third_party --- .gitmodules | 3 +++ third_party/requests | 1 + 2 files changed, 4 insertions(+) create mode 160000 third_party/requests diff --git a/.gitmodules b/.gitmodules index 68dde940..ad2f1a83 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "third_party/requests-futures"] path = third_party/requests-futures url = https://github.com/ross/requests-futures +[submodule "third_party/requests"] + path = third_party/requests + url = https://github.com/kennethreitz/requests diff --git a/third_party/requests b/third_party/requests new file mode 160000 index 00000000..9968a10f --- /dev/null +++ b/third_party/requests @@ -0,0 +1 @@ +Subproject commit 9968a10fcfad7268b552808c4f8946eecafc956a From 9d0a6c96d715fa6b61f940c2b5e9bdc64e9eee35 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 1 Oct 2013 16:21:17 -0700 Subject: [PATCH 058/149] Event and completion request are now async This results in a much snappier Vim. --- autoload/youcompleteme.vim | 8 +++--- python/ycm/client/base_request.py | 37 +++++++++++++++++-------- python/ycm/client/command_request.py | 5 +--- python/ycm/client/completion_request.py | 24 ++++++++-------- python/ycm/client/event_notification.py | 2 +- python/ycm/server/ycmd.py | 3 ++ python/ycm/utils.py | 12 ++++++++ python/ycm/youcompleteme.py | 1 + 8 files changed, 60 insertions(+), 32 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 9290a0b7..550151f9 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -40,6 +40,8 @@ function! youcompleteme#Enable() py import sys py import vim exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )' + py from ycm import utils + py utils.AddThirdPartyFoldersToSysPath() py from ycm import base py from ycm import vimsupport py from ycm import user_options_store @@ -497,13 +499,11 @@ python << EOF def GetCompletions( query ): request = ycm_state.GetCurrentCompletionRequest() request.Start( query ) - results_ready = False - while not results_ready: - results_ready = request.Done() + while not request.Done(): if bool( int( vim.eval( 'complete_check()' ) ) ): return { 'words' : [], 'refresh' : 'always'} - results = base.AdjustCandidateInsertionText( request.Results() ) + results = base.AdjustCandidateInsertionText( request.Response() ) return { 'words' : results, 'refresh' : 'always' } EOF diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 84b95e81..4c46a5d9 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -20,6 +20,7 @@ import vim import json import requests +from requests_futures.sessions import FuturesSession from ycm import vimsupport HEADERS = {'content-type': 'application/json'} @@ -46,22 +47,22 @@ class BaseRequest( object ): return {} + # This is the blocking version of the method. See below for async. @staticmethod def PostDataToHandler( data, handler ): - response = requests.post( _BuildUri( handler ), - data = json.dumps( data ), - headers = HEADERS ) - if response.status_code == requests.codes.server_error: - raise ServerError( response.json()[ 'message' ] ) + return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data, + handler ) ) - # We let Requests handle the other status types, we only handle the 500 - # error code. - response.raise_for_status() - if response.text: - return response.json() - return None + # This returns a future! Use JsonFromFuture to get the value. + @staticmethod + def PostDataToHandlerAsync( data, handler ): + return BaseRequest.session.post( _BuildUri( handler ), + data = json.dumps( data ), + headers = HEADERS ) + + session = FuturesSession( max_workers = 4 ) server_location = 'http://localhost:6666' @@ -83,6 +84,20 @@ def BuildRequestData( start_column = None, query = None ): return request_data +def JsonFromFuture( future ): + response = future.result() + if response.status_code == requests.codes.server_error: + raise ServerError( response.json()[ 'message' ] ) + + # We let Requests handle the other status types, we only handle the 500 + # error code. + response.raise_for_status() + + if response.text: + return response.json() + return None + + def _BuildUri( handler ): return ''.join( [ BaseRequest.server_location, '/', handler ] ) diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py index d3fe5ad3..0efbbae7 100644 --- a/python/ycm/client/command_request.py +++ b/python/ycm/client/command_request.py @@ -18,7 +18,6 @@ # along with YouCompleteMe. If not, see . import vim -import time from ycm.client.base_request import BaseRequest, BuildRequestData, ServerError from ycm import vimsupport from ycm.utils import ToUtf8IfNeeded @@ -70,10 +69,8 @@ class CommandRequest( BaseRequest ): def SendCommandRequest( arguments, completer ): request = CommandRequest( arguments, completer ) + # This is a blocking call. request.Start() - while not request.Done(): - time.sleep( 0.1 ) - request.RunPostCommandActionsIfNeeded() return request.Response() diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index 076f26ac..a7c8541e 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -19,7 +19,8 @@ from ycm import base from ycm import vimsupport -from ycm.client.base_request import BaseRequest, BuildRequestData +from ycm.client.base_request import ( BaseRequest, BuildRequestData, + JsonFromFuture ) class CompletionRequest( BaseRequest ): @@ -30,27 +31,26 @@ class CompletionRequest( BaseRequest ): self._request_data = BuildRequestData( self._completion_start_column ) - # TODO: Do we need this anymore? - # def ShouldComplete( self ): - # return ( self._do_filetype_completion or - # self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) ) - - def CompletionStartColumn( self ): return self._completion_start_column def Start( self, query ): self._request_data[ 'query' ] = query - self._response = self.PostDataToHandler( self._request_data, - 'get_completions' ) + self._response_future = self.PostDataToHandlerAsync( self._request_data, + 'get_completions' ) - def Results( self ): - if not self._response: + def Done( self ): + return self._response_future.done() + + + def Response( self ): + if not self._response_future: return [] try: - return [ _ConvertCompletionDataToVimData( x ) for x in self._response ] + return [ _ConvertCompletionDataToVimData( x ) + for x in JsonFromFuture( self._response_future ) ] except Exception as e: vimsupport.PostVimMessage( str( e ) ) return [] diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index 4aa7825a..62c0fa65 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -41,7 +41,7 @@ class EventNotification( BaseRequest ): # quietly to the Vim message log because nothing bad will happen if the # server misses some events and we don't want to annoy the user. try: - self.PostDataToHandler( request_data, 'event_notification' ) + self.PostDataToHandlerAsync( request_data, 'event_notification' ) except: vimsupport.EchoText( traceback.format_exc() ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 098e95d1..fb290c8b 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -30,6 +30,9 @@ sys.path.insert( 0, os.path.join( os.path.dirname( os.path.abspath( __file__ ) ), '../..' ) ) +from ycm import utils +utils.AddThirdPartyFoldersToSysPath() + import logging import json import bottle diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 9f1ae775..f861947b 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -52,3 +52,15 @@ def TerminateProcess( pid ): ctypes.windll.kernel32.CloseHandle( handle ) else: os.kill( pid, signal.SIGTERM ) + + +def AddThirdPartyFoldersToSysPath(): + path_to_third_party = os.path.join( + os.path.dirname( os.path.abspath( __file__ ) ), + '../../third_party' ) + + for folder in os.listdir( path_to_third_party ): + sys.path.insert( 0, os.path.realpath( os.path.join( path_to_third_party, + folder ) ) ) + + diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 78ee83cf..7777f390 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -165,6 +165,7 @@ class YouCompleteMe( object ): def OnVimLeave( self ): + # TODO: There should be a faster way of shutting down the server self._server_popen.terminate() os.remove( self._temp_options_filename ) From 9742302cbdc85b60b86fdd1651e928b713cb455e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 2 Oct 2013 12:23:26 -0700 Subject: [PATCH 059/149] 'get_completions' handler is now 'completions' --- python/ycm/client/completion_request.py | 2 +- python/ycm/server/tests/basic_test.py | 8 ++++---- python/ycm/server/ycmd.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index a7c8541e..d55ade1e 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -38,7 +38,7 @@ class CompletionRequest( BaseRequest ): def Start( self, query ): self._request_data[ 'query' ] = query self._response_future = self.PostDataToHandlerAsync( self._request_data, - 'get_completions' ) + 'completions' ) def Done( self ): diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 74b24e13..eebce437 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -72,7 +72,7 @@ def GetCompletions_IdentifierCompleter_Works_test(): eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'foogoo' ) ], - app.post_json( '/get_completions', completion_data ).json ) + app.post_json( '/completions', completion_data ).json ) @with_setup( Setup ) @@ -111,7 +111,7 @@ int main() } } - results = app.post_json( '/get_completions', completion_data ).json + results = app.post_json( '/completions', completion_data ).json assert_that( results, has_items( CompletionEntryMatcher( 'c' ), CompletionEntryMatcher( 'x' ), CompletionEntryMatcher( 'y' ) ) ) @@ -139,7 +139,7 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'zoo' ) ], - app.post_json( '/get_completions', completion_data ).json ) + app.post_json( '/completions', completion_data ).json ) @with_setup( Setup ) @@ -166,7 +166,7 @@ def GetCompletions_UltiSnipsCompleter_Works_test(): eq_( [ BuildCompletionData( 'foo', ' bar' ), BuildCompletionData( 'zoo', ' goo' ) ], - app.post_json( '/get_completions', completion_data ).json ) + app.post_json( '/completions', completion_data ).json ) @with_setup( Setup ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index fb290c8b..b23d0b34 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -79,7 +79,7 @@ def RunCompleterCommand(): request_data ) ) -@app.post( '/get_completions' ) +@app.post( '/completions' ) def GetCompletions(): LOGGER.info( 'Received completion request') request_data = request.json From 7248979bb4b97fd31500303c23d652ab0a8bf528 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 2 Oct 2013 17:09:25 -0700 Subject: [PATCH 060/149] We now handle the starting FileReadyToParse event The problem was that when you start vim like "vim foo.cc", the FileReadyToParse event is sent to the server before it's actually started up. Basically, a race condition. We _really_ don't want to miss that event. For C++ files, it tells the server to start compiling the file. So now PostDataToHandlerAsync in BaseRequest will retry the request 3 times (with exponential backoff) before failing, thus giving the server time to boot. --- python/ycm/client/base_request.py | 44 ++++++++++++++++++++++--- python/ycm/client/event_notification.py | 12 +------ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 4c46a5d9..d67836a5 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -20,10 +20,15 @@ import vim import json import requests +from retries import retries from requests_futures.sessions import FuturesSession +from concurrent.futures import ThreadPoolExecutor from ycm import vimsupport HEADERS = {'content-type': 'application/json'} +# TODO: This TPE might be the reason we're shutting down slowly. It seems that +# it waits for all worker threads to stop before letting the interpreter exit. +EXECUTOR = ThreadPoolExecutor( max_workers = 4 ) class ServerError( Exception ): def __init__( self, message ): @@ -57,12 +62,24 @@ class BaseRequest( object ): # This returns a future! Use JsonFromFuture to get the value. @staticmethod def PostDataToHandlerAsync( data, handler ): - return BaseRequest.session.post( _BuildUri( handler ), - data = json.dumps( data ), - headers = HEADERS ) + def PostData( data, handler ): + return BaseRequest.session.post( _BuildUri( handler ), + data = json.dumps( data ), + headers = HEADERS ) + + @retries( 3, delay = 0.5 ) + def DelayedPostData( data, handler ): + return requests.post( _BuildUri( handler ), + data = json.dumps( data ), + headers = HEADERS ) + + if not _CheckServerIsHealthyWithCache(): + return EXECUTOR.submit( DelayedPostData, data, handler ) + + return PostData( data, handler ) - session = FuturesSession( max_workers = 4 ) + session = FuturesSession( executor = EXECUTOR ) server_location = 'http://localhost:6666' @@ -102,3 +119,22 @@ def _BuildUri( handler ): return ''.join( [ BaseRequest.server_location, '/', handler ] ) +SERVER_HEALTHY = False + +def _CheckServerIsHealthyWithCache(): + global SERVER_HEALTHY + + def _ServerIsHealthy(): + response = requests.get( _BuildUri( 'healthy' ) ) + response.raise_for_status() + return response.json() + + if SERVER_HEALTHY: + return True + + try: + SERVER_HEALTHY = _ServerIsHealthy() + return SERVER_HEALTHY + except: + return False + diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index 62c0fa65..c39a4f43 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -17,8 +17,6 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import traceback -from ycm import vimsupport from ycm.client.base_request import BaseRequest, BuildRequestData @@ -35,15 +33,7 @@ class EventNotification( BaseRequest ): request_data.update( self._extra_data ) request_data[ 'event_name' ] = self._event_name - # On occasion, Vim tries to send event notifications to the server before - # it's up. This causes intrusive exception messages to interrupt the user. - # While we do want to log these exceptions just in case, we post them - # quietly to the Vim message log because nothing bad will happen if the - # server misses some events and we don't want to annoy the user. - try: - self.PostDataToHandlerAsync( request_data, 'event_notification' ) - except: - vimsupport.EchoText( traceback.format_exc() ) + self.PostDataToHandlerAsync( request_data, 'event_notification' ) def SendEventNotificationAsync( event_name, extra_data = None ): From 5ef945fa0a856ea0caaaa9f3dc6d4facea5b91a7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 10:08:45 -0700 Subject: [PATCH 061/149] Adding 'qf' (quickfix) filetype to ignore list --- python/ycm/server/default_settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 4aeaa025..69e334cf 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file From e44fd87582cfd3c5cf09214b0c41ec42560bd28a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 10:11:21 -0700 Subject: [PATCH 062/149] Added 'gitcommit' to filetype-completion blacklist --- python/ycm/server/default_settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 69e334cf..0ae36f9b 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "gitcommit": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file From b9bb788a2a393fefbefb6073fbeb4bae309e247d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 10:14:31 -0700 Subject: [PATCH 063/149] Diagnostics work again... somewhat. There appear to be timing issues for the diag requests. Somehow, we're sending out-of-date diagnostics and then not updating the UI when things change. That needs to be fixed. --- autoload/youcompleteme.vim | 25 +++--- cpp/ycm/ClangCompleter/TranslationUnit.cpp | 18 +---- python/ycm/client/command_request.py | 2 +- python/ycm/client/diagnostics_request.py | 75 ++++++++++++++++++ python/ycm/completers/completer.py | 6 +- python/ycm/completers/cpp/clang_completer.py | 49 ++++-------- python/ycm/server/default_settings.json | 2 +- python/ycm/server/tests/basic_test.py | 44 ++++++++++- python/ycm/server/ycmd.py | 17 +++++ python/ycm/utils.py | 10 +++ python/ycm/vimsupport.py | 7 ++ python/ycm/youcompleteme.py | 46 +++++++---- third_party/retries/retries.py | 80 ++++++++++++++++++++ 13 files changed, 298 insertions(+), 83 deletions(-) create mode 100644 python/ycm/client/diagnostics_request.py create mode 100644 third_party/retries/retries.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 550151f9..0c103d5c 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -260,7 +260,7 @@ function! s:OnCursorHold() call s:SetUpCompleteopt() " Order is important here; we need to extract any done diagnostics before " reparsing the file again - " call s:UpdateDiagnosticNotifications() + call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -272,6 +272,7 @@ function! s:OnFileReadyToParse() let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse if buffer_changed + py ycm_state.RequestDiagnosticsForCurrentFile() py ycm_state.OnFileReadyToParse() endif let b:ycm_changedtick.file_ready_to_parse = b:changedtick @@ -327,7 +328,7 @@ function! s:OnCursorMovedNormalMode() return endif - " call s:UpdateDiagnosticNotifications() + call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -338,7 +339,7 @@ function! s:OnInsertLeave() endif let s:omnifunc_mode = 0 - " call s:UpdateDiagnosticNotifications() + call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() py ycm_state.OnInsertLeave() if g:ycm_autoclose_preview_window_after_completion || @@ -408,10 +409,16 @@ endfunction function! s:UpdateDiagnosticNotifications() - if get( g:, 'loaded_syntastic_plugin', 0 ) && - \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) && - \ pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' ) && - \ g:ycm_register_as_syntastic_checker + let should_display_diagnostics = + \ get( g:, 'loaded_syntastic_plugin', 0 ) && + \ g:ycm_register_as_syntastic_checker && + \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) + + if !should_display_diagnostics + return + endif + + if pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' ) SyntasticCheck endif endfunction @@ -566,9 +573,7 @@ command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic() " required (currently that's on buffer save) OR when the SyntasticCheck command " is invoked function! youcompleteme#CurrentFileDiagnostics() - " TODO: Make this work again. - " return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' ) - return [] + return pyeval( 'ycm_state.GetDiagnosticsFromStoredRequest()' ) endfunction diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.cpp b/cpp/ycm/ClangCompleter/TranslationUnit.cpp index 0acc0681..efaaf5ff 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnit.cpp @@ -92,25 +92,11 @@ void TranslationUnit::Destroy() { std::vector< Diagnostic > TranslationUnit::LatestDiagnostics() { - std::vector< Diagnostic > diagnostics; - if ( !clang_translation_unit_ ) - return diagnostics; + return std::vector< Diagnostic >(); unique_lock< mutex > lock( diagnostics_mutex_ ); - - // We don't need the latest diags after we return them once so we swap the - // internal data with a new, empty diag vector. This vector is then returned - // and on C++11 compilers a move ctor is invoked, thus no copy is created. - // Theoretically, just returning the value of a - // [boost::|std::]move(latest_diagnostics_) call _should_ leave the - // latest_diagnostics_ vector in an emtpy, valid state but I'm not going to - // rely on that. I just had to look this up in the standard to be sure, and - // future readers of this code (myself included) should not be forced to do - // that to understand what the hell is going on. - - std::swap( latest_diagnostics_, diagnostics ); - return diagnostics; + return latest_diagnostics_; } diff --git a/python/ycm/client/command_request.py b/python/ycm/client/command_request.py index 0efbbae7..f2b92ab3 100644 --- a/python/ycm/client/command_request.py +++ b/python/ycm/client/command_request.py @@ -30,7 +30,7 @@ class CommandRequest( BaseRequest ): self._completer_target = ( completer_target if completer_target else 'filetype_default' ) self._is_goto_command = ( - True if arguments and arguments[ 0 ].startswith( 'GoTo' ) else False ) + arguments and arguments[ 0 ].startswith( 'GoTo' ) ) self._response = None diff --git a/python/ycm/client/diagnostics_request.py b/python/ycm/client/diagnostics_request.py new file mode 100644 index 00000000..505f927c --- /dev/null +++ b/python/ycm/client/diagnostics_request.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import traceback +from ycm import vimsupport +from ycm.client.base_request import ( BaseRequest, BuildRequestData, + JsonFromFuture ) + + +class DiagnosticsRequest( BaseRequest ): + def __init__( self ): + super( DiagnosticsRequest, self ).__init__() + self._cached_response = None + + + def Start( self ): + request_data = BuildRequestData() + + try: + self._response_future = self.PostDataToHandlerAsync( request_data, + 'diagnostics' ) + except: + vimsupport.EchoText( traceback.format_exc() ) + + + def Done( self ): + return self._response_future.done() + + + def Response( self ): + if self._cached_response: + return self._cached_response + + if not self._response_future: + return [] + try: + self._cached_response = [ _ConvertDiagnosticDataToVimData( x ) + for x in JsonFromFuture( + self._response_future ) ] + return self._cached_response + except Exception as e: + vimsupport.PostVimMessage( str( e ) ) + return [] + + +def _ConvertDiagnosticDataToVimData( diagnostic ): + # see :h getqflist for a description of the dictionary fields + # Note that, as usual, Vim is completely inconsistent about whether + # line/column numbers are 1 or 0 based in its various APIs. Here, it wants + # them to be 1-based. + return { + 'bufnr' : vimsupport.GetBufferNumberForFilename( diagnostic[ 'filepath' ]), + 'lnum' : diagnostic[ 'line_num' ] + 1, + 'col' : diagnostic[ 'column_num' ] + 1, + 'text' : diagnostic[ 'text' ], + 'type' : diagnostic[ 'kind' ], + 'valid' : 1 + } + diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index c44b5572..99daa29b 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -233,11 +233,7 @@ class Completer( object ): pass - def DiagnosticsForCurrentFileReady( self ): - return False - - - def GetDiagnosticsForCurrentFile( self ): + def GetDiagnosticsForCurrentFile( self, request_data ): return [] diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 31a0bf10..e2f48718 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -44,7 +44,6 @@ class ClangCompleter( Completer ): self._max_diagnostics_to_display = user_options[ 'max_diagnostics_to_display' ] self._completer = ycm_core.ClangCompleter() - self._last_prepared_diagnostics = [] self._flags = Flags() self._diagnostic_store = None self._logger = logging.getLogger( __name__ ) @@ -213,13 +212,6 @@ class ClangCompleter( Completer ): ToUtf8IfNeeded( request_data[ 'unloaded_buffer' ] ) ) - def DiagnosticsForCurrentFileReady( self ): - # if not self.parse_future: - # return False - # return self.parse_future.ResultsReady() - pass - - def GettingCompletions( self, request_data ): return self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( request_data[ 'filepath' ] ) ) @@ -227,19 +219,11 @@ class ClangCompleter( Completer ): def GetDiagnosticsForCurrentFile( self, request_data ): filename = request_data[ 'filepath' ] - if self.DiagnosticsForCurrentFileReady(): - diagnostics = self._completer.DiagnosticsForFile( - ToUtf8IfNeeded( filename ) ) - self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) - self._last_prepared_diagnostics = [ - responses.BuildDiagnosticData( x ) for x in - diagnostics[ : self._max_diagnostics_to_display ] ] - # self.parse_future = None - - # if self.extra_parse_desired: - # self.OnFileReadyToParse( request_data ) - - return self._last_prepared_diagnostics + diagnostics = self._completer.DiagnosticsForFile( + ToUtf8IfNeeded( filename ) ) + self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) + return [ ConvertToDiagnosticResponse( x ) for x in + diagnostics[ : self._max_diagnostics_to_display ] ] def GetDetailedDiagnostic( self, request_data ): @@ -279,6 +263,7 @@ class ClangCompleter( Completer ): source, list( flags ) ) + def _FlagsForRequest( self, request_data ): filename = request_data[ 'filepath' ] if 'compilation_flags' in request_data: @@ -286,20 +271,6 @@ class ClangCompleter( Completer ): filename ) return self._flags.FlagsForFile( filename ) -# TODO: Make this work again -# def DiagnosticToDict( diagnostic ): -# # see :h getqflist for a description of the dictionary fields -# return { -# # TODO: wrap the bufnr generation into a function -# 'bufnr' : int( vim.eval( "bufnr('{0}', 1)".format( -# diagnostic.filename_ ) ) ), -# 'lnum' : diagnostic.line_number_, -# 'col' : diagnostic.column_number_, -# 'text' : diagnostic.text_, -# 'type' : diagnostic.kind_, -# 'valid' : 1 -# } - def ConvertCompletionData( completion_data ): return responses.BuildCompletionData( @@ -326,3 +297,11 @@ def InCFamilyFile( filetypes ): return ClangAvailableForFiletypes( filetypes ) +def ConvertToDiagnosticResponse( diagnostic ): + return responses.BuildDiagnosticData( diagnostic.filename_, + diagnostic.line_number_ - 1, + diagnostic.column_number_ - 1, + diagnostic.text_, + diagnostic.kind_ ) + + diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 0ae36f9b..8dc9005e 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": {}, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "gitcommit": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": { "gitcommit": 1, }, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index eebce437..4f71da82 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -21,7 +21,8 @@ from webtest import TestApp from .. import ycmd from ..responses import BuildCompletionData from nose.tools import ok_, eq_, with_setup -from hamcrest import assert_that, has_items, has_entry +from hamcrest import ( assert_that, has_items, has_entry, contains, + contains_string, has_entries ) import bottle bottle.debug( True ) @@ -281,6 +282,47 @@ def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): app.post_json( '/defined_subcommands', subcommands_data ).json ) +@with_setup( Setup ) +def GetDiagnostics_ClangCompleter_ZeroBasedLineAndColumn_test(): + app = TestApp( ycmd.app ) + contents = """ +struct Foo { + int x // semicolon missing here! + int y; + int c; + int d; +}; +""" + + filename = '/foo.cpp' + diag_data = { + 'compilation_flags': ['-x', 'c++'], + 'line_num': 0, + 'column_num': 0, + 'filetypes': ['cpp'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': contents, + 'filetypes': ['cpp'] + } + } + } + + event_data = diag_data.copy() + event_data.update( { + 'event_name': 'FileReadyToParse', + } ) + + app.post_json( '/event_notification', event_data ) + results = app.post_json( '/diagnostics', diag_data ).json + assert_that( results, + contains( + has_entries( { 'text': contains_string( "expected ';'" ), + 'line_num': 2, + 'column_num': 7 } ) ) ) + + @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index b23d0b34..ef467058 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -94,18 +94,35 @@ def GetCompletions(): return _JsonResponse( completer.ComputeCandidates( request_data ) ) +@app.post( '/diagnostics' ) +def GetDiagnostics(): + LOGGER.info( 'Received diagnostics request') + request_data = request.json + completer = _GetCompleterForRequestData( request_data ) + + return _JsonResponse( completer.GetDiagnosticsForCurrentFile( + request_data ) ) + + @app.get( '/user_options' ) def GetUserOptions(): LOGGER.info( 'Received user options GET request') return _JsonResponse( dict( SERVER_STATE.user_options ) ) +@app.get( '/healthy' ) +def GetHealthy(): + LOGGER.info( 'Received health request') + return _JsonResponse( True ) + + @app.post( '/user_options' ) def SetUserOptions(): LOGGER.info( 'Received user options POST request') _SetUserOptions( request.json ) +# TODO: Rename this to 'semantic_completion_available' @app.post( '/filetype_completion_available') def FiletypeCompletionAvailable(): LOGGER.info( 'Received filetype completion available request') diff --git a/python/ycm/utils.py b/python/ycm/utils.py index f861947b..72dfd268 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -21,6 +21,7 @@ import tempfile import os import sys import signal +import functools def IsIdentifierChar( char ): return char.isalnum() or char == '_' @@ -63,4 +64,13 @@ def AddThirdPartyFoldersToSysPath(): sys.path.insert( 0, os.path.realpath( os.path.join( path_to_third_party, folder ) ) ) +def Memoize( obj ): + cache = obj.cache = {} + @functools.wraps( obj ) + def memoizer( *args, **kwargs ): + key = str( args ) + str( kwargs ) + if key not in cache: + cache[ key ] = obj( *args, **kwargs ) + return cache[ key ] + return memoizer diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 78047003..d46ec868 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -18,6 +18,7 @@ # along with YouCompleteMe. If not, see . import vim +import os def CurrentLineAndColumn(): """Returns the 0-based current line and 0-based current column.""" @@ -75,6 +76,12 @@ def GetUnsavedAndCurrentBufferData(): return buffers_data +def GetBufferNumberForFilename( filename, open_file_if_needed = True ): + return int( vim.eval( "bufnr('{0}', {1})".format( + os.path.realpath( filename ), + int( open_file_if_needed ) ) ) ) + + # Both |line| and |column| need to be 1-based def JumpToLocation( filename, line, column ): # Add an entry to the jumplist diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 7777f390..9741e741 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -29,6 +29,7 @@ from ycm.completers.general import syntax_parse from ycm.client.base_request import BaseRequest, BuildRequestData from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest +from ycm.client.diagnostics_request import DiagnosticsRequest from ycm.client.event_notification import SendEventNotificationAsync try: @@ -43,7 +44,8 @@ class YouCompleteMe( object ): def __init__( self, user_options ): self._user_options = user_options self._omnicomp = OmniCompleter( user_options ) - self._current_completion_request = None + self._latest_completion_request = None + self._latest_diagnostics_request = None self._server_stdout = None self._server_stderr = None self._server_popen = None @@ -91,8 +93,8 @@ class YouCompleteMe( object ): # We have to store a reference to the newly created CompletionRequest # because VimScript can't store a reference to a Python object across # function calls... Thus we need to keep this request somewhere. - self._current_completion_request = CompletionRequest() - return self._current_completion_request + self._latest_completion_request = CompletionRequest() + return self._latest_completion_request def SendCommandRequest( self, arguments, completer ): @@ -105,7 +107,7 @@ class YouCompleteMe( object ): def GetCurrentCompletionRequest( self ): - return self._current_completion_request + return self._latest_completion_request def GetOmniCompleter( self ): @@ -114,8 +116,7 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): try: - return BaseRequest.PostDataToHandler( BuildRequestData(), - 'filetype_completion_available') + return _NativeFiletypeCompletionAvailableForFile( vim.current.buffer.name ) except: return False @@ -174,17 +175,25 @@ class YouCompleteMe( object ): SendEventNotificationAsync( 'CurrentIdentifierFinished' ) - # TODO: Make this work again. def DiagnosticsForCurrentFileReady( self ): - # if self.FiletypeCompletionUsable(): - # return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady() - return False + return bool( self._latest_diagnostics_request and + self._latest_diagnostics_request.Done() ) - # TODO: Make this work again. - def GetDiagnosticsForCurrentFile( self ): - # if self.FiletypeCompletionUsable(): - # return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile() + def RequestDiagnosticsForCurrentFile( self ): + self._latest_diagnostics_request = DiagnosticsRequest() + self._latest_diagnostics_request.Start() + + + def GetDiagnosticsFromStoredRequest( self ): + if self._latest_diagnostics_request: + to_return = self._latest_diagnostics_request.Response() + # We set the diagnostics request to None because we want to prevent + # Syntastic from repeatedly refreshing the buffer with the same diags. + # Setting this to None makes DiagnosticsForCurrentFileReady return False + # until the next request is created. + self._latest_diagnostics_request = None + return to_return return [] @@ -260,3 +269,12 @@ def _AddUltiSnipsDataIfNeeded( extra_data ): } for x in rawsnips ] +# 'filepath' is here only as a key for Memoize +# This can't be a nested function inside NativeFiletypeCompletionAvailable +# because then the Memoize decorator wouldn't work (nested functions are +# re-created on every call to the outer function). +@utils.Memoize +def _NativeFiletypeCompletionAvailableForFile( filepath ): + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'filetype_completion_available') + diff --git a/third_party/retries/retries.py b/third_party/retries/retries.py new file mode 100644 index 00000000..1d7131d5 --- /dev/null +++ b/third_party/retries/retries.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# +# Copyright 2012 by Jeff Laughlin Consulting LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import sys +from time import sleep + +# Source: https://gist.github.com/n1ywb/2570004 + +def example_exc_handler(tries_remaining, exception, delay): + """Example exception handler; prints a warning to stderr. + + tries_remaining: The number of tries remaining. + exception: The exception instance which was raised. + """ + print >> sys.stderr, "Caught '%s', %d tries remaining, sleeping for %s seconds" % (exception, tries_remaining, delay) + + +def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None): + """Function decorator implementing retrying logic. + + delay: Sleep this many seconds * backoff * try number after failure + backoff: Multiply delay by this factor after each failure + exceptions: A tuple of exception classes; default (Exception,) + hook: A function with the signature myhook(tries_remaining, exception); + default None + + The decorator will call the function up to max_tries times if it raises + an exception. + + By default it catches instances of the Exception class and subclasses. + This will recover after all but the most fatal errors. You may specify a + custom tuple of exception classes with the 'exceptions' argument; the + function will only be retried if it raises one of the specified + exceptions. + + Additionally you may specify a hook function which will be called prior + to retrying with the number of remaining tries and the exception instance; + see given example. This is primarily intended to give the opportunity to + log the failure. Hook is not called after failure if no retries remain. + """ + def dec(func): + def f2(*args, **kwargs): + mydelay = delay + tries = range(max_tries) + tries.reverse() + for tries_remaining in tries: + try: + return func(*args, **kwargs) + except exceptions as e: + if tries_remaining > 0: + if hook is not None: + hook(tries_remaining, e, mydelay) + sleep(mydelay) + mydelay = mydelay * backoff + else: + raise + else: + break + return f2 + return dec From c274b7d4f80b0d018819aa72c401391dbef71995 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 13:15:43 -0700 Subject: [PATCH 064/149] Fixing the diagnostic-related race conditions Now, every FileReadyToParse event returns diagnostics, if any. This is instead of the previous system where the diagnostics were being fetched in a different request (this caused race conditions). --- autoload/youcompleteme.vim | 15 ++-- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 20 ++---- cpp/ycm/ClangCompleter/ClangCompleter.h | 4 +- cpp/ycm/ClangCompleter/TranslationUnit.cpp | 5 +- cpp/ycm/ClangCompleter/TranslationUnit.h | 3 +- cpp/ycm/ycm_core.cpp | 1 - python/ycm/client/diagnostics_request.py | 75 -------------------- python/ycm/client/event_notification.py | 45 +++++++++++- python/ycm/completers/cpp/clang_completer.py | 15 ++-- python/ycm/server/default_settings.json | 2 +- python/ycm/server/tests/basic_test.py | 5 +- python/ycm/server/ycmd.py | 19 ++--- python/ycm/youcompleteme.py | 25 +++---- 13 files changed, 89 insertions(+), 145 deletions(-) delete mode 100644 python/ycm/client/diagnostics_request.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 0c103d5c..f50952d4 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -258,9 +258,6 @@ function! s:OnCursorHold() endif call s:SetUpCompleteopt() - " Order is important here; we need to extract any done diagnostics before - " reparsing the file again - call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -270,9 +267,14 @@ function! s:OnFileReadyToParse() " happen for special buffers. call s:SetUpYcmChangedTick() + " Order is important here; we need to extract any done diagnostics before + " reparsing the file again. If we sent the new parse request first, then + " the response would always be pending when we called + " UpdateDiagnosticNotifications. + call s:UpdateDiagnosticNotifications() + let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse if buffer_changed - py ycm_state.RequestDiagnosticsForCurrentFile() py ycm_state.OnFileReadyToParse() endif let b:ycm_changedtick.file_ready_to_parse = b:changedtick @@ -328,7 +330,6 @@ function! s:OnCursorMovedNormalMode() return endif - call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() endfunction @@ -339,7 +340,6 @@ function! s:OnInsertLeave() endif let s:omnifunc_mode = 0 - call s:UpdateDiagnosticNotifications() call s:OnFileReadyToParse() py ycm_state.OnInsertLeave() if g:ycm_autoclose_preview_window_after_completion || @@ -660,13 +660,14 @@ function! s:ForceCompile() endfunction +" TODO: Make this work again. function! s:ForceCompileAndDiagnostics() let compilation_succeeded = s:ForceCompile() if !compilation_succeeded return endif - call s:UpdateDiagnosticNotifications() + " call s:UpdateDiagnosticNotifications() echom "Diagnostics refreshed." endfunction diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index c7239c44..a8497293 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -58,17 +58,6 @@ ClangCompleter::~ClangCompleter() { } -std::vector< Diagnostic > ClangCompleter::DiagnosticsForFile( - const std::string &filename ) { - shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); - - if ( !unit ) - return std::vector< Diagnostic >(); - - return unit->LatestDiagnostics(); -} - - bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) { shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); @@ -82,7 +71,7 @@ bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) { } -void ClangCompleter::UpdateTranslationUnit( +std::vector< Diagnostic > ClangCompleter::UpdateTranslationUnit( const std::string &filename, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ) { @@ -94,13 +83,14 @@ void ClangCompleter::UpdateTranslationUnit( translation_unit_created ); if ( !unit ) - return; + return std::vector< Diagnostic >(); try { // There's no point in reparsing a TU that was just created, it was just // parsed in the TU constructor if ( !translation_unit_created ) - unit->Reparse( unsaved_files ); + return unit->Reparse( unsaved_files ); + return unit->LatestDiagnostics(); } catch ( ClangParseError & ) { @@ -109,6 +99,8 @@ void ClangCompleter::UpdateTranslationUnit( // TU map. translation_unit_store_.Remove( filename ); } + + return std::vector< Diagnostic >(); } diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 70fb3252..7f89019e 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -43,11 +43,9 @@ public: ClangCompleter(); ~ClangCompleter(); - std::vector< Diagnostic > DiagnosticsForFile( const std::string &filename ); - bool UpdatingTranslationUnit( const std::string &filename ); - void UpdateTranslationUnit( + std::vector< Diagnostic > UpdateTranslationUnit( const std::string &filename, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ); diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.cpp b/cpp/ycm/ClangCompleter/TranslationUnit.cpp index efaaf5ff..9638c2df 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnit.cpp @@ -111,12 +111,15 @@ bool TranslationUnit::IsCurrentlyUpdating() const { } -void TranslationUnit::Reparse( +std::vector< Diagnostic > TranslationUnit::Reparse( const std::vector< UnsavedFile > &unsaved_files ) { std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles( unsaved_files ); Reparse( cxunsaved_files ); + + unique_lock< mutex > lock( diagnostics_mutex_ ); + return latest_diagnostics_; } diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.h b/cpp/ycm/ClangCompleter/TranslationUnit.h index 16110b40..d1469805 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.h +++ b/cpp/ycm/ClangCompleter/TranslationUnit.h @@ -56,7 +56,8 @@ public: bool IsCurrentlyUpdating() const; - void Reparse( const std::vector< UnsavedFile > &unsaved_files ); + std::vector< Diagnostic > Reparse( + const std::vector< UnsavedFile > &unsaved_files ); void ReparseForIndexing( const std::vector< UnsavedFile > &unsaved_files ); diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index 80626d63..ba348bcc 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -95,7 +95,6 @@ BOOST_PYTHON_MODULE(ycm_core) .def( vector_indexing_suite< std::vector< UnsavedFile > >() ); class_< ClangCompleter, boost::noncopyable >( "ClangCompleter" ) - .def( "DiagnosticsForFile", &ClangCompleter::DiagnosticsForFile ) .def( "GetDeclarationLocation", &ClangCompleter::GetDeclarationLocation ) .def( "GetDefinitionLocation", &ClangCompleter::GetDefinitionLocation ) .def( "DeleteCachesForFile", &ClangCompleter::DeleteCachesForFile ) diff --git a/python/ycm/client/diagnostics_request.py b/python/ycm/client/diagnostics_request.py deleted file mode 100644 index 505f927c..00000000 --- a/python/ycm/client/diagnostics_request.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (C) 2013 Strahinja Val Markovic -# -# This file is part of YouCompleteMe. -# -# YouCompleteMe is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# YouCompleteMe is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with YouCompleteMe. If not, see . - -import traceback -from ycm import vimsupport -from ycm.client.base_request import ( BaseRequest, BuildRequestData, - JsonFromFuture ) - - -class DiagnosticsRequest( BaseRequest ): - def __init__( self ): - super( DiagnosticsRequest, self ).__init__() - self._cached_response = None - - - def Start( self ): - request_data = BuildRequestData() - - try: - self._response_future = self.PostDataToHandlerAsync( request_data, - 'diagnostics' ) - except: - vimsupport.EchoText( traceback.format_exc() ) - - - def Done( self ): - return self._response_future.done() - - - def Response( self ): - if self._cached_response: - return self._cached_response - - if not self._response_future: - return [] - try: - self._cached_response = [ _ConvertDiagnosticDataToVimData( x ) - for x in JsonFromFuture( - self._response_future ) ] - return self._cached_response - except Exception as e: - vimsupport.PostVimMessage( str( e ) ) - return [] - - -def _ConvertDiagnosticDataToVimData( diagnostic ): - # see :h getqflist for a description of the dictionary fields - # Note that, as usual, Vim is completely inconsistent about whether - # line/column numbers are 1 or 0 based in its various APIs. Here, it wants - # them to be 1-based. - return { - 'bufnr' : vimsupport.GetBufferNumberForFilename( diagnostic[ 'filepath' ]), - 'lnum' : diagnostic[ 'line_num' ] + 1, - 'col' : diagnostic[ 'column_num' ] + 1, - 'text' : diagnostic[ 'text' ], - 'type' : diagnostic[ 'kind' ], - 'valid' : 1 - } - diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index c39a4f43..2121dbfa 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -17,14 +17,16 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -from ycm.client.base_request import BaseRequest, BuildRequestData - +from ycm import vimsupport +from ycm.client.base_request import ( BaseRequest, BuildRequestData, + JsonFromFuture ) class EventNotification( BaseRequest ): def __init__( self, event_name, extra_data = None ): super( EventNotification, self ).__init__() self._event_name = event_name self._extra_data = extra_data + self._cached_response = None def Start( self ): @@ -33,7 +35,44 @@ class EventNotification( BaseRequest ): request_data.update( self._extra_data ) request_data[ 'event_name' ] = self._event_name - self.PostDataToHandlerAsync( request_data, 'event_notification' ) + self._response_future = self.PostDataToHandlerAsync( request_data, + 'event_notification' ) + + + def Done( self ): + return self._response_future.done() + + + def Response( self ): + if self._cached_response: + return self._cached_response + + if not self._response_future or self._event_name != 'FileReadyToParse': + return [] + + try: + self._cached_response = [ _ConvertDiagnosticDataToVimData( x ) + for x in JsonFromFuture( + self._response_future ) ] + return self._cached_response + except Exception as e: + vimsupport.PostVimMessage( str( e ) ) + return [] + + +def _ConvertDiagnosticDataToVimData( diagnostic ): + # see :h getqflist for a description of the dictionary fields + # Note that, as usual, Vim is completely inconsistent about whether + # line/column numbers are 1 or 0 based in its various APIs. Here, it wants + # them to be 1-based. + return { + 'bufnr' : vimsupport.GetBufferNumberForFilename( diagnostic[ 'filepath' ]), + 'lnum' : diagnostic[ 'line_num' ] + 1, + 'col' : diagnostic[ 'column_num' ] + 1, + 'text' : diagnostic[ 'text' ], + 'type' : diagnostic[ 'kind' ], + 'valid' : 1 + } def SendEventNotificationAsync( event_name, extra_data = None ): diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index e2f48718..a8e5e676 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -201,11 +201,15 @@ class ClangCompleter( Completer ): self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) raise ValueError( NO_COMPILE_FLAGS_MESSAGE ) - self._completer.UpdateTranslationUnit( + diagnostics = self._completer.UpdateTranslationUnit( ToUtf8IfNeeded( filename ), self.GetUnsavedFilesVector( request_data ), flags ) + self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) + return [ ConvertToDiagnosticResponse( x ) for x in + diagnostics[ : self._max_diagnostics_to_display ] ] + def OnBufferUnload( self, request_data ): self._completer.DeleteCachesForFile( @@ -217,15 +221,6 @@ class ClangCompleter( Completer ): ToUtf8IfNeeded( request_data[ 'filepath' ] ) ) - def GetDiagnosticsForCurrentFile( self, request_data ): - filename = request_data[ 'filepath' ] - diagnostics = self._completer.DiagnosticsForFile( - ToUtf8IfNeeded( filename ) ) - self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics ) - return [ ConvertToDiagnosticResponse( x ) for x in - diagnostics[ : self._max_diagnostics_to_display ] ] - - def GetDetailedDiagnostic( self, request_data ): current_line = request_data[ 'line_num' ] + 1 current_column = request_data[ 'column_num' ] + 1 diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 8dc9005e..1f55dd30 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": { "gitcommit": 1, }, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": { "gitcommit": 1 }, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 4f71da82..5ff6a218 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -283,7 +283,7 @@ def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): @with_setup( Setup ) -def GetDiagnostics_ClangCompleter_ZeroBasedLineAndColumn_test(): +def Diagnostics_ClangCompleter_ZeroBasedLineAndColumn_test(): app = TestApp( ycmd.app ) contents = """ struct Foo { @@ -314,8 +314,7 @@ struct Foo { 'event_name': 'FileReadyToParse', } ) - app.post_json( '/event_notification', event_data ) - results = app.post_json( '/diagnostics', diag_data ).json + results = app.post_json( '/event_notification', event_data ).json assert_that( results, contains( has_entries( { 'text': contains_string( "expected ';'" ), diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index ef467058..8183cb89 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -63,9 +63,14 @@ def EventNotification(): getattr( SERVER_STATE.GetGeneralCompleter(), event_handler )( request_data ) filetypes = request_data[ 'filetypes' ] + response_data = None if SERVER_STATE.FiletypeCompletionUsable( filetypes ): - getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), - event_handler )( request_data ) + response_data = getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), + event_handler )( request_data ) + + if response_data: + return _JsonResponse( response_data ) + @app.post( '/run_completer_command' ) @@ -94,16 +99,6 @@ def GetCompletions(): return _JsonResponse( completer.ComputeCandidates( request_data ) ) -@app.post( '/diagnostics' ) -def GetDiagnostics(): - LOGGER.info( 'Received diagnostics request') - request_data = request.json - completer = _GetCompleterForRequestData( request_data ) - - return _JsonResponse( completer.GetDiagnosticsForCurrentFile( - request_data ) ) - - @app.get( '/user_options' ) def GetUserOptions(): LOGGER.info( 'Received user options GET request') diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 9741e741..2f691f72 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -29,8 +29,8 @@ from ycm.completers.general import syntax_parse from ycm.client.base_request import BaseRequest, BuildRequestData from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest -from ycm.client.diagnostics_request import DiagnosticsRequest -from ycm.client.event_notification import SendEventNotificationAsync +from ycm.client.event_notification import ( SendEventNotificationAsync, + EventNotification ) try: from UltiSnips import UltiSnips_Manager @@ -45,7 +45,7 @@ class YouCompleteMe( object ): self._user_options = user_options self._omnicomp = OmniCompleter( user_options ) self._latest_completion_request = None - self._latest_diagnostics_request = None + self._latest_file_parse_request = None self._server_stdout = None self._server_stderr = None self._server_popen = None @@ -147,7 +147,9 @@ class YouCompleteMe( object ): if self._user_options[ 'seed_identifiers_with_syntax' ]: self._AddSyntaxDataIfNeeded( extra_data ) - SendEventNotificationAsync( 'FileReadyToParse', extra_data ) + self._latest_file_parse_request = EventNotification( 'FileReadyToParse', + extra_data ) + self._latest_file_parse_request.Start() def OnBufferUnload( self, deleted_buffer_file ): @@ -176,23 +178,18 @@ class YouCompleteMe( object ): def DiagnosticsForCurrentFileReady( self ): - return bool( self._latest_diagnostics_request and - self._latest_diagnostics_request.Done() ) - - - def RequestDiagnosticsForCurrentFile( self ): - self._latest_diagnostics_request = DiagnosticsRequest() - self._latest_diagnostics_request.Start() + return bool( self._latest_file_parse_request and + self._latest_file_parse_request.Done() ) def GetDiagnosticsFromStoredRequest( self ): - if self._latest_diagnostics_request: - to_return = self._latest_diagnostics_request.Response() + if self._latest_file_parse_request: + to_return = self._latest_file_parse_request.Response() # We set the diagnostics request to None because we want to prevent # Syntastic from repeatedly refreshing the buffer with the same diags. # Setting this to None makes DiagnosticsForCurrentFileReady return False # until the next request is created. - self._latest_diagnostics_request = None + self._latest_file_parse_request = None return to_return return [] From 54255318f18ea6761fd586a40a282878dc05a7dd Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 13:29:04 -0700 Subject: [PATCH 065/149] Changing line endings for settings.json to unix --- python/ycm/server/default_settings.json | 33 ++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 1f55dd30..ff263f0e 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1 +1,32 @@ -{ "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, "semantic_triggers": {}, "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": { "gitcommit": 1 }, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, "auto_stop_csharp_server": 1, "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { "*": "1" }, "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": "1", "qf": "1", "notes": "1", "markdown": "1", "unite": "1", "text": "1" } } \ No newline at end of file +{ + "filepath_completion_use_working_dir": 0, + "min_num_of_chars_for_completion": 2, + "semantic_triggers": {}, + "collect_identifiers_from_comments_and_strings": 0, + "filetype_specific_completion_to_disable": { + "gitcommit": 1 + }, + "collect_identifiers_from_tags_files": 0, + "extra_conf_globlist": [], + "global_ycm_extra_conf": "", + "confirm_extra_conf": 1, + "complete_in_comments": 0, + "complete_in_strings": 1, + "min_num_identifier_candidate_chars": 0, + "max_diagnostics_to_display": 30, + "auto_stop_csharp_server": 1, + "seed_identifiers_with_syntax": 0, + "csharp_server_port": 2000, + "filetype_whitelist": { + "*": "1" + }, + "auto_start_csharp_server": 1, + "filetype_blacklist": { + "tagbar": "1", + "qf": "1", + "notes": "1", + "markdown": "1", + "unite": "1", + "text": "1" + } +} From 62f813367dc72de42eecb630492be90ae07c9bf0 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 14:02:22 -0700 Subject: [PATCH 066/149] settings.json has integers for 1/0, not strings --- python/ycm/server/default_settings.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index ff263f0e..d38d5235 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -18,15 +18,15 @@ "seed_identifiers_with_syntax": 0, "csharp_server_port": 2000, "filetype_whitelist": { - "*": "1" + "*": 1 }, "auto_start_csharp_server": 1, "filetype_blacklist": { - "tagbar": "1", - "qf": "1", - "notes": "1", - "markdown": "1", - "unite": "1", - "text": "1" + "tagbar": 1, + "qf": 1, + "notes": 1, + "markdown": 1, + "unite": 1, + "text": 1 } } From 159a8ecdfa6343a63dcfcd3e651201c73ab7eaa7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 14:17:05 -0700 Subject: [PATCH 067/149] YcmDiags and ForceCompilation work again --- autoload/youcompleteme.vim | 14 +++----------- plugin/youcompleteme.vim | 1 + python/ycm/youcompleteme.py | 7 ------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index f50952d4..507b919d 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -562,6 +562,7 @@ function! youcompleteme#OmniComplete( findstart, base ) endfunction +" TODO: Make this work again function! s:ShowDetailedDiagnostic() py ycm_state.ShowDetailedDiagnostic() endfunction @@ -646,28 +647,19 @@ function! s:ForceCompile() break endif - let getting_completions = pyeval( - \ 'ycm_state.GettingCompletions()' ) - - if !getting_completions - echom "Unable to retrieve diagnostics, see output of `:mes` for possible details." - return 0 - endif - sleep 100m endwhile return 1 endfunction -" TODO: Make this work again. function! s:ForceCompileAndDiagnostics() let compilation_succeeded = s:ForceCompile() if !compilation_succeeded return endif - " call s:UpdateDiagnosticNotifications() + call s:UpdateDiagnosticNotifications() echom "Diagnostics refreshed." endfunction @@ -680,7 +672,7 @@ function! s:ShowDiagnostics() return endif - let diags = pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' ) + let diags = pyeval( 'ycm_state.GetDiagnosticsFromStoredRequest()' ) if !empty( diags ) call setloclist( 0, diags ) lopen diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 939d2dea..4cc56f36 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -77,6 +77,7 @@ let g:ycm_filetype_blacklist = \ get( g:, 'ycm_filetype_blacklist', \ get( g:, 'ycm_filetypes_to_completely_ignore', { \ 'notes' : 1, + \ 'qf': 1, \ 'markdown' : 1, \ 'text' : 1, \ 'unite' : 1, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 2f691f72..66ebd111 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -201,13 +201,6 @@ class YouCompleteMe( object ): pass - # TODO: Make this work again. - def GettingCompletions( self ): - # if self.FiletypeCompletionUsable(): - # return self.GetFiletypeCompleter().GettingCompletions() - return False - - def DebugInfo( self ): debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), 'debug_info' ) From c43327d176a06ff938d370f75d87bff4bc7ad1bf Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 14:49:51 -0700 Subject: [PATCH 068/149] YcmShowDetailedDiagnostic works again --- autoload/youcompleteme.vim | 1 - python/ycm/completers/cpp/clang_completer.py | 6 +-- python/ycm/server/tests/basic_test.py | 46 +++++++++++++++++--- python/ycm/server/ycmd.py | 9 ++++ python/ycm/youcompleteme.py | 10 ++--- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 507b919d..b1554152 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -562,7 +562,6 @@ function! youcompleteme#OmniComplete( findstart, base ) endfunction -" TODO: Make this work again function! s:ShowDetailedDiagnostic() py ycm_state.ShowDetailedDiagnostic() endfunction diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index a8e5e676..c9e6d209 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -227,13 +227,11 @@ class ClangCompleter( Completer ): current_file = request_data[ 'filepath' ] if not self._diagnostic_store: - return responses.BuildDisplayMessageResponse( - NO_DIAGNOSTIC_MESSAGE ) + raise ValueError( NO_DIAGNOSTIC_MESSAGE ) diagnostics = self._diagnostic_store[ current_file ][ current_line ] if not diagnostics: - return responses.BuildDisplayMessageResponse( - NO_DIAGNOSTIC_MESSAGE ) + raise ValueError( NO_DIAGNOSTIC_MESSAGE ) closest_diagnostic = None distance_to_closest_diagnostic = 999 diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 5ff6a218..e09e6449 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -295,7 +295,8 @@ struct Foo { """ filename = '/foo.cpp' - diag_data = { + event_data = { + 'event_name': 'FileReadyToParse', 'compilation_flags': ['-x', 'c++'], 'line_num': 0, 'column_num': 0, @@ -309,17 +310,50 @@ struct Foo { } } + results = app.post_json( '/event_notification', event_data ).json + assert_that( results, + contains( + has_entries( { 'text': contains_string( "expected ';'" ), + 'line_num': 2, + 'column_num': 7 } ) ) ) + + +@with_setup( Setup ) +def GetDetailedDiagnostic_ClangCompleter_Works_test(): + app = TestApp( ycmd.app ) + contents = """ +struct Foo { + int x // semicolon missing here! + int y; + int c; + int d; +}; +""" + + filename = '/foo.cpp' + diag_data = { + 'compilation_flags': ['-x', 'c++'], + 'line_num': 2, + 'column_num': 0, + 'filetypes': ['cpp'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': contents, + 'filetypes': ['cpp'] + } + } + } + event_data = diag_data.copy() event_data.update( { 'event_name': 'FileReadyToParse', } ) - results = app.post_json( '/event_notification', event_data ).json + app.post_json( '/event_notification', event_data ) + results = app.post_json( '/detailed_diagnostic', diag_data ).json assert_that( results, - contains( - has_entries( { 'text': contains_string( "expected ';'" ), - 'line_num': 2, - 'column_num': 7 } ) ) ) + has_entry( 'message', contains_string( "expected ';'" ) ) ) @with_setup( Setup ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 8183cb89..50751b9a 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -133,6 +133,15 @@ def DefinedSubcommands(): return _JsonResponse( completer.DefinedSubcommands() ) +@app.post( '/detailed_diagnostic') +def GetDetailedDiagnostic(): + LOGGER.info( 'Received detailed diagnostic request') + request_data = request.json + completer = _GetCompleterForRequestData( request_data ) + + return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) ) + + @app.post( '/debug_info') def DebugInfo(): # This can't be at the top level because of possible extra conf preload diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 66ebd111..ad46f12b 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -194,11 +194,11 @@ class YouCompleteMe( object ): return [] - # TODO: Make this work again. - def GetDetailedDiagnostic( self ): - # if self.FiletypeCompletionUsable(): - # return self.GetFiletypeCompleter().GetDetailedDiagnostic() - pass + def ShowDetailedDiagnostic( self ): + debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), + 'detailed_diagnostic' ) + if 'message' in debug_info: + vimsupport.EchoText( debug_info[ 'message' ] ) def DebugInfo( self ): From 6b11edb2e2b43c91027babbe0cbac43a468b0f04 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 15:44:53 -0700 Subject: [PATCH 069/149] Fix use-after-free bugs UnsavedFile should now actually be storing the data, not just the pointers to it. --- cpp/ycm/ClangCompleter/ClangUtils.cpp | 7 ++----- cpp/ycm/ClangCompleter/ClangUtils.h | 2 ++ cpp/ycm/ClangCompleter/UnsavedFile.h | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangUtils.cpp b/cpp/ycm/ClangCompleter/ClangUtils.cpp index 30ec2767..cf4445c6 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.cpp +++ b/cpp/ycm/ClangCompleter/ClangUtils.cpp @@ -112,11 +112,8 @@ std::vector< CXUnsavedFile > ToCXUnsavedFiles( std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() ); for ( uint i = 0; i < unsaved_files.size(); ++i ) { - X_VERIFY( unsaved_files[ i ].filename_ ); - X_VERIFY( unsaved_files[ i ].contents_ ); - X_VERIFY( unsaved_files[ i ].length_ ); - clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_; - clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_; + clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_.c_str(); + clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_.c_str(); clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_; } diff --git a/cpp/ycm/ClangCompleter/ClangUtils.h b/cpp/ycm/ClangCompleter/ClangUtils.h index b7dd7a43..088c43b2 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.h +++ b/cpp/ycm/ClangCompleter/ClangUtils.h @@ -37,6 +37,8 @@ std::string CXStringToString( CXString text ); std::vector< CompletionData > ToCompletionDataVector( CXCodeCompleteResults *results ); +// NOTE: CXUnsavedFiles store pointers to data in UnsavedFiles, so UnsavedFiles +// need to outlive CXUnsavedFiles! std::vector< CXUnsavedFile > ToCXUnsavedFiles( const std::vector< UnsavedFile > &unsaved_files ); diff --git a/cpp/ycm/ClangCompleter/UnsavedFile.h b/cpp/ycm/ClangCompleter/UnsavedFile.h index d35041a3..69bcd2ce 100644 --- a/cpp/ycm/ClangCompleter/UnsavedFile.h +++ b/cpp/ycm/ClangCompleter/UnsavedFile.h @@ -18,13 +18,13 @@ #ifndef UNSAVEDFILE_H_0GIYZQL4 #define UNSAVEDFILE_H_0GIYZQL4 -#include +#include struct UnsavedFile { - UnsavedFile() : filename_( NULL ), contents_( NULL ), length_( 0 ) {} + UnsavedFile() : filename_( "" ), contents_( "" ), length_( 0 ) {} - const char *filename_; - const char *contents_; + std::string filename_; + std::string contents_; unsigned long length_; // We need this to be able to export this struct to Python via Boost.Python's From a471ce761f01aaf4d372759d25c183e05a7cbfbb Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 3 Oct 2013 15:51:05 -0700 Subject: [PATCH 070/149] Minor code style changes --- cpp/ycm/ClangCompleter/TranslationUnit.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.cpp b/cpp/ycm/ClangCompleter/TranslationUnit.cpp index 9638c2df..fd01fcc6 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnit.cpp @@ -57,7 +57,7 @@ TranslationUnit::TranslationUnit( std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles( unsaved_files ); const CXUnsavedFile *unsaved = cxunsaved_files.size() > 0 - ? &cxunsaved_files [0] : NULL; + ? &cxunsaved_files[ 0 ] : NULL; clang_translation_unit_ = clang_parseTranslationUnit( clang_index, @@ -146,7 +146,8 @@ std::vector< CompletionData > TranslationUnit::CandidatesForLocation( std::vector< CXUnsavedFile > cxunsaved_files = ToCXUnsavedFiles( unsaved_files ); const CXUnsavedFile *unsaved = cxunsaved_files.size() > 0 - ? &cxunsaved_files [0] : NULL; + ? &cxunsaved_files[ 0 ] : NULL; + // codeCompleteAt reparses the TU if the underlying source file has changed on // disk since the last time the TU was updated and there are no unsaved files. // If there are unsaved files, then codeCompleteAt will parse the in-memory @@ -242,7 +243,8 @@ void TranslationUnit::Reparse( std::vector< CXUnsavedFile > &unsaved_files, if ( !clang_translation_unit_ ) return; CXUnsavedFile *unsaved = unsaved_files.size() > 0 - ? &unsaved_files [0] : NULL; + ? &unsaved_files[ 0 ] : NULL; + failure = clang_reparseTranslationUnit( clang_translation_unit_, unsaved_files.size(), unsaved, From afa1afc49bbdf55d223cc0ab0fb6911c9da01e9a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 10:18:31 -0700 Subject: [PATCH 071/149] Handling no diagnostic data from event response --- python/ycm/client/event_notification.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index 2121dbfa..5f79b4a5 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -51,14 +51,18 @@ class EventNotification( BaseRequest ): return [] try: - self._cached_response = [ _ConvertDiagnosticDataToVimData( x ) - for x in JsonFromFuture( - self._response_future ) ] - return self._cached_response + self._cached_response = JsonFromFuture( self._response_future ) except Exception as e: vimsupport.PostVimMessage( str( e ) ) return [] + if not self._cached_response: + return [] + + self._cached_response = [ _ConvertDiagnosticDataToVimData( x ) + for x in self._cached_response ] + return self._cached_response + def _ConvertDiagnosticDataToVimData( diagnostic ): # see :h getqflist for a description of the dictionary fields From 534f6f57d46bec70a158564c5be6b40f1cd2bd06 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 10:30:58 -0700 Subject: [PATCH 072/149] Fix issue with slow cursor moving in Python This happened when moving the cursor in normal mode. The problem was that we were calling SyntasticCheck on every cursor move because YCM would think that the FileReadyToParse event processing returned diagnostics... but YCM only integrates with Syntastic for C-family files. Fixed by only triggering SyntasticCheck in C-family files. --- autoload/youcompleteme.vim | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index b1554152..48cc8723 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -29,6 +29,14 @@ let s:cursor_moved = 0 let s:moved_vertically_in_insert_mode = 0 let s:previous_num_chars_on_current_line = -1 +let s:forced_syntastic_checker_for = { + \ 'cpp': 1, + \ 'c': 1, + \ 'objc': 1, + \ 'objcpp': 1, + \ } + + function! youcompleteme#Enable() " When vim is in diff mode, don't run if &diff @@ -165,6 +173,12 @@ function! s:ForceSyntasticCFamilyChecker() endfunction +function! s:ForcedAsSyntasticCheckerForCurrentFiletype() + return g:ycm_register_as_syntastic_checker && + \ get( s:forced_syntastic_checker_for, &filetype, 0 ) +endfunction + + function! s:AllowedToCompleteInCurrentFile() if empty( &filetype ) || getbufvar(winbufnr(winnr()), "&buftype") ==# 'nofile' return 0 @@ -411,7 +425,7 @@ endfunction function! s:UpdateDiagnosticNotifications() let should_display_diagnostics = \ get( g:, 'loaded_syntastic_plugin', 0 ) && - \ g:ycm_register_as_syntastic_checker && + \ s:ForcedAsSyntasticCheckerForCurrentFiletype() && \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) if !should_display_diagnostics From 8bc888d71192403808200fadc7fdd61f33569253 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 12:23:33 -0700 Subject: [PATCH 073/149] Vim now loads most defaults from the json file --- autoload/youcompleteme.vim | 6 ++ plugin/youcompleteme.vim | 81 ++----------------------- python/ycm/base.py | 17 ++++-- python/ycm/server/default_settings.json | 16 ++--- python/ycm/user_options_store.py | 4 +- python/ycm/vimsupport.py | 21 +++++++ 6 files changed, 54 insertions(+), 91 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 48cc8723..a9e76d4e 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -51,6 +51,7 @@ function! youcompleteme#Enable() py from ycm import utils py utils.AddThirdPartyFoldersToSysPath() py from ycm import base + py base.LoadJsonDefaultsIntoVim() py from ycm import vimsupport py from ycm import user_options_store py user_options_store.SetAll( base.BuildServerConf() ) @@ -161,6 +162,11 @@ function! s:SetUpBackwardsCompatibility() let g:ycm_complete_in_strings = 1 let g:ycm_complete_in_comments = 1 endif + + " ycm_filetypes_to_completely_ignore is the old name for fileype_blacklist + if has_key( g:, 'ycm_filetypes_to_completely_ignore' ) + let g:filetype_blacklist = g:ycm_filetypes_to_completely_ignore + endif endfunction diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 4cc56f36..d6879287 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -60,32 +60,11 @@ endif let g:loaded_youcompleteme = 1 -let g:ycm_min_num_of_chars_for_completion = - \ get( g:, 'ycm_min_num_of_chars_for_completion', 2 ) - -let g:ycm_min_num_identifier_candidate_chars = - \ get( g:, 'ycm_min_num_identifier_candidate_chars', 0 ) - -let g:ycm_filetype_whitelist = - \ get( g:, 'ycm_filetype_whitelist', { - \ '*' : 1, - \ } ) - -" The fallback to g:ycm_filetypes_to_completely_ignore is here because of -" backwards compatibility with previous versions of YCM. -let g:ycm_filetype_blacklist = - \ get( g:, 'ycm_filetype_blacklist', - \ get( g:, 'ycm_filetypes_to_completely_ignore', { - \ 'notes' : 1, - \ 'qf': 1, - \ 'markdown' : 1, - \ 'text' : 1, - \ 'unite' : 1, - \ 'tagbar' : 1, - \ } ) ) - -let g:ycm_filetype_specific_completion_to_disable = - \ get( g:, 'ycm_filetype_specific_completion_to_disable', {} ) +" NOTE: Most defaults are in default_settings.json. They are loaded into Vim +" global with the 'ycm_' prefix if such a key does not already exist; thus, the +" user can override the defaults. +" The only defaults that are here are the ones that are only relevant to the YCM +" Vim client and not the server. let g:ycm_register_as_syntastic_checker = \ get( g:, 'ycm_register_as_syntastic_checker', 1 ) @@ -96,30 +75,12 @@ let g:ycm_allow_changing_updatetime = let g:ycm_add_preview_to_completeopt = \ get( g:, 'ycm_add_preview_to_completeopt', 0 ) -let g:ycm_complete_in_comments = - \ get( g:, 'ycm_complete_in_comments', 0 ) - -let g:ycm_complete_in_strings = - \ get( g:, 'ycm_complete_in_strings', 1 ) - -let g:ycm_collect_identifiers_from_comments_and_strings = - \ get( g:, 'ycm_collect_identifiers_from_comments_and_strings', 0 ) - -let g:ycm_collect_identifiers_from_tags_files = - \ get( g:, 'ycm_collect_identifiers_from_tags_files', 0 ) - -let g:ycm_seed_identifiers_with_syntax = - \ get( g:, 'ycm_seed_identifiers_with_syntax', 0 ) - let g:ycm_autoclose_preview_window_after_completion = \ get( g:, 'ycm_autoclose_preview_window_after_completion', 0 ) let g:ycm_autoclose_preview_window_after_insertion = \ get( g:, 'ycm_autoclose_preview_window_after_insertion', 0 ) -let g:ycm_max_diagnostics_to_display = - \ get( g:, 'ycm_max_diagnostics_to_display', 30 ) - let g:ycm_key_list_select_completion = \ get( g:, 'ycm_key_list_select_completion', ['', ''] ) @@ -132,41 +93,9 @@ let g:ycm_key_invoke_completion = let g:ycm_key_detailed_diagnostics = \ get( g:, 'ycm_key_detailed_diagnostics', 'd' ) -let g:ycm_global_ycm_extra_conf = - \ get( g:, 'ycm_global_ycm_extra_conf', '' ) - -let g:ycm_confirm_extra_conf = - \ get( g:, 'ycm_confirm_extra_conf', 1 ) - -let g:ycm_extra_conf_globlist = - \ get( g:, 'ycm_extra_conf_globlist', [] ) - -let g:ycm_filepath_completion_use_working_dir = - \ get( g:, 'ycm_filepath_completion_use_working_dir', 0 ) - -" Default semantic triggers are in python/ycm/completers/completer_utils.py -" these just append new triggers to the default dict. -let g:ycm_semantic_triggers = - \ get( g:, 'ycm_semantic_triggers', {} ) - let g:ycm_cache_omnifunc = \ get( g:, 'ycm_cache_omnifunc', 1 ) -let g:ycm_auto_start_csharp_server = - \ get( g:, 'ycm_auto_start_csharp_server', 1 ) - -let g:ycm_auto_stop_csharp_server = - \ get( g:, 'ycm_auto_stop_csharp_server', 1 ) - -let g:ycm_csharp_server_port = - \ get( g:, 'ycm_csharp_server_port', 2000 ) - -let g:ycm_server_use_vim_stdout = - \ get( g:, 'ycm_server_use_vim_stdout', 0 ) - -let g:ycm_server_log_level = - \ get( g:, 'ycm_server_log_level', 'info' ) - " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. augroup youcompletemeStart diff --git a/python/ycm/base.py b/python/ycm/base.py index e87a09cc..555c6c7f 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -22,6 +22,7 @@ import re import vim from ycm import vimsupport from ycm import utils +from ycm import user_options_store YCM_VAR_PREFIX = 'ycm_' @@ -40,12 +41,7 @@ def BuildServerConf(): """Builds a dictionary mapping YCM Vim user options to values. Option names don't have the 'ycm_' prefix.""" - try: - # vim.vars is fairly new so it might not exist - vim_globals = vim.vars - except: - vim_globals = vim.eval( 'g:' ) - + vim_globals = vimsupport.GetReadOnlyVimGlobals() server_conf = {} for key, value in vim_globals.items(): if not key.startswith( YCM_VAR_PREFIX ): @@ -60,6 +56,15 @@ def BuildServerConf(): return server_conf +def LoadJsonDefaultsIntoVim(): + defaults = user_options_store.DefaultOptions() + vim_defaults = {} + for key, value in defaults.iteritems(): + vim_defaults[ 'ycm_' + key ] = value + + vimsupport.LoadDictIntoVimGlobals( vim_defaults, overwrite = False ) + + def CompletionStartColumn(): """Returns the 0-based index where the completion string should start. So if the user enters: diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index d38d5235..73682183 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -1,26 +1,23 @@ { "filepath_completion_use_working_dir": 0, "min_num_of_chars_for_completion": 2, + "min_num_identifier_candidate_chars": 0, "semantic_triggers": {}, - "collect_identifiers_from_comments_and_strings": 0, "filetype_specific_completion_to_disable": { "gitcommit": 1 }, + "seed_identifiers_with_syntax": 0, + "collect_identifiers_from_comments_and_strings": 0, "collect_identifiers_from_tags_files": 0, "extra_conf_globlist": [], "global_ycm_extra_conf": "", "confirm_extra_conf": 1, "complete_in_comments": 0, "complete_in_strings": 1, - "min_num_identifier_candidate_chars": 0, "max_diagnostics_to_display": 30, - "auto_stop_csharp_server": 1, - "seed_identifiers_with_syntax": 0, - "csharp_server_port": 2000, "filetype_whitelist": { "*": 1 }, - "auto_start_csharp_server": 1, "filetype_blacklist": { "tagbar": 1, "qf": 1, @@ -28,5 +25,10 @@ "markdown": 1, "unite": 1, "text": 1 - } + }, + "server_use_vim_stdout": 0, + "server_log_level": "info", + "auto_start_csharp_server": 1, + "auto_stop_csharp_server": 1, + "csharp_server_port": 2000 } diff --git a/python/ycm/user_options_store.py b/python/ycm/user_options_store.py index b453f01e..7a4c8328 100644 --- a/python/ycm/user_options_store.py +++ b/python/ycm/user_options_store.py @@ -37,10 +37,10 @@ def Value( key ): def LoadDefaults(): - SetAll( _DefaultOptions() ) + SetAll( DefaultOptions() ) -def _DefaultOptions(): +def DefaultOptions(): settings_path = os.path.join( os.path.dirname( os.path.abspath( __file__ ) ), 'server/default_settings.json' ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index d46ec868..493804dd 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -19,6 +19,7 @@ import vim import os +import json def CurrentLineAndColumn(): """Returns the 0-based current line and 0-based current column.""" @@ -82,6 +83,26 @@ def GetBufferNumberForFilename( filename, open_file_if_needed = True ): int( open_file_if_needed ) ) ) ) +# Given a dict like {'a': 1}, loads it into Vim as if you ran 'let g:a = 1' +# When |overwrite| is True, overwrites the existing value in Vim. +def LoadDictIntoVimGlobals( new_globals, overwrite = True ): + extend_option = '"force"' if overwrite else '"keep"' + + # We need to use json.dumps because that won't use the 'u' prefix on strings + # which Vim would bork on. + vim.eval( 'extend( g:, {}, {})'.format( json.dumps( new_globals ), + extend_option ) ) + + +# Changing the returned dict will NOT change the value in Vim. +def GetReadOnlyVimGlobals(): + try: + # vim.vars is fairly new so it might not exist + return vim.vars + except: + return vim.eval( 'g:' ) + + # Both |line| and |column| need to be 1-based def JumpToLocation( filename, line, column ): # Add an entry to the jumplist From d7904a7eb16d861ecd9a0b8f6ff473a59a1acf2c Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 12:45:02 -0700 Subject: [PATCH 074/149] Adding the Waitress webserver to third_party --- .gitmodules | 3 +++ third_party/waitress | 1 + 2 files changed, 4 insertions(+) create mode 160000 third_party/waitress diff --git a/.gitmodules b/.gitmodules index ad2f1a83..feae5bb5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "third_party/requests"] path = third_party/requests url = https://github.com/kennethreitz/requests +[submodule "third_party/waitress"] + path = third_party/waitress + url = https://github.com/Pylons/waitress diff --git a/third_party/waitress b/third_party/waitress new file mode 160000 index 00000000..c957f1d7 --- /dev/null +++ b/third_party/waitress @@ -0,0 +1 @@ +Subproject commit c957f1d70ab82ba15ce0dce8fca7cf70fe2f2b97 From 88a260d448fe6e204a6f4f6eb115d34a937d51de Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 12:45:33 -0700 Subject: [PATCH 075/149] ycmd now uses Waitress instead of CherryPy --- python/ycm/server/ycmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 50751b9a..acc7c2f8 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -233,7 +233,7 @@ def Main(): level = numeric_level ) LOGGER = logging.getLogger( __name__ ) - run( app = app, host = args.host, port = args.port, server='cherrypy' ) + run( app = app, host = args.host, port = args.port, server='waitress' ) if __name__ == "__main__": From 6e0f3247e149f83c776425476267d75ccfdf9ed3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 12:48:03 -0700 Subject: [PATCH 076/149] Adding Bottle to third_party --- .gitmodules | 3 +++ third_party/bottle | 1 + 2 files changed, 4 insertions(+) create mode 160000 third_party/bottle diff --git a/.gitmodules b/.gitmodules index feae5bb5..f94366c0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "third_party/waitress"] path = third_party/waitress url = https://github.com/Pylons/waitress +[submodule "third_party/bottle"] + path = third_party/bottle + url = https://github.com/defnull/bottle diff --git a/third_party/bottle b/third_party/bottle new file mode 160000 index 00000000..154369b2 --- /dev/null +++ b/third_party/bottle @@ -0,0 +1 @@ +Subproject commit 154369b2b9f7393ca9d3d73de7e046e2342cf00a From 5902a5e52119a297ee842e8ccb9140e3f11d2ec1 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 13:00:55 -0700 Subject: [PATCH 077/149] frozendict now as submodule in third_party --- .gitmodules | 3 ++ python/ycm/frozendict.py | 54 -------------------------------- python/ycm/user_options_store.py | 2 +- third_party/frozendict | 1 + 4 files changed, 5 insertions(+), 55 deletions(-) delete mode 100644 python/ycm/frozendict.py create mode 160000 third_party/frozendict diff --git a/.gitmodules b/.gitmodules index f94366c0..a0e24625 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "third_party/bottle"] path = third_party/bottle url = https://github.com/defnull/bottle +[submodule "third_party/frozendict"] + path = third_party/frozendict + url = https://github.com/slezica/python-frozendict diff --git a/python/ycm/frozendict.py b/python/ycm/frozendict.py deleted file mode 100644 index ec0aa5ad..00000000 --- a/python/ycm/frozendict.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python -# -# Source: https://github.com/slezica/python-frozendict -# -# Copyright (c) 2012 Santiago Lezica -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -import collections, operator - -class frozendict(collections.Mapping): - - def __init__(self, *args, **kwargs): - self.__dict = dict(*args, **kwargs) - self.__hash = None - - def __getitem__(self, key): - return self.__dict[key] - - def copy(self, **add_or_replace): - new = frozendict(self) - new.__dict.update(add_or_replace) # Feels like cheating - return new - - def __iter__(self): - return iter(self.__dict) - - def __len__(self): - return len(self.__dict) - - def __repr__(self): - return '' % repr(self.__dict) - - def __hash__(self): - if self.__hash is None: - self.__hash = reduce(operator.xor, map(hash, self.iteritems()), 0) - - return self.__hash diff --git a/python/ycm/user_options_store.py b/python/ycm/user_options_store.py index 7a4c8328..fdb422a9 100644 --- a/python/ycm/user_options_store.py +++ b/python/ycm/user_options_store.py @@ -19,7 +19,7 @@ import json import os -from ycm.frozendict import frozendict +from frozendict import frozendict _USER_OPTIONS = {} diff --git a/third_party/frozendict b/third_party/frozendict new file mode 160000 index 00000000..2ea45f3f --- /dev/null +++ b/third_party/frozendict @@ -0,0 +1 @@ +Subproject commit 2ea45f3f429c5283f9f24738812f0ee152a8f4c1 From da723b1425550ad54abc8faa7788b9b095dc44d7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 13:49:10 -0700 Subject: [PATCH 078/149] Moving Jedi under third_party --- .gitmodules | 6 +++--- {python/ycm/completers/python => third_party}/jedi | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename {python/ycm/completers/python => third_party}/jedi (100%) diff --git a/.gitmodules b/.gitmodules index a0e24625..6940b5c7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ -[submodule "python/ycm/completers/python/jedi"] - path = python/ycm/completers/python/jedi - url = https://github.com/davidhalter/jedi.git +[submodule "third_party/jedi"] + path = third_party/jedi + url = https://github.com/davidhalter/jedi [submodule "python/ycm/completers/cs/OmniSharpServer"] path = python/ycm/completers/cs/OmniSharpServer url = https://github.com/nosami/OmniSharpServer.git diff --git a/python/ycm/completers/python/jedi b/third_party/jedi similarity index 100% rename from python/ycm/completers/python/jedi rename to third_party/jedi From 9747bbc26f5bed9a433c86488fcd15e7887a54b0 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 13:50:57 -0700 Subject: [PATCH 079/149] Removing the sys.path changes from jedi_completer This not needed anymore, the correct path to jedi is added to sys.path in ycmd.py --- python/ycm/completers/python/jedi_completer.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/python/ycm/completers/python/jedi_completer.py b/python/ycm/completers/python/jedi_completer.py index f1599276..158b4171 100644 --- a/python/ycm/completers/python/jedi_completer.py +++ b/python/ycm/completers/python/jedi_completer.py @@ -22,20 +22,12 @@ from ycm.completers.completer import Completer from ycm.server import responses -import sys -from os.path import join, abspath, dirname - -# We need to add the jedi package to sys.path, but it's important that we clean -# up after ourselves, because ycm.YouCompletMe.GetFiletypeCompleterForFiletype -# removes sys.path[0] after importing completers.python.hook -sys.path.insert( 0, join( abspath( dirname( __file__ ) ), 'jedi' ) ) try: import jedi except ImportError: raise ImportError( 'Error importing jedi. Make sure the jedi submodule has been checked out. ' 'In the YouCompleteMe folder, run "git submodule update --init --recursive"') -sys.path.pop( 0 ) class JediCompleter( Completer ): From 070d39b2a9c678e63ed1386e96e385c3e83cf744 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 15:21:30 -0700 Subject: [PATCH 080/149] Adding a run_tests script This is now also used by Travis CI. --- .travis.yml | 6 ++-- install.sh | 2 +- python/ycm/server/tests/basic_test.py | 2 +- run_tests.sh | 46 +++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100755 run_tests.sh diff --git a/.travis.yml b/.travis.yml index fcec140b..123041c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ install: compiler: - gcc - clang -script: flake8 --exclude=jedi --select=F,C9 --max-complexity=10 python && ./install.sh && nosetests python +script: ./run_tests.sh env: - - YCM_TESTRUN=1 EXTRA_CMAKE_ARGS="" - - YCM_TESTRUN=1 EXTRA_CMAKE_ARGS="-DUSE_CLANG_COMPLETER=ON" + - USE_CLANG_COMPLETER="true" + - USE_CLANG_COMPLETER="false" diff --git a/install.sh b/install.sh index 273a8478..14b0b62d 100755 --- a/install.sh +++ b/install.sh @@ -138,7 +138,7 @@ fi if [ -z "$YCM_TESTRUN" ]; then install $cmake_args $EXTRA_CMAKE_ARGS else - testrun $cmake_args -DUSE_DEV_FLAGS=ON $EXTRA_CMAKE_ARGS + testrun $cmake_args $EXTRA_CMAKE_ARGS fi if $omnisharp_completer; then diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index e09e6449..1514effb 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -360,7 +360,7 @@ struct Foo { def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) request_data = { - 'filetypes': ['cpp'] + 'filetypes': ['python'] } ok_( app.post_json( '/filetype_completion_available', diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 00000000..a1b47f97 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +set -e + +function usage { + echo "Usage: $0 [--no-clang-completer]" + exit 0 +} + +flake8 --select=F,C9 --max-complexity=10 python + +use_clang_completer=true +for flag in $@; do + case "$flag" in + --no-clang-completer) + use_clang_completer=false + ;; + *) + usage + ;; + esac +done + +if [ -n "$USE_CLANG_COMPLETER" ]; then + use_clang_completer=$USE_CLANG_COMPLETER +fi + +if $use_clang_completer; then + extra_cmake_args="-DUSE_CLANG_COMPLETER=ON -DUSE_DEV_FLAGS=ON" +else + extra_cmake_args="-DUSE_DEV_FLAGS=ON" +fi + +EXTRA_CMAKE_ARGS=$extra_cmake_args YCM_TESTRUN=1 ./install.sh + +for directory in third_party/*; do + if [ -d "${directory}" ]; then + export PYTHONPATH=$(realpath ${directory}):$PYTHONPATH + fi +done + +if $use_clang_completer; then + nosetests -v python +else + nosetests -v --exclude=".*Clang.*" python +fi From ddef46fdbea3b42050b56ec3d2180392db0db498 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 15:34:08 -0700 Subject: [PATCH 081/149] Removing usage of 'realpath' from run_test It's not portable to other systems because it's Debian specific. --- run_tests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_tests.sh b/run_tests.sh index a1b47f97..8f27be7c 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -35,7 +35,7 @@ EXTRA_CMAKE_ARGS=$extra_cmake_args YCM_TESTRUN=1 ./install.sh for directory in third_party/*; do if [ -d "${directory}" ]; then - export PYTHONPATH=$(realpath ${directory}):$PYTHONPATH + export PYTHONPATH=$PWD/${directory}:$PYTHONPATH fi done From cb98dc8537b017cf5a044f41f1837f2123bd7c69 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 15:44:16 -0700 Subject: [PATCH 082/149] Fixing python 2.6 compatibility string.format() requires the number inside '{}' for Python 2.6. --- python/ycm/completers/cpp/clang_completer.py | 2 +- python/ycm/completers/cs/cs_completer.py | 4 ++-- python/ycm/server/server_state.py | 2 +- python/ycm/vimsupport.py | 4 ++-- python/ycm/youcompleteme.py | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index c9e6d209..7f50a7ba 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -33,7 +33,7 @@ NO_COMPILE_FLAGS_MESSAGE = 'Still no compile flags, no completions yet.' INVALID_FILE_MESSAGE = 'File is invalid.' NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?' FILE_TOO_SHORT_MESSAGE = ( - 'File is less than {} lines long; not compiling.'.format( + 'File is less than {0} lines long; not compiling.'.format( MIN_LINES_IN_FILE_TO_PARSE ) ) NO_DIAGNOSTIC_MESSAGE = 'No diagnostic for current line!' diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 5af69592..ac0c7bf6 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -100,7 +100,7 @@ class CsharpCompleter( Completer ): def DebugInfo( self ): if self._ServerIsRunning(): - return 'Server running at: {}\nLogfiles:\n{}\n{}'.format( + return 'Server running at: {0}\nLogfiles:\n{1}\n{2}'.format( self._PortToHost(), self._filename_stdout, self._filename_stderr ) else: return 'Server is not running' @@ -118,7 +118,7 @@ class CsharpCompleter( Completer ): solutionfile = solutionfiles[ 0 ] else: raise RuntimeError( - 'Found multiple solution files instead of one!\n{}'.format( + 'Found multiple solution files instead of one!\n{0}'.format( solutionfiles ) ) omnisharp = os.path.join( diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index afcf6c32..611b062c 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -74,7 +74,7 @@ class ServerState( object ): if completer: return completer - raise ValueError( 'No semantic completer exists for filetypes: {}'.format( + raise ValueError( 'No semantic completer exists for filetypes: {0}'.format( current_filetypes ) ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 493804dd..dacf3a4e 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -90,8 +90,8 @@ def LoadDictIntoVimGlobals( new_globals, overwrite = True ): # We need to use json.dumps because that won't use the 'u' prefix on strings # which Vim would bork on. - vim.eval( 'extend( g:, {}, {})'.format( json.dumps( new_globals ), - extend_option ) ) + vim.eval( 'extend( g:, {0}, {1})'.format( json.dumps( new_globals ), + extend_option ) ) # Changing the returned dict will NOT change the value in Vim. diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index ad46f12b..f63c4872 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -204,10 +204,10 @@ class YouCompleteMe( object ): def DebugInfo( self ): debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), 'debug_info' ) - debug_info += '\nServer running at: {}'.format( + debug_info += '\nServer running at: {0}'.format( BaseRequest.server_location ) if self._server_stderr or self._server_stdout: - debug_info += '\nServer logfiles:\n {}\n {}'.format( + debug_info += '\nServer logfiles:\n {0}\n {1}'.format( self._server_stdout, self._server_stderr ) From 8856af0970f4c79eefef330a5e058b32890f15be Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 15:58:30 -0700 Subject: [PATCH 083/149] Adding argparse to third_party (for Python 2.6) argparse is in the Python stdlib for Python 2.7+, but not 2.6 which we try to support. --- .gitmodules | 3 +++ third_party/argparse | 1 + 2 files changed, 4 insertions(+) create mode 160000 third_party/argparse diff --git a/.gitmodules b/.gitmodules index 6940b5c7..7dfea5ba 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "third_party/frozendict"] path = third_party/frozendict url = https://github.com/slezica/python-frozendict +[submodule "third_party/argparse"] + path = third_party/argparse + url = https://github.com/bewest/argparse diff --git a/third_party/argparse b/third_party/argparse new file mode 160000 index 00000000..46af816d --- /dev/null +++ b/third_party/argparse @@ -0,0 +1 @@ +Subproject commit 46af816db4812eab5f4639717bf1ad2eb17cc1ff From 60aa5581c1b610c8d0250fe97dea0c1505f51966 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 18:24:52 -0700 Subject: [PATCH 084/149] Adding python-futures to third_party Download source: https://pythonfutures.googlecode.com/archive/05e0c9c1b3d493f0c7e5833723a1ea99d024bad4.zip --- third_party/pythonfutures/CHANGES | 44 ++ third_party/pythonfutures/LICENSE | 21 + .../pythonfutures/concurrent/__init__.py | 3 + .../concurrent/futures/__init__.py | 18 + .../pythonfutures/concurrent/futures/_base.py | 574 ++++++++++++++ .../concurrent/futures/_compat.py | 101 +++ .../concurrent/futures/process.py | 363 +++++++++ .../concurrent/futures/thread.py | 138 ++++ third_party/pythonfutures/crawl.py | 74 ++ third_party/pythonfutures/docs/conf.py | 194 +++++ third_party/pythonfutures/docs/index.rst | 345 +++++++++ third_party/pythonfutures/docs/make.bat | 112 +++ third_party/pythonfutures/futures/__init__.py | 24 + third_party/pythonfutures/futures/process.py | 1 + third_party/pythonfutures/futures/thread.py | 1 + third_party/pythonfutures/primes.py | 50 ++ third_party/pythonfutures/setup.cfg | 6 + third_party/pythonfutures/setup.py | 33 + third_party/pythonfutures/test_futures.py | 723 ++++++++++++++++++ third_party/pythonfutures/tox.ini | 8 + 20 files changed, 2833 insertions(+) create mode 100755 third_party/pythonfutures/CHANGES create mode 100755 third_party/pythonfutures/LICENSE create mode 100755 third_party/pythonfutures/concurrent/__init__.py create mode 100755 third_party/pythonfutures/concurrent/futures/__init__.py create mode 100755 third_party/pythonfutures/concurrent/futures/_base.py create mode 100755 third_party/pythonfutures/concurrent/futures/_compat.py create mode 100755 third_party/pythonfutures/concurrent/futures/process.py create mode 100755 third_party/pythonfutures/concurrent/futures/thread.py create mode 100755 third_party/pythonfutures/crawl.py create mode 100755 third_party/pythonfutures/docs/conf.py create mode 100755 third_party/pythonfutures/docs/index.rst create mode 100755 third_party/pythonfutures/docs/make.bat create mode 100755 third_party/pythonfutures/futures/__init__.py create mode 100755 third_party/pythonfutures/futures/process.py create mode 100755 third_party/pythonfutures/futures/thread.py create mode 100755 third_party/pythonfutures/primes.py create mode 100755 third_party/pythonfutures/setup.cfg create mode 100755 third_party/pythonfutures/setup.py create mode 100755 third_party/pythonfutures/test_futures.py create mode 100755 third_party/pythonfutures/tox.ini diff --git a/third_party/pythonfutures/CHANGES b/third_party/pythonfutures/CHANGES new file mode 100755 index 00000000..81df636f --- /dev/null +++ b/third_party/pythonfutures/CHANGES @@ -0,0 +1,44 @@ +2.1.4 +===== + +- Ported the library again from Python 3.2.5 to get the latest bug fixes + + +2.1.3 +===== + +- Fixed race condition in wait(return_when=ALL_COMPLETED) + (http://bugs.python.org/issue14406) -- thanks Ralf Schmitt +- Added missing setUp() methods to several test classes + + +2.1.2 +===== + +- Fixed installation problem on Python 3.1 + + +2.1.1 +===== + +- Fixed missing 'concurrent' package declaration in setup.py + + +2.1 +=== + +- Moved the code from the 'futures' package to 'concurrent.futures' to provide + a drop in backport that matches the code in Python 3.2 standard library +- Deprecated the old 'futures' package + + +2.0 +=== + +- Changed implementation to match PEP 3148 + + +1.0 +=== + +Initial release. diff --git a/third_party/pythonfutures/LICENSE b/third_party/pythonfutures/LICENSE new file mode 100755 index 00000000..c430db0f --- /dev/null +++ b/third_party/pythonfutures/LICENSE @@ -0,0 +1,21 @@ +Copyright 2009 Brian Quinlan. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY BRIAN QUINLAN "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +HALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/third_party/pythonfutures/concurrent/__init__.py b/third_party/pythonfutures/concurrent/__init__.py new file mode 100755 index 00000000..b36383a6 --- /dev/null +++ b/third_party/pythonfutures/concurrent/__init__.py @@ -0,0 +1,3 @@ +from pkgutil import extend_path + +__path__ = extend_path(__path__, __name__) diff --git a/third_party/pythonfutures/concurrent/futures/__init__.py b/third_party/pythonfutures/concurrent/futures/__init__.py new file mode 100755 index 00000000..b5231f8a --- /dev/null +++ b/third_party/pythonfutures/concurrent/futures/__init__.py @@ -0,0 +1,18 @@ +# Copyright 2009 Brian Quinlan. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +"""Execute computations asynchronously using threads or processes.""" + +__author__ = 'Brian Quinlan (brian@sweetapp.com)' + +from concurrent.futures._base import (FIRST_COMPLETED, + FIRST_EXCEPTION, + ALL_COMPLETED, + CancelledError, + TimeoutError, + Future, + Executor, + wait, + as_completed) +from concurrent.futures.process import ProcessPoolExecutor +from concurrent.futures.thread import ThreadPoolExecutor diff --git a/third_party/pythonfutures/concurrent/futures/_base.py b/third_party/pythonfutures/concurrent/futures/_base.py new file mode 100755 index 00000000..8ed69b7d --- /dev/null +++ b/third_party/pythonfutures/concurrent/futures/_base.py @@ -0,0 +1,574 @@ +# Copyright 2009 Brian Quinlan. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +from __future__ import with_statement +import logging +import threading +import time + +try: + from collections import namedtuple +except ImportError: + from concurrent.futures._compat import namedtuple + +__author__ = 'Brian Quinlan (brian@sweetapp.com)' + +FIRST_COMPLETED = 'FIRST_COMPLETED' +FIRST_EXCEPTION = 'FIRST_EXCEPTION' +ALL_COMPLETED = 'ALL_COMPLETED' +_AS_COMPLETED = '_AS_COMPLETED' + +# Possible future states (for internal use by the futures package). +PENDING = 'PENDING' +RUNNING = 'RUNNING' +# The future was cancelled by the user... +CANCELLED = 'CANCELLED' +# ...and _Waiter.add_cancelled() was called by a worker. +CANCELLED_AND_NOTIFIED = 'CANCELLED_AND_NOTIFIED' +FINISHED = 'FINISHED' + +_FUTURE_STATES = [ + PENDING, + RUNNING, + CANCELLED, + CANCELLED_AND_NOTIFIED, + FINISHED +] + +_STATE_TO_DESCRIPTION_MAP = { + PENDING: "pending", + RUNNING: "running", + CANCELLED: "cancelled", + CANCELLED_AND_NOTIFIED: "cancelled", + FINISHED: "finished" +} + +# Logger for internal use by the futures package. +LOGGER = logging.getLogger("concurrent.futures") + +class Error(Exception): + """Base class for all future-related exceptions.""" + pass + +class CancelledError(Error): + """The Future was cancelled.""" + pass + +class TimeoutError(Error): + """The operation exceeded the given deadline.""" + pass + +class _Waiter(object): + """Provides the event that wait() and as_completed() block on.""" + def __init__(self): + self.event = threading.Event() + self.finished_futures = [] + + def add_result(self, future): + self.finished_futures.append(future) + + def add_exception(self, future): + self.finished_futures.append(future) + + def add_cancelled(self, future): + self.finished_futures.append(future) + +class _AsCompletedWaiter(_Waiter): + """Used by as_completed().""" + + def __init__(self): + super(_AsCompletedWaiter, self).__init__() + self.lock = threading.Lock() + + def add_result(self, future): + with self.lock: + super(_AsCompletedWaiter, self).add_result(future) + self.event.set() + + def add_exception(self, future): + with self.lock: + super(_AsCompletedWaiter, self).add_exception(future) + self.event.set() + + def add_cancelled(self, future): + with self.lock: + super(_AsCompletedWaiter, self).add_cancelled(future) + self.event.set() + +class _FirstCompletedWaiter(_Waiter): + """Used by wait(return_when=FIRST_COMPLETED).""" + + def add_result(self, future): + super(_FirstCompletedWaiter, self).add_result(future) + self.event.set() + + def add_exception(self, future): + super(_FirstCompletedWaiter, self).add_exception(future) + self.event.set() + + def add_cancelled(self, future): + super(_FirstCompletedWaiter, self).add_cancelled(future) + self.event.set() + +class _AllCompletedWaiter(_Waiter): + """Used by wait(return_when=FIRST_EXCEPTION and ALL_COMPLETED).""" + + def __init__(self, num_pending_calls, stop_on_exception): + self.num_pending_calls = num_pending_calls + self.stop_on_exception = stop_on_exception + self.lock = threading.Lock() + super(_AllCompletedWaiter, self).__init__() + + def _decrement_pending_calls(self): + with self.lock: + self.num_pending_calls -= 1 + if not self.num_pending_calls: + self.event.set() + + def add_result(self, future): + super(_AllCompletedWaiter, self).add_result(future) + self._decrement_pending_calls() + + def add_exception(self, future): + super(_AllCompletedWaiter, self).add_exception(future) + if self.stop_on_exception: + self.event.set() + else: + self._decrement_pending_calls() + + def add_cancelled(self, future): + super(_AllCompletedWaiter, self).add_cancelled(future) + self._decrement_pending_calls() + +class _AcquireFutures(object): + """A context manager that does an ordered acquire of Future conditions.""" + + def __init__(self, futures): + self.futures = sorted(futures, key=id) + + def __enter__(self): + for future in self.futures: + future._condition.acquire() + + def __exit__(self, *args): + for future in self.futures: + future._condition.release() + +def _create_and_install_waiters(fs, return_when): + if return_when == _AS_COMPLETED: + waiter = _AsCompletedWaiter() + elif return_when == FIRST_COMPLETED: + waiter = _FirstCompletedWaiter() + else: + pending_count = sum( + f._state not in [CANCELLED_AND_NOTIFIED, FINISHED] for f in fs) + + if return_when == FIRST_EXCEPTION: + waiter = _AllCompletedWaiter(pending_count, stop_on_exception=True) + elif return_when == ALL_COMPLETED: + waiter = _AllCompletedWaiter(pending_count, stop_on_exception=False) + else: + raise ValueError("Invalid return condition: %r" % return_when) + + for f in fs: + f._waiters.append(waiter) + + return waiter + +def as_completed(fs, timeout=None): + """An iterator over the given futures that yields each as it completes. + + Args: + fs: The sequence of Futures (possibly created by different Executors) to + iterate over. + timeout: The maximum number of seconds to wait. If None, then there + is no limit on the wait time. + + Returns: + An iterator that yields the given Futures as they complete (finished or + cancelled). + + Raises: + TimeoutError: If the entire result iterator could not be generated + before the given timeout. + """ + if timeout is not None: + end_time = timeout + time.time() + + with _AcquireFutures(fs): + finished = set( + f for f in fs + if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) + pending = set(fs) - finished + waiter = _create_and_install_waiters(fs, _AS_COMPLETED) + + try: + for future in finished: + yield future + + while pending: + if timeout is None: + wait_timeout = None + else: + wait_timeout = end_time - time.time() + if wait_timeout < 0: + raise TimeoutError( + '%d (of %d) futures unfinished' % ( + len(pending), len(fs))) + + waiter.event.wait(wait_timeout) + + with waiter.lock: + finished = waiter.finished_futures + waiter.finished_futures = [] + waiter.event.clear() + + for future in finished: + yield future + pending.remove(future) + + finally: + for f in fs: + f._waiters.remove(waiter) + +DoneAndNotDoneFutures = namedtuple( + 'DoneAndNotDoneFutures', 'done not_done') +def wait(fs, timeout=None, return_when=ALL_COMPLETED): + """Wait for the futures in the given sequence to complete. + + Args: + fs: The sequence of Futures (possibly created by different Executors) to + wait upon. + timeout: The maximum number of seconds to wait. If None, then there + is no limit on the wait time. + return_when: Indicates when this function should return. The options + are: + + FIRST_COMPLETED - Return when any future finishes or is + cancelled. + FIRST_EXCEPTION - Return when any future finishes by raising an + exception. If no future raises an exception + then it is equivalent to ALL_COMPLETED. + ALL_COMPLETED - Return when all futures finish or are cancelled. + + Returns: + A named 2-tuple of sets. The first set, named 'done', contains the + futures that completed (is finished or cancelled) before the wait + completed. The second set, named 'not_done', contains uncompleted + futures. + """ + with _AcquireFutures(fs): + done = set(f for f in fs + if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) + not_done = set(fs) - done + + if (return_when == FIRST_COMPLETED) and done: + return DoneAndNotDoneFutures(done, not_done) + elif (return_when == FIRST_EXCEPTION) and done: + if any(f for f in done + if not f.cancelled() and f.exception() is not None): + return DoneAndNotDoneFutures(done, not_done) + + if len(done) == len(fs): + return DoneAndNotDoneFutures(done, not_done) + + waiter = _create_and_install_waiters(fs, return_when) + + waiter.event.wait(timeout) + for f in fs: + f._waiters.remove(waiter) + + done.update(waiter.finished_futures) + return DoneAndNotDoneFutures(done, set(fs) - done) + +class Future(object): + """Represents the result of an asynchronous computation.""" + + def __init__(self): + """Initializes the future. Should not be called by clients.""" + self._condition = threading.Condition() + self._state = PENDING + self._result = None + self._exception = None + self._waiters = [] + self._done_callbacks = [] + + def _invoke_callbacks(self): + for callback in self._done_callbacks: + try: + callback(self) + except Exception: + LOGGER.exception('exception calling callback for %r', self) + + def __repr__(self): + with self._condition: + if self._state == FINISHED: + if self._exception: + return '' % ( + hex(id(self)), + _STATE_TO_DESCRIPTION_MAP[self._state], + self._exception.__class__.__name__) + else: + return '' % ( + hex(id(self)), + _STATE_TO_DESCRIPTION_MAP[self._state], + self._result.__class__.__name__) + return '' % ( + hex(id(self)), + _STATE_TO_DESCRIPTION_MAP[self._state]) + + def cancel(self): + """Cancel the future if possible. + + Returns True if the future was cancelled, False otherwise. A future + cannot be cancelled if it is running or has already completed. + """ + with self._condition: + if self._state in [RUNNING, FINISHED]: + return False + + if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: + return True + + self._state = CANCELLED + self._condition.notify_all() + + self._invoke_callbacks() + return True + + def cancelled(self): + """Return True if the future has cancelled.""" + with self._condition: + return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED] + + def running(self): + """Return True if the future is currently executing.""" + with self._condition: + return self._state == RUNNING + + def done(self): + """Return True of the future was cancelled or finished executing.""" + with self._condition: + return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED] + + def __get_result(self): + if self._exception: + raise self._exception + else: + return self._result + + def add_done_callback(self, fn): + """Attaches a callable that will be called when the future finishes. + + Args: + fn: A callable that will be called with this future as its only + argument when the future completes or is cancelled. The callable + will always be called by a thread in the same process in which + it was added. If the future has already completed or been + cancelled then the callable will be called immediately. These + callables are called in the order that they were added. + """ + with self._condition: + if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]: + self._done_callbacks.append(fn) + return + fn(self) + + def result(self, timeout=None): + """Return the result of the call that the future represents. + + Args: + timeout: The number of seconds to wait for the result if the future + isn't done. If None, then there is no limit on the wait time. + + Returns: + The result of the call that the future represents. + + Raises: + CancelledError: If the future was cancelled. + TimeoutError: If the future didn't finish executing before the given + timeout. + Exception: If the call raised then that exception will be raised. + """ + with self._condition: + if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: + raise CancelledError() + elif self._state == FINISHED: + return self.__get_result() + + self._condition.wait(timeout) + + if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: + raise CancelledError() + elif self._state == FINISHED: + return self.__get_result() + else: + raise TimeoutError() + + def exception(self, timeout=None): + """Return the exception raised by the call that the future represents. + + Args: + timeout: The number of seconds to wait for the exception if the + future isn't done. If None, then there is no limit on the wait + time. + + Returns: + The exception raised by the call that the future represents or None + if the call completed without raising. + + Raises: + CancelledError: If the future was cancelled. + TimeoutError: If the future didn't finish executing before the given + timeout. + """ + + with self._condition: + if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: + raise CancelledError() + elif self._state == FINISHED: + return self._exception + + self._condition.wait(timeout) + + if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: + raise CancelledError() + elif self._state == FINISHED: + return self._exception + else: + raise TimeoutError() + + # The following methods should only be used by Executors and in tests. + def set_running_or_notify_cancel(self): + """Mark the future as running or process any cancel notifications. + + Should only be used by Executor implementations and unit tests. + + If the future has been cancelled (cancel() was called and returned + True) then any threads waiting on the future completing (though calls + to as_completed() or wait()) are notified and False is returned. + + If the future was not cancelled then it is put in the running state + (future calls to running() will return True) and True is returned. + + This method should be called by Executor implementations before + executing the work associated with this future. If this method returns + False then the work should not be executed. + + Returns: + False if the Future was cancelled, True otherwise. + + Raises: + RuntimeError: if this method was already called or if set_result() + or set_exception() was called. + """ + with self._condition: + if self._state == CANCELLED: + self._state = CANCELLED_AND_NOTIFIED + for waiter in self._waiters: + waiter.add_cancelled(self) + # self._condition.notify_all() is not necessary because + # self.cancel() triggers a notification. + return False + elif self._state == PENDING: + self._state = RUNNING + return True + else: + LOGGER.critical('Future %s in unexpected state: %s', + id(self.future), + self.future._state) + raise RuntimeError('Future in unexpected state') + + def set_result(self, result): + """Sets the return value of work associated with the future. + + Should only be used by Executor implementations and unit tests. + """ + with self._condition: + self._result = result + self._state = FINISHED + for waiter in self._waiters: + waiter.add_result(self) + self._condition.notify_all() + self._invoke_callbacks() + + def set_exception(self, exception): + """Sets the result of the future as being the given exception. + + Should only be used by Executor implementations and unit tests. + """ + with self._condition: + self._exception = exception + self._state = FINISHED + for waiter in self._waiters: + waiter.add_exception(self) + self._condition.notify_all() + self._invoke_callbacks() + +class Executor(object): + """This is an abstract base class for concrete asynchronous executors.""" + + def submit(self, fn, *args, **kwargs): + """Submits a callable to be executed with the given arguments. + + Schedules the callable to be executed as fn(*args, **kwargs) and returns + a Future instance representing the execution of the callable. + + Returns: + A Future representing the given call. + """ + raise NotImplementedError() + + def map(self, fn, *iterables, **kwargs): + """Returns a iterator equivalent to map(fn, iter). + + Args: + fn: A callable that will take as many arguments as there are + passed iterables. + timeout: The maximum number of seconds to wait. If None, then there + is no limit on the wait time. + + Returns: + An iterator equivalent to: map(func, *iterables) but the calls may + be evaluated out-of-order. + + Raises: + TimeoutError: If the entire result iterator could not be generated + before the given timeout. + Exception: If fn(*args) raises for any values. + """ + timeout = kwargs.get('timeout') + if timeout is not None: + end_time = timeout + time.time() + + fs = [self.submit(fn, *args) for args in zip(*iterables)] + + try: + for future in fs: + if timeout is None: + yield future.result() + else: + yield future.result(end_time - time.time()) + finally: + for future in fs: + future.cancel() + + def shutdown(self, wait=True): + """Clean-up the resources associated with the Executor. + + It is safe to call this method several times. Otherwise, no other + methods can be called after this one. + + Args: + wait: If True then shutdown will not return until all running + futures have finished executing and the resources used by the + executor have been reclaimed. + """ + pass + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.shutdown(wait=True) + return False diff --git a/third_party/pythonfutures/concurrent/futures/_compat.py b/third_party/pythonfutures/concurrent/futures/_compat.py new file mode 100755 index 00000000..11462326 --- /dev/null +++ b/third_party/pythonfutures/concurrent/futures/_compat.py @@ -0,0 +1,101 @@ +from keyword import iskeyword as _iskeyword +from operator import itemgetter as _itemgetter +import sys as _sys + + +def namedtuple(typename, field_names): + """Returns a new subclass of tuple with named fields. + + >>> Point = namedtuple('Point', 'x y') + >>> Point.__doc__ # docstring for the new class + 'Point(x, y)' + >>> p = Point(11, y=22) # instantiate with positional args or keywords + >>> p[0] + p[1] # indexable like a plain tuple + 33 + >>> x, y = p # unpack like a regular tuple + >>> x, y + (11, 22) + >>> p.x + p.y # fields also accessable by name + 33 + >>> d = p._asdict() # convert to a dictionary + >>> d['x'] + 11 + >>> Point(**d) # convert from a dictionary + Point(x=11, y=22) + >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields + Point(x=100, y=22) + + """ + + # Parse and validate the field names. Validation serves two purposes, + # generating informative error messages and preventing template injection attacks. + if isinstance(field_names, basestring): + field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas + field_names = tuple(map(str, field_names)) + for name in (typename,) + field_names: + if not all(c.isalnum() or c=='_' for c in name): + raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name) + if _iskeyword(name): + raise ValueError('Type names and field names cannot be a keyword: %r' % name) + if name[0].isdigit(): + raise ValueError('Type names and field names cannot start with a number: %r' % name) + seen_names = set() + for name in field_names: + if name.startswith('_'): + raise ValueError('Field names cannot start with an underscore: %r' % name) + if name in seen_names: + raise ValueError('Encountered duplicate field name: %r' % name) + seen_names.add(name) + + # Create and fill-in the class template + numfields = len(field_names) + argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes + reprtxt = ', '.join('%s=%%r' % name for name in field_names) + dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) + template = '''class %(typename)s(tuple): + '%(typename)s(%(argtxt)s)' \n + __slots__ = () \n + _fields = %(field_names)r \n + def __new__(_cls, %(argtxt)s): + return _tuple.__new__(_cls, (%(argtxt)s)) \n + @classmethod + def _make(cls, iterable, new=tuple.__new__, len=len): + 'Make a new %(typename)s object from a sequence or iterable' + result = new(cls, iterable) + if len(result) != %(numfields)d: + raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result)) + return result \n + def __repr__(self): + return '%(typename)s(%(reprtxt)s)' %% self \n + def _asdict(t): + 'Return a new dict which maps field names to their values' + return {%(dicttxt)s} \n + def _replace(_self, **kwds): + 'Return a new %(typename)s object replacing specified fields with new values' + result = _self._make(map(kwds.pop, %(field_names)r, _self)) + if kwds: + raise ValueError('Got unexpected field names: %%r' %% kwds.keys()) + return result \n + def __getnewargs__(self): + return tuple(self) \n\n''' % locals() + for i, name in enumerate(field_names): + template += ' %s = _property(_itemgetter(%d))\n' % (name, i) + + # Execute the template string in a temporary namespace and + # support tracing utilities by setting a value for frame.f_globals['__name__'] + namespace = dict(_itemgetter=_itemgetter, __name__='namedtuple_%s' % typename, + _property=property, _tuple=tuple) + try: + exec(template, namespace) + except SyntaxError: + e = _sys.exc_info()[1] + raise SyntaxError(e.message + ':\n' + template) + result = namespace[typename] + + # For pickling to work, the __module__ variable needs to be set to the frame + # where the named tuple is created. Bypass this step in enviroments where + # sys._getframe is not defined (Jython for example). + if hasattr(_sys, '_getframe'): + result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__') + + return result diff --git a/third_party/pythonfutures/concurrent/futures/process.py b/third_party/pythonfutures/concurrent/futures/process.py new file mode 100755 index 00000000..98684f8e --- /dev/null +++ b/third_party/pythonfutures/concurrent/futures/process.py @@ -0,0 +1,363 @@ +# Copyright 2009 Brian Quinlan. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +"""Implements ProcessPoolExecutor. + +The follow diagram and text describe the data-flow through the system: + +|======================= In-process =====================|== Out-of-process ==| + ++----------+ +----------+ +--------+ +-----------+ +---------+ +| | => | Work Ids | => | | => | Call Q | => | | +| | +----------+ | | +-----------+ | | +| | | ... | | | | ... | | | +| | | 6 | | | | 5, call() | | | +| | | 7 | | | | ... | | | +| Process | | ... | | Local | +-----------+ | Process | +| Pool | +----------+ | Worker | | #1..n | +| Executor | | Thread | | | +| | +----------- + | | +-----------+ | | +| | <=> | Work Items | <=> | | <= | Result Q | <= | | +| | +------------+ | | +-----------+ | | +| | | 6: call() | | | | ... | | | +| | | future | | | | 4, result | | | +| | | ... | | | | 3, except | | | ++----------+ +------------+ +--------+ +-----------+ +---------+ + +Executor.submit() called: +- creates a uniquely numbered _WorkItem and adds it to the "Work Items" dict +- adds the id of the _WorkItem to the "Work Ids" queue + +Local worker thread: +- reads work ids from the "Work Ids" queue and looks up the corresponding + WorkItem from the "Work Items" dict: if the work item has been cancelled then + it is simply removed from the dict, otherwise it is repackaged as a + _CallItem and put in the "Call Q". New _CallItems are put in the "Call Q" + until "Call Q" is full. NOTE: the size of the "Call Q" is kept small because + calls placed in the "Call Q" can no longer be cancelled with Future.cancel(). +- reads _ResultItems from "Result Q", updates the future stored in the + "Work Items" dict and deletes the dict entry + +Process #1..n: +- reads _CallItems from "Call Q", executes the calls, and puts the resulting + _ResultItems in "Request Q" +""" + +from __future__ import with_statement +import atexit +import multiprocessing +import threading +import weakref +import sys + +from concurrent.futures import _base + +try: + import queue +except ImportError: + import Queue as queue + +__author__ = 'Brian Quinlan (brian@sweetapp.com)' + +# Workers are created as daemon threads and processes. This is done to allow the +# interpreter to exit when there are still idle processes in a +# ProcessPoolExecutor's process pool (i.e. shutdown() was not called). However, +# allowing workers to die with the interpreter has two undesirable properties: +# - The workers would still be running during interpretor shutdown, +# meaning that they would fail in unpredictable ways. +# - The workers could be killed while evaluating a work item, which could +# be bad if the callable being evaluated has external side-effects e.g. +# writing to a file. +# +# To work around this problem, an exit handler is installed which tells the +# workers to exit when their work queues are empty and then waits until the +# threads/processes finish. + +_threads_queues = weakref.WeakKeyDictionary() +_shutdown = False + +def _python_exit(): + global _shutdown + _shutdown = True + items = list(_threads_queues.items()) + for t, q in items: + q.put(None) + for t, q in items: + t.join() + +# Controls how many more calls than processes will be queued in the call queue. +# A smaller number will mean that processes spend more time idle waiting for +# work while a larger number will make Future.cancel() succeed less frequently +# (Futures in the call queue cannot be cancelled). +EXTRA_QUEUED_CALLS = 1 + +class _WorkItem(object): + def __init__(self, future, fn, args, kwargs): + self.future = future + self.fn = fn + self.args = args + self.kwargs = kwargs + +class _ResultItem(object): + def __init__(self, work_id, exception=None, result=None): + self.work_id = work_id + self.exception = exception + self.result = result + +class _CallItem(object): + def __init__(self, work_id, fn, args, kwargs): + self.work_id = work_id + self.fn = fn + self.args = args + self.kwargs = kwargs + +def _process_worker(call_queue, result_queue): + """Evaluates calls from call_queue and places the results in result_queue. + + This worker is run in a separate process. + + Args: + call_queue: A multiprocessing.Queue of _CallItems that will be read and + evaluated by the worker. + result_queue: A multiprocessing.Queue of _ResultItems that will written + to by the worker. + shutdown: A multiprocessing.Event that will be set as a signal to the + worker that it should exit when call_queue is empty. + """ + while True: + call_item = call_queue.get(block=True) + if call_item is None: + # Wake up queue management thread + result_queue.put(None) + return + try: + r = call_item.fn(*call_item.args, **call_item.kwargs) + except BaseException: + e = sys.exc_info()[1] + result_queue.put(_ResultItem(call_item.work_id, + exception=e)) + else: + result_queue.put(_ResultItem(call_item.work_id, + result=r)) + +def _add_call_item_to_queue(pending_work_items, + work_ids, + call_queue): + """Fills call_queue with _WorkItems from pending_work_items. + + This function never blocks. + + Args: + pending_work_items: A dict mapping work ids to _WorkItems e.g. + {5: <_WorkItem...>, 6: <_WorkItem...>, ...} + work_ids: A queue.Queue of work ids e.g. Queue([5, 6, ...]). Work ids + are consumed and the corresponding _WorkItems from + pending_work_items are transformed into _CallItems and put in + call_queue. + call_queue: A multiprocessing.Queue that will be filled with _CallItems + derived from _WorkItems. + """ + while True: + if call_queue.full(): + return + try: + work_id = work_ids.get(block=False) + except queue.Empty: + return + else: + work_item = pending_work_items[work_id] + + if work_item.future.set_running_or_notify_cancel(): + call_queue.put(_CallItem(work_id, + work_item.fn, + work_item.args, + work_item.kwargs), + block=True) + else: + del pending_work_items[work_id] + continue + +def _queue_management_worker(executor_reference, + processes, + pending_work_items, + work_ids_queue, + call_queue, + result_queue): + """Manages the communication between this process and the worker processes. + + This function is run in a local thread. + + Args: + executor_reference: A weakref.ref to the ProcessPoolExecutor that owns + this thread. Used to determine if the ProcessPoolExecutor has been + garbage collected and that this function can exit. + process: A list of the multiprocessing.Process instances used as + workers. + pending_work_items: A dict mapping work ids to _WorkItems e.g. + {5: <_WorkItem...>, 6: <_WorkItem...>, ...} + work_ids_queue: A queue.Queue of work ids e.g. Queue([5, 6, ...]). + call_queue: A multiprocessing.Queue that will be filled with _CallItems + derived from _WorkItems for processing by the process workers. + result_queue: A multiprocessing.Queue of _ResultItems generated by the + process workers. + """ + nb_shutdown_processes = [0] + def shutdown_one_process(): + """Tell a worker to terminate, which will in turn wake us again""" + call_queue.put(None) + nb_shutdown_processes[0] += 1 + while True: + _add_call_item_to_queue(pending_work_items, + work_ids_queue, + call_queue) + + result_item = result_queue.get(block=True) + if result_item is not None: + work_item = pending_work_items[result_item.work_id] + del pending_work_items[result_item.work_id] + + if result_item.exception: + work_item.future.set_exception(result_item.exception) + else: + work_item.future.set_result(result_item.result) + # Check whether we should start shutting down. + executor = executor_reference() + # No more work items can be added if: + # - The interpreter is shutting down OR + # - The executor that owns this worker has been collected OR + # - The executor that owns this worker has been shutdown. + if _shutdown or executor is None or executor._shutdown_thread: + # Since no new work items can be added, it is safe to shutdown + # this thread if there are no pending work items. + if not pending_work_items: + while nb_shutdown_processes[0] < len(processes): + shutdown_one_process() + # If .join() is not called on the created processes then + # some multiprocessing.Queue methods may deadlock on Mac OS + # X. + for p in processes: + p.join() + call_queue.close() + return + del executor + +_system_limits_checked = False +_system_limited = None +def _check_system_limits(): + global _system_limits_checked, _system_limited + if _system_limits_checked: + if _system_limited: + raise NotImplementedError(_system_limited) + _system_limits_checked = True + try: + import os + nsems_max = os.sysconf("SC_SEM_NSEMS_MAX") + except (AttributeError, ValueError): + # sysconf not available or setting not available + return + if nsems_max == -1: + # indetermine limit, assume that limit is determined + # by available memory only + return + if nsems_max >= 256: + # minimum number of semaphores available + # according to POSIX + return + _system_limited = "system provides too few semaphores (%d available, 256 necessary)" % nsems_max + raise NotImplementedError(_system_limited) + +class ProcessPoolExecutor(_base.Executor): + def __init__(self, max_workers=None): + """Initializes a new ProcessPoolExecutor instance. + + Args: + max_workers: The maximum number of processes that can be used to + execute the given calls. If None or not given then as many + worker processes will be created as the machine has processors. + """ + _check_system_limits() + + if max_workers is None: + self._max_workers = multiprocessing.cpu_count() + else: + self._max_workers = max_workers + + # Make the call queue slightly larger than the number of processes to + # prevent the worker processes from idling. But don't make it too big + # because futures in the call queue cannot be cancelled. + self._call_queue = multiprocessing.Queue(self._max_workers + + EXTRA_QUEUED_CALLS) + self._result_queue = multiprocessing.Queue() + self._work_ids = queue.Queue() + self._queue_management_thread = None + self._processes = set() + + # Shutdown is a two-step process. + self._shutdown_thread = False + self._shutdown_lock = threading.Lock() + self._queue_count = 0 + self._pending_work_items = {} + + def _start_queue_management_thread(self): + # When the executor gets lost, the weakref callback will wake up + # the queue management thread. + def weakref_cb(_, q=self._result_queue): + q.put(None) + if self._queue_management_thread is None: + self._queue_management_thread = threading.Thread( + target=_queue_management_worker, + args=(weakref.ref(self, weakref_cb), + self._processes, + self._pending_work_items, + self._work_ids, + self._call_queue, + self._result_queue)) + self._queue_management_thread.daemon = True + self._queue_management_thread.start() + _threads_queues[self._queue_management_thread] = self._result_queue + + def _adjust_process_count(self): + for _ in range(len(self._processes), self._max_workers): + p = multiprocessing.Process( + target=_process_worker, + args=(self._call_queue, + self._result_queue)) + p.start() + self._processes.add(p) + + def submit(self, fn, *args, **kwargs): + with self._shutdown_lock: + if self._shutdown_thread: + raise RuntimeError('cannot schedule new futures after shutdown') + + f = _base.Future() + w = _WorkItem(f, fn, args, kwargs) + + self._pending_work_items[self._queue_count] = w + self._work_ids.put(self._queue_count) + self._queue_count += 1 + # Wake up queue management thread + self._result_queue.put(None) + + self._start_queue_management_thread() + self._adjust_process_count() + return f + submit.__doc__ = _base.Executor.submit.__doc__ + + def shutdown(self, wait=True): + with self._shutdown_lock: + self._shutdown_thread = True + if self._queue_management_thread: + # Wake up queue management thread + self._result_queue.put(None) + if wait: + self._queue_management_thread.join() + # To reduce the risk of openning too many files, remove references to + # objects that use file descriptors. + self._queue_management_thread = None + self._call_queue = None + self._result_queue = None + self._processes = None + shutdown.__doc__ = _base.Executor.shutdown.__doc__ + +atexit.register(_python_exit) diff --git a/third_party/pythonfutures/concurrent/futures/thread.py b/third_party/pythonfutures/concurrent/futures/thread.py new file mode 100755 index 00000000..a45959d3 --- /dev/null +++ b/third_party/pythonfutures/concurrent/futures/thread.py @@ -0,0 +1,138 @@ +# Copyright 2009 Brian Quinlan. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +"""Implements ThreadPoolExecutor.""" + +from __future__ import with_statement +import atexit +import threading +import weakref +import sys + +from concurrent.futures import _base + +try: + import queue +except ImportError: + import Queue as queue + +__author__ = 'Brian Quinlan (brian@sweetapp.com)' + +# Workers are created as daemon threads. This is done to allow the interpreter +# to exit when there are still idle threads in a ThreadPoolExecutor's thread +# pool (i.e. shutdown() was not called). However, allowing workers to die with +# the interpreter has two undesirable properties: +# - The workers would still be running during interpretor shutdown, +# meaning that they would fail in unpredictable ways. +# - The workers could be killed while evaluating a work item, which could +# be bad if the callable being evaluated has external side-effects e.g. +# writing to a file. +# +# To work around this problem, an exit handler is installed which tells the +# workers to exit when their work queues are empty and then waits until the +# threads finish. + +_threads_queues = weakref.WeakKeyDictionary() +_shutdown = False + +def _python_exit(): + global _shutdown + _shutdown = True + items = list(_threads_queues.items()) + for t, q in items: + q.put(None) + for t, q in items: + t.join() + +atexit.register(_python_exit) + +class _WorkItem(object): + def __init__(self, future, fn, args, kwargs): + self.future = future + self.fn = fn + self.args = args + self.kwargs = kwargs + + def run(self): + if not self.future.set_running_or_notify_cancel(): + return + + try: + result = self.fn(*self.args, **self.kwargs) + except BaseException: + e = sys.exc_info()[1] + self.future.set_exception(e) + else: + self.future.set_result(result) + +def _worker(executor_reference, work_queue): + try: + while True: + work_item = work_queue.get(block=True) + if work_item is not None: + work_item.run() + continue + executor = executor_reference() + # Exit if: + # - The interpreter is shutting down OR + # - The executor that owns the worker has been collected OR + # - The executor that owns the worker has been shutdown. + if _shutdown or executor is None or executor._shutdown: + # Notice other workers + work_queue.put(None) + return + del executor + except BaseException: + _base.LOGGER.critical('Exception in worker', exc_info=True) + +class ThreadPoolExecutor(_base.Executor): + def __init__(self, max_workers): + """Initializes a new ThreadPoolExecutor instance. + + Args: + max_workers: The maximum number of threads that can be used to + execute the given calls. + """ + self._max_workers = max_workers + self._work_queue = queue.Queue() + self._threads = set() + self._shutdown = False + self._shutdown_lock = threading.Lock() + + def submit(self, fn, *args, **kwargs): + with self._shutdown_lock: + if self._shutdown: + raise RuntimeError('cannot schedule new futures after shutdown') + + f = _base.Future() + w = _WorkItem(f, fn, args, kwargs) + + self._work_queue.put(w) + self._adjust_thread_count() + return f + submit.__doc__ = _base.Executor.submit.__doc__ + + def _adjust_thread_count(self): + # When the executor gets lost, the weakref callback will wake up + # the worker threads. + def weakref_cb(_, q=self._work_queue): + q.put(None) + # TODO(bquinlan): Should avoid creating new threads if there are more + # idle threads than items in the work queue. + if len(self._threads) < self._max_workers: + t = threading.Thread(target=_worker, + args=(weakref.ref(self, weakref_cb), + self._work_queue)) + t.daemon = True + t.start() + self._threads.add(t) + _threads_queues[t] = self._work_queue + + def shutdown(self, wait=True): + with self._shutdown_lock: + self._shutdown = True + self._work_queue.put(None) + if wait: + for t in self._threads: + t.join() + shutdown.__doc__ = _base.Executor.shutdown.__doc__ diff --git a/third_party/pythonfutures/crawl.py b/third_party/pythonfutures/crawl.py new file mode 100755 index 00000000..86e0af7f --- /dev/null +++ b/third_party/pythonfutures/crawl.py @@ -0,0 +1,74 @@ +"""Compare the speed of downloading URLs sequentially vs. using futures.""" + +import functools +import time +import timeit +import sys + +try: + from urllib2 import urlopen +except ImportError: + from urllib.request import urlopen + +from concurrent.futures import (as_completed, ThreadPoolExecutor, + ProcessPoolExecutor) + +URLS = ['http://www.google.com/', + 'http://www.apple.com/', + 'http://www.ibm.com', + 'http://www.thisurlprobablydoesnotexist.com', + 'http://www.slashdot.org/', + 'http://www.python.org/', + 'http://www.bing.com/', + 'http://www.facebook.com/', + 'http://www.yahoo.com/', + 'http://www.youtube.com/', + 'http://www.blogger.com/'] + +def load_url(url, timeout): + kwargs = {'timeout': timeout} if sys.version_info >= (2, 6) else {} + return urlopen(url, **kwargs).read() + +def download_urls_sequential(urls, timeout=60): + url_to_content = {} + for url in urls: + try: + url_to_content[url] = load_url(url, timeout=timeout) + except: + pass + return url_to_content + +def download_urls_with_executor(urls, executor, timeout=60): + try: + url_to_content = {} + future_to_url = dict((executor.submit(load_url, url, timeout), url) + for url in urls) + + for future in as_completed(future_to_url): + try: + url_to_content[future_to_url[future]] = future.result() + except: + pass + return url_to_content + finally: + executor.shutdown() + +def main(): + for name, fn in [('sequential', + functools.partial(download_urls_sequential, URLS)), + ('processes', + functools.partial(download_urls_with_executor, + URLS, + ProcessPoolExecutor(10))), + ('threads', + functools.partial(download_urls_with_executor, + URLS, + ThreadPoolExecutor(10)))]: + sys.stdout.write('%s: ' % name.ljust(12)) + start = time.time() + url_map = fn() + sys.stdout.write('%.2f seconds (%d of %d downloaded)\n' % + (time.time() - start, len(url_map), len(URLS))) + +if __name__ == '__main__': + main() diff --git a/third_party/pythonfutures/docs/conf.py b/third_party/pythonfutures/docs/conf.py new file mode 100755 index 00000000..124cd518 --- /dev/null +++ b/third_party/pythonfutures/docs/conf.py @@ -0,0 +1,194 @@ +# -*- coding: utf-8 -*- +# +# futures documentation build configuration file, created by +# sphinx-quickstart on Wed Jun 3 19:35:34 2009. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.append(os.path.abspath('.')) + +# -- General configuration ----------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = [] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'futures' +copyright = u'2009-2011, Brian Quinlan' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '2.1.3' +# The full version, including alpha/beta/rc tags. +release = '2.1.3' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of documents that shouldn't be included in the build. +#unused_docs = [] + +# List of directories, relative to source directory, that shouldn't be searched +# for source files. +exclude_trees = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. Major themes that come with +# Sphinx are currently 'default' and 'sphinxdoc'. +html_theme = 'default' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_use_modindex = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = '' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'futuresdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +# The paper size ('letter' or 'a4'). +#latex_paper_size = 'letter' + +# The font size ('10pt', '11pt' or '12pt'). +#latex_font_size = '10pt' + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'futures.tex', u'futures Documentation', + u'Brian Quinlan', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# Additional stuff for the LaTeX preamble. +#latex_preamble = '' + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_use_modindex = True diff --git a/third_party/pythonfutures/docs/index.rst b/third_party/pythonfutures/docs/index.rst new file mode 100755 index 00000000..525ce6ab --- /dev/null +++ b/third_party/pythonfutures/docs/index.rst @@ -0,0 +1,345 @@ +:mod:`concurrent.futures` --- Asynchronous computation +====================================================== + +.. module:: concurrent.futures + :synopsis: Execute computations asynchronously using threads or processes. + +The :mod:`concurrent.futures` module provides a high-level interface for +asynchronously executing callables. + +The asynchronous execution can be be performed by threads using +:class:`ThreadPoolExecutor` or seperate processes using +:class:`ProcessPoolExecutor`. Both implement the same interface, which is +defined by the abstract :class:`Executor` class. + +Executor Objects +---------------- + +:class:`Executor` is an abstract class that provides methods to execute calls +asynchronously. It should not be used directly, but through its two +subclasses: :class:`ThreadPoolExecutor` and :class:`ProcessPoolExecutor`. + +.. method:: Executor.submit(fn, *args, **kwargs) + + Schedules the callable to be executed as *fn*(*\*args*, *\*\*kwargs*) and + returns a :class:`Future` representing the execution of the callable. + +:: + + with ThreadPoolExecutor(max_workers=1) as executor: + future = executor.submit(pow, 323, 1235) + print(future.result()) + +.. method:: Executor.map(func, *iterables, timeout=None) + + Equivalent to map(*func*, *\*iterables*) but func is executed asynchronously + and several calls to *func* may be made concurrently. The returned iterator + raises a :exc:`TimeoutError` if :meth:`__next__()` is called and the result + isn't available after *timeout* seconds from the original call to + :meth:`map()`. *timeout* can be an int or float. If *timeout* is not + specified or ``None`` then there is no limit to the wait time. If a call + raises an exception then that exception will be raised when its value is + retrieved from the iterator. + +.. method:: Executor.shutdown(wait=True) + + Signal the executor that it should free any resources that it is using when + the currently pending futures are done executing. Calls to + :meth:`Executor.submit` and :meth:`Executor.map` made after shutdown will + raise :exc:`RuntimeError`. + + If *wait* is `True` then this method will not return until all the pending + futures are done executing and the resources associated with the executor + have been freed. If *wait* is `False` then this method will return + immediately and the resources associated with the executor will be freed + when all pending futures are done executing. Regardless of the value of + *wait*, the entire Python program will not exit until all pending futures + are done executing. + + You can avoid having to call this method explicitly if you use the `with` + statement, which will shutdown the `Executor` (waiting as if + `Executor.shutdown` were called with *wait* set to `True`): + +:: + + import shutil + with ThreadPoolExecutor(max_workers=4) as e: + e.submit(shutil.copy, 'src1.txt', 'dest1.txt') + e.submit(shutil.copy, 'src2.txt', 'dest2.txt') + e.submit(shutil.copy, 'src3.txt', 'dest3.txt') + e.submit(shutil.copy, 'src3.txt', 'dest4.txt') + + +ThreadPoolExecutor Objects +-------------------------- + +The :class:`ThreadPoolExecutor` class is an :class:`Executor` subclass that uses +a pool of threads to execute calls asynchronously. + +Deadlock can occur when the callable associated with a :class:`Future` waits on +the results of another :class:`Future`. For example: + +:: + + import time + def wait_on_b(): + time.sleep(5) + print(b.result()) # b will never complete because it is waiting on a. + return 5 + + def wait_on_a(): + time.sleep(5) + print(a.result()) # a will never complete because it is waiting on b. + return 6 + + + executor = ThreadPoolExecutor(max_workers=2) + a = executor.submit(wait_on_b) + b = executor.submit(wait_on_a) + +And: + +:: + + def wait_on_future(): + f = executor.submit(pow, 5, 2) + # This will never complete because there is only one worker thread and + # it is executing this function. + print(f.result()) + + executor = ThreadPoolExecutor(max_workers=1) + executor.submit(wait_on_future) + +.. class:: ThreadPoolExecutor(max_workers) + + Executes calls asynchronously using at pool of at most *max_workers* threads. + +.. _threadpoolexecutor-example: + +ThreadPoolExecutor Example +^^^^^^^^^^^^^^^^^^^^^^^^^^ +:: + + from concurrent import futures + import urllib.request + + URLS = ['http://www.foxnews.com/', + 'http://www.cnn.com/', + 'http://europe.wsj.com/', + 'http://www.bbc.co.uk/', + 'http://some-made-up-domain.com/'] + + def load_url(url, timeout): + return urllib.request.urlopen(url, timeout=timeout).read() + + with futures.ThreadPoolExecutor(max_workers=5) as executor: + future_to_url = dict((executor.submit(load_url, url, 60), url) + for url in URLS) + + for future in futures.as_completed(future_to_url): + url = future_to_url[future] + if future.exception() is not None: + print('%r generated an exception: %s' % (url, + future.exception())) + else: + print('%r page is %d bytes' % (url, len(future.result()))) + +ProcessPoolExecutor Objects +--------------------------- + +The :class:`ProcessPoolExecutor` class is an :class:`Executor` subclass that +uses a pool of processes to execute calls asynchronously. +:class:`ProcessPoolExecutor` uses the :mod:`multiprocessing` module, which +allows it to side-step the :term:`Global Interpreter Lock` but also means that +only picklable objects can be executed and returned. + +Calling :class:`Executor` or :class:`Future` methods from a callable submitted +to a :class:`ProcessPoolExecutor` will result in deadlock. + +.. class:: ProcessPoolExecutor(max_workers=None) + + Executes calls asynchronously using a pool of at most *max_workers* + processes. If *max_workers* is ``None`` or not given then as many worker + processes will be created as the machine has processors. + +.. _processpoolexecutor-example: + +ProcessPoolExecutor Example +^^^^^^^^^^^^^^^^^^^^^^^^^^^ +:: + + import math + + PRIMES = [ + 112272535095293, + 112582705942171, + 112272535095293, + 115280095190773, + 115797848077099, + 1099726899285419] + + def is_prime(n): + if n % 2 == 0: + return False + + sqrt_n = int(math.floor(math.sqrt(n))) + for i in range(3, sqrt_n + 1, 2): + if n % i == 0: + return False + return True + + def main(): + with futures.ProcessPoolExecutor() as executor: + for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): + print('%d is prime: %s' % (number, prime)) + + if __name__ == '__main__': + main() + +Future Objects +-------------- + +The :class:`Future` class encapulates the asynchronous execution of a callable. +:class:`Future` instances are created by :meth:`Executor.submit`. + +.. method:: Future.cancel() + + Attempt to cancel the call. If the call is currently being executed then + it cannot be cancelled and the method will return `False`, otherwise the call + will be cancelled and the method will return `True`. + +.. method:: Future.cancelled() + + Return `True` if the call was successfully cancelled. + +.. method:: Future.running() + + Return `True` if the call is currently being executed and cannot be + cancelled. + +.. method:: Future.done() + + Return `True` if the call was successfully cancelled or finished running. + +.. method:: Future.result(timeout=None) + + Return the value returned by the call. If the call hasn't yet completed then + this method will wait up to *timeout* seconds. If the call hasn't completed + in *timeout* seconds then a :exc:`TimeoutError` will be raised. *timeout* can + be an int or float.If *timeout* is not specified or ``None`` then there is no + limit to the wait time. + + If the future is cancelled before completing then :exc:`CancelledError` will + be raised. + + If the call raised then this method will raise the same exception. + +.. method:: Future.exception(timeout=None) + + Return the exception raised by the call. If the call hasn't yet completed + then this method will wait up to *timeout* seconds. If the call hasn't + completed in *timeout* seconds then a :exc:`TimeoutError` will be raised. + *timeout* can be an int or float. If *timeout* is not specified or ``None`` + then there is no limit to the wait time. + + If the future is cancelled before completing then :exc:`CancelledError` will + be raised. + + If the call completed without raising then ``None`` is returned. + +.. method:: Future.add_done_callback(fn) + + Attaches the callable *fn* to the future. *fn* will be called, with the + future as its only argument, when the future is cancelled or finishes + running. + + Added callables are called in the order that they were added and are always + called in a thread belonging to the process that added them. If the callable + raises an :exc:`Exception` then it will be logged and ignored. If the + callable raises another :exc:`BaseException` then the behavior is not + defined. + + If the future has already completed or been cancelled then *fn* will be + called immediately. + +Internal Future Methods +^^^^^^^^^^^^^^^^^^^^^^^ + +The following :class:`Future` methods are meant for use in unit tests and +:class:`Executor` implementations. + +.. method:: Future.set_running_or_notify_cancel() + + This method should only be called by :class:`Executor` implementations before + executing the work associated with the :class:`Future` and by unit tests. + + If the method returns `False` then the :class:`Future` was cancelled i.e. + :meth:`Future.cancel` was called and returned `True`. Any threads waiting + on the :class:`Future` completing (i.e. through :func:`as_completed` or + :func:`wait`) will be woken up. + + If the method returns `True` then the :class:`Future` was not cancelled + and has been put in the running state i.e. calls to + :meth:`Future.running` will return `True`. + + This method can only be called once and cannot be called after + :meth:`Future.set_result` or :meth:`Future.set_exception` have been + called. + +.. method:: Future.set_result(result) + + Sets the result of the work associated with the :class:`Future` to *result*. + + This method should only be used by Executor implementations and unit tests. + +.. method:: Future.set_exception(exception) + + Sets the result of the work associated with the :class:`Future` to the + :class:`Exception` *exception*. + + This method should only be used by Executor implementations and unit tests. + +Module Functions +---------------- + +.. function:: wait(fs, timeout=None, return_when=ALL_COMPLETED) + + Wait for the :class:`Future` instances (possibly created by different + :class:`Executor` instances) given by *fs* to complete. Returns a named + 2-tuple of sets. The first set, named "done", contains the futures that + completed (finished or were cancelled) before the wait completed. The second + set, named "not_done", contains uncompleted futures. + + *timeout* can be used to control the maximum number of seconds to wait before + returning. *timeout* can be an int or float. If *timeout* is not specified or + ``None`` then there is no limit to the wait time. + + *return_when* indicates when this function should return. It must be one of + the following constants: + + +-----------------------------+----------------------------------------+ + | Constant | Description | + +=============================+========================================+ + | :const:`FIRST_COMPLETED` | The function will return when any | + | | future finishes or is cancelled. | + +-----------------------------+----------------------------------------+ + | :const:`FIRST_EXCEPTION` | The function will return when any | + | | future finishes by raising an | + | | exception. If no future raises an | + | | exception then it is equivalent to | + | | `ALL_COMPLETED`. | + +-----------------------------+----------------------------------------+ + | :const:`ALL_COMPLETED` | The function will return when all | + | | futures finish or are cancelled. | + +-----------------------------+----------------------------------------+ + +.. function:: as_completed(fs, timeout=None) + + Returns an iterator over the :class:`Future` instances (possibly created + by different :class:`Executor` instances) given by *fs* that yields futures + as they complete (finished or were cancelled). Any futures that completed + before :func:`as_completed()` was called will be yielded first. The returned + iterator raises a :exc:`TimeoutError` if :meth:`__next__()` is called and + the result isn't available after *timeout* seconds from the original call + to :func:`as_completed()`. *timeout* can be an int or float. If *timeout* + is not specified or ``None`` then there is no limit to the wait time. diff --git a/third_party/pythonfutures/docs/make.bat b/third_party/pythonfutures/docs/make.bat new file mode 100755 index 00000000..3e8021b5 --- /dev/null +++ b/third_party/pythonfutures/docs/make.bat @@ -0,0 +1,112 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +set SPHINXBUILD=sphinx-build +set ALLSPHINXOPTS=-d _build/doctrees %SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. changes to make an overview over all changed/added/deprecated items + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (_build\*) do rmdir /q /s %%i + del /q /s _build\* + goto end +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% _build/html + echo. + echo.Build finished. The HTML pages are in _build/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% _build/dirhtml + echo. + echo.Build finished. The HTML pages are in _build/dirhtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% _build/pickle + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% _build/json + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% _build/htmlhelp + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in _build/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% _build/qthelp + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in _build/qthelp, like this: + echo.^> qcollectiongenerator _build\qthelp\futures.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile _build\qthelp\futures.ghc + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% _build/latex + echo. + echo.Build finished; the LaTeX files are in _build/latex. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% _build/changes + echo. + echo.The overview file is in _build/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% _build/linkcheck + echo. + echo.Link check complete; look for any errors in the above output ^ +or in _build/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% _build/doctest + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in _build/doctest/output.txt. + goto end +) + +:end diff --git a/third_party/pythonfutures/futures/__init__.py b/third_party/pythonfutures/futures/__init__.py new file mode 100755 index 00000000..8f8b2348 --- /dev/null +++ b/third_party/pythonfutures/futures/__init__.py @@ -0,0 +1,24 @@ +# Copyright 2009 Brian Quinlan. All Rights Reserved. +# Licensed to PSF under a Contributor Agreement. + +"""Execute computations asynchronously using threads or processes.""" + +import warnings + +from concurrent.futures import (FIRST_COMPLETED, + FIRST_EXCEPTION, + ALL_COMPLETED, + CancelledError, + TimeoutError, + Future, + Executor, + wait, + as_completed, + ProcessPoolExecutor, + ThreadPoolExecutor) + +__author__ = 'Brian Quinlan (brian@sweetapp.com)' + +warnings.warn('The futures package has been deprecated. ' + 'Use the concurrent.futures package instead.', + DeprecationWarning) diff --git a/third_party/pythonfutures/futures/process.py b/third_party/pythonfutures/futures/process.py new file mode 100755 index 00000000..e9d37b16 --- /dev/null +++ b/third_party/pythonfutures/futures/process.py @@ -0,0 +1 @@ +from concurrent.futures import ProcessPoolExecutor diff --git a/third_party/pythonfutures/futures/thread.py b/third_party/pythonfutures/futures/thread.py new file mode 100755 index 00000000..f6bd05de --- /dev/null +++ b/third_party/pythonfutures/futures/thread.py @@ -0,0 +1 @@ +from concurrent.futures import ThreadPoolExecutor diff --git a/third_party/pythonfutures/primes.py b/third_party/pythonfutures/primes.py new file mode 100755 index 00000000..0da2b3e6 --- /dev/null +++ b/third_party/pythonfutures/primes.py @@ -0,0 +1,50 @@ +from __future__ import with_statement +import math +import time +import sys + +from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor + +PRIMES = [ + 112272535095293, + 112582705942171, + 112272535095293, + 115280095190773, + 115797848077099, + 117450548693743, + 993960000099397] + +def is_prime(n): + if n % 2 == 0: + return False + + sqrt_n = int(math.floor(math.sqrt(n))) + for i in range(3, sqrt_n + 1, 2): + if n % i == 0: + return False + return True + +def sequential(): + return list(map(is_prime, PRIMES)) + +def with_process_pool_executor(): + with ProcessPoolExecutor(10) as executor: + return list(executor.map(is_prime, PRIMES)) + +def with_thread_pool_executor(): + with ThreadPoolExecutor(10) as executor: + return list(executor.map(is_prime, PRIMES)) + +def main(): + for name, fn in [('sequential', sequential), + ('processes', with_process_pool_executor), + ('threads', with_thread_pool_executor)]: + sys.stdout.write('%s: ' % name.ljust(12)) + start = time.time() + if fn() != [True] * len(PRIMES): + sys.stdout.write('failed\n') + else: + sys.stdout.write('%.2f seconds\n' % (time.time() - start)) + +if __name__ == '__main__': + main() diff --git a/third_party/pythonfutures/setup.cfg b/third_party/pythonfutures/setup.cfg new file mode 100755 index 00000000..0a9f4f52 --- /dev/null +++ b/third_party/pythonfutures/setup.cfg @@ -0,0 +1,6 @@ +[build_sphinx] +source-dir = docs +build-dir = build/sphinx + +[upload_docs] +upload-dir = build/sphinx/html diff --git a/third_party/pythonfutures/setup.py b/third_party/pythonfutures/setup.py new file mode 100755 index 00000000..c08461ed --- /dev/null +++ b/third_party/pythonfutures/setup.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +import sys + +extras = {} +try: + from setuptools import setup + extras['zip_safe'] = False + if sys.version_info < (2, 6): + extras['install_requires'] = ['multiprocessing'] +except ImportError: + from distutils.core import setup + +setup(name='futures', + version='2.1.4', + description='Backport of the concurrent.futures package from Python 3.2', + author='Brian Quinlan', + author_email='brian@sweetapp.com', + maintainer='Alex Gronholm', + maintainer_email='alex.gronholm+pypi@nextday.fi', + url='http://code.google.com/p/pythonfutures', + download_url='http://pypi.python.org/pypi/futures/', + packages=['futures', 'concurrent', 'concurrent.futures'], + license='BSD', + classifiers=['License :: OSI Approved :: BSD License', + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Programming Language :: Python :: 2.5', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.1'], + **extras + ) diff --git a/third_party/pythonfutures/test_futures.py b/third_party/pythonfutures/test_futures.py new file mode 100755 index 00000000..dd7fd3e6 --- /dev/null +++ b/third_party/pythonfutures/test_futures.py @@ -0,0 +1,723 @@ +from __future__ import with_statement +import os +import subprocess +import sys +import threading +import functools +import contextlib +import logging +import re +import time + +from concurrent import futures +from concurrent.futures._base import ( + PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future) + +try: + import unittest2 as unittest +except ImportError: + import unittest + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +try: + from test import test_support +except ImportError: + from test import support as test_support + +try: + next +except NameError: + next = lambda x: x.next() + + +def reap_threads(func): + """Use this function when threads are being used. This will + ensure that the threads are cleaned up even when the test fails. + If threading is unavailable this function does nothing. + """ + @functools.wraps(func) + def decorator(*args): + key = test_support.threading_setup() + try: + return func(*args) + finally: + test_support.threading_cleanup(*key) + return decorator + + +# Executing the interpreter in a subprocess +def _assert_python(expected_success, *args, **env_vars): + cmd_line = [sys.executable] + if not env_vars: + cmd_line.append('-E') + # Need to preserve the original environment, for in-place testing of + # shared library builds. + env = os.environ.copy() + # But a special flag that can be set to override -- in this case, the + # caller is responsible to pass the full environment. + if env_vars.pop('__cleanenv', None): + env = {} + env.update(env_vars) + cmd_line.extend(args) + p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env) + try: + out, err = p.communicate() + finally: + subprocess._cleanup() + p.stdout.close() + p.stderr.close() + rc = p.returncode + err = strip_python_stderr(err) + if (rc and expected_success) or (not rc and not expected_success): + raise AssertionError( + "Process return code is %d, " + "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) + return rc, out, err + + +def assert_python_ok(*args, **env_vars): + """ + Assert that running the interpreter with `args` and optional environment + variables `env_vars` is ok and return a (return code, stdout, stderr) tuple. + """ + return _assert_python(True, *args, **env_vars) + + +def strip_python_stderr(stderr): + """Strip the stderr of a Python process from potential debug output + emitted by the interpreter. + + This will typically be run on the result of the communicate() method + of a subprocess.Popen object. + """ + stderr = re.sub(r"\[\d+ refs\]\r?\n?$".encode(), "".encode(), stderr).strip() + return stderr + + +@contextlib.contextmanager +def captured_stderr(): + """Return a context manager used by captured_stdout/stdin/stderr + that temporarily replaces the sys stream *stream_name* with a StringIO.""" + logging_stream = StringIO() + handler = logging.StreamHandler(logging_stream) + logging.root.addHandler(handler) + + try: + yield logging_stream + finally: + logging.root.removeHandler(handler) + + +def create_future(state=PENDING, exception=None, result=None): + f = Future() + f._state = state + f._exception = exception + f._result = result + return f + + +PENDING_FUTURE = create_future(state=PENDING) +RUNNING_FUTURE = create_future(state=RUNNING) +CANCELLED_FUTURE = create_future(state=CANCELLED) +CANCELLED_AND_NOTIFIED_FUTURE = create_future(state=CANCELLED_AND_NOTIFIED) +EXCEPTION_FUTURE = create_future(state=FINISHED, exception=IOError()) +SUCCESSFUL_FUTURE = create_future(state=FINISHED, result=42) + + +def mul(x, y): + return x * y + + +def sleep_and_raise(t): + time.sleep(t) + raise Exception('this is an exception') + +def sleep_and_print(t, msg): + time.sleep(t) + print(msg) + sys.stdout.flush() + + +class ExecutorMixin: + worker_count = 5 + + def setUp(self): + self.t1 = time.time() + try: + self.executor = self.executor_type(max_workers=self.worker_count) + except NotImplementedError: + e = sys.exc_info()[1] + self.skipTest(str(e)) + self._prime_executor() + + def tearDown(self): + self.executor.shutdown(wait=True) + dt = time.time() - self.t1 + if test_support.verbose: + print("%.2fs" % dt) + self.assertLess(dt, 60, "synchronization issue: test lasted too long") + + def _prime_executor(self): + # Make sure that the executor is ready to do work before running the + # tests. This should reduce the probability of timeouts in the tests. + futures = [self.executor.submit(time.sleep, 0.1) + for _ in range(self.worker_count)] + + for f in futures: + f.result() + + +class ThreadPoolMixin(ExecutorMixin): + executor_type = futures.ThreadPoolExecutor + + +class ProcessPoolMixin(ExecutorMixin): + executor_type = futures.ProcessPoolExecutor + + +class ExecutorShutdownTest(unittest.TestCase): + def test_run_after_shutdown(self): + self.executor.shutdown() + self.assertRaises(RuntimeError, + self.executor.submit, + pow, 2, 5) + + def test_interpreter_shutdown(self): + # Test the atexit hook for shutdown of worker threads and processes + rc, out, err = assert_python_ok('-c', """if 1: + from concurrent.futures import %s + from time import sleep + from test_futures import sleep_and_print + t = %s(5) + t.submit(sleep_and_print, 1.0, "apple") + """ % (self.executor_type.__name__, self.executor_type.__name__)) + # Errors in atexit hooks don't change the process exit code, check + # stderr manually. + self.assertFalse(err) + self.assertEqual(out.strip(), "apple".encode()) + + def test_hang_issue12364(self): + fs = [self.executor.submit(time.sleep, 0.1) for _ in range(50)] + self.executor.shutdown() + for f in fs: + f.result() + + +class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest): + def _prime_executor(self): + pass + + def test_threads_terminate(self): + self.executor.submit(mul, 21, 2) + self.executor.submit(mul, 6, 7) + self.executor.submit(mul, 3, 14) + self.assertEqual(len(self.executor._threads), 3) + self.executor.shutdown() + for t in self.executor._threads: + t.join() + + def test_context_manager_shutdown(self): + with futures.ThreadPoolExecutor(max_workers=5) as e: + executor = e + self.assertEqual(list(e.map(abs, range(-5, 5))), + [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) + + for t in executor._threads: + t.join() + + def test_del_shutdown(self): + executor = futures.ThreadPoolExecutor(max_workers=5) + executor.map(abs, range(-5, 5)) + threads = executor._threads + del executor + + for t in threads: + t.join() + + +class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest): + def _prime_executor(self): + pass + + def test_processes_terminate(self): + self.executor.submit(mul, 21, 2) + self.executor.submit(mul, 6, 7) + self.executor.submit(mul, 3, 14) + self.assertEqual(len(self.executor._processes), 5) + processes = self.executor._processes + self.executor.shutdown() + + for p in processes: + p.join() + + def test_context_manager_shutdown(self): + with futures.ProcessPoolExecutor(max_workers=5) as e: + processes = e._processes + self.assertEqual(list(e.map(abs, range(-5, 5))), + [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) + + for p in processes: + p.join() + + def test_del_shutdown(self): + executor = futures.ProcessPoolExecutor(max_workers=5) + list(executor.map(abs, range(-5, 5))) + queue_management_thread = executor._queue_management_thread + processes = executor._processes + del executor + + queue_management_thread.join() + for p in processes: + p.join() + + +class WaitTests(unittest.TestCase): + + def test_first_completed(self): + future1 = self.executor.submit(mul, 21, 2) + future2 = self.executor.submit(time.sleep, 1.5) + + done, not_done = futures.wait( + [CANCELLED_FUTURE, future1, future2], + return_when=futures.FIRST_COMPLETED) + + self.assertEqual(set([future1]), done) + self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) + + def test_first_completed_some_already_completed(self): + future1 = self.executor.submit(time.sleep, 1.5) + + finished, pending = futures.wait( + [CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], + return_when=futures.FIRST_COMPLETED) + + self.assertEqual( + set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), + finished) + self.assertEqual(set([future1]), pending) + + def test_first_exception(self): + future1 = self.executor.submit(mul, 2, 21) + future2 = self.executor.submit(sleep_and_raise, 1.5) + future3 = self.executor.submit(time.sleep, 3) + + finished, pending = futures.wait( + [future1, future2, future3], + return_when=futures.FIRST_EXCEPTION) + + self.assertEqual(set([future1, future2]), finished) + self.assertEqual(set([future3]), pending) + + def test_first_exception_some_already_complete(self): + future1 = self.executor.submit(divmod, 21, 0) + future2 = self.executor.submit(time.sleep, 1.5) + + finished, pending = futures.wait( + [SUCCESSFUL_FUTURE, + CANCELLED_FUTURE, + CANCELLED_AND_NOTIFIED_FUTURE, + future1, future2], + return_when=futures.FIRST_EXCEPTION) + + self.assertEqual(set([SUCCESSFUL_FUTURE, + CANCELLED_AND_NOTIFIED_FUTURE, + future1]), finished) + self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) + + def test_first_exception_one_already_failed(self): + future1 = self.executor.submit(time.sleep, 2) + + finished, pending = futures.wait( + [EXCEPTION_FUTURE, future1], + return_when=futures.FIRST_EXCEPTION) + + self.assertEqual(set([EXCEPTION_FUTURE]), finished) + self.assertEqual(set([future1]), pending) + + def test_all_completed(self): + future1 = self.executor.submit(divmod, 2, 0) + future2 = self.executor.submit(mul, 2, 21) + + finished, pending = futures.wait( + [SUCCESSFUL_FUTURE, + CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + future1, + future2], + return_when=futures.ALL_COMPLETED) + + self.assertEqual(set([SUCCESSFUL_FUTURE, + CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + future1, + future2]), finished) + self.assertEqual(set(), pending) + + def test_timeout(self): + future1 = self.executor.submit(mul, 6, 7) + future2 = self.executor.submit(time.sleep, 3) + + finished, pending = futures.wait( + [CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE, + future1, future2], + timeout=1.5, + return_when=futures.ALL_COMPLETED) + + self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE, + future1]), finished) + self.assertEqual(set([future2]), pending) + + +class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests): + + def test_pending_calls_race(self): + # Issue #14406: multi-threaded race condition when waiting on all + # futures. + event = threading.Event() + def future_func(): + event.wait() + oldswitchinterval = sys.getcheckinterval() + sys.setcheckinterval(1) + try: + fs = set(self.executor.submit(future_func) for i in range(100)) + event.set() + futures.wait(fs, return_when=futures.ALL_COMPLETED) + finally: + sys.setcheckinterval(oldswitchinterval) + + +class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests): + pass + + +class AsCompletedTests(unittest.TestCase): + # TODO(brian@sweetapp.com): Should have a test with a non-zero timeout. + def test_no_timeout(self): + future1 = self.executor.submit(mul, 2, 21) + future2 = self.executor.submit(mul, 7, 6) + + completed = set(futures.as_completed( + [CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE, + future1, future2])) + self.assertEqual(set( + [CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE, + future1, future2]), + completed) + + def test_zero_timeout(self): + future1 = self.executor.submit(time.sleep, 2) + completed_futures = set() + try: + for future in futures.as_completed( + [CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE, + future1], + timeout=0): + completed_futures.add(future) + except futures.TimeoutError: + pass + + self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, + EXCEPTION_FUTURE, + SUCCESSFUL_FUTURE]), + completed_futures) + + +class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests): + pass + + +class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests): + pass + + +class ExecutorTest(unittest.TestCase): + # Executor.shutdown() and context manager usage is tested by + # ExecutorShutdownTest. + def test_submit(self): + future = self.executor.submit(pow, 2, 8) + self.assertEqual(256, future.result()) + + def test_submit_keyword(self): + future = self.executor.submit(mul, 2, y=8) + self.assertEqual(16, future.result()) + + def test_map(self): + self.assertEqual( + list(self.executor.map(pow, range(10), range(10))), + list(map(pow, range(10), range(10)))) + + def test_map_exception(self): + i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) + self.assertEqual(next(i), (0, 1)) + self.assertEqual(next(i), (0, 1)) + self.assertRaises(ZeroDivisionError, next, i) + + def test_map_timeout(self): + results = [] + try: + for i in self.executor.map(time.sleep, + [0, 0, 3], + timeout=1.5): + results.append(i) + except futures.TimeoutError: + pass + else: + self.fail('expected TimeoutError') + + self.assertEqual([None, None], results) + + +class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest): + pass + + +class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest): + pass + + +class FutureTests(unittest.TestCase): + def test_done_callback_with_result(self): + callback_result = [None] + def fn(callback_future): + callback_result[0] = callback_future.result() + + f = Future() + f.add_done_callback(fn) + f.set_result(5) + self.assertEqual(5, callback_result[0]) + + def test_done_callback_with_exception(self): + callback_exception = [None] + def fn(callback_future): + callback_exception[0] = callback_future.exception() + + f = Future() + f.add_done_callback(fn) + f.set_exception(Exception('test')) + self.assertEqual(('test',), callback_exception[0].args) + + def test_done_callback_with_cancel(self): + was_cancelled = [None] + def fn(callback_future): + was_cancelled[0] = callback_future.cancelled() + + f = Future() + f.add_done_callback(fn) + self.assertTrue(f.cancel()) + self.assertTrue(was_cancelled[0]) + + def test_done_callback_raises(self): + with captured_stderr() as stderr: + raising_was_called = [False] + fn_was_called = [False] + + def raising_fn(callback_future): + raising_was_called[0] = True + raise Exception('doh!') + + def fn(callback_future): + fn_was_called[0] = True + + f = Future() + f.add_done_callback(raising_fn) + f.add_done_callback(fn) + f.set_result(5) + self.assertTrue(raising_was_called) + self.assertTrue(fn_was_called) + self.assertIn('Exception: doh!', stderr.getvalue()) + + def test_done_callback_already_successful(self): + callback_result = [None] + def fn(callback_future): + callback_result[0] = callback_future.result() + + f = Future() + f.set_result(5) + f.add_done_callback(fn) + self.assertEqual(5, callback_result[0]) + + def test_done_callback_already_failed(self): + callback_exception = [None] + def fn(callback_future): + callback_exception[0] = callback_future.exception() + + f = Future() + f.set_exception(Exception('test')) + f.add_done_callback(fn) + self.assertEqual(('test',), callback_exception[0].args) + + def test_done_callback_already_cancelled(self): + was_cancelled = [None] + def fn(callback_future): + was_cancelled[0] = callback_future.cancelled() + + f = Future() + self.assertTrue(f.cancel()) + f.add_done_callback(fn) + self.assertTrue(was_cancelled[0]) + + def test_repr(self): + self.assertRegexpMatches(repr(PENDING_FUTURE), + '') + self.assertRegexpMatches(repr(RUNNING_FUTURE), + '') + self.assertRegexpMatches(repr(CANCELLED_FUTURE), + '') + self.assertRegexpMatches(repr(CANCELLED_AND_NOTIFIED_FUTURE), + '') + self.assertRegexpMatches( + repr(EXCEPTION_FUTURE), + '') + self.assertRegexpMatches( + repr(SUCCESSFUL_FUTURE), + '') + + def test_cancel(self): + f1 = create_future(state=PENDING) + f2 = create_future(state=RUNNING) + f3 = create_future(state=CANCELLED) + f4 = create_future(state=CANCELLED_AND_NOTIFIED) + f5 = create_future(state=FINISHED, exception=IOError()) + f6 = create_future(state=FINISHED, result=5) + + self.assertTrue(f1.cancel()) + self.assertEqual(f1._state, CANCELLED) + + self.assertFalse(f2.cancel()) + self.assertEqual(f2._state, RUNNING) + + self.assertTrue(f3.cancel()) + self.assertEqual(f3._state, CANCELLED) + + self.assertTrue(f4.cancel()) + self.assertEqual(f4._state, CANCELLED_AND_NOTIFIED) + + self.assertFalse(f5.cancel()) + self.assertEqual(f5._state, FINISHED) + + self.assertFalse(f6.cancel()) + self.assertEqual(f6._state, FINISHED) + + def test_cancelled(self): + self.assertFalse(PENDING_FUTURE.cancelled()) + self.assertFalse(RUNNING_FUTURE.cancelled()) + self.assertTrue(CANCELLED_FUTURE.cancelled()) + self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.cancelled()) + self.assertFalse(EXCEPTION_FUTURE.cancelled()) + self.assertFalse(SUCCESSFUL_FUTURE.cancelled()) + + def test_done(self): + self.assertFalse(PENDING_FUTURE.done()) + self.assertFalse(RUNNING_FUTURE.done()) + self.assertTrue(CANCELLED_FUTURE.done()) + self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.done()) + self.assertTrue(EXCEPTION_FUTURE.done()) + self.assertTrue(SUCCESSFUL_FUTURE.done()) + + def test_running(self): + self.assertFalse(PENDING_FUTURE.running()) + self.assertTrue(RUNNING_FUTURE.running()) + self.assertFalse(CANCELLED_FUTURE.running()) + self.assertFalse(CANCELLED_AND_NOTIFIED_FUTURE.running()) + self.assertFalse(EXCEPTION_FUTURE.running()) + self.assertFalse(SUCCESSFUL_FUTURE.running()) + + def test_result_with_timeout(self): + self.assertRaises(futures.TimeoutError, + PENDING_FUTURE.result, timeout=0) + self.assertRaises(futures.TimeoutError, + RUNNING_FUTURE.result, timeout=0) + self.assertRaises(futures.CancelledError, + CANCELLED_FUTURE.result, timeout=0) + self.assertRaises(futures.CancelledError, + CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) + self.assertRaises(IOError, EXCEPTION_FUTURE.result, timeout=0) + self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42) + + def test_result_with_success(self): + # TODO(brian@sweetapp.com): This test is timing dependant. + def notification(): + # Wait until the main thread is waiting for the result. + time.sleep(1) + f1.set_result(42) + + f1 = create_future(state=PENDING) + t = threading.Thread(target=notification) + t.start() + + self.assertEqual(f1.result(timeout=5), 42) + + def test_result_with_cancel(self): + # TODO(brian@sweetapp.com): This test is timing dependant. + def notification(): + # Wait until the main thread is waiting for the result. + time.sleep(1) + f1.cancel() + + f1 = create_future(state=PENDING) + t = threading.Thread(target=notification) + t.start() + + self.assertRaises(futures.CancelledError, f1.result, timeout=5) + + def test_exception_with_timeout(self): + self.assertRaises(futures.TimeoutError, + PENDING_FUTURE.exception, timeout=0) + self.assertRaises(futures.TimeoutError, + RUNNING_FUTURE.exception, timeout=0) + self.assertRaises(futures.CancelledError, + CANCELLED_FUTURE.exception, timeout=0) + self.assertRaises(futures.CancelledError, + CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) + self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), + IOError)) + self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None) + + def test_exception_with_success(self): + def notification(): + # Wait until the main thread is waiting for the exception. + time.sleep(1) + with f1._condition: + f1._state = FINISHED + f1._exception = IOError() + f1._condition.notify_all() + + f1 = create_future(state=PENDING) + t = threading.Thread(target=notification) + t.start() + + self.assertTrue(isinstance(f1.exception(timeout=5), IOError)) + +@reap_threads +def test_main(): + try: + test_support.run_unittest(ProcessPoolExecutorTest, + ThreadPoolExecutorTest, + ProcessPoolWaitTests, + ThreadPoolWaitTests, + ProcessPoolAsCompletedTests, + ThreadPoolAsCompletedTests, + FutureTests, + ProcessPoolShutdownTest, + ThreadPoolShutdownTest) + finally: + test_support.reap_children() + +if __name__ == "__main__": + test_main() diff --git a/third_party/pythonfutures/tox.ini b/third_party/pythonfutures/tox.ini new file mode 100755 index 00000000..c1ff2f13 --- /dev/null +++ b/third_party/pythonfutures/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist = py26,py27,py31 + +[testenv] +commands={envpython} test_futures.py [] + +[testenv:py26] +deps=unittest2 From f0c9878f870e244ffa3299fdee4cbffcfd53f84f Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 18:26:59 -0700 Subject: [PATCH 085/149] Ensuring we are serializing real Python objects Trying to json.dump Vim dictionaries fails. --- python/ycm/base.py | 2 +- python/ycm/vimsupport.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python/ycm/base.py b/python/ycm/base.py index 555c6c7f..dfcb5f7b 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -41,7 +41,7 @@ def BuildServerConf(): """Builds a dictionary mapping YCM Vim user options to values. Option names don't have the 'ycm_' prefix.""" - vim_globals = vimsupport.GetReadOnlyVimGlobals() + vim_globals = vimsupport.GetReadOnlyVimGlobals( force_python_objects = True ) server_conf = {} for key, value in vim_globals.items(): if not key.startswith( YCM_VAR_PREFIX ): diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index dacf3a4e..c4663c09 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -95,7 +95,10 @@ def LoadDictIntoVimGlobals( new_globals, overwrite = True ): # Changing the returned dict will NOT change the value in Vim. -def GetReadOnlyVimGlobals(): +def GetReadOnlyVimGlobals( force_python_objects = False ): + if force_python_objects: + return vim.eval( 'g:' ) + try: # vim.vars is fairly new so it might not exist return vim.vars From 1bba4a38ccf3a87d138ad0563b72cc88410790f3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 18:27:47 -0700 Subject: [PATCH 086/149] Ensuring the temp dir exists before returning it --- python/ycm/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 72dfd268..ec63afc2 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -38,7 +38,10 @@ def ToUtf8IfNeeded( string_or_unicode ): def PathToTempDir(): - return os.path.join( tempfile.gettempdir(), 'ycm_temp' ) + tempdir = os.path.join( tempfile.gettempdir(), 'ycm_temp' ) + if not os.path.exists( tempdir ): + os.makedirs( tempdir ) + return tempdir # From here: http://stackoverflow.com/a/8536476/1672783 From 915d092364ee9cdec845d9f3e471a99f0ba00975 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 18:32:39 -0700 Subject: [PATCH 087/149] Prefixing clang version with "Clang version" It's easier to know what that magic string is actually referring to. --- python/ycm/server/ycmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index acc7c2f8..c679d596 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -154,7 +154,7 @@ def DebugInfo(): has_clang_support ) ) if has_clang_support: - output.append( ycm_core.ClangVersion() ) + output.append( 'Clang version: ' + ycm_core.ClangVersion() ) request_data = request.json try: From a9d7105e1bfcb22a36e1805f0b08be7ee92f465b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 19:45:47 -0700 Subject: [PATCH 088/149] YCM now working on new buffers with ft set We used to demand a name be set for the buffer. Fixes #568. --- autoload/youcompleteme.vim | 5 ++++- python/ycm/client/base_request.py | 3 ++- python/ycm/vimsupport.py | 16 ++++++++++++++-- python/ycm/youcompleteme.py | 3 ++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index a9e76d4e..9efa2e92 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -87,7 +87,10 @@ function! youcompleteme#Enable() " is read. This is because youcompleteme#Enable() is called on VimEnter and " that happens *after" BufRead/BufEnter has already triggered for the " initial file. - autocmd BufRead,BufEnter * call s:OnBufferVisit() + " We also need to trigger buf init code on the FileType event because when + " the user does :enew and then :set ft=something, we need to run buf init + " code again. + autocmd BufRead,BufEnter,FileType * call s:OnBufferVisit() autocmd BufUnload * call s:OnBufferUnload( expand( ':p' ) ) autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd InsertLeave * call s:OnInsertLeave() diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index d67836a5..9f59f287 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -85,13 +85,14 @@ class BaseRequest( object ): def BuildRequestData( start_column = None, query = None ): line, column = vimsupport.CurrentLineAndColumn() + filepath = vimsupport.GetCurrentBufferFilepath() request_data = { 'filetypes': vimsupport.CurrentFiletypes(), 'line_num': line, 'column_num': column, 'start_column': start_column, 'line_value': vim.current.line, - 'filepath': vim.current.buffer.name, + 'filepath': filepath, 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() } diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index c4663c09..21d1daf2 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -69,7 +69,7 @@ def GetUnsavedAndCurrentBufferData(): buffer_object == vim.current.buffer ): continue - buffers_data[ buffer_object.name ] = { + buffers_data[ GetBufferFilepath( buffer_object ) ] = { 'contents': '\n'.join( buffer_object ), 'filetypes': FiletypesForBuffer( buffer_object ) } @@ -83,6 +83,18 @@ def GetBufferNumberForFilename( filename, open_file_if_needed = True ): int( open_file_if_needed ) ) ) ) +def GetCurrentBufferFilepath(): + return GetBufferFilepath( vim.current.buffer ) + + +def GetBufferFilepath( buffer_object ): + if buffer_object.name: + return buffer_object.name + # Buffers that have just been created by a command like :enew don't have any + # buffer name so we use the buffer number for that. + return os.path.join( os.getcwd(), str( buffer_object.number ) ) + + # Given a dict like {'a': 1}, loads it into Vim as if you ran 'let g:a = 1' # When |overwrite| is True, overwrites the existing value in Vim. def LoadDictIntoVimGlobals( new_globals, overwrite = True ): @@ -111,7 +123,7 @@ def JumpToLocation( filename, line, column ): # Add an entry to the jumplist vim.command( "normal! m'" ) - if filename != vim.current.buffer.name: + if filename != GetCurrentBufferFilepath(): # We prefix the command with 'keepjumps' so that opening the file is not # recorded in the jumplist. So when we open the file and move the cursor to # a location in it, the user can use CTRL-O to jump back to the original diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index f63c4872..2fd1adf5 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -116,7 +116,8 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): try: - return _NativeFiletypeCompletionAvailableForFile( vim.current.buffer.name ) + return _NativeFiletypeCompletionAvailableForFile( + vimsupport.GetCurrentBufferFilepath() ) except: return False From a836f6814216c5fc8d788cf96a6f127b527c6134 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sun, 6 Oct 2013 20:36:11 -0700 Subject: [PATCH 089/149] Fileype completer calls InCFamilyFile correctly --- python/ycm/completers/general/filename_completer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/ycm/completers/general/filename_completer.py b/python/ycm/completers/general/filename_completer.py index b6549b64..87f10bd7 100644 --- a/python/ycm/completers/general/filename_completer.py +++ b/python/ycm/completers/general/filename_completer.py @@ -77,9 +77,10 @@ class FilenameCompleter( Completer ): current_line = request_data[ 'line_value' ] start_column = request_data[ 'start_column' ] filepath = request_data[ 'filepath' ] + filetypes = request_data[ 'file_data' ][ filepath ][ 'filetypes' ] line = current_line[ :start_column ] - if InCFamilyFile(): + if InCFamilyFile( filetypes ): include_match = self._include_regex.search( line ) if include_match: path_dir = line[ include_match.end(): ] From 99b0f018b3ac2b98194c68877353fe6c1d9db25f Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 4 Oct 2013 17:34:20 -0700 Subject: [PATCH 090/149] Adding a minor TODO --- python/ycm/server/tests/basic_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 1514effb..b061c73c 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -27,6 +27,8 @@ import bottle bottle.debug( True ) +# TODO: Split this file into multiple files. + # 'contents' should be just one line of text def RequestDataForFileWithContents( filename, contents = None ): real_contents = contents if contents else '' From ff7fa74fc9da5d30d800370855a2aa8ad9e9059e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 13:09:34 -0700 Subject: [PATCH 091/149] works again (forces semantic completion) --- autoload/youcompleteme.vim | 7 +++--- python/ycm/client/completion_request.py | 4 +++- python/ycm/completers/completer.py | 5 +++-- python/ycm/server/server_state.py | 6 ++++- python/ycm/server/tests/basic_test.py | 29 ++++++++++++++++++++++--- python/ycm/utils.py | 5 +++++ python/ycm/youcompleteme.py | 4 ++-- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 9efa2e92..fd6f77e7 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -566,8 +566,7 @@ function! youcompleteme#Complete( findstart, base ) return -2 endif - py request = ycm_state.CreateCompletionRequest() - return pyeval( 'request.CompletionStartColumn()' ) + return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' ) else return s:CompletionsForQuery( a:base ) endif @@ -577,8 +576,8 @@ endfunction function! youcompleteme#OmniComplete( findstart, base ) if a:findstart let s:omnifunc_mode = 1 - " TODO: Force semantic mode here ( needs to work) - return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' ) + py request = ycm_state.CreateCompletionRequest( force_semantic = True ) + return pyeval( 'request.CompletionStartColumn()' ) else return s:CompletionsForQuery( a:base ) endif diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index d55ade1e..46298c54 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -24,11 +24,13 @@ from ycm.client.base_request import ( BaseRequest, BuildRequestData, class CompletionRequest( BaseRequest ): - def __init__( self ): + def __init__( self, force_semantic = False ): super( CompletionRequest, self ).__init__() self._completion_start_column = base.CompletionStartColumn() self._request_data = BuildRequestData( self._completion_start_column ) + if force_semantic: + self._request_data[ 'force_semantic' ] = True def CompletionStartColumn( self ): diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 99daa29b..ed068b0d 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -19,7 +19,7 @@ import abc import ycm_core -from ycm.utils import ToUtf8IfNeeded +from ycm.utils import ToUtf8IfNeeded, ForceSemanticCompletion from ycm.completers.completer_utils import TriggersForFiletype NO_USER_COMMANDS = 'This completer does not define any commands.' @@ -149,7 +149,8 @@ class Completer( object ): # It's highly likely you DON'T want to override this function but the *Inner # version of it. def ComputeCandidates( self, request_data ): - if not self.ShouldUseNow( request_data ): + if ( not ForceSemanticCompletion( request_data ) and + not self.ShouldUseNow( request_data ) ): return [] if ( request_data[ 'query' ] and diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 611b062c..2e9bd9ff 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -20,6 +20,7 @@ import imp import os from ycm import extra_conf_store +from ycm.utils import ForceSemanticCompletion from ycm.completers.general.general_completer_store import GeneralCompleterStore @@ -85,6 +86,7 @@ class ServerState( object ): except: return False + def FiletypeCompletionUsable( self, filetypes ): return ( self.CurrentFiletypeCompletionEnabled( filetypes ) and self.FiletypeCompletionAvailable( filetypes ) ) @@ -97,7 +99,9 @@ class ServerState( object ): def ShouldUseFiletypeCompleter( self, request_data ): filetypes = request_data[ 'filetypes' ] if self.FiletypeCompletionUsable( filetypes ): - return self.GetFiletypeCompleter( filetypes ).ShouldUseNow( request_data ) + return ( ForceSemanticCompletion( request_data ) or + self.GetFiletypeCompleter( filetypes ).ShouldUseNow( + request_data ) ) return False diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index b061c73c..4c1c74ac 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -30,16 +30,23 @@ bottle.debug( True ) # TODO: Split this file into multiple files. # 'contents' should be just one line of text -def RequestDataForFileWithContents( filename, contents = None ): +def RequestDataForFileWithContents( filename, + contents = None, + filetype = None ): real_contents = contents if contents else '' + filetype_to_use = filetype or 'foo' return { - 'filetypes': ['foo'], + 'query': '', + 'line_num': 0, + 'column_num': 0, + 'start_column': 0, + 'filetypes': [ filetype_to_use ], 'filepath': filename, 'line_value': real_contents, 'file_data': { filename: { 'contents': real_contents, - 'filetypes': ['foo'] + 'filetypes': [ filetype_to_use ] } } } @@ -120,6 +127,22 @@ int main() CompletionEntryMatcher( 'y' ) ) ) +@with_setup( Setup ) +def GetCompletions_ForceSemantic_Works_test(): + app = TestApp( ycmd.app ) + + completion_data = RequestDataForFileWithContents( 'foo.py', + filetype = 'python' ) + completion_data.update( { + 'force_semantic': True, + } ) + + results = app.post_json( '/completions', completion_data ).json + assert_that( results, has_items( CompletionEntryMatcher( 'abs' ), + CompletionEntryMatcher( 'open' ), + CompletionEntryMatcher( 'bool' ) ) ) + + @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/utils.py b/python/ycm/utils.py index ec63afc2..28fa7d05 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -77,3 +77,8 @@ def Memoize( obj ): cache[ key ] = obj( *args, **kwargs ) return cache[ key ] return memoizer + + +def ForceSemanticCompletion( request_data ): + return ( 'force_semantic' in request_data and + bool( request_data[ 'force_semantic' ] ) ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 2fd1adf5..e424cd5e 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -89,11 +89,11 @@ class YouCompleteMe( object ): shell = True ) - def CreateCompletionRequest( self ): + def CreateCompletionRequest( self, force_semantic = False ): # We have to store a reference to the newly created CompletionRequest # because VimScript can't store a reference to a Python object across # function calls... Thus we need to keep this request somewhere. - self._latest_completion_request = CompletionRequest() + self._latest_completion_request = CompletionRequest( force_semantic ) return self._latest_completion_request From 1db0e720bca6ad2e4ac7c016dfd2c71441961a91 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 13:55:29 -0700 Subject: [PATCH 092/149] Refactored the server tests for simplicity --- python/ycm/server/tests/basic_test.py | 225 +++++++++----------------- 1 file changed, 76 insertions(+), 149 deletions(-) diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 4c1c74ac..0dee7ba7 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -29,28 +29,40 @@ bottle.debug( True ) # TODO: Split this file into multiple files. -# 'contents' should be just one line of text -def RequestDataForFileWithContents( filename, - contents = None, - filetype = None ): - real_contents = contents if contents else '' - filetype_to_use = filetype or 'foo' - return { +def BuildRequest( **kwargs ): + filepath = kwargs[ 'filepath' ] if 'filepath' in kwargs else '/foo' + contents = kwargs[ 'contents' ] if 'contents' in kwargs else '' + filetype = kwargs[ 'filetype' ] if 'filetype' in kwargs else 'foo' + + request = { 'query': '', 'line_num': 0, 'column_num': 0, 'start_column': 0, - 'filetypes': [ filetype_to_use ], - 'filepath': filename, - 'line_value': real_contents, + 'filetypes': [ filetype ], + 'filepath': filepath, + 'line_value': contents, 'file_data': { - filename: { - 'contents': real_contents, - 'filetypes': [ filetype_to_use ] + filepath: { + 'contents': contents, + 'filetypes': [ filetype ] } } } + for key, value in kwargs.iteritems(): + if key in [ 'contents', 'filetype', 'filepath' ]: + continue + request[ key ] = value + + if key == 'line_num': + lines = contents.splitlines() + if len( lines ) > 1: + # NOTE: assumes 0-based line_num + request[ 'line_value' ] = lines[ value ] + + return request + # TODO: Make the other tests use this helper too instead of BuildCompletionData def CompletionEntryMatcher( insertion_text ): @@ -64,21 +76,14 @@ def Setup(): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_Works_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar', 'foo foogoo ba' ) - event_data.update( { - 'event_name': 'FileReadyToParse', - } ) + event_data = BuildRequest( contents = 'foo foogoo ba', + event_name = 'FileReadyToParse' ) app.post_json( '/event_notification', event_data ) - completion_data = RequestDataForFileWithContents( '/foo/bar', - 'oo foo foogoo ba' ) - completion_data.update( { - 'query': 'oo', - 'line_num': 0, - 'column_num': 2, - 'start_column': 0, - } ) + completion_data = BuildRequest( contents = 'oo foo foogoo ba', + query = 'oo', + column_num = 2 ) eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'foogoo' ) ], @@ -102,24 +107,14 @@ int main() } """ - filename = '/foo.cpp' - completion_data = { - 'compilation_flags': ['-x', 'c++'], - # 0-based line and column! - 'query': '', - 'line_num': 10, - 'column_num': 6, - 'start_column': 6, - 'line_value': ' foo.', - 'filetypes': ['cpp'], - 'filepath': filename, - 'file_data': { - filename: { - 'contents': contents, - 'filetypes': ['cpp'] - } - } - } + # 0-based line and column! + completion_data = BuildRequest( filepath = '/foo.cpp', + filetype = 'cpp', + contents = contents, + line_num = 10, + column_num = 6, + start_column = 6, + compilation_flags = ['-x', 'c++'] ) results = app.post_json( '/completions', completion_data ).json assert_that( results, has_items( CompletionEntryMatcher( 'c' ), @@ -131,11 +126,8 @@ int main() def GetCompletions_ForceSemantic_Works_test(): app = TestApp( ycmd.app ) - completion_data = RequestDataForFileWithContents( 'foo.py', - filetype = 'python' ) - completion_data.update( { - 'force_semantic': True, - } ) + completion_data = BuildRequest( filetype = 'python', + force_semantic = True ) results = app.post_json( '/completions', completion_data ).json assert_that( results, has_items( CompletionEntryMatcher( 'abs' ), @@ -146,22 +138,14 @@ def GetCompletions_ForceSemantic_Works_test(): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar' ) - event_data.update( { - 'event_name': 'FileReadyToParse', - 'syntax_keywords': ['foo', 'bar', 'zoo'] - } ) + event_data = BuildRequest( event_name = 'FileReadyToParse', + syntax_keywords = ['foo', 'bar', 'zoo'] ) app.post_json( '/event_notification', event_data ) - completion_data = RequestDataForFileWithContents( '/foo/bar', - 'oo ' ) - completion_data.update( { - 'query': 'oo', - 'line_num': 0, - 'column_num': 2, - 'start_column': 0, - } ) + completion_data = BuildRequest( contents = 'oo ', + query = 'oo', + column_num = 2 ) eq_( [ BuildCompletionData( 'foo' ), BuildCompletionData( 'zoo' ) ], @@ -171,24 +155,18 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): @with_setup( Setup ) def GetCompletions_UltiSnipsCompleter_Works_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar' ) - event_data.update( { - 'event_name': 'BufferVisit', - 'ultisnips_snippets': [ + event_data = BuildRequest( + event_name = 'BufferVisit', + ultisnips_snippets = [ {'trigger': 'foo', 'description': 'bar'}, {'trigger': 'zoo', 'description': 'goo'}, - ] - } ) + ] ) app.post_json( '/event_notification', event_data ) - completion_data = RequestDataForFileWithContents( '/foo/bar', 'oo ' ) - completion_data.update( { - 'query': 'oo', - 'line_num': 0, - 'column_num': 2, - 'start_column': 0, - } ) + completion_data = BuildRequest( contents = 'oo ', + query = 'oo', + column_num = 2 ) eq_( [ BuildCompletionData( 'foo', ' bar' ), BuildCompletionData( 'zoo', ' goo' ) ], @@ -205,20 +183,12 @@ def foo(): foo() """ - goto_data = { - 'completer_target': 'filetype_default', - 'command_arguments': ['GoToDefinition'], - 'line_num': 4, - 'column_num': 0, - 'filetypes': ['python'], - 'filepath': '/foo.py', - 'file_data': { - '/foo.py': { - 'contents': contents, - 'filetypes': ['python'] - } - } - } + goto_data = BuildRequest( completer_target = 'filetype_default', + command_arguments = ['GoToDefinition'], + line_num = 4, + contents = contents, + filetype = 'python', + filepath = '/foo.py' ) # 0-based line and column! eq_( { @@ -246,26 +216,17 @@ int main() } """ - filename = '/foo.cpp' - goto_data = { - 'compilation_flags': ['-x', 'c++'], - 'completer_target': 'filetype_default', - 'command_arguments': ['GoToDefinition'], - 'line_num': 9, - 'column_num': 2, - 'filetypes': ['cpp'], - 'filepath': filename, - 'file_data': { - filename: { - 'contents': contents, - 'filetypes': ['cpp'] - } - } - } + goto_data = BuildRequest( completer_target = 'filetype_default', + command_arguments = ['GoToDefinition'], + compilation_flags = ['-x', 'c++'], + line_num = 9, + column_num = 2, + contents = contents, + filetype = 'cpp' ) # 0-based line and column! eq_( { - 'filepath': '/foo.cpp', + 'filepath': '/foo', 'line_num': 1, 'column_num': 7 }, @@ -275,10 +236,7 @@ int main() @with_setup( Setup ) def DefinedSubcommands_Works_test(): app = TestApp( ycmd.app ) - subcommands_data = RequestDataForFileWithContents( '/foo/bar' ) - subcommands_data.update( { - 'completer_target': 'python', - } ) + subcommands_data = BuildRequest( completer_target = 'python' ) eq_( [ 'GoToDefinition', 'GoToDeclaration', @@ -289,17 +247,7 @@ def DefinedSubcommands_Works_test(): @with_setup( Setup ) def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): app = TestApp( ycmd.app ) - filename = 'foo.py' - subcommands_data = { - 'filetypes': ['python'], - 'filepath': filename, - 'file_data': { - filename: { - 'contents': '', - 'filetypes': ['python'] - } - } - } + subcommands_data = BuildRequest( filetype = 'python' ) eq_( [ 'GoToDefinition', 'GoToDeclaration', @@ -319,21 +267,10 @@ struct Foo { }; """ - filename = '/foo.cpp' - event_data = { - 'event_name': 'FileReadyToParse', - 'compilation_flags': ['-x', 'c++'], - 'line_num': 0, - 'column_num': 0, - 'filetypes': ['cpp'], - 'filepath': filename, - 'file_data': { - filename: { - 'contents': contents, - 'filetypes': ['cpp'] - } - } - } + event_data = BuildRequest( compilation_flags = ['-x', 'c++'], + event_name = 'FileReadyToParse', + contents = contents, + filetype = 'cpp' ) results = app.post_json( '/event_notification', event_data ).json assert_that( results, @@ -355,20 +292,10 @@ struct Foo { }; """ - filename = '/foo.cpp' - diag_data = { - 'compilation_flags': ['-x', 'c++'], - 'line_num': 2, - 'column_num': 0, - 'filetypes': ['cpp'], - 'filepath': filename, - 'file_data': { - filename: { - 'contents': contents, - 'filetypes': ['cpp'] - } - } - } + diag_data = BuildRequest( compilation_flags = ['-x', 'c++'], + line_num = 2, + contents = contents, + filetype = 'cpp' ) event_data = diag_data.copy() event_data.update( { From 4b73739b096d6db3a3076cf2df20cf31567e42fd Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 14:21:46 -0700 Subject: [PATCH 093/149] Removing some obsolete todos --- python/ycm/client/base_request.py | 2 -- python/ycm/youcompleteme.py | 1 - 2 files changed, 3 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 9f59f287..7932bca4 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -26,8 +26,6 @@ from concurrent.futures import ThreadPoolExecutor from ycm import vimsupport HEADERS = {'content-type': 'application/json'} -# TODO: This TPE might be the reason we're shutting down slowly. It seems that -# it waits for all worker threads to stop before letting the interpreter exit. EXECUTOR = ThreadPoolExecutor( max_workers = 4 ) class ServerError( Exception ): diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index e424cd5e..d6a7a2bb 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -169,7 +169,6 @@ class YouCompleteMe( object ): def OnVimLeave( self ): - # TODO: There should be a faster way of shutting down the server self._server_popen.terminate() os.remove( self._temp_options_filename ) From c7be1f1b47f5cc31f5dcca55f5fd244b35fc7498 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 15:47:48 -0700 Subject: [PATCH 094/149] Omni completion works again --- autoload/youcompleteme.vim | 2 + python/ycm/client/completion_request.py | 10 ++-- python/ycm/client/omni_completion_request.py | 39 +++++++++++++ python/ycm/completers/all/omni_completer.py | 58 ++++++++------------ python/ycm/youcompleteme.py | 23 +++----- 5 files changed, 78 insertions(+), 54 deletions(-) create mode 100644 python/ycm/client/omni_completion_request.py diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index fd6f77e7..de3bc3c9 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -308,6 +308,8 @@ function! s:SetCompleteFunc() let &completefunc = 'youcompleteme#Complete' let &l:completefunc = 'youcompleteme#Complete' + " TODO: This makes startup slower because it blocks on the server. Explore + " other options. if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) let &omnifunc = 'youcompleteme#OmniComplete' let &l:omnifunc = 'youcompleteme#OmniComplete' diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index 46298c54..977a1122 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -28,9 +28,11 @@ class CompletionRequest( BaseRequest ): super( CompletionRequest, self ).__init__() self._completion_start_column = base.CompletionStartColumn() - self._request_data = BuildRequestData( self._completion_start_column ) + + # This field is also used by the omni_completion_request subclass + self.request_data = BuildRequestData( self._completion_start_column ) if force_semantic: - self._request_data[ 'force_semantic' ] = True + self.request_data[ 'force_semantic' ] = True def CompletionStartColumn( self ): @@ -38,8 +40,8 @@ class CompletionRequest( BaseRequest ): def Start( self, query ): - self._request_data[ 'query' ] = query - self._response_future = self.PostDataToHandlerAsync( self._request_data, + self.request_data[ 'query' ] = query + self._response_future = self.PostDataToHandlerAsync( self.request_data, 'completions' ) diff --git a/python/ycm/client/omni_completion_request.py b/python/ycm/client/omni_completion_request.py new file mode 100644 index 00000000..2eb26a41 --- /dev/null +++ b/python/ycm/client/omni_completion_request.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from ycm.client.completion_request import CompletionRequest + + +class OmniCompletionRequest( CompletionRequest ): + def __init__( self, omni_completer ): + super( OmniCompletionRequest, self ).__init__() + self._omni_completer = omni_completer + + + def Start( self, query ): + self.request_data[ 'query' ] = query + self._results = self._omni_completer.ComputeCandidates( self.request_data ) + + + def Done( self ): + return True + + + def Response( self ): + return self._results diff --git a/python/ycm/completers/all/omni_completer.py b/python/ycm/completers/all/omni_completer.py index d0a51786..41d81452 100644 --- a/python/ycm/completers/all/omni_completer.py +++ b/python/ycm/completers/all/omni_completer.py @@ -28,14 +28,17 @@ OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" ' class OmniCompleter( Completer ): def __init__( self, user_options ): super( OmniCompleter, self ).__init__( user_options ) - self.omnifunc = None - self.stored_candidates = None + self._omnifunc = None def SupportedFiletypes( self ): return [] + def Available( self ): + return bool( self._omnifunc ) + + def ShouldUseCache( self ): return bool( self.user_options[ 'cache_omnifunc' ] ) @@ -47,31 +50,31 @@ class OmniCompleter( Completer ): def ShouldUseNowInner( self, request_data ): - if not self.omnifunc: + if not self._omnifunc: return False return super( OmniCompleter, self ).ShouldUseNowInner( request_data ) - def CandidatesForQueryAsync( self, request_data ): + def ComputeCandidates( self, request_data ): if self.ShouldUseCache(): - return super( OmniCompleter, self ).CandidatesForQueryAsync( + return super( OmniCompleter, self ).ComputeCandidates( request_data ) else: - return self.CandidatesForQueryAsyncInner( request_data ) + if self.ShouldUseNowInner( request_data ): + return self.ComputeCandidatesInner( request_data ) + return [] - def CandidatesForQueryAsyncInner( self, request_data ): - if not self.omnifunc: - self.stored_candidates = None - return + def ComputeCandidatesInner( self, request_data ): + if not self._omnifunc: + return [] try: - return_value = int( vim.eval( self.omnifunc + '(1,"")' ) ) + return_value = int( vim.eval( self._omnifunc + '(1,"")' ) ) if return_value < 0: - self.stored_candidates = None - return + return [] - omnifunc_call = [ self.omnifunc, + omnifunc_call = [ self._omnifunc, "(0,'", vimsupport.EscapeForVim( request_data[ 'query' ] ), "')" ] @@ -79,34 +82,17 @@ class OmniCompleter( Completer ): items = vim.eval( ''.join( omnifunc_call ) ) if 'words' in items: - items = items['words'] + items = items[ 'words' ] if not hasattr( items, '__iter__' ): raise TypeError( OMNIFUNC_NOT_LIST ) - self.stored_candidates = filter( bool, items ) - except (TypeError, ValueError) as error: + return filter( bool, items ) + except ( TypeError, ValueError ) as error: vimsupport.PostVimMessage( OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) ) - self.stored_candidates = None - return - - - - def AsyncCandidateRequestReadyInner( self ): - return True + return [] def OnFileReadyToParse( self, request_data ): - self.omnifunc = vim.eval( '&omnifunc' ) - - - def CandidatesFromStoredRequest( self ): - if self.ShouldUseCache(): - return super( OmniCompleter, self ).CandidatesFromStoredRequest() - else: - return self.CandidatesFromStoredRequestInner() - - - def CandidatesFromStoredRequestInner( self ): - return self.stored_candidates if self.stored_candidates else [] + self._omnifunc = vim.eval( '&omnifunc' ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index d6a7a2bb..3e64e1f3 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -29,6 +29,7 @@ from ycm.completers.general import syntax_parse from ycm.client.base_request import BaseRequest, BuildRequestData from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest +from ycm.client.omni_completion_request import OmniCompletionRequest from ycm.client.event_notification import ( SendEventNotificationAsync, EventNotification ) @@ -93,7 +94,12 @@ class YouCompleteMe( object ): # We have to store a reference to the newly created CompletionRequest # because VimScript can't store a reference to a Python object across # function calls... Thus we need to keep this request somewhere. - self._latest_completion_request = CompletionRequest( force_semantic ) + if ( not self.NativeFiletypeCompletionAvailable() and + self.CurrentFiletypeCompletionEnabled() and + self._omnicomp.Available() ): + self._latest_completion_request = OmniCompletionRequest( self._omnicomp ) + else: + self._latest_completion_request = CompletionRequest( force_semantic ) return self._latest_completion_request @@ -122,25 +128,14 @@ class YouCompleteMe( object ): return False - # TODO: This may not be needed at all when the server is ready. Evaluate this - # later. - # def FiletypeCompletionAvailable( self ): - # return bool( self.GetFiletypeCompleter() ) - - def NativeFiletypeCompletionUsable( self ): return ( self.CurrentFiletypeCompletionEnabled() and self.NativeFiletypeCompletionAvailable() ) - # TODO: This may not be needed at all when the server is ready. Evaluate this - # later. - # def FiletypeCompletionUsable( self ): - # return ( self.CurrentFiletypeCompletionEnabled() and - # self.FiletypeCompletionAvailable() ) - - def OnFileReadyToParse( self ): + self._omnicomp.OnFileReadyToParse( None ) + extra_data = {} if self._user_options[ 'collect_identifiers_from_tags_files' ]: extra_data[ 'tag_files' ] = _GetTagFiles() From 5000d2e4ae21c001b25e5b3a3962a963d76cfe1d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 16:10:48 -0700 Subject: [PATCH 095/149] NativeFiletypeCompletionAvailable now a local call It used to block on the server to get the data. Now it doesn't anymore. This speeds up Vim startup. --- autoload/youcompleteme.vim | 2 -- python/ycm/completers/completer_utils.py | 13 +++++++++++++ python/ycm/server/server_state.py | 15 ++------------- python/ycm/youcompleteme.py | 19 +++---------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index de3bc3c9..fd6f77e7 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -308,8 +308,6 @@ function! s:SetCompleteFunc() let &completefunc = 'youcompleteme#Complete' let &l:completefunc = 'youcompleteme#Complete' - " TODO: This makes startup slower because it blocks on the server. Explore - " other options. if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) let &omnifunc = 'youcompleteme#OmniComplete' let &l:omnifunc = 'youcompleteme#OmniComplete' diff --git a/python/ycm/completers/completer_utils.py b/python/ycm/completers/completer_utils.py index 4fbd9b20..c700e690 100644 --- a/python/ycm/completers/completer_utils.py +++ b/python/ycm/completers/completer_utils.py @@ -19,6 +19,7 @@ from collections import defaultdict from copy import deepcopy +import os DEFAULT_FILETYPE_TRIGGERS = { 'c' : ['->', '.'], @@ -63,3 +64,15 @@ def TriggersForFiletype( user_triggers ): return _FiletypeDictUnion( default_triggers, dict( user_triggers ) ) + +def _PathToCompletersFolder(): + dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) + return os.path.join( dir_of_current_script ) + + +def PathToFiletypeCompleterPluginLoader( filetype ): + return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' ) + + +def FiletypeCompleterExistsForFiletype( filetype ): + return os.path.exists( PathToFiletypeCompleterPluginLoader( filetype ) ) diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 2e9bd9ff..88b75cb6 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -22,6 +22,7 @@ import os from ycm import extra_conf_store from ycm.utils import ForceSemanticCompletion from ycm.completers.general.general_completer_store import GeneralCompleterStore +from ycm.completers.completer_utils import PathToFiletypeCompleterPluginLoader class ServerState( object ): @@ -52,8 +53,7 @@ class ServerState( object ): except KeyError: pass - module_path = _PathToFiletypeCompleterPluginLoader( filetype ) - + module_path = PathToFiletypeCompleterPluginLoader( filetype ) completer = None supported_filetypes = [ filetype ] if os.path.exists( module_path ): @@ -114,14 +114,3 @@ class ServerState( object ): 'filetype_specific_completion_to_disable' ] return not all([ x in filetype_to_disable for x in current_filetypes ]) - - -def _PathToCompletersFolder(): - dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) - return os.path.join( dir_of_current_script, '..', 'completers' ) - - -def _PathToFiletypeCompleterPluginLoader( filetype ): - return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' ) - - diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 3e64e1f3..4d7f67b4 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -26,6 +26,7 @@ from ycm import vimsupport from ycm import utils from ycm.completers.all.omni_completer import OmniCompleter from ycm.completers.general import syntax_parse +from ycm.completers.completer_utils import FiletypeCompleterExistsForFiletype from ycm.client.base_request import BaseRequest, BuildRequestData from ycm.client.command_request import SendCommandRequest from ycm.client.completion_request import CompletionRequest @@ -121,11 +122,8 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): - try: - return _NativeFiletypeCompletionAvailableForFile( - vimsupport.GetCurrentBufferFilepath() ) - except: - return False + return any( [ FiletypeCompleterExistsForFiletype( x ) for x in + vimsupport.CurrentFiletypes() ] ) def NativeFiletypeCompletionUsable( self ): @@ -252,14 +250,3 @@ def _AddUltiSnipsDataIfNeeded( extra_data ): extra_data[ 'ultisnips_snippets' ] = [ { 'trigger': x.trigger, 'description': x.description } for x in rawsnips ] - - -# 'filepath' is here only as a key for Memoize -# This can't be a nested function inside NativeFiletypeCompletionAvailable -# because then the Memoize decorator wouldn't work (nested functions are -# re-created on every call to the outer function). -@utils.Memoize -def _NativeFiletypeCompletionAvailableForFile( filepath ): - return BaseRequest.PostDataToHandler( BuildRequestData(), - 'filetype_completion_available') - From ebb1627f2ed0dceec03e9ac928236461efed539d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 16:28:40 -0700 Subject: [PATCH 096/149] Resolving a minor TODO /filetype_completion_available handler renamed to /semantic_completion_available --- python/ycm/server/tests/basic_test.py | 2 +- python/ycm/server/ycmd.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 0dee7ba7..0ba9e1b1 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -315,7 +315,7 @@ def FiletypeCompletionAvailable_Works_test(): 'filetypes': ['python'] } - ok_( app.post_json( '/filetype_completion_available', + ok_( app.post_json( '/semantic_completion_available', request_data ).json ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index c679d596..2d3e3f31 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -117,8 +117,7 @@ def SetUserOptions(): _SetUserOptions( request.json ) -# TODO: Rename this to 'semantic_completion_available' -@app.post( '/filetype_completion_available') +@app.post( '/semantic_completion_available') def FiletypeCompletionAvailable(): LOGGER.info( 'Received filetype completion available request') return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable( From f0650ddc7fabf60f362fc36893b8dc5995e102a8 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 7 Oct 2013 16:53:41 -0700 Subject: [PATCH 097/149] Minor style fixes --- python/ycm/server/ycmd.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 2d3e3f31..889b2b7d 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -54,7 +54,7 @@ app = bottle.Bottle() @app.post( '/event_notification' ) def EventNotification(): - LOGGER.info( 'Received event notification') + LOGGER.info( 'Received event notification' ) request_data = request.json event_name = request_data[ 'event_name' ] LOGGER.debug( 'Event name: %s', event_name ) @@ -75,7 +75,7 @@ def EventNotification(): @app.post( '/run_completer_command' ) def RunCompleterCommand(): - LOGGER.info( 'Received command request') + LOGGER.info( 'Received command request' ) request_data = request.json completer = _GetCompleterForRequestData( request_data ) @@ -86,7 +86,7 @@ def RunCompleterCommand(): @app.post( '/completions' ) def GetCompletions(): - LOGGER.info( 'Received completion request') + LOGGER.info( 'Received completion request' ) request_data = request.json do_filetype_completion = SERVER_STATE.ShouldUseFiletypeCompleter( request_data ) @@ -101,51 +101,51 @@ def GetCompletions(): @app.get( '/user_options' ) def GetUserOptions(): - LOGGER.info( 'Received user options GET request') + LOGGER.info( 'Received user options GET request' ) return _JsonResponse( dict( SERVER_STATE.user_options ) ) @app.get( '/healthy' ) def GetHealthy(): - LOGGER.info( 'Received health request') + LOGGER.info( 'Received health request' ) return _JsonResponse( True ) @app.post( '/user_options' ) def SetUserOptions(): - LOGGER.info( 'Received user options POST request') + LOGGER.info( 'Received user options POST request' ) _SetUserOptions( request.json ) -@app.post( '/semantic_completion_available') +@app.post( '/semantic_completion_available' ) def FiletypeCompletionAvailable(): - LOGGER.info( 'Received filetype completion available request') + LOGGER.info( 'Received filetype completion available request' ) return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable( request.json[ 'filetypes' ] ) ) -@app.post( '/defined_subcommands') +@app.post( '/defined_subcommands' ) def DefinedSubcommands(): - LOGGER.info( 'Received defined subcommands request') + LOGGER.info( 'Received defined subcommands request' ) completer = _GetCompleterForRequestData( request.json ) return _JsonResponse( completer.DefinedSubcommands() ) -@app.post( '/detailed_diagnostic') +@app.post( '/detailed_diagnostic' ) def GetDetailedDiagnostic(): - LOGGER.info( 'Received detailed diagnostic request') + LOGGER.info( 'Received detailed diagnostic request' ) request_data = request.json completer = _GetCompleterForRequestData( request_data ) return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) ) -@app.post( '/debug_info') +@app.post( '/debug_info' ) def DebugInfo(): # This can't be at the top level because of possible extra conf preload import ycm_core - LOGGER.info( 'Received debug info request') + LOGGER.info( 'Received debug info request' ) output = [] has_clang_support = ycm_core.HasClangSupport() From 3d55748400f1267f08c53322791d43b74625a058 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 16:21:43 -0700 Subject: [PATCH 098/149] Correctly handling ycm_extra_conf files now The user is asked about loading unknown extra conf files, as they were before. --- python/ycm/client/base_request.py | 15 +++--- python/ycm/client/completion_request.py | 2 +- python/ycm/client/event_notification.py | 12 ++++- python/ycm/completers/cpp/clang_completer.py | 3 ++ python/ycm/completers/cpp/flags.py | 6 ++- python/ycm/extra_conf_store.py | 50 +++++++++++------- python/ycm/server/responses.py | 20 +++++++- python/ycm/server/tests/basic_test.py | 54 +++++++++++++++++++- python/ycm/server/tests/testdata/basic.cpp | 13 +++++ python/ycm/server/ycmd.py | 25 +++++++-- 10 files changed, 162 insertions(+), 38 deletions(-) create mode 100644 python/ycm/server/tests/testdata/basic.cpp diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 7932bca4..ddd2b2c8 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -24,15 +24,11 @@ from retries import retries from requests_futures.sessions import FuturesSession from concurrent.futures import ThreadPoolExecutor from ycm import vimsupport +from ycm.server.responses import ServerError, UnknownExtraConf HEADERS = {'content-type': 'application/json'} EXECUTOR = ThreadPoolExecutor( max_workers = 4 ) -class ServerError( Exception ): - def __init__( self, message ): - super( ServerError, self ).__init__( message ) - - class BaseRequest( object ): def __init__( self ): pass @@ -103,7 +99,7 @@ def BuildRequestData( start_column = None, query = None ): def JsonFromFuture( future ): response = future.result() if response.status_code == requests.codes.server_error: - raise ServerError( response.json()[ 'message' ] ) + _RaiseExceptionForData( response.json() ) # We let Requests handle the other status types, we only handle the 500 # error code. @@ -137,3 +133,10 @@ def _CheckServerIsHealthyWithCache(): except: return False + +def _RaiseExceptionForData( data ): + if data[ 'exception' ][ 'TYPE' ] == UnknownExtraConf.__name__: + raise UnknownExtraConf( data[ 'exception' ][ 'extra_conf_file' ] ) + + raise ServerError( '{0}: {1}'.format( data[ 'exception' ][ 'TYPE' ], + data[ 'message' ] ) ) diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index 977a1122..e8af21a7 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -57,7 +57,7 @@ class CompletionRequest( BaseRequest ): for x in JsonFromFuture( self._response_future ) ] except Exception as e: vimsupport.PostVimMessage( str( e ) ) - return [] + return [] def _ConvertCompletionDataToVimData( completion_data ): diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index 5f79b4a5..9652413f 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -18,9 +18,11 @@ # along with YouCompleteMe. If not, see . from ycm import vimsupport +from ycm.server.responses import UnknownExtraConf from ycm.client.base_request import ( BaseRequest, BuildRequestData, JsonFromFuture ) + class EventNotification( BaseRequest ): def __init__( self, event_name, extra_data = None ): super( EventNotification, self ).__init__() @@ -51,10 +53,13 @@ class EventNotification( BaseRequest ): return [] try: - self._cached_response = JsonFromFuture( self._response_future ) + try: + self._cached_response = JsonFromFuture( self._response_future ) + except UnknownExtraConf as e: + if vimsupport.Confirm( str( e ) ): + _LoadExtraConfFile( e.extra_conf_file ) except Exception as e: vimsupport.PostVimMessage( str( e ) ) - return [] if not self._cached_response: return [] @@ -83,3 +88,6 @@ def SendEventNotificationAsync( event_name, extra_data = None ): event = EventNotification( event_name, extra_data ) event.Start() +def _LoadExtraConfFile( filepath ): + BaseRequest.PostDataToHandler( { 'filepath': filepath }, + 'load_extra_conf_file' ) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 7f50a7ba..154452fa 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -79,6 +79,9 @@ class ClangCompleter( Completer ): if self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): self._logger.info( PARSING_FILE_MESSAGE ) + # TODO: For this exception and the NO_COMPILE_FLAGS one, use a special + # exception class so that the client can be more silent about these + # messages. raise RuntimeError( PARSING_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) diff --git a/python/ycm/completers/cpp/flags.py b/python/ycm/completers/cpp/flags.py index a896986f..385bdc03 100644 --- a/python/ycm/completers/cpp/flags.py +++ b/python/ycm/completers/cpp/flags.py @@ -37,6 +37,7 @@ class Flags( object ): # It's caches all the way down... self.flags_for_file = {} self.special_clang_flags = _SpecialClangIncludes() + self.no_extra_conf_file_warning_posted = False def FlagsForFile( self, filename, add_special_clang_flags = True ): @@ -45,7 +46,10 @@ class Flags( object ): except KeyError: module = extra_conf_store.ModuleForSourceFile( filename ) if not module: - raise RuntimeError( NO_EXTRA_CONF_FILENAME_MESSAGE ) + if not self.no_extra_conf_file_warning_posted: + self.no_extra_conf_file_warning_posted = True + raise RuntimeError( NO_EXTRA_CONF_FILENAME_MESSAGE ) + return None results = module.FlagsForFile( filename ) diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 277b9d61..5253e021 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -24,27 +24,29 @@ import imp import random import string import sys +from threading import Lock from ycm import user_options_store +from ycm.server.responses import UnknownExtraConf from fnmatch import fnmatch # Constants YCM_EXTRA_CONF_FILENAME = '.ycm_extra_conf.py' -CONFIRM_CONF_FILE_MESSAGE = ('Found {0}. Load? \n\n(Question can be turned ' - 'off with options, see YCM docs)') # Singleton variables _module_for_module_file = {} +_module_for_module_file_lock = Lock() _module_file_for_source_file = {} +_module_file_for_source_file_lock = Lock() -class UnknownExtraConf( Exception ): - def __init__( self, extra_conf_file ): - message = CONFIRM_CONF_FILE_MESSAGE.format( extra_conf_file ) - super( UnknownExtraConf, self ).__init__( message ) - self.extra_conf_file = extra_conf_file + +def Reset(): + global _module_for_module_file, _module_file_for_source_file + _module_for_module_file = {} + _module_file_for_source_file = {} def ModuleForSourceFile( filename ): - return _Load( ModuleFileForSourceFile( filename ) ) + return Load( ModuleFileForSourceFile( filename ) ) def ModuleFileForSourceFile( filename ): @@ -52,11 +54,12 @@ def ModuleFileForSourceFile( filename ): order and return the filename of the first module that was allowed to load. If no module was found or allowed to load, None is returned.""" - if not filename in _module_file_for_source_file: - for module_file in _ExtraConfModuleSourceFilesForFile( filename ): - if _Load( module_file ): - _module_file_for_source_file[ filename ] = module_file - break + with _module_file_for_source_file_lock: + if not filename in _module_file_for_source_file: + for module_file in _ExtraConfModuleSourceFilesForFile( filename ): + if Load( module_file ): + _module_file_for_source_file[ filename ] = module_file + break return _module_file_for_source_file.setdefault( filename ) @@ -84,7 +87,8 @@ def _CallExtraConfMethod( function_name ): def _Disable( module_file ): """Disables the loading of a module for the current session.""" - _module_for_module_file[ module_file ] = None + with _module_for_module_file_lock: + _module_for_module_file[ module_file ] = None def _ShouldLoad( module_file ): @@ -102,10 +106,15 @@ def _ShouldLoad( module_file ): if _MatchesGlobPattern( module_file, glob.lstrip('!') ): return not is_blacklisted + # We disable the file if it's unknown so that we don't ask the user about it + # repeatedly. Raising UnknownExtraConf should result in the client sending + # another request to load the module file if the user explicitly chooses to do + # that. + _Disable( module_file ) raise UnknownExtraConf( module_file ) -def _Load( module_file, force = False ): +def Load( module_file, force = False ): """Load and return the module contained in a file. Using force = True the module will be loaded regardless of the criteria in _ShouldLoad. @@ -115,11 +124,13 @@ def _Load( module_file, force = False ): return None if not force: - if module_file in _module_for_module_file: - return _module_for_module_file[ module_file ] + with _module_for_module_file_lock: + if module_file in _module_for_module_file: + return _module_for_module_file[ module_file ] if not _ShouldLoad( module_file ): - return _Disable( module_file ) + _Disable( module_file ) + return None # This has to be here because a long time ago, the ycm_extra_conf.py files # used to import clang_helpers.py from the cpp folder. This is not needed @@ -129,7 +140,8 @@ def _Load( module_file, force = False ): module = imp.load_source( _RandomName(), module_file ) del sys.path[ 0 ] - _module_for_module_file[ module_file ] = module + with _module_for_module_file_lock: + _module_for_module_file[ module_file ] = module return module diff --git a/python/ycm/server/responses.py b/python/ycm/server/responses.py index 450106d9..f0ca6364 100644 --- a/python/ycm/server/responses.py +++ b/python/ycm/server/responses.py @@ -19,6 +19,21 @@ import os +CONFIRM_CONF_FILE_MESSAGE = ('Found {0}. Load? \n\n(Question can be turned ' + 'off with options, see YCM docs)') + +class ServerError( Exception ): + def __init__( self, message ): + super( ServerError, self ).__init__( message ) + + +class UnknownExtraConf( ServerError ): + def __init__( self, extra_conf_file ): + message = CONFIRM_CONF_FILE_MESSAGE.format( extra_conf_file ) + super( UnknownExtraConf, self ).__init__( message ) + self.extra_conf_file = extra_conf_file + + def BuildGoToResponse( filepath, line_num, column_num, description = None ): response = { @@ -80,9 +95,10 @@ def BuildDiagnosticData( filepath, } -def BuildExceptionResponse( error_message, traceback ): +def BuildExceptionResponse( exception, traceback ): return { - 'message': error_message, + 'exception': exception, + 'message': str( exception ), 'traceback': traceback } diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 0ba9e1b1..8c2cdc5b 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -17,9 +17,11 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +import os +import httplib from webtest import TestApp from .. import ycmd -from ..responses import BuildCompletionData +from ..responses import BuildCompletionData, UnknownExtraConf from nose.tools import ok_, eq_, with_setup from hamcrest import ( assert_that, has_items, has_entry, contains, contains_string, has_entries ) @@ -73,6 +75,15 @@ def Setup(): ycmd.SetServerStateToDefaults() +def PathToTestDataDir(): + dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) ) + return os.path.join( dir_of_current_script, 'testdata' ) + + +def PathToTestFile( test_basename ): + return os.path.join( PathToTestDataDir(), test_basename ) + + @with_setup( Setup ) def GetCompletions_IdentifierCompleter_Works_test(): app = TestApp( ycmd.app ) @@ -91,7 +102,7 @@ def GetCompletions_IdentifierCompleter_Works_test(): @with_setup( Setup ) -def GetCompletions_ClangCompleter_Works_test(): +def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test(): app = TestApp( ycmd.app ) contents = """ struct Foo { @@ -122,6 +133,45 @@ int main() CompletionEntryMatcher( 'y' ) ) ) +@with_setup( Setup ) +def GetCompletions_ClangCompleter_UnknownExtraConfException_test(): + app = TestApp( ycmd.app ) + filepath = PathToTestFile( 'basic.cpp' ) + completion_data = BuildRequest( filepath = filepath, + filetype = 'cpp', + contents = open( filepath ).read(), + force_semantic = True ) + + response = app.post_json( '/completions', + completion_data, + expect_errors = True ) + + eq_( response.status_code, httplib.INTERNAL_SERVER_ERROR ) + assert_that( response.json, + has_entry( 'exception', + has_entry( 'TYPE', UnknownExtraConf.__name__ ) ) ) + + +@with_setup( Setup ) +def GetCompletions_ClangCompleter_WorksWhenExtraConfExplicitlyAllowed_test(): + app = TestApp( ycmd.app ) + app.post_json( '/load_extra_conf_file', + { 'filepath': PathToTestFile( '.ycm_extra_conf.py' ) } ) + + filepath = PathToTestFile( 'basic.cpp' ) + completion_data = BuildRequest( filepath = filepath, + filetype = 'cpp', + contents = open( filepath ).read(), + line_num = 10, + column_num = 6, + start_column = 6 ) + + results = app.post_json( '/completions', completion_data ).json + assert_that( results, has_items( CompletionEntryMatcher( 'c' ), + CompletionEntryMatcher( 'x' ), + CompletionEntryMatcher( 'y' ) ) ) + + @with_setup( Setup ) def GetCompletions_ForceSemantic_Works_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/tests/testdata/basic.cpp b/python/ycm/server/tests/testdata/basic.cpp new file mode 100644 index 00000000..a20e2dd0 --- /dev/null +++ b/python/ycm/server/tests/testdata/basic.cpp @@ -0,0 +1,13 @@ +struct Foo { + int x; + int y; + char c; +}; + +int main() +{ + Foo foo; + // The location after the dot is line 11, col 7 + foo. +} + diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 889b2b7d..0619c193 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -36,17 +36,19 @@ utils.AddThirdPartyFoldersToSysPath() import logging import json import bottle +import argparse +import httplib from bottle import run, request, response import server_state from ycm import user_options_store from ycm.server.responses import BuildExceptionResponse -import argparse -import httplib +from ycm import extra_conf_store # num bytes for the request body buffer; request.json only works if the request # size is less than this bottle.Request.MEMFILE_MAX = 300 * 1024 +# TODO: rename these to _lower_case SERVER_STATE = None LOGGER = None app = bottle.Bottle() @@ -72,7 +74,6 @@ def EventNotification(): return _JsonResponse( response_data ) - @app.post( '/run_completer_command' ) def RunCompleterCommand(): LOGGER.info( 'Received command request' ) @@ -141,6 +142,13 @@ def GetDetailedDiagnostic(): return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) ) +@app.post( '/load_extra_conf_file' ) +def LoadExtraConfFile(): + LOGGER.info( 'Received extra conf load request' ) + request_data = request.json + extra_conf_store.Load( request_data[ 'filepath' ], force = True ) + + @app.post( '/debug_info' ) def DebugInfo(): # This can't be at the top level because of possible extra conf preload @@ -167,13 +175,19 @@ def DebugInfo(): # The type of the param is Bottle.HTTPError @app.error( httplib.INTERNAL_SERVER_ERROR ) def ErrorHandler( httperror ): - return _JsonResponse( BuildExceptionResponse( str( httperror.exception ), + return _JsonResponse( BuildExceptionResponse( httperror.exception, httperror.traceback ) ) def _JsonResponse( data ): response.set_header( 'Content-Type', 'application/json' ) - return json.dumps( data ) + return json.dumps( data, default = _UniversalSerialize ) + + +def _UniversalSerialize( obj ): + serialized = obj.__dict__.copy() + serialized[ 'TYPE' ] = type( obj ).__name__ + return serialized def _GetCompleterForRequestData( request_data ): @@ -205,6 +219,7 @@ def SetServerStateToDefaults(): LOGGER = logging.getLogger( __name__ ) user_options_store.LoadDefaults() SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) + extra_conf_store.Reset() def Main(): From 3dbd407f7ab72bfddbc3e82d5aa6507e005477ef Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 19:02:20 -0700 Subject: [PATCH 099/149] Adding forgotten test file --- python/ycm/server/tests/testdata/.ycm_extra_conf.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 python/ycm/server/tests/testdata/.ycm_extra_conf.py diff --git a/python/ycm/server/tests/testdata/.ycm_extra_conf.py b/python/ycm/server/tests/testdata/.ycm_extra_conf.py new file mode 100644 index 00000000..b94aed95 --- /dev/null +++ b/python/ycm/server/tests/testdata/.ycm_extra_conf.py @@ -0,0 +1,5 @@ +def FlagsForFile( filename ): + return { + 'flags': ['-x', 'c++'], + 'do_cache': True + } From daa0d506556b5063b4cf2e63bffa2d0c8cdb4082 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 20:20:15 -0700 Subject: [PATCH 100/149] Removed the logging code from Clang completer The exceptions are already logged by Bottle so what's the point. Also removed an outdated TODO. --- python/ycm/completers/cpp/clang_completer.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index 154452fa..f892b304 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -19,7 +19,6 @@ from collections import defaultdict import ycm_core -import logging from ycm.server import responses from ycm import extra_conf_store from ycm.utils import ToUtf8IfNeeded @@ -46,7 +45,6 @@ class ClangCompleter( Completer ): self._completer = ycm_core.ClangCompleter() self._flags = Flags() self._diagnostic_store = None - self._logger = logging.getLogger( __name__ ) def SupportedFiletypes( self ): @@ -78,15 +76,10 @@ class ClangCompleter( Completer ): return if self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ): - self._logger.info( PARSING_FILE_MESSAGE ) - # TODO: For this exception and the NO_COMPILE_FLAGS one, use a special - # exception class so that the client can be more silent about these - # messages. raise RuntimeError( PARSING_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) if not flags: - self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) raise RuntimeError( NO_COMPILE_FLAGS_MESSAGE ) files = self.GetUnsavedFilesVector( request_data ) @@ -100,7 +93,6 @@ class ClangCompleter( Completer ): flags ) if not results: - self._logger.warning( NO_COMPLETIONS_MESSAGE ) raise RuntimeError( NO_COMPLETIONS_MESSAGE ) return [ ConvertCompletionData( x ) for x in results ] @@ -132,12 +124,10 @@ class ClangCompleter( Completer ): def _LocationForGoTo( self, goto_function, request_data ): filename = request_data[ 'filepath' ] if not filename: - self._logger.warning( INVALID_FILE_MESSAGE ) raise ValueError( INVALID_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) if not flags: - self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) raise ValueError( NO_COMPILE_FLAGS_MESSAGE ) files = self.GetUnsavedFilesVector( request_data ) @@ -192,16 +182,13 @@ class ClangCompleter( Completer ): filename = request_data[ 'filepath' ] contents = request_data[ 'file_data' ][ filename ][ 'contents' ] if contents.count( '\n' ) < MIN_LINES_IN_FILE_TO_PARSE: - self._logger.warning( FILE_TOO_SHORT_MESSAGE ) raise ValueError( FILE_TOO_SHORT_MESSAGE ) if not filename: - self._logger.warning( INVALID_FILE_MESSAGE ) raise ValueError( INVALID_FILE_MESSAGE ) flags = self._FlagsForRequest( request_data ) if not flags: - self._logger.info( NO_COMPILE_FLAGS_MESSAGE ) raise ValueError( NO_COMPILE_FLAGS_MESSAGE ) diagnostics = self._completer.UpdateTranslationUnit( From 7afd76b4d4dcbc0e2a29b1d6a6d764951f283611 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 20:49:00 -0700 Subject: [PATCH 101/149] Better url joining in base_request --- python/ycm/client/base_request.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index ddd2b2c8..8781c5d3 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -20,6 +20,7 @@ import vim import json import requests +import urlparse from retries import retries from requests_futures.sessions import FuturesSession from concurrent.futures import ThreadPoolExecutor @@ -111,7 +112,7 @@ def JsonFromFuture( future ): def _BuildUri( handler ): - return ''.join( [ BaseRequest.server_location, '/', handler ] ) + return urlparse.urljoin( BaseRequest.server_location, handler ) SERVER_HEALTHY = False From d84f2b0e8e1d65c20005f16d3185935087370a76 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 20:52:04 -0700 Subject: [PATCH 102/149] cs_completer works again --- python/ycm/completers/cs/cs_completer.py | 39 +++++++++++------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index ac0c7bf6..3470cf4b 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -36,6 +36,8 @@ SERVER_NOT_FOUND_MSG = ( 'OmniSharp server binary not found at {0}. ' + 'Did you compile it? You can do so by running ' + '"./install.sh --omnisharp-completer".' ) +CS_SERVER_PORT_RANGE_START = 10001 + class CsharpCompleter( Completer ): """ A Completer that uses the Omnisharp server as completion engine. @@ -46,9 +48,6 @@ class CsharpCompleter( Completer ): self._omnisharp_port = None self._logger = logging.getLogger( __name__ ) - if self.user_options[ 'auto_start_csharp_server' ]: - self._StartServer() - def Shutdown( self ): if ( self.user_options[ 'auto_start_csharp_server' ] and @@ -78,6 +77,12 @@ class CsharpCompleter( Completer ): 'GoToDefinitionElseDeclaration' ] + def OnFileReadyToParse( self, request_data ): + if ( not self._omnisharp_port and + self.user_options[ 'auto_start_csharp_server' ] ): + self._StartServer( request_data ) + + def OnUserCommand( self, arguments, request_data ): if not arguments: raise ValueError( self.UserCommandsHelpMessage() ) @@ -101,14 +106,16 @@ class CsharpCompleter( Completer ): def DebugInfo( self ): if self._ServerIsRunning(): return 'Server running at: {0}\nLogfiles:\n{1}\n{2}'.format( - self._PortToHost(), self._filename_stdout, self._filename_stderr ) + self._ServerLocation(), self._filename_stdout, self._filename_stderr ) else: return 'Server is not running' def _StartServer( self, request_data ): """ Start the OmniSharp server """ - self._omnisharp_port = self._FindFreePort() + self._logger.info( 'startup' ) + + self._omnisharp_port = CS_SERVER_PORT_RANGE_START + os.getpid() solutionfiles, folder = _FindSolutionFiles( request_data[ 'filepath' ] ) if len( solutionfiles ) == 0: @@ -192,29 +199,17 @@ class CsharpCompleter( Completer ): def _ServerIsRunning( self ): """ Check if our OmniSharp server is running """ return ( self._omnisharp_port != None and - self._GetResponse( '/checkalivestatus', silent=True ) != None ) + self._GetResponse( '/checkalivestatus', silent = True ) != None ) - def _FindFreePort( self ): - """ Find port without an OmniSharp server running on it """ - port = self.user_options[ 'csharp_server_port' ] - while self._GetResponse( '/checkalivestatus', - silent=True, - port=port ) != None: - port += 1 - return port + def _ServerLocation( self ): + return 'http://localhost:' + str( self._omnisharp_port ) - def _PortToHost( self, port=None ): - if port == None: - port = self._omnisharp_port - return 'http://localhost:' + str( port ) - - - def _GetResponse( self, endPoint, parameters={}, silent=False, port=None ): + def _GetResponse( self, handler, parameters = {}, silent = False ): """ Handle communication with server """ # TODO: Replace usage of urllib with Requests - target = urlparse.urljoin( self._PortToHost( port ), endPoint ) + target = urlparse.urljoin( self._ServerLocation(), handler ) parameters = urllib.urlencode( parameters ) response = urllib2.urlopen( target, parameters ) return json.loads( response.read() ) From 3b057cc66763944fbfa8f1eda56da55f14cc6c09 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 21:30:53 -0700 Subject: [PATCH 103/149] Basic integration test for the cs_completer --- python/ycm/server/tests/basic_test.py | 29 +++++++++++++ .../server/tests/testdata/testy/Program.cs | 12 ++++++ .../testdata/testy/Properties/AssemblyInfo.cs | 22 ++++++++++ .../server/tests/testdata/testy/testy.csproj | 41 +++++++++++++++++++ .../ycm/server/tests/testdata/testy/testy.sln | 20 +++++++++ .../tests/testdata/testy/testy.userprefs | 13 ++++++ 6 files changed, 137 insertions(+) create mode 100644 python/ycm/server/tests/testdata/testy/Program.cs create mode 100644 python/ycm/server/tests/testdata/testy/Properties/AssemblyInfo.cs create mode 100644 python/ycm/server/tests/testdata/testy/testy.csproj create mode 100644 python/ycm/server/tests/testdata/testy/testy.sln create mode 100644 python/ycm/server/tests/testdata/testy/testy.userprefs diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 8c2cdc5b..56efa454 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -19,6 +19,7 @@ import os import httplib +import time from webtest import TestApp from .. import ycmd from ..responses import BuildCompletionData, UnknownExtraConf @@ -101,6 +102,34 @@ def GetCompletions_IdentifierCompleter_Works_test(): app.post_json( '/completions', completion_data ).json ) +@with_setup( Setup ) +def GetCompletions_CsCompleter_Works_test(): + app = TestApp( ycmd.app ) + filepath = PathToTestFile( 'testy/Program.cs' ) + contents = open( filepath ).read() + event_data = BuildRequest( filepath = filepath, + filetype = 'cs', + contents = contents, + event_name = 'FileReadyToParse' ) + + app.post_json( '/event_notification', event_data ) + + # We need to wait until the server has started up. + # TODO: This is a HORRIBLE hack. Fix it! + time.sleep( 2 ) + + completion_data = BuildRequest( filepath = filepath, + filetype = 'cs', + contents = contents, + line_num = 8, + column_num = 11, + start_column = 11 ) + + results = app.post_json( '/completions', completion_data ).json + assert_that( results, has_items( CompletionEntryMatcher( 'CursorLeft' ), + CompletionEntryMatcher( 'CursorSize' ) ) ) + + @with_setup( Setup ) def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/tests/testdata/testy/Program.cs b/python/ycm/server/tests/testdata/testy/Program.cs new file mode 100644 index 00000000..cac606c8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace testy +{ + class MainClass + { + public static void Main (string[] args) + { + Console. + } + } +} diff --git a/python/ycm/server/tests/testdata/testy/Properties/AssemblyInfo.cs b/python/ycm/server/tests/testdata/testy/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..bed715b8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle ("testy")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("valloric")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion ("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/python/ycm/server/tests/testdata/testy/testy.csproj b/python/ycm/server/tests/testdata/testy/testy.csproj new file mode 100644 index 00000000..1f1c0dd1 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy/testy.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F} + Exe + testy + testy + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/python/ycm/server/tests/testdata/testy/testy.sln b/python/ycm/server/tests/testdata/testy/testy.sln new file mode 100644 index 00000000..8c266824 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy/testy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testy", "testy.csproj", "{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.ActiveCfg = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.Build.0 = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.ActiveCfg = Release|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = testy.csproj + EndGlobalSection +EndGlobal diff --git a/python/ycm/server/tests/testdata/testy/testy.userprefs b/python/ycm/server/tests/testdata/testy/testy.userprefs new file mode 100644 index 00000000..1895a66a --- /dev/null +++ b/python/ycm/server/tests/testdata/testy/testy.userprefs @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file From afe270b6c4562f742d18ffe6a9771f59725e803b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 22:02:55 -0700 Subject: [PATCH 104/149] run_tests.sh now builds omnisharp binaries --- run_tests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_tests.sh b/run_tests.sh index 8f27be7c..06e00e46 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -31,7 +31,7 @@ else extra_cmake_args="-DUSE_DEV_FLAGS=ON" fi -EXTRA_CMAKE_ARGS=$extra_cmake_args YCM_TESTRUN=1 ./install.sh +EXTRA_CMAKE_ARGS=$extra_cmake_args YCM_TESTRUN=1 ./install.sh --omnisharp-completer for directory in third_party/*; do if [ -d "${directory}" ]; then From 8debd30864f0371b4b38f764f6ad496b7de8ffa8 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 22:16:13 -0700 Subject: [PATCH 105/149] Telling Travis to install mono-devel --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 123041c4..ab376549 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ python: - "2.7" install: - pip install -r python/test_requirements.txt --use-mirrors + - apt-get install mono-devel compiler: - gcc - clang From aac33b61e93f406aae6f8cfc4d9a035b0604c621 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 8 Oct 2013 22:18:29 -0700 Subject: [PATCH 106/149] Sigh, travis install needs sudo for apt-get --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ab376549..7b1fa8d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "2.7" install: - pip install -r python/test_requirements.txt --use-mirrors - - apt-get install mono-devel + - sudo apt-get install mono-devel compiler: - gcc - clang From 70a51be209ff1387cd9a5700342e888f4736a66a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 9 Oct 2013 13:17:53 -0700 Subject: [PATCH 107/149] Making the cs_completer test less flaky --- python/ycm/completers/cs/cs_completer.py | 10 ++++++++-- python/ycm/server/tests/basic_test.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 3470cf4b..5a5a8d1f 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -72,6 +72,7 @@ class CsharpCompleter( Completer ): return [ 'StartServer', 'StopServer', 'RestartServer', + 'ServerRunning', 'GoToDefinition', 'GoToDeclaration', 'GoToDefinitionElseDeclaration' ] @@ -96,6 +97,8 @@ class CsharpCompleter( Completer ): if self._ServerIsRunning(): self._StopServer() self._StartServer( request_data ) + elif command == 'ServerRunning': + return self._ServerIsRunning() elif command in [ 'GoToDefinition', 'GoToDeclaration', 'GoToDefinitionElseDeclaration' ]: @@ -198,8 +201,11 @@ class CsharpCompleter( Completer ): def _ServerIsRunning( self ): """ Check if our OmniSharp server is running """ - return ( self._omnisharp_port != None and - self._GetResponse( '/checkalivestatus', silent = True ) != None ) + try: + return bool( self._omnisharp_port and + self._GetResponse( '/checkalivestatus', silent = True ) ) + except: + return False def _ServerLocation( self ): diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 56efa454..0faa9cc3 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -115,8 +115,14 @@ def GetCompletions_CsCompleter_Works_test(): app.post_json( '/event_notification', event_data ) # We need to wait until the server has started up. - # TODO: This is a HORRIBLE hack. Fix it! - time.sleep( 2 ) + while True: + result = app.post_json( '/run_completer_command', + BuildRequest( completer_target = 'filetype_default', + command_arguments = ['ServerRunning'], + filetype = 'cs' ) ).json + if result: + break + time.sleep( 0.2 ) completion_data = BuildRequest( filepath = filepath, filetype = 'cs', From a25ed01a7c4e8c798b9cdc9af6a6d14ec8e96a15 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 9 Oct 2013 16:59:48 -0700 Subject: [PATCH 108/149] Starting ycmd without shell = True This should make it easier to shut down the server on some machines. Fixes #577. --- python/ycm/youcompleteme.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 4d7f67b4..18aa5b70 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -87,8 +87,7 @@ class YouCompleteMe( object ): with open( self._server_stdout, 'w' ) as fstdout: self._server_popen = subprocess.Popen( command, stdout = fstdout, - stderr = fstderr, - shell = True ) + stderr = fstderr ) def CreateCompletionRequest( self, force_semantic = False ): From 9482ad189e174614a9a5a7cba0b767ec1d3e2a12 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 9 Oct 2013 19:18:36 -0700 Subject: [PATCH 109/149] Using full path to Python in popen call This is the root of the problem in issue #577. Fixes #577. --- python/ycm/youcompleteme.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 18aa5b70..73274190 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -20,6 +20,7 @@ import os import vim import subprocess +import sys import tempfile import json from ycm import vimsupport @@ -61,7 +62,7 @@ class YouCompleteMe( object ): with tempfile.NamedTemporaryFile( delete = False ) as options_file: self._temp_options_filename = options_file.name json.dump( dict( self._user_options ), options_file ) - command = ''.join( [ 'python ', + command = ''.join( [ sys.executable + ' ', _PathToServerScript(), ' --port=', str( server_port ), From 98ef568359aca9848c8b3b116b18a24aca71ca04 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 9 Oct 2013 19:28:27 -0700 Subject: [PATCH 110/149] Refactored the popen call for ycmd Also removed shell = true in the other branch that calls popen --- python/ycm/youcompleteme.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 73274190..43a5db1a 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -62,19 +62,16 @@ class YouCompleteMe( object ): with tempfile.NamedTemporaryFile( delete = False ) as options_file: self._temp_options_filename = options_file.name json.dump( dict( self._user_options ), options_file ) - command = ''.join( [ sys.executable + ' ', - _PathToServerScript(), - ' --port=', - str( server_port ), - ' --options_file=', - options_file.name, - ' --log=', - self._user_options[ 'server_log_level' ] ] ) + args = [ sys.executable, + _PathToServerScript(), + '--port={0}'.format( server_port ), + '--options_file={0}'.format( options_file.name ), + '--log={0}'.format( self._user_options[ 'server_log_level' ] ) ] BaseRequest.server_location = 'http://localhost:' + str( server_port ) if self._user_options[ 'server_use_vim_stdout' ]: - self._server_popen = subprocess.Popen( command, shell = True ) + self._server_popen = subprocess.Popen( args ) else: filename_format = os.path.join( utils.PathToTempDir(), 'server_{port}_{std}.log' ) @@ -86,7 +83,7 @@ class YouCompleteMe( object ): with open( self._server_stderr, 'w' ) as fstderr: with open( self._server_stdout, 'w' ) as fstdout: - self._server_popen = subprocess.Popen( command, + self._server_popen = subprocess.Popen( args, stdout = fstdout, stderr = fstderr ) From 8ce07f508c27ca407e909a09b4137cf7e6a1b7b7 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 9 Oct 2013 20:20:34 -0700 Subject: [PATCH 111/149] again filters the semantic completions Fixes #576. --- python/ycm/completers/completer.py | 15 ++++++++---- python/ycm/server/tests/basic_test.py | 34 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index ed068b0d..4668076d 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -153,13 +153,18 @@ class Completer( object ): not self.ShouldUseNow( request_data ) ): return [] - if ( request_data[ 'query' ] and - self._completions_cache and + candidates = self._GetCandidatesFromSubclass( request_data ) + if request_data[ 'query' ]: + candidates = self.FilterAndSortCandidates( candidates, + request_data[ 'query' ] ) + return candidates + + + def _GetCandidatesFromSubclass( self, request_data ): + if ( self._completions_cache and self._completions_cache.CacheValid( request_data[ 'line_num' ], request_data[ 'start_column' ] ) ): - return self.FilterAndSortCandidates( - self._completions_cache.raw_completions, - request_data[ 'query' ] ) + return self._completions_cache.raw_completions else: self._completions_cache = CompletionsCache() self._completions_cache.raw_completions = self.ComputeCandidatesInner( diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 0faa9cc3..9618bc7a 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -25,7 +25,7 @@ from .. import ycmd from ..responses import BuildCompletionData, UnknownExtraConf from nose.tools import ok_, eq_, with_setup from hamcrest import ( assert_that, has_items, has_entry, contains, - contains_string, has_entries ) + contains_string, has_entries, contains_inanyorder ) import bottle bottle.debug( True ) @@ -207,6 +207,38 @@ def GetCompletions_ClangCompleter_WorksWhenExtraConfExplicitlyAllowed_test(): CompletionEntryMatcher( 'y' ) ) ) +@with_setup( Setup ) +def GetCompletions_ClangCompleter_ForceSemantic_OnlyFileteredCompletions_test(): + app = TestApp( ycmd.app ) + contents = """ +int main() +{ + int foobar; + int floozar; + int gooboo; + int bleble; + + fooar +} +""" + + # 0-based line and column! + completion_data = BuildRequest( filepath = '/foo.cpp', + filetype = 'cpp', + force_semantic = True, + contents = contents, + line_num = 8, + column_num = 7, + start_column = 7, + query = 'fooar', + compilation_flags = ['-x', 'c++'] ) + + results = app.post_json( '/completions', completion_data ).json + assert_that( results, + contains_inanyorder( CompletionEntryMatcher( 'foobar' ), + CompletionEntryMatcher( 'floozar' ) ) ) + + @with_setup( Setup ) def GetCompletions_ForceSemantic_Works_test(): app = TestApp( ycmd.app ) From 7a73eb14d8fee890e300abbfa92b75900ddec59d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 10 Oct 2013 11:32:20 -0700 Subject: [PATCH 112/149] Fix problems with unknown extra conf at ycmd start Fixes #579. --- python/ycm/extra_conf_store.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 5253e021..9b189153 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -79,7 +79,11 @@ def _CallExtraConfMethod( function_name ): vim_current_working_directory = os.getcwd() path_to_dummy = os.path.join( vim_current_working_directory, 'DUMMY_FILE' ) # The dummy file in the Vim CWD ensures we find the correct extra conf file - module = ModuleForSourceFile( path_to_dummy ) + try: + module = ModuleForSourceFile( path_to_dummy ) + except UnknownExtraConf: + return + if not module or not hasattr( module, function_name ): return getattr( module, function_name )() From 78e3607b00ccbfaa244bd3867f88f051802a13cd Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 10 Oct 2013 12:55:49 -0700 Subject: [PATCH 113/149] We now only run extra conf preload for global file This changes functionality, but since this is an undocumented, non-public API, it's fine. The reason this is required is because of issue #579; if we try to run extra conf preload on non-global extra conf, we might not have the permission to load it. The global extra conf is something the user explicitly has to set so it's always fine to load that. --- python/ycm/extra_conf_store.py | 20 +++++++++----------- python/ycm/server/server_state.py | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 9b189153..0543a13a 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -64,26 +64,24 @@ def ModuleFileForSourceFile( filename ): return _module_file_for_source_file.setdefault( filename ) -def CallExtraConfYcmCorePreloadIfExists(): - _CallExtraConfMethod( 'YcmCorePreload' ) +def CallGlobalExtraConfYcmCorePreloadIfExists(): + _CallGlobalExtraConfMethod( 'YcmCorePreload' ) def Shutdown(): # VimClose is for the sake of backwards compatibility; it's a no-op when it # doesn't exist. - _CallExtraConfMethod( 'VimClose' ) - _CallExtraConfMethod( 'Shutdown' ) + _CallGlobalExtraConfMethod( 'VimClose' ) + _CallGlobalExtraConfMethod( 'Shutdown' ) -def _CallExtraConfMethod( function_name ): - vim_current_working_directory = os.getcwd() - path_to_dummy = os.path.join( vim_current_working_directory, 'DUMMY_FILE' ) - # The dummy file in the Vim CWD ensures we find the correct extra conf file - try: - module = ModuleForSourceFile( path_to_dummy ) - except UnknownExtraConf: +def _CallGlobalExtraConfMethod( function_name ): + global_ycm_extra_conf = _GlobalYcmExtraConfFileLocation() + if not ( global_ycm_extra_conf and + os.path.exists( global_ycm_extra_conf ) ): return + module = Load( global_ycm_extra_conf, force = True ) if not module or not hasattr( module, function_name ): return getattr( module, function_name )() diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 88b75cb6..0e831d33 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -30,7 +30,7 @@ class ServerState( object ): self._user_options = user_options self._filetype_completers = {} self._gencomp = GeneralCompleterStore( self._user_options ) - extra_conf_store.CallExtraConfYcmCorePreloadIfExists() + extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists() @property From db45e243dd6bab1e7b4914a571354add4a93c691 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 11 Oct 2013 11:11:02 -0700 Subject: [PATCH 114/149] Getting path to python exe on Windows correctly Fixes #581. --- python/ycm/utils.py | 30 +++++++++++++++++++++++++++++- python/ycm/youcompleteme.py | 3 +-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 28fa7d05..5d2281e4 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -23,6 +23,10 @@ import sys import signal import functools +WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' +WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' + + def IsIdentifierChar( char ): return char.isalnum() or char == '_' @@ -44,9 +48,33 @@ def PathToTempDir(): return tempdir +def PathToPythonInterpreter(): + # This is a bit tricky. Normally, sys.executable has the full path to the + # Python interpreter. But this code is also executed from inside Vim's + # embedded Python. On Unix machines, even that Python returns a good value for + # sys.executable, but on Windows it returns the path to the Vim binary, which + # is useless to us (issue #581). So we check the common install location for + # Python on Windows, first for Python 2.7 and then for 2.6. + # + # I'm open to better ideas on how to do this. + + if OnWindows(): + if os.path.exists( WIN_PYTHON27_PATH ): + return WIN_PYTHON27_PATH + elif os.path.exists( WIN_PYTHON26_PATH ): + return WIN_PYTHON26_PATH + raise RuntimeError( 'Python 2.7/2.6 not installed!' ) + else: + return sys.executable + + +def OnWindows(): + return sys.platform == 'win32' + + # From here: http://stackoverflow.com/a/8536476/1672783 def TerminateProcess( pid ): - if sys.platform == 'win32': + if OnWindows(): import ctypes PROCESS_TERMINATE = 1 handle = ctypes.windll.kernel32.OpenProcess( PROCESS_TERMINATE, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 43a5db1a..abc802e8 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -20,7 +20,6 @@ import os import vim import subprocess -import sys import tempfile import json from ycm import vimsupport @@ -62,7 +61,7 @@ class YouCompleteMe( object ): with tempfile.NamedTemporaryFile( delete = False ) as options_file: self._temp_options_filename = options_file.name json.dump( dict( self._user_options ), options_file ) - args = [ sys.executable, + args = [ utils.PathToPythonInterpreter(), _PathToServerScript(), '--port={0}'.format( server_port ), '--options_file={0}'.format( options_file.name ), From 3ae10395eaadd0a95425620751a7f923cc336323 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 11 Oct 2013 19:09:21 -0700 Subject: [PATCH 115/149] Preventing Vim thread block on file save Syntastic would run SyntasticCheck on file save, which would unconditionally call _latest_file_parse_request.Response() and thus block until the request returned from the server. We don't want that, so we throw in an explicit check for the request being ready. --- python/ycm/youcompleteme.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index abc802e8..8d5d1143 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -172,7 +172,7 @@ class YouCompleteMe( object ): def GetDiagnosticsFromStoredRequest( self ): - if self._latest_file_parse_request: + if self.DiagnosticsForCurrentFileReady(): to_return = self._latest_file_parse_request.Response() # We set the diagnostics request to None because we want to prevent # Syntastic from repeatedly refreshing the buffer with the same diags. From f6432e1498300956ededaceb6f542ecb03e3f452 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 11 Oct 2013 19:27:04 -0700 Subject: [PATCH 116/149] Releasing Python's GIL in C++ code where possible Without this, all requests to the server become effectively serialized. --- cpp/ycm/ClangCompleter/ClangCompleter.cpp | 7 ++++ .../ClangCompleter/CompilationDatabase.cpp | 2 + cpp/ycm/IdentifierCompleter.cpp | 5 +++ cpp/ycm/PythonSupport.cpp | 34 ++++++++------- cpp/ycm/ReleaseGil.h | 42 +++++++++++++++++++ cpp/ycm/tests/CMakeLists.txt | 2 +- cpp/ycm/tests/main.cpp | 13 ++++++ cpp/ycm/ycm_core.cpp | 5 ++- python/ycm/base.py | 2 +- 9 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 cpp/ycm/ReleaseGil.h create mode 100644 cpp/ycm/tests/main.cpp diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index a8497293..7c4a74ab 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -25,6 +25,7 @@ #include "CompletionData.h" #include "Utils.h" #include "ClangUtils.h" +#include "ReleaseGil.h" #include #include @@ -59,6 +60,7 @@ ClangCompleter::~ClangCompleter() { bool ClangCompleter::UpdatingTranslationUnit( const std::string &filename ) { + ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.Get( filename ); if ( !unit ) @@ -75,6 +77,7 @@ std::vector< Diagnostic > ClangCompleter::UpdateTranslationUnit( const std::string &filename, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ) { + ReleaseGil unlock; bool translation_unit_created; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, @@ -111,6 +114,7 @@ ClangCompleter::CandidatesForLocationInFile( int column, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ) { + ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -129,6 +133,7 @@ Location ClangCompleter::GetDeclarationLocation( int column, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ) { + ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -146,6 +151,7 @@ Location ClangCompleter::GetDefinitionLocation( int column, const std::vector< UnsavedFile > &unsaved_files, const std::vector< std::string > &flags ) { + ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -158,6 +164,7 @@ Location ClangCompleter::GetDefinitionLocation( void ClangCompleter::DeleteCachesForFile( const std::string &filename ) { + ReleaseGil unlock; translation_unit_store_.Remove( filename ); } diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp index 7a3b5f43..c1f94694 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp @@ -18,6 +18,7 @@ #include "CompilationDatabase.h" #include "ClangUtils.h" #include "standard.h" +#include "ReleaseGil.h" #include #include @@ -58,6 +59,7 @@ bool CompilationDatabase::DatabaseSuccessfullyLoaded() { CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile( const std::string &path_to_file ) { + ReleaseGil unlock; CompilationInfoForFile info; if ( !is_loaded_ ) diff --git a/cpp/ycm/IdentifierCompleter.cpp b/cpp/ycm/IdentifierCompleter.cpp index 36ca2436..a6bd2674 100644 --- a/cpp/ycm/IdentifierCompleter.cpp +++ b/cpp/ycm/IdentifierCompleter.cpp @@ -22,6 +22,7 @@ #include "IdentifierUtils.h" #include "Result.h" #include "Utils.h" +#include "ReleaseGil.h" #include @@ -49,6 +50,7 @@ void IdentifierCompleter::AddIdentifiersToDatabase( const std::vector< std::string > &new_candidates, const std::string &filetype, const std::string &filepath ) { + ReleaseGil unlock; identifier_database_.AddIdentifiers( new_candidates, filetype, filepath ); @@ -57,6 +59,7 @@ void IdentifierCompleter::AddIdentifiersToDatabase( void IdentifierCompleter::AddIdentifiersToDatabaseFromTagFiles( const std::vector< std::string > &absolute_paths_to_tag_files ) { + ReleaseGil unlock; foreach( const std::string & path, absolute_paths_to_tag_files ) { identifier_database_.AddIdentifiers( ExtractIdentifiersFromTagsFile( path ) ); @@ -69,6 +72,7 @@ void IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer( const std::string &filetype, const std::string &filepath, bool collect_from_comments_and_strings ) { + ReleaseGil unlock; identifier_database_.ClearCandidatesStoredForFile( filetype, filepath ); std::string new_contents = @@ -92,6 +96,7 @@ std::vector< std::string > IdentifierCompleter::CandidatesForQuery( std::vector< std::string > IdentifierCompleter::CandidatesForQueryAndType( const std::string &query, const std::string &filetype ) const { + ReleaseGil unlock; std::vector< Result > results; identifier_database_.ResultsForQueryAndType( query, filetype, results ); diff --git a/cpp/ycm/PythonSupport.cpp b/cpp/ycm/PythonSupport.cpp index 7b94a7a3..1b14cd63 100644 --- a/cpp/ycm/PythonSupport.cpp +++ b/cpp/ycm/PythonSupport.cpp @@ -20,6 +20,8 @@ #include "Result.h" #include "Candidate.h" #include "CandidateRepository.h" +#include "ReleaseGil.h" + #include #include #include @@ -76,31 +78,33 @@ boost::python::list FilterAndSortCandidates( return candidates; } + int num_candidates = len( candidates ); std::vector< const Candidate * > repository_candidates = CandidatesFromObjectList( candidates, candidate_property ); - Bitset query_bitset = LetterBitsetFromString( query ); - bool query_has_uppercase_letters = any_of( query, is_upper() ); - - int num_candidates = len( candidates ); std::vector< ResultAnd< int > > object_and_results; + { + ReleaseGil unlock; + Bitset query_bitset = LetterBitsetFromString( query ); + bool query_has_uppercase_letters = any_of( query, is_upper() ); - for ( int i = 0; i < num_candidates; ++i ) { - const Candidate *candidate = repository_candidates[ i ]; + for ( int i = 0; i < num_candidates; ++i ) { + const Candidate *candidate = repository_candidates[ i ]; - if ( !candidate->MatchesQueryBitset( query_bitset ) ) - continue; + if ( !candidate->MatchesQueryBitset( query_bitset ) ) + continue; - Result result = candidate->QueryMatchResult( query, - query_has_uppercase_letters ); + Result result = candidate->QueryMatchResult( query, + query_has_uppercase_letters ); - if ( result.IsSubsequence() ) { - ResultAnd< int > object_and_result( i, result ); - object_and_results.push_back( boost::move( object_and_result ) ); + if ( result.IsSubsequence() ) { + ResultAnd< int > object_and_result( i, result ); + object_and_results.push_back( boost::move( object_and_result ) ); + } } - } - std::sort( object_and_results.begin(), object_and_results.end() ); + std::sort( object_and_results.begin(), object_and_results.end() ); + } foreach ( const ResultAnd< int > &object_and_result, object_and_results ) { diff --git a/cpp/ycm/ReleaseGil.h b/cpp/ycm/ReleaseGil.h new file mode 100644 index 00000000..c22de2be --- /dev/null +++ b/cpp/ycm/ReleaseGil.h @@ -0,0 +1,42 @@ +// Copyright (C) 2013 Strahinja Val Markovic +// +// This file is part of YouCompleteMe. +// +// YouCompleteMe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// YouCompleteMe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with YouCompleteMe. If not, see . + +#ifndef RELEASEGIL_H_RDIEBSQ1 +#define RELEASEGIL_H_RDIEBSQ1 + +#include + +namespace YouCompleteMe { + +class ReleaseGil { +public: + ReleaseGil() { + thread_state_ = PyEval_SaveThread(); + } + + ~ReleaseGil() { + PyEval_RestoreThread( thread_state_ ); + } + +private: + PyThreadState *thread_state_; +}; + +} // namespace YouCompleteMe + +#endif /* end of include guard: RELEASEGIL_H_RDIEBSQ1 */ + diff --git a/cpp/ycm/tests/CMakeLists.txt b/cpp/ycm/tests/CMakeLists.txt index c4d8aa7c..951c0aba 100644 --- a/cpp/ycm/tests/CMakeLists.txt +++ b/cpp/ycm/tests/CMakeLists.txt @@ -68,7 +68,7 @@ add_executable( ${PROJECT_NAME} target_link_libraries( ${PROJECT_NAME} ycm_core - gmock_main ) + gmock ) if ( NOT CMAKE_GENERATOR_IS_XCODE ) diff --git a/cpp/ycm/tests/main.cpp b/cpp/ycm/tests/main.cpp new file mode 100644 index 00000000..62c5198c --- /dev/null +++ b/cpp/ycm/tests/main.cpp @@ -0,0 +1,13 @@ +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include + +int main( int argc, char **argv ) { + Py_Initialize(); + // Necessary because of usage of the ReleaseGil class + PyEval_InitThreads(); + + testing::InitGoogleMock(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index ba348bcc..d252e1ee 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -45,7 +45,7 @@ int YcmCoreVersion() { // We increment this every time when we want to force users to recompile // ycm_core. - return 5; + return 6; } @@ -54,6 +54,9 @@ BOOST_PYTHON_MODULE(ycm_core) using namespace boost::python; using namespace YouCompleteMe; + // Necessary because of usage of the ReleaseGil class + PyEval_InitThreads(); + def( "HasClangSupport", HasClangSupport ); def( "FilterAndSortCandidates", FilterAndSortCandidates ); def( "YcmCoreVersion", YcmCoreVersion ); diff --git a/python/ycm/base.py b/python/ycm/base.py index dfcb5f7b..8bf94784 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -152,7 +152,7 @@ def AdjustCandidateInsertionText( candidates ): return new_candidates -COMPATIBLE_WITH_CORE_VERSION = 5 +COMPATIBLE_WITH_CORE_VERSION = 6 def CompatibleWithYcmCore(): try: From a04ae37ead06ed15960b01711e6b5b41e7323317 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 11 Oct 2013 20:12:04 -0700 Subject: [PATCH 117/149] Client & server threads increased from 4 to 10 --- python/ycm/client/base_request.py | 2 +- python/ycm/server/ycmd.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 8781c5d3..48da08b9 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -28,7 +28,7 @@ from ycm import vimsupport from ycm.server.responses import ServerError, UnknownExtraConf HEADERS = {'content-type': 'application/json'} -EXECUTOR = ThreadPoolExecutor( max_workers = 4 ) +EXECUTOR = ThreadPoolExecutor( max_workers = 10 ) class BaseRequest( object ): def __init__( self ): diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 0619c193..ec20875c 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -38,7 +38,8 @@ import json import bottle import argparse import httplib -from bottle import run, request, response +import waitress +from bottle import request, response import server_state from ycm import user_options_store from ycm.server.responses import BuildExceptionResponse @@ -247,7 +248,7 @@ def Main(): level = numeric_level ) LOGGER = logging.getLogger( __name__ ) - run( app = app, host = args.host, port = args.port, server='waitress' ) + waitress.serve( app, host = args.host, port = args.port, threads = 10 ) if __name__ == "__main__": From 6331df95ccbc8d8c9574dc8f257e68209bea3ac0 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 10:22:57 -0700 Subject: [PATCH 118/149] compilation db caller can now know if db busy --- cpp/ycm/ClangCompleter/CompilationDatabase.cpp | 8 ++++++++ cpp/ycm/ClangCompleter/CompilationDatabase.h | 7 +++++++ cpp/ycm/ycm_core.cpp | 2 ++ 3 files changed, 17 insertions(+) diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp index c1f94694..249d9608 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.cpp +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.cpp @@ -26,6 +26,8 @@ #include using boost::lock_guard; +using boost::unique_lock; +using boost::try_to_lock_t; using boost::remove_pointer; using boost::shared_ptr; using boost::mutex; @@ -57,6 +59,12 @@ bool CompilationDatabase::DatabaseSuccessfullyLoaded() { } +bool CompilationDatabase::AlreadyGettingFlags() { + unique_lock< mutex > lock( compilation_database_mutex_, try_to_lock_t() ); + return !lock.owns_lock(); +} + + CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile( const std::string &path_to_file ) { ReleaseGil unlock; diff --git a/cpp/ycm/ClangCompleter/CompilationDatabase.h b/cpp/ycm/ClangCompleter/CompilationDatabase.h index 346c4299..bf75b3ee 100644 --- a/cpp/ycm/ClangCompleter/CompilationDatabase.h +++ b/cpp/ycm/ClangCompleter/CompilationDatabase.h @@ -34,6 +34,7 @@ struct CompilationInfoForFile { }; +// Access to Clang's internal CompilationDatabase. This class is thread-safe. class CompilationDatabase : boost::noncopyable { public: CompilationDatabase( const std::string &path_to_directory ); @@ -41,6 +42,12 @@ public: bool DatabaseSuccessfullyLoaded(); + // Returns true when a separate thread is already getting flags; this is + // useful so that the caller doesn't need to block. + bool AlreadyGettingFlags(); + + // NOTE: Multiple calls to this function from separate threads will be + // serialized since Clang internals are not thread-safe. CompilationInfoForFile GetCompilationInfoForFile( const std::string &path_to_file ); diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index d252e1ee..4aa3003d 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -140,6 +140,8 @@ BOOST_PYTHON_MODULE(ycm_core) "CompilationDatabase", init< std::string >() ) .def( "DatabaseSuccessfullyLoaded", &CompilationDatabase::DatabaseSuccessfullyLoaded ) + .def( "AlreadyGettingFlags", + &CompilationDatabase::AlreadyGettingFlags ) .def( "GetCompilationInfoForFile", &CompilationDatabase::GetCompilationInfoForFile ); From c3fcaf2b29228c411c057ab4138722dc76d6f73e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 11:08:15 -0700 Subject: [PATCH 119/149] Strict 0.5s time budget for completion requests Vim still loves to block the main GUI thread on occasion when asking for completions... to counteract this stupidity, we enforce a hard budget of 0.5s for all completion requests. If the server doesn't respond by then (it should, unless something really bad happened), we give up. --- python/ycm/client/base_request.py | 20 +++++++++++++------- python/ycm/client/completion_request.py | 6 ++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 48da08b9..3efd803e 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -48,19 +48,25 @@ class BaseRequest( object ): # This is the blocking version of the method. See below for async. + # |timeout| is num seconds to tolerate no response from server before giving + # up; see Requests docs for details (we just pass the param along). @staticmethod - def PostDataToHandler( data, handler ): + def PostDataToHandler( data, handler, timeout = None ): return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data, - handler ) ) + handler, + timeout ) ) # This returns a future! Use JsonFromFuture to get the value. + # |timeout| is num seconds to tolerate no response from server before giving + # up; see Requests docs for details (we just pass the param along). @staticmethod - def PostDataToHandlerAsync( data, handler ): - def PostData( data, handler ): + def PostDataToHandlerAsync( data, handler, timeout = None ): + def PostData( data, handler, timeout ): return BaseRequest.session.post( _BuildUri( handler ), - data = json.dumps( data ), - headers = HEADERS ) + data = json.dumps( data ), + headers = HEADERS, + timeout = timeout ) @retries( 3, delay = 0.5 ) def DelayedPostData( data, handler ): @@ -71,7 +77,7 @@ class BaseRequest( object ): if not _CheckServerIsHealthyWithCache(): return EXECUTOR.submit( DelayedPostData, data, handler ) - return PostData( data, handler ) + return PostData( data, handler, timeout ) session = FuturesSession( executor = EXECUTOR ) diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index e8af21a7..bb0001c2 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -20,8 +20,9 @@ from ycm import base from ycm import vimsupport from ycm.client.base_request import ( BaseRequest, BuildRequestData, - JsonFromFuture ) + JsonFromFuture ) +TIMEOUT_SECONDS = 0.5 class CompletionRequest( BaseRequest ): def __init__( self, force_semantic = False ): @@ -42,7 +43,8 @@ class CompletionRequest( BaseRequest ): def Start( self, query ): self.request_data[ 'query' ] = query self._response_future = self.PostDataToHandlerAsync( self.request_data, - 'completions' ) + 'completions', + TIMEOUT_SECONDS ) def Done( self ): From a534a58477f760ff7ed2795ab62c213bd96eebaa Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 12:32:18 -0700 Subject: [PATCH 120/149] Checking if ultisnips data present before using it Mentioned in issue #583, but it's not the root cause. --- python/ycm/completers/general/ultisnips_completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/completers/general/ultisnips_completer.py b/python/ycm/completers/general/ultisnips_completer.py index 7a364f4d..ca7b666e 100644 --- a/python/ycm/completers/general/ultisnips_completer.py +++ b/python/ycm/completers/general/ultisnips_completer.py @@ -45,7 +45,7 @@ class UltiSnipsCompleter( GeneralCompleter ): def OnBufferVisit( self, request_data ): - raw_candidates = request_data[ 'ultisnips_snippets' ] + raw_candidates = request_data.get( 'ultisnips_snippets', [] ) self._candidates = [ responses.BuildCompletionData( str( snip[ 'trigger' ] ), From bc607724f055ae09276be2d77255d71af6993d34 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 13:29:28 -0700 Subject: [PATCH 121/149] Ensuring ident completion works always A bug turned it off when omni completion was available. Fixes #583. --- python/ycm/client/base_request.py | 9 +++++--- python/ycm/completers/all/omni_completer.py | 25 ++++++++++++++++----- python/ycm/youcompleteme.py | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 3efd803e..45ce8926 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -84,7 +84,9 @@ class BaseRequest( object ): server_location = 'http://localhost:6666' -def BuildRequestData( start_column = None, query = None ): +def BuildRequestData( start_column = None, + query = None, + include_buffer_data = True ): line, column = vimsupport.CurrentLineAndColumn() filepath = vimsupport.GetCurrentBufferFilepath() request_data = { @@ -93,10 +95,11 @@ def BuildRequestData( start_column = None, query = None ): 'column_num': column, 'start_column': start_column, 'line_value': vim.current.line, - 'filepath': filepath, - 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() + 'filepath': filepath } + if include_buffer_data: + request_data[ 'file_data' ] = vimsupport.GetUnsavedAndCurrentBufferData() if query: request_data[ 'query' ] = query diff --git a/python/ycm/completers/all/omni_completer.py b/python/ycm/completers/all/omni_completer.py index 41d81452..5bfffeff 100644 --- a/python/ycm/completers/all/omni_completer.py +++ b/python/ycm/completers/all/omni_completer.py @@ -19,7 +19,9 @@ import vim from ycm import vimsupport +from ycm import base from ycm.completers.completer import Completer +from ycm.client.base_request import BuildRequestData OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!' OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" ' @@ -35,15 +37,21 @@ class OmniCompleter( Completer ): return [] - def Available( self ): - return bool( self._omnifunc ) - - def ShouldUseCache( self ): return bool( self.user_options[ 'cache_omnifunc' ] ) - def ShouldUseNow( self, request_data ): + # We let the caller call this without passing in request_data. This is useful + # for figuring out should we even be preparing the "real" request_data in + # omni_completion_request. The real request_data is much bigger and takes + # longer to prepare, and we want to avoid creating it twice. + def ShouldUseNow( self, request_data = None ): + if not self._omnifunc: + return False + + if not request_data: + request_data = _BuildRequestDataSubstitute() + if self.ShouldUseCache(): return super( OmniCompleter, self ).ShouldUseNow( request_data ) return self.ShouldUseNowInner( request_data ) @@ -96,3 +104,10 @@ class OmniCompleter( Completer ): def OnFileReadyToParse( self, request_data ): self._omnifunc = vim.eval( '&omnifunc' ) + +def _BuildRequestDataSubstitute(): + data = BuildRequestData( include_buffer_data = False ) + data[ 'start_column' ] = base.CompletionStartColumn() + return data + + diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 8d5d1143..68665084 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -93,7 +93,7 @@ class YouCompleteMe( object ): # function calls... Thus we need to keep this request somewhere. if ( not self.NativeFiletypeCompletionAvailable() and self.CurrentFiletypeCompletionEnabled() and - self._omnicomp.Available() ): + self._omnicomp.ShouldUseNow() ): self._latest_completion_request = OmniCompletionRequest( self._omnicomp ) else: self._latest_completion_request = CompletionRequest( force_semantic ) From b903867cdd881f0648ec61ccaa8524179c2eaaa3 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 15:29:00 -0700 Subject: [PATCH 122/149] ycm_core now imported after extra conf preload --- python/ycm/server/handlers.py | 210 ++++++++++++++++++++++++ python/ycm/server/server_state.py | 3 - python/ycm/server/server_utils.py | 34 ++++ python/ycm/server/tests/basic_test.py | 40 ++--- python/ycm/server/ycmd.py | 225 +++----------------------- 5 files changed, 289 insertions(+), 223 deletions(-) create mode 100644 python/ycm/server/handlers.py create mode 100644 python/ycm/server/server_utils.py diff --git a/python/ycm/server/handlers.py b/python/ycm/server/handlers.py new file mode 100644 index 00000000..2ebf1c68 --- /dev/null +++ b/python/ycm/server/handlers.py @@ -0,0 +1,210 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import atexit +import logging +import json +import bottle +import httplib +from bottle import request, response +import server_state +from ycm import user_options_store +from ycm.server.responses import BuildExceptionResponse +from ycm import extra_conf_store + +# num bytes for the request body buffer; request.json only works if the request +# size is less than this +bottle.Request.MEMFILE_MAX = 300 * 1024 + +# TODO: rename these to _lower_case +SERVER_STATE = None +LOGGER = logging.getLogger( __name__ ) +app = bottle.Bottle() + + +@app.post( '/event_notification' ) +def EventNotification(): + LOGGER.info( 'Received event notification' ) + request_data = request.json + event_name = request_data[ 'event_name' ] + LOGGER.debug( 'Event name: %s', event_name ) + + event_handler = 'On' + event_name + getattr( SERVER_STATE.GetGeneralCompleter(), event_handler )( request_data ) + + filetypes = request_data[ 'filetypes' ] + response_data = None + if SERVER_STATE.FiletypeCompletionUsable( filetypes ): + response_data = getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), + event_handler )( request_data ) + + if response_data: + return _JsonResponse( response_data ) + + +@app.post( '/run_completer_command' ) +def RunCompleterCommand(): + LOGGER.info( 'Received command request' ) + request_data = request.json + completer = _GetCompleterForRequestData( request_data ) + + return _JsonResponse( completer.OnUserCommand( + request_data[ 'command_arguments' ], + request_data ) ) + + +@app.post( '/completions' ) +def GetCompletions(): + LOGGER.info( 'Received completion request' ) + request_data = request.json + do_filetype_completion = SERVER_STATE.ShouldUseFiletypeCompleter( + request_data ) + LOGGER.debug( 'Using filetype completion: %s', do_filetype_completion ) + filetypes = request_data[ 'filetypes' ] + completer = ( SERVER_STATE.GetFiletypeCompleter( filetypes ) if + do_filetype_completion else + SERVER_STATE.GetGeneralCompleter() ) + + return _JsonResponse( completer.ComputeCandidates( request_data ) ) + + +@app.get( '/user_options' ) +def GetUserOptions(): + LOGGER.info( 'Received user options GET request' ) + return _JsonResponse( dict( SERVER_STATE.user_options ) ) + + +@app.get( '/healthy' ) +def GetHealthy(): + LOGGER.info( 'Received health request' ) + return _JsonResponse( True ) + + +@app.post( '/user_options' ) +def SetUserOptions(): + LOGGER.info( 'Received user options POST request' ) + UpdateUserOptions( request.json ) + + +@app.post( '/semantic_completion_available' ) +def FiletypeCompletionAvailable(): + LOGGER.info( 'Received filetype completion available request' ) + return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable( + request.json[ 'filetypes' ] ) ) + + +@app.post( '/defined_subcommands' ) +def DefinedSubcommands(): + LOGGER.info( 'Received defined subcommands request' ) + completer = _GetCompleterForRequestData( request.json ) + + return _JsonResponse( completer.DefinedSubcommands() ) + + +@app.post( '/detailed_diagnostic' ) +def GetDetailedDiagnostic(): + LOGGER.info( 'Received detailed diagnostic request' ) + request_data = request.json + completer = _GetCompleterForRequestData( request_data ) + + return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) ) + + +@app.post( '/load_extra_conf_file' ) +def LoadExtraConfFile(): + LOGGER.info( 'Received extra conf load request' ) + request_data = request.json + extra_conf_store.Load( request_data[ 'filepath' ], force = True ) + + +@app.post( '/debug_info' ) +def DebugInfo(): + # This can't be at the top level because of possible extra conf preload + import ycm_core + LOGGER.info( 'Received debug info request' ) + + output = [] + has_clang_support = ycm_core.HasClangSupport() + output.append( 'Server has Clang support compiled in: {0}'.format( + has_clang_support ) ) + + if has_clang_support: + output.append( 'Clang version: ' + ycm_core.ClangVersion() ) + + request_data = request.json + try: + output.append( + _GetCompleterForRequestData( request_data ).DebugInfo( request_data) ) + except: + pass + return _JsonResponse( '\n'.join( output ) ) + + +# The type of the param is Bottle.HTTPError +@app.error( httplib.INTERNAL_SERVER_ERROR ) +def ErrorHandler( httperror ): + return _JsonResponse( BuildExceptionResponse( httperror.exception, + httperror.traceback ) ) + + +def _JsonResponse( data ): + response.set_header( 'Content-Type', 'application/json' ) + return json.dumps( data, default = _UniversalSerialize ) + + +def _UniversalSerialize( obj ): + serialized = obj.__dict__.copy() + serialized[ 'TYPE' ] = type( obj ).__name__ + return serialized + + +def _GetCompleterForRequestData( request_data ): + completer_target = request_data.get( 'completer_target', None ) + + if completer_target == 'identifier': + return SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() + elif completer_target == 'filetype_default' or not completer_target: + return SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) + else: + return SERVER_STATE.GetFiletypeCompleter( [ completer_target ] ) + + +@atexit.register +def _ServerShutdown(): + if SERVER_STATE: + SERVER_STATE.Shutdown() + extra_conf_store.Shutdown() + + +def UpdateUserOptions( options ): + global SERVER_STATE + + if not options: + return + + user_options_store.SetAll( options ) + SERVER_STATE = server_state.ServerState( options ) + + +def SetServerStateToDefaults(): + global SERVER_STATE, LOGGER + LOGGER = logging.getLogger( __name__ ) + user_options_store.LoadDefaults() + SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) + extra_conf_store.Reset() diff --git a/python/ycm/server/server_state.py b/python/ycm/server/server_state.py index 0e831d33..39cd1243 100644 --- a/python/ycm/server/server_state.py +++ b/python/ycm/server/server_state.py @@ -19,7 +19,6 @@ import imp import os -from ycm import extra_conf_store from ycm.utils import ForceSemanticCompletion from ycm.completers.general.general_completer_store import GeneralCompleterStore from ycm.completers.completer_utils import PathToFiletypeCompleterPluginLoader @@ -30,7 +29,6 @@ class ServerState( object ): self._user_options = user_options self._filetype_completers = {} self._gencomp = GeneralCompleterStore( self._user_options ) - extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists() @property @@ -44,7 +42,6 @@ class ServerState( object ): completer.Shutdown() self._gencomp.Shutdown() - extra_conf_store.Shutdown() def _GetFiletypeCompleterForFiletype( self, filetype ): diff --git a/python/ycm/server/server_utils.py b/python/ycm/server/server_utils.py new file mode 100644 index 00000000..96d566fe --- /dev/null +++ b/python/ycm/server/server_utils.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import sys +import os + +def SetUpPythonPath(): + # We want to have the YouCompleteMe/python directory on the Python PATH + # because all the code already assumes that it's there. This is a relic from + # before the client/server architecture. + # TODO: Fix things so that this is not needed anymore when we split ycmd into + # a separate repository. + sys.path.insert( 0, os.path.join( + os.path.dirname( os.path.abspath( __file__ ) ), + '../..' ) ) + + from ycm import utils + utils.AddThirdPartyFoldersToSysPath() diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 9618bc7a..6d226324 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -20,8 +20,10 @@ import os import httplib import time +from ..server_utils import SetUpPythonPath +SetUpPythonPath() from webtest import TestApp -from .. import ycmd +from .. import handlers from ..responses import BuildCompletionData, UnknownExtraConf from nose.tools import ok_, eq_, with_setup from hamcrest import ( assert_that, has_items, has_entry, contains, @@ -73,7 +75,7 @@ def CompletionEntryMatcher( insertion_text ): def Setup(): - ycmd.SetServerStateToDefaults() + handlers.SetServerStateToDefaults() def PathToTestDataDir(): @@ -87,7 +89,7 @@ def PathToTestFile( test_basename ): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) event_data = BuildRequest( contents = 'foo foogoo ba', event_name = 'FileReadyToParse' ) @@ -104,7 +106,7 @@ def GetCompletions_IdentifierCompleter_Works_test(): @with_setup( Setup ) def GetCompletions_CsCompleter_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) filepath = PathToTestFile( 'testy/Program.cs' ) contents = open( filepath ).read() event_data = BuildRequest( filepath = filepath, @@ -138,7 +140,7 @@ def GetCompletions_CsCompleter_Works_test(): @with_setup( Setup ) def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ struct Foo { int x; @@ -170,7 +172,7 @@ int main() @with_setup( Setup ) def GetCompletions_ClangCompleter_UnknownExtraConfException_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) filepath = PathToTestFile( 'basic.cpp' ) completion_data = BuildRequest( filepath = filepath, filetype = 'cpp', @@ -189,7 +191,7 @@ def GetCompletions_ClangCompleter_UnknownExtraConfException_test(): @with_setup( Setup ) def GetCompletions_ClangCompleter_WorksWhenExtraConfExplicitlyAllowed_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) app.post_json( '/load_extra_conf_file', { 'filepath': PathToTestFile( '.ycm_extra_conf.py' ) } ) @@ -209,7 +211,7 @@ def GetCompletions_ClangCompleter_WorksWhenExtraConfExplicitlyAllowed_test(): @with_setup( Setup ) def GetCompletions_ClangCompleter_ForceSemantic_OnlyFileteredCompletions_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ int main() { @@ -241,7 +243,7 @@ int main() @with_setup( Setup ) def GetCompletions_ForceSemantic_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) completion_data = BuildRequest( filetype = 'python', force_semantic = True ) @@ -254,7 +256,7 @@ def GetCompletions_ForceSemantic_Works_test(): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) event_data = BuildRequest( event_name = 'FileReadyToParse', syntax_keywords = ['foo', 'bar', 'zoo'] ) @@ -271,7 +273,7 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): @with_setup( Setup ) def GetCompletions_UltiSnipsCompleter_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) event_data = BuildRequest( event_name = 'BufferVisit', ultisnips_snippets = [ @@ -292,7 +294,7 @@ def GetCompletions_UltiSnipsCompleter_Works_test(): @with_setup( Setup ) def RunCompleterCommand_GoTo_Jedi_ZeroBasedLineAndColumn_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ def foo(): pass @@ -318,7 +320,7 @@ foo() @with_setup( Setup ) def RunCompleterCommand_GoTo_Clang_ZeroBasedLineAndColumn_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ struct Foo { int x; @@ -352,7 +354,7 @@ int main() @with_setup( Setup ) def DefinedSubcommands_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) subcommands_data = BuildRequest( completer_target = 'python' ) eq_( [ 'GoToDefinition', @@ -363,7 +365,7 @@ def DefinedSubcommands_Works_test(): @with_setup( Setup ) def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) subcommands_data = BuildRequest( filetype = 'python' ) eq_( [ 'GoToDefinition', @@ -374,7 +376,7 @@ def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): @with_setup( Setup ) def Diagnostics_ClangCompleter_ZeroBasedLineAndColumn_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ struct Foo { int x // semicolon missing here! @@ -399,7 +401,7 @@ struct Foo { @with_setup( Setup ) def GetDetailedDiagnostic_ClangCompleter_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) contents = """ struct Foo { int x // semicolon missing here! @@ -427,7 +429,7 @@ struct Foo { @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) request_data = { 'filetypes': ['python'] } @@ -438,7 +440,7 @@ def FiletypeCompletionAvailable_Works_test(): @with_setup( Setup ) def UserOptions_Works_test(): - app = TestApp( ycmd.app ) + app = TestApp( handlers.app ) options = app.get( '/user_options' ).json ok_( len( options ) ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index ec20875c..a0c78247 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -17,214 +17,24 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +from server_utils import SetUpPythonPath +SetUpPythonPath() + import sys -import os -import atexit - -# We want to have the YouCompleteMe/python directory on the Python PATH because -# all the code already assumes that it's there. This is a relic from before the -# client/server architecture. -# TODO: Fix things so that this is not needed anymore when we split ycmd into a -# separate repository. -sys.path.insert( 0, os.path.join( - os.path.dirname( os.path.abspath( __file__ ) ), - '../..' ) ) - -from ycm import utils -utils.AddThirdPartyFoldersToSysPath() - import logging import json -import bottle import argparse -import httplib import waitress -from bottle import request, response -import server_state from ycm import user_options_store -from ycm.server.responses import BuildExceptionResponse from ycm import extra_conf_store -# num bytes for the request body buffer; request.json only works if the request -# size is less than this -bottle.Request.MEMFILE_MAX = 300 * 1024 -# TODO: rename these to _lower_case -SERVER_STATE = None -LOGGER = None -app = bottle.Bottle() - - -@app.post( '/event_notification' ) -def EventNotification(): - LOGGER.info( 'Received event notification' ) - request_data = request.json - event_name = request_data[ 'event_name' ] - LOGGER.debug( 'Event name: %s', event_name ) - - event_handler = 'On' + event_name - getattr( SERVER_STATE.GetGeneralCompleter(), event_handler )( request_data ) - - filetypes = request_data[ 'filetypes' ] - response_data = None - if SERVER_STATE.FiletypeCompletionUsable( filetypes ): - response_data = getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ), - event_handler )( request_data ) - - if response_data: - return _JsonResponse( response_data ) - - -@app.post( '/run_completer_command' ) -def RunCompleterCommand(): - LOGGER.info( 'Received command request' ) - request_data = request.json - completer = _GetCompleterForRequestData( request_data ) - - return _JsonResponse( completer.OnUserCommand( - request_data[ 'command_arguments' ], - request_data ) ) - - -@app.post( '/completions' ) -def GetCompletions(): - LOGGER.info( 'Received completion request' ) - request_data = request.json - do_filetype_completion = SERVER_STATE.ShouldUseFiletypeCompleter( - request_data ) - LOGGER.debug( 'Using filetype completion: %s', do_filetype_completion ) - filetypes = request_data[ 'filetypes' ] - completer = ( SERVER_STATE.GetFiletypeCompleter( filetypes ) if - do_filetype_completion else - SERVER_STATE.GetGeneralCompleter() ) - - return _JsonResponse( completer.ComputeCandidates( request_data ) ) - - -@app.get( '/user_options' ) -def GetUserOptions(): - LOGGER.info( 'Received user options GET request' ) - return _JsonResponse( dict( SERVER_STATE.user_options ) ) - - -@app.get( '/healthy' ) -def GetHealthy(): - LOGGER.info( 'Received health request' ) - return _JsonResponse( True ) - - -@app.post( '/user_options' ) -def SetUserOptions(): - LOGGER.info( 'Received user options POST request' ) - _SetUserOptions( request.json ) - - -@app.post( '/semantic_completion_available' ) -def FiletypeCompletionAvailable(): - LOGGER.info( 'Received filetype completion available request' ) - return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable( - request.json[ 'filetypes' ] ) ) - - -@app.post( '/defined_subcommands' ) -def DefinedSubcommands(): - LOGGER.info( 'Received defined subcommands request' ) - completer = _GetCompleterForRequestData( request.json ) - - return _JsonResponse( completer.DefinedSubcommands() ) - - -@app.post( '/detailed_diagnostic' ) -def GetDetailedDiagnostic(): - LOGGER.info( 'Received detailed diagnostic request' ) - request_data = request.json - completer = _GetCompleterForRequestData( request_data ) - - return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) ) - - -@app.post( '/load_extra_conf_file' ) -def LoadExtraConfFile(): - LOGGER.info( 'Received extra conf load request' ) - request_data = request.json - extra_conf_store.Load( request_data[ 'filepath' ], force = True ) - - -@app.post( '/debug_info' ) -def DebugInfo(): - # This can't be at the top level because of possible extra conf preload - import ycm_core - LOGGER.info( 'Received debug info request' ) - - output = [] - has_clang_support = ycm_core.HasClangSupport() - output.append( 'Server has Clang support compiled in: {0}'.format( - has_clang_support ) ) - - if has_clang_support: - output.append( 'Clang version: ' + ycm_core.ClangVersion() ) - - request_data = request.json - try: - output.append( - _GetCompleterForRequestData( request_data ).DebugInfo( request_data) ) - except: - pass - return _JsonResponse( '\n'.join( output ) ) - - -# The type of the param is Bottle.HTTPError -@app.error( httplib.INTERNAL_SERVER_ERROR ) -def ErrorHandler( httperror ): - return _JsonResponse( BuildExceptionResponse( httperror.exception, - httperror.traceback ) ) - - -def _JsonResponse( data ): - response.set_header( 'Content-Type', 'application/json' ) - return json.dumps( data, default = _UniversalSerialize ) - - -def _UniversalSerialize( obj ): - serialized = obj.__dict__.copy() - serialized[ 'TYPE' ] = type( obj ).__name__ - return serialized - - -def _GetCompleterForRequestData( request_data ): - completer_target = request_data.get( 'completer_target', None ) - - if completer_target == 'identifier': - return SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() - elif completer_target == 'filetype_default' or not completer_target: - return SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) - else: - return SERVER_STATE.GetFiletypeCompleter( [ completer_target ] ) - - -@atexit.register -def _ServerShutdown(): - if SERVER_STATE: - SERVER_STATE.Shutdown() - - -def _SetUserOptions( options ): - global SERVER_STATE - - user_options_store.SetAll( options ) - SERVER_STATE = server_state.ServerState( options ) - - -def SetServerStateToDefaults(): - global SERVER_STATE, LOGGER - LOGGER = logging.getLogger( __name__ ) - user_options_store.LoadDefaults() - SERVER_STATE = server_state.ServerState( user_options_store.GetAll() ) - extra_conf_store.Reset() +def YcmCoreSanityCheck(): + if 'ycm_core' in sys.modules: + raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' ) def Main(): - global LOGGER parser = argparse.ArgumentParser() parser.add_argument( '--host', type = str, default = 'localhost', help = 'server hostname') @@ -237,18 +47,31 @@ def Main(): help = 'file with user options, in JSON format' ) args = parser.parse_args() - if args.options_file: - _SetUserOptions( json.load( open( args.options_file, 'r' ) ) ) - numeric_level = getattr( logging, args.log.upper(), None ) if not isinstance( numeric_level, int ): raise ValueError( 'Invalid log level: %s' % args.log ) + # Has to be called before any call to logging.getLogger() logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s', level = numeric_level ) - LOGGER = logging.getLogger( __name__ ) - waitress.serve( app, host = args.host, port = args.port, threads = 10 ) + options = None + if args.options_file: + options = json.load( open( args.options_file, 'r' ) ) + user_options_store.SetAll( options ) + # This ensures that ycm_core is not loaded before extra conf preload + # was run. + YcmCoreSanityCheck() + extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists() + + # This can't be a top-level import because it transitively imports ycm_core + # which we want to be imported ONLY after extra conf preload has executed. + import handlers + handlers.UpdateUserOptions( options ) + waitress.serve( handlers.app, + host = args.host, + port = args.port, + threads = 10 ) if __name__ == "__main__": From 98f549aeae1c7902f4c69003fa8e77c804e10a15 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 20:38:45 -0700 Subject: [PATCH 123/149] More robust way of picking an unused local port Fixes #584. --- python/ycm/completers/cs/cs_completer.py | 4 +--- python/ycm/utils.py | 10 ++++++++++ python/ycm/youcompleteme.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 5a5a8d1f..4bf2f527 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -31,12 +31,10 @@ import json import subprocess import logging - SERVER_NOT_FOUND_MSG = ( 'OmniSharp server binary not found at {0}. ' + 'Did you compile it? You can do so by running ' + '"./install.sh --omnisharp-completer".' ) -CS_SERVER_PORT_RANGE_START = 10001 class CsharpCompleter( Completer ): """ @@ -118,7 +116,7 @@ class CsharpCompleter( Completer ): """ Start the OmniSharp server """ self._logger.info( 'startup' ) - self._omnisharp_port = CS_SERVER_PORT_RANGE_START + os.getpid() + self._omnisharp_port = utils.GetUnusedLocalhostPort() solutionfiles, folder = _FindSolutionFiles( request_data[ 'filepath' ] ) if len( solutionfiles ) == 0: diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 5d2281e4..0f36dca0 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -22,6 +22,7 @@ import os import sys import signal import functools +import socket WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' @@ -48,6 +49,15 @@ def PathToTempDir(): return tempdir +def GetUnusedLocalhostPort(): + sock = socket.socket() + # This tells the OS to give us any free port in the range [1024 - 65535] + sock.bind( ( '', 0 ) ) + port = sock.getsockname()[ 1 ] + sock.close() + return port + + def PathToPythonInterpreter(): # This is a bit tricky. Normally, sys.executable has the full path to the # Python interpreter. But this code is also executed from inside Vim's diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 68665084..48538742 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -57,7 +57,7 @@ class YouCompleteMe( object ): def _SetupServer( self ): - server_port = SERVER_PORT_RANGE_START + os.getpid() + server_port = utils.GetUnusedLocalhostPort() with tempfile.NamedTemporaryFile( delete = False ) as options_file: self._temp_options_filename = options_file.name json.dump( dict( self._user_options ), options_file ) From 016434e84696a4f76e9c4994eaacf89e9bb267c9 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 14 Oct 2013 20:46:42 -0700 Subject: [PATCH 124/149] Deleting some dead code --- python/ycm/youcompleteme.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 48538742..56811635 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -40,8 +40,6 @@ try: except ImportError: USE_ULTISNIPS_DATA = False -SERVER_PORT_RANGE_START = 10000 - class YouCompleteMe( object ): def __init__( self, user_options ): self._user_options = user_options From 436017bd4dd2d86d3a20e777d02290c406031750 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 15 Oct 2013 11:19:56 -0700 Subject: [PATCH 125/149] Now using new ycm_client_support shared lib This means we can now load just ycm_client_support (which is a much smaller library) into Vim and ycm_core into ycmd. Since ycm_client_support never depends on libclang.so, we never have to load that into Vim which makes things much, much easier. --- autoload/youcompleteme.vim | 2 +- cpp/BoostParts/CMakeLists.txt | 4 +- cpp/ycm/CMakeLists.txt | 89 +++++++++++++++++++++--------- cpp/ycm/tests/CMakeLists.txt | 5 +- cpp/ycm/versioning.cpp | 26 +++++++++ cpp/ycm/versioning.h | 22 ++++++++ cpp/ycm/ycm_client_support.cpp | 44 +++++++++++++++ cpp/ycm/ycm_core.cpp | 11 +--- plugin/youcompleteme.vim | 12 ++-- python/ycm/base.py | 16 +----- python/ycm/completers/completer.py | 4 +- python/ycm/server/handlers.py | 15 ++++- python/ycm/server/ycmd.py | 9 +-- python/ycm/vimsupport.py | 9 ++- python/ycm/youcompleteme.py | 18 ++++++ style_format.sh | 1 + 16 files changed, 221 insertions(+), 66 deletions(-) create mode 100644 cpp/ycm/versioning.cpp create mode 100644 cpp/ycm/versioning.h create mode 100644 cpp/ycm/ycm_client_support.cpp diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index fd6f77e7..11152f71 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -58,7 +58,7 @@ function! youcompleteme#Enable() if !pyeval( 'base.CompatibleWithYcmCore()') echohl WarningMsg | - \ echomsg "YouCompleteMe unavailable: ycm_core too old, PLEASE RECOMPILE ycm_core" | + \ echomsg "YouCompleteMe unavailable: YCM support libs too old, PLEASE RECOMPILE" | \ echohl None return endif diff --git a/cpp/BoostParts/CMakeLists.txt b/cpp/BoostParts/CMakeLists.txt index 0b1da46e..b2a648df 100644 --- a/cpp/BoostParts/CMakeLists.txt +++ b/cpp/BoostParts/CMakeLists.txt @@ -26,8 +26,8 @@ cmake_minimum_required( VERSION 2.8 ) project( BoostParts ) -set( Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5 ) -find_package( PythonLibs 2.5 REQUIRED ) +set( Python_ADDITIONAL_VERSIONS 2.7 2.6 ) +find_package( PythonLibs 2.6 REQUIRED ) if ( NOT PYTHONLIBS_VERSION_STRING VERSION_LESS "3.0.0" ) message( FATAL_ERROR diff --git a/cpp/ycm/CMakeLists.txt b/cpp/ycm/CMakeLists.txt index 3c0654d8..fa7d30b1 100644 --- a/cpp/ycm/CMakeLists.txt +++ b/cpp/ycm/CMakeLists.txt @@ -17,10 +17,12 @@ cmake_minimum_required( VERSION 2.8 ) -project( ycm_core ) +project( ycm_support_libs ) +set( CLIENT_LIB "ycm_client_support" ) +set( SERVER_LIB "ycm_core" ) -set( Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5 ) -find_package( PythonLibs 2.5 REQUIRED ) +set( Python_ADDITIONAL_VERSIONS 2.7 2.6 ) +find_package( PythonLibs 2.6 REQUIRED ) if ( NOT PYTHONLIBS_VERSION_STRING VERSION_LESS "3.0.0" ) message( FATAL_ERROR @@ -138,15 +140,15 @@ include_directories( ${CLANG_INCLUDES_DIR} ) -file( GLOB_RECURSE SOURCES *.h *.cpp ) +file( GLOB_RECURSE SERVER_SOURCES *.h *.cpp ) # The test sources are a part of a different target, so we remove them # The CMakeFiles cpp file is picked up when the user creates an in-source build, -# and we don't want that. -file( GLOB_RECURSE to_remove tests/*.h tests/*.cpp CMakeFiles/*.cpp ) +# and we don't want that. We also remove client-specific code +file( GLOB_RECURSE to_remove tests/*.h tests/*.cpp CMakeFiles/*.cpp *client* ) if( to_remove ) - list( REMOVE_ITEM SOURCES ${to_remove} ) + list( REMOVE_ITEM SERVER_SOURCES ${to_remove} ) endif() if ( USE_CLANG_COMPLETER ) @@ -158,7 +160,7 @@ else() file( GLOB_RECURSE to_remove_clang ClangCompleter/*.h ClangCompleter/*.cpp ) if( to_remove_clang ) - list( REMOVE_ITEM SOURCES ${to_remove_clang} ) + list( REMOVE_ITEM SERVER_SOURCES ${to_remove_clang} ) endif() endif() @@ -217,11 +219,33 @@ endif() ############################################################################# -add_library( ${PROJECT_NAME} SHARED - ${SOURCES} +# We don't actually need all of the files this picks up, just the ones needed by +# PythonSupport.cpp. But this is easier to maintain and dead code elemination +# will remove unused code. +file( GLOB CLIENT_SOURCES *.h *.cpp ) +file( GLOB SERVER_SPECIFIC *ycm_core* ) + +if( SERVER_SPECIFIC ) + list( REMOVE_ITEM CLIENT_SOURCES ${SERVER_SPECIFIC} ) +endif() + +add_library( ${CLIENT_LIB} SHARED + ${CLIENT_SOURCES} ) -target_link_libraries( ${PROJECT_NAME} +target_link_libraries( ${CLIENT_LIB} + BoostParts + ${PYTHON_LIBRARIES} + ${EXTRA_LIBS} + ) + +############################################################################# + +add_library( ${SERVER_LIB} SHARED + ${SERVER_SOURCES} + ) + +target_link_libraries( ${SERVER_LIB} BoostParts ${PYTHON_LIBRARIES} ${LIBCLANG_TARGET} @@ -231,35 +255,43 @@ target_link_libraries( ${PROJECT_NAME} if( LIBCLANG_TARGET ) if( NOT WIN32 ) add_custom_command( - TARGET ${PROJECT_NAME} + TARGET ${SERVER_LIB} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy "${LIBCLANG_TARGET}" "$" + COMMAND ${CMAKE_COMMAND} -E copy "${LIBCLANG_TARGET}" "$" ) else() add_custom_command( - TARGET ${PROJECT_NAME} + TARGET ${SERVER_LIB} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy "${PATH_TO_LLVM_ROOT}/bin/libclang.dll" "$") + COMMAND ${CMAKE_COMMAND} -E copy "${PATH_TO_LLVM_ROOT}/bin/libclang.dll" "$") endif() endif() +############################################################################# + +# Convenience target that builds both support libs. +add_custom_target( ${PROJECT_NAME} + DEPENDS ${CLIENT_LIB} ${SERVER_LIB} ) + + ############################################################################# # Things are a bit different on Macs when using an external libclang.dylib; here # we want to make sure we use @loader_path/libclang.dylib instead of -# @rpath/libclang.dylib in the final ycm_core.so. If we use the @rpath version, -# then it may load the system libclang which the user explicitely does not want -# (otherwise the user would specify USE_SYSTEM_LIBCLANG). With @loader_path, we -# make sure that only the libclang.dylib present in the same directory as our -# ycm_core.so is used. +# @rpath/libclang.dylib in the final ycm_core.so. If we use the +# @rpath version, then it may load the system libclang which the user +# explicitely does not want (otherwise the user would specify +# USE_SYSTEM_LIBCLANG). With @loader_path, we make sure that only the +# libclang.dylib present in the same directory as our ycm_core.so +# is used. if ( EXTERNAL_LIBCLANG_PATH AND APPLE ) - add_custom_command( TARGET ${PROJECT_NAME} + add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND install_name_tool "-change" "@rpath/libclang.dylib" "@loader_path/libclang.dylib" - "$" + "$" ) endif() @@ -268,19 +300,24 @@ endif() # We don't want the "lib" prefix, it can screw up python when it tries to search # for our module -set_target_properties( ${PROJECT_NAME} PROPERTIES PREFIX "") +set_target_properties( ${CLIENT_LIB} PROPERTIES PREFIX "") +set_target_properties( ${SERVER_LIB} PROPERTIES PREFIX "") if ( WIN32 OR CYGWIN ) # This is the extension for compiled Python modules on Windows - set_target_properties( ${PROJECT_NAME} PROPERTIES SUFFIX ".pyd") + set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".pyd") + set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".pyd") else() # Even on macs, we want a .so extension instead of a .dylib which is what # cmake would give us by default. Python won't recognize a .dylib as a module, # but it will recognize a .so - set_target_properties( ${PROJECT_NAME} PROPERTIES SUFFIX ".so") + set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".so") + set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".so") endif() -set_target_properties( ${PROJECT_NAME} PROPERTIES +set_target_properties( ${CLIENT_LIB} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../python ) +set_target_properties( ${SERVER_LIB} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../python ) ############################################################################# diff --git a/cpp/ycm/tests/CMakeLists.txt b/cpp/ycm/tests/CMakeLists.txt index 951c0aba..1383cc0f 100644 --- a/cpp/ycm/tests/CMakeLists.txt +++ b/cpp/ycm/tests/CMakeLists.txt @@ -28,7 +28,7 @@ endif() add_subdirectory( gmock ) include_directories( - ${ycm_core_SOURCE_DIR} + ${ycm_support_libs_SOURCE_DIR} ) include_directories( @@ -67,7 +67,8 @@ add_executable( ${PROJECT_NAME} ) target_link_libraries( ${PROJECT_NAME} - ycm_core + ${SERVER_LIB} + ${CLIENT_LIB} gmock ) diff --git a/cpp/ycm/versioning.cpp b/cpp/ycm/versioning.cpp new file mode 100644 index 00000000..0007ff2e --- /dev/null +++ b/cpp/ycm/versioning.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2013 Strahinja Val Markovic +// +// This file is part of YouCompleteMe. +// +// YouCompleteMe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// YouCompleteMe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with YouCompleteMe. If not, see . + +namespace YouCompleteMe { + +int YcmCoreVersion() { + // We increment this every time when we want to force users to recompile + // ycm_core. + return 7; +} + +} // namespace YouCompleteMe diff --git a/cpp/ycm/versioning.h b/cpp/ycm/versioning.h new file mode 100644 index 00000000..2b2fc081 --- /dev/null +++ b/cpp/ycm/versioning.h @@ -0,0 +1,22 @@ +// Copyright (C) 2013 Strahinja Val Markovic +// +// This file is part of YouCompleteMe. +// +// YouCompleteMe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// YouCompleteMe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with YouCompleteMe. If not, see . + +namespace YouCompleteMe { + +int YcmCoreVersion(); + +} // namespace YouCompleteMe diff --git a/cpp/ycm/ycm_client_support.cpp b/cpp/ycm/ycm_client_support.cpp new file mode 100644 index 00000000..25b77194 --- /dev/null +++ b/cpp/ycm/ycm_client_support.cpp @@ -0,0 +1,44 @@ +// Copyright (C) 2013 Strahinja Val Markovic +// +// This file is part of YouCompleteMe. +// +// YouCompleteMe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// YouCompleteMe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with YouCompleteMe. If not, see . + +#include "IdentifierCompleter.h" +#include "PythonSupport.h" +#include "versioning.h" + +#include +#include + + +BOOST_PYTHON_MODULE(ycm_client_support) +{ + using namespace boost::python; + using namespace YouCompleteMe; + + // Necessary because of usage of the ReleaseGil class + PyEval_InitThreads(); + + def( "FilterAndSortCandidates", FilterAndSortCandidates ); + def( "YcmCoreVersion", YcmCoreVersion ); +} + +// Boost.Thread forces us to implement this. +// We don't use any thread-specific (local) storage so it's fine to implement +// this as an empty function. +namespace boost { +void tss_cleanup_implemented() {} +}; + diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index 4aa3003d..be347652 100644 --- a/cpp/ycm/ycm_core.cpp +++ b/cpp/ycm/ycm_core.cpp @@ -17,6 +17,7 @@ #include "IdentifierCompleter.h" #include "PythonSupport.h" +#include "versioning.h" #ifdef USE_CLANG_COMPLETER # include "ClangCompleter.h" @@ -32,8 +33,7 @@ #include #include -bool HasClangSupport() -{ +bool HasClangSupport() { #ifdef USE_CLANG_COMPLETER return true; #else @@ -41,13 +41,6 @@ bool HasClangSupport() #endif // USE_CLANG_COMPLETER } -int YcmCoreVersion() -{ - // We increment this every time when we want to force users to recompile - // ycm_core. - return 6; -} - BOOST_PYTHON_MODULE(ycm_core) { diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index d6879287..384a098a 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -37,11 +37,14 @@ let s:script_folder_path = escape( expand( ':p:h' ), '\' ) function! s:HasYcmCore() let path_prefix = s:script_folder_path . '/../python/' - if filereadable(path_prefix . 'ycm_core.so') + if filereadable(path_prefix . 'ycm_client_support.so') && + \ filereadable(path_prefix . 'ycm_core.so') return 1 - elseif filereadable(path_prefix . 'ycm_core.pyd') + elseif filereadable(path_prefix . 'ycm_client_support.pyd') && + \ filereadable(path_prefix . 'ycm_core.pyd') return 1 - elseif filereadable(path_prefix . 'ycm_core.dll') + elseif filereadable(path_prefix . 'ycm_client_support.dll') + \ filereadable(path_prefix . 'ycm_core.dll') return 1 endif return 0 @@ -52,7 +55,8 @@ let g:ycm_check_if_ycm_core_present = if g:ycm_check_if_ycm_core_present && !s:HasYcmCore() echohl WarningMsg | - \ echomsg "ycm_core.[so|pyd|dll] not detected; you need to compile " . + \ echomsg "ycm_client_support.[so|pyd|dll] and " . + \ "ycm_core.[so|pyd|dll] not detected; you need to compile " . \ "YCM before using it. Read the docs!" | \ echohl None finish diff --git a/python/ycm/base.py b/python/ycm/base.py index 8bf94784..e578fa3b 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -17,25 +17,15 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . -import os import re import vim from ycm import vimsupport from ycm import utils from ycm import user_options_store +import ycm_client_support YCM_VAR_PREFIX = 'ycm_' -try: - import ycm_core -except ImportError as e: - vimsupport.PostVimMessage( - 'Error importing ycm_core. Are you sure you have placed a version 3.2+ ' - 'libclang.[so|dll|dylib] in folder "{0}"? See the Installation Guide in ' - 'the docs. Full error: {1}'.format( - os.path.dirname( os.path.dirname( os.path.abspath( __file__ ) ) ), - str( e ) ) ) - def BuildServerConf(): """Builds a dictionary mapping YCM Vim user options to values. Option names @@ -152,11 +142,11 @@ def AdjustCandidateInsertionText( candidates ): return new_candidates -COMPATIBLE_WITH_CORE_VERSION = 6 +COMPATIBLE_WITH_CORE_VERSION = 7 def CompatibleWithYcmCore(): try: - current_core_version = ycm_core.YcmCoreVersion() + current_core_version = ycm_client_support.YcmCoreVersion() except AttributeError: return False diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 4668076d..3e248531 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -18,7 +18,7 @@ # along with YouCompleteMe. If not, see . import abc -import ycm_core +import ycm_client_support from ycm.utils import ToUtf8IfNeeded, ForceSemanticCompletion from ycm.completers.completer_utils import TriggersForFiletype @@ -207,7 +207,7 @@ class Completer( object ): elif 'insertion_text' in candidates[ 0 ]: sort_property = 'insertion_text' - matches = ycm_core.FilterAndSortCandidates( + matches = ycm_client_support.FilterAndSortCandidates( candidates, sort_property, ToUtf8IfNeeded( query ) ) diff --git a/python/ycm/server/handlers.py b/python/ycm/server/handlers.py index 2ebf1c68..a20134f7 100644 --- a/python/ycm/server/handlers.py +++ b/python/ycm/server/handlers.py @@ -17,6 +17,18 @@ # You should have received a copy of the GNU General Public License # along with YouCompleteMe. If not, see . +from os import path + +try: + import ycm_core +except ImportError as e: + raise RuntimeError( + 'Error importing ycm_core. Are you sure you have placed a ' + 'version 3.2+ libclang.[so|dll|dylib] in folder "{0}"? ' + 'See the Installation Guide in the docs. Full error: {1}'.format( + path.realpath( path.join( path.abspath( __file__ ), '../../..' ) ), + str( e ) ) ) + import atexit import logging import json @@ -28,6 +40,7 @@ from ycm import user_options_store from ycm.server.responses import BuildExceptionResponse from ycm import extra_conf_store + # num bytes for the request body buffer; request.json only works if the request # size is less than this bottle.Request.MEMFILE_MAX = 300 * 1024 @@ -135,8 +148,6 @@ def LoadExtraConfFile(): @app.post( '/debug_info' ) def DebugInfo(): - # This can't be at the top level because of possible extra conf preload - import ycm_core LOGGER.info( 'Received debug info request' ) output = [] diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index a0c78247..1173e792 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -59,13 +59,14 @@ def Main(): if args.options_file: options = json.load( open( args.options_file, 'r' ) ) user_options_store.SetAll( options ) - # This ensures that ycm_core is not loaded before extra conf preload - # was run. + # This ensures that ycm_core is not loaded before extra conf + # preload was run. YcmCoreSanityCheck() extra_conf_store.CallGlobalExtraConfYcmCorePreloadIfExists() - # This can't be a top-level import because it transitively imports ycm_core - # which we want to be imported ONLY after extra conf preload has executed. + # This can't be a top-level import because it transitively imports + # ycm_core which we want to be imported ONLY after extra conf + # preload has executed. import handlers handlers.UpdateUserOptions( options ) waitress.serve( handlers.app, diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 21d1daf2..7138cf49 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -146,7 +146,14 @@ def NumLinesInBuffer( buffer_object ): # time of writing, YCM only uses the GUI thread inside Vim (this used to not be # the case). def PostVimMessage( message ): - vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None" + vim.command( "echohl WarningMsg | echom '{0}' | echohl None" + .format( EscapeForVim( str( message ) ) ) ) + +# Unlike PostVimMesasge, this supports messages with newlines in them because it +# uses 'echo' instead of 'echomsg'. This also means that the message will NOT +# appear in Vim's message log. +def PostMultiLineNotice( message ): + vim.command( "echohl WarningMsg | echo '{0}' | echohl None" .format( EscapeForVim( str( message ) ) ) ) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 56811635..be59c4c6 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -40,6 +40,11 @@ try: except ImportError: USE_ULTISNIPS_DATA = False +SERVER_CRASH_MESSAGE_STDERR_FILE = 'The ycmd server crashed with output:\n' +SERVER_CRASH_MESSAGE_SAME_STDERR = ( + 'The ycmd server crashed, check console output for logs!' ) + + class YouCompleteMe( object ): def __init__( self, user_options ): self._user_options = user_options @@ -83,6 +88,18 @@ class YouCompleteMe( object ): self._server_popen = subprocess.Popen( args, stdout = fstdout, stderr = fstderr ) + self._CheckIfServerCrashed() + + + def _CheckIfServerCrashed( self ): + server_crashed = self._server_popen.poll() + if server_crashed: + if self._server_stderr: + with open( self._server_stderr, 'r' ) as server_stderr_file: + vimsupport.PostMultiLineNotice( SERVER_CRASH_MESSAGE_STDERR_FILE + + server_stderr_file.read() ) + else: + vimsupport.PostVimMessage( SERVER_CRASH_MESSAGE_SAME_STDERR ) def CreateCompletionRequest( self, force_semantic = False ): @@ -126,6 +143,7 @@ class YouCompleteMe( object ): def OnFileReadyToParse( self ): + self._CheckIfServerCrashed() self._omnicomp.OnFileReadyToParse( None ) extra_data = {} diff --git a/style_format.sh b/style_format.sh index 2988c68b..d0983cf5 100755 --- a/style_format.sh +++ b/style_format.sh @@ -19,6 +19,7 @@ astyle \ --recursive \ --exclude=gmock \ --exclude=testdata \ +--exclude=ycm_client_support.cpp \ --exclude=ycm_core.cpp \ --exclude=CustomAssert.h \ --exclude=CustomAssert.cpp \ From 18002e17df9f74ca7750f2b9fa2d65d344dd0a5a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 15 Oct 2013 15:27:54 -0700 Subject: [PATCH 126/149] Better handling of crashed ycmd; restart command Previously the YCM Vim client would go bonkers when ycmd crashed. Now the user can continue using Vim just without YCM functionality. Also added a :YcmRestartServer command to let the user restart ycmd if it crashed. With a little luck, this will be rarely necessary. --- autoload/youcompleteme.vim | 13 ++++++- python/ycm/server/handlers.py | 1 + python/ycm/youcompleteme.py | 70 ++++++++++++++++++++++++++--------- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 11152f71..64d43c91 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -566,7 +566,11 @@ function! youcompleteme#Complete( findstart, base ) return -2 endif - return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' ) + py request = ycm_state.CreateCompletionRequest() + if !pyeval( 'bool(request)' ) + return -2 + endif + return pyeval( 'request.CompletionStartColumn()' ) else return s:CompletionsForQuery( a:base ) endif @@ -584,6 +588,13 @@ function! youcompleteme#OmniComplete( findstart, base ) endfunction +function! s:RestartServer() + py ycm_state.RestartServer() +endfunction + +command! YcmRestartServer call s:RestartServer() + + function! s:ShowDetailedDiagnostic() py ycm_state.ShowDetailedDiagnostic() endfunction diff --git a/python/ycm/server/handlers.py b/python/ycm/server/handlers.py index a20134f7..9c53fcde 100644 --- a/python/ycm/server/handlers.py +++ b/python/ycm/server/handlers.py @@ -198,6 +198,7 @@ def _GetCompleterForRequestData( request_data ): @atexit.register def _ServerShutdown(): + LOGGER.info( 'Server shutting down' ) if SERVER_STATE: SERVER_STATE.Shutdown() extra_conf_store.Shutdown() diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index be59c4c6..6e9180be 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -88,18 +88,30 @@ class YouCompleteMe( object ): self._server_popen = subprocess.Popen( args, stdout = fstdout, stderr = fstderr ) - self._CheckIfServerCrashed() + self._NotifyUserIfServerCrashed() - def _CheckIfServerCrashed( self ): - server_crashed = self._server_popen.poll() - if server_crashed: - if self._server_stderr: - with open( self._server_stderr, 'r' ) as server_stderr_file: - vimsupport.PostMultiLineNotice( SERVER_CRASH_MESSAGE_STDERR_FILE + - server_stderr_file.read() ) - else: - vimsupport.PostVimMessage( SERVER_CRASH_MESSAGE_SAME_STDERR ) + def _IsServerAlive( self ): + returncode = self._server_popen.poll() + # When the process hasn't finished yet, poll() returns None. + return returncode is None + + + def _NotifyUserIfServerCrashed( self ): + if self._IsServerAlive(): + return + if self._server_stderr: + with open( self._server_stderr, 'r' ) as server_stderr_file: + vimsupport.PostMultiLineNotice( SERVER_CRASH_MESSAGE_STDERR_FILE + + server_stderr_file.read() ) + else: + vimsupport.PostVimMessage( SERVER_CRASH_MESSAGE_SAME_STDERR ) + + + def RestartServer( self ): + vimsupport.PostVimMessage( 'Restarting ycmd server...' ) + self.OnVimLeave() + self._SetupServer() def CreateCompletionRequest( self, force_semantic = False ): @@ -111,17 +123,23 @@ class YouCompleteMe( object ): self._omnicomp.ShouldUseNow() ): self._latest_completion_request = OmniCompletionRequest( self._omnicomp ) else: - self._latest_completion_request = CompletionRequest( force_semantic ) + self._latest_completion_request = ( CompletionRequest( force_semantic ) + if self._IsServerAlive() else + None ) return self._latest_completion_request def SendCommandRequest( self, arguments, completer ): - return SendCommandRequest( arguments, completer ) + if self._IsServerAlive(): + return SendCommandRequest( arguments, completer ) def GetDefinedSubcommands( self ): - return BaseRequest.PostDataToHandler( BuildRequestData(), - 'defined_subcommands' ) + if self._IsServerAlive(): + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'defined_subcommands' ) + else: + return [] def GetCurrentCompletionRequest( self ): @@ -143,9 +161,11 @@ class YouCompleteMe( object ): def OnFileReadyToParse( self ): - self._CheckIfServerCrashed() self._omnicomp.OnFileReadyToParse( None ) + if not self._IsServerAlive(): + self._NotifyUserIfServerCrashed() + extra_data = {} if self._user_options[ 'collect_identifiers_from_tags_files' ]: extra_data[ 'tag_files' ] = _GetTagFiles() @@ -159,26 +179,35 @@ class YouCompleteMe( object ): def OnBufferUnload( self, deleted_buffer_file ): + if not self._IsServerAlive(): + return SendEventNotificationAsync( 'BufferUnload', { 'unloaded_buffer': deleted_buffer_file } ) def OnBufferVisit( self ): + if not self._IsServerAlive(): + return extra_data = {} _AddUltiSnipsDataIfNeeded( extra_data ) SendEventNotificationAsync( 'BufferVisit', extra_data ) def OnInsertLeave( self ): + if not self._IsServerAlive(): + return SendEventNotificationAsync( 'InsertLeave' ) def OnVimLeave( self ): - self._server_popen.terminate() + if self._IsServerAlive(): + self._server_popen.terminate() os.remove( self._temp_options_filename ) def OnCurrentIdentifierFinished( self ): + if not self._IsServerAlive(): + return SendEventNotificationAsync( 'CurrentIdentifierFinished' ) @@ -200,6 +229,8 @@ class YouCompleteMe( object ): def ShowDetailedDiagnostic( self ): + if not self._IsServerAlive(): + return debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), 'detailed_diagnostic' ) if 'message' in debug_info: @@ -207,8 +238,11 @@ class YouCompleteMe( object ): def DebugInfo( self ): - debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), - 'debug_info' ) + if self._IsServerAlive(): + debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), + 'debug_info' ) + else: + debug_info = 'Server crashed, no debug info from server' debug_info += '\nServer running at: {0}'.format( BaseRequest.server_location ) if self._server_stderr or self._server_stdout: From aadb93e889710dc469813f69d739078a8b035e2e Mon Sep 17 00:00:00 2001 From: Andrea Cedraro Date: Wed, 16 Oct 2013 12:25:25 +0200 Subject: [PATCH 127/149] Fix error on startup caused by malformed expression --- plugin/youcompleteme.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 384a098a..24f9f504 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -43,7 +43,7 @@ function! s:HasYcmCore() elseif filereadable(path_prefix . 'ycm_client_support.pyd') && \ filereadable(path_prefix . 'ycm_core.pyd') return 1 - elseif filereadable(path_prefix . 'ycm_client_support.dll') + elseif filereadable(path_prefix . 'ycm_client_support.dll') && \ filereadable(path_prefix . 'ycm_core.dll') return 1 endif From e20c88a4b51f996c6b4281a27231fa314c41321d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 16 Oct 2013 09:55:40 -0700 Subject: [PATCH 128/149] install.sh script works again Fixes #589 --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 14b0b62d..3a5a2fe1 100755 --- a/install.sh +++ b/install.sh @@ -77,7 +77,7 @@ function install { cmake -G "Unix Makefiles" "$@" . $ycm_dir/cpp fi - make -j $(num_cores) ycm_core + make -j $(num_cores) ycm_support_libs popd rm -rf $build_dir } From 5b37a2e36d50a19e11b57cbf9eb480efc38f989f Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 16 Oct 2013 14:37:42 -0700 Subject: [PATCH 129/149] Don't print traceback when no detailed diagnostic We should only print the message. --- python/ycm/youcompleteme.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 6e9180be..211d43de 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -33,6 +33,7 @@ from ycm.client.completion_request import CompletionRequest from ycm.client.omni_completion_request import OmniCompletionRequest from ycm.client.event_notification import ( SendEventNotificationAsync, EventNotification ) +from ycm.server.responses import ServerError try: from UltiSnips import UltiSnips_Manager @@ -231,10 +232,13 @@ class YouCompleteMe( object ): def ShowDetailedDiagnostic( self ): if not self._IsServerAlive(): return - debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), - 'detailed_diagnostic' ) - if 'message' in debug_info: - vimsupport.EchoText( debug_info[ 'message' ] ) + try: + debug_info = BaseRequest.PostDataToHandler( BuildRequestData(), + 'detailed_diagnostic' ) + if 'message' in debug_info: + vimsupport.EchoText( debug_info[ 'message' ] ) + except ServerError as e: + vimsupport.PostVimMessage( str( e ) ) def DebugInfo( self ): From b4d5b4ffbb77fde363112b359d99c68e1c80d441 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 12:21:33 -0700 Subject: [PATCH 130/149] More logging for extra conf store --- python/ycm/extra_conf_store.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/ycm/extra_conf_store.py b/python/ycm/extra_conf_store.py index 0543a13a..d49576c4 100644 --- a/python/ycm/extra_conf_store.py +++ b/python/ycm/extra_conf_store.py @@ -24,6 +24,7 @@ import imp import random import string import sys +import logging from threading import Lock from ycm import user_options_store from ycm.server.responses import UnknownExtraConf @@ -76,14 +77,21 @@ def Shutdown(): def _CallGlobalExtraConfMethod( function_name ): + logger = _Logger() global_ycm_extra_conf = _GlobalYcmExtraConfFileLocation() if not ( global_ycm_extra_conf and os.path.exists( global_ycm_extra_conf ) ): + logger.debug( 'No global extra conf, not calling method ' + function_name ) return module = Load( global_ycm_extra_conf, force = True ) if not module or not hasattr( module, function_name ): + logger.debug( 'Global extra conf not loaded or no function ' + + function_name ) return + + logger.info( 'Calling global extra conf method {0} on conf file {1}'.format( + function_name, global_ycm_extra_conf ) ) getattr( module, function_name )() @@ -214,3 +222,7 @@ def _RandomName(): def _GlobalYcmExtraConfFileLocation(): return os.path.expanduser( user_options_store.Value( 'global_ycm_extra_conf' ) ) + + +def _Logger(): + return logging.getLogger( __name__ ) From 5070835d019b36069fbc804d689e1ba40d6e0528 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 12:21:59 -0700 Subject: [PATCH 131/149] Ensuring server cleanup on OS signals atexit won't run registered functions for SIGTERM which we send to the server. This prevents clean shutdown. Also making sure that the server logfiles are deleted as well. --- python/ycm/server/default_settings.json | 1 + python/ycm/server/handlers.py | 2 +- python/ycm/server/ycmd.py | 16 ++++++++++++++++ python/ycm/youcompleteme.py | 6 ++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 73682183..4ee627a7 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -28,6 +28,7 @@ }, "server_use_vim_stdout": 0, "server_log_level": "info", + "server_keep_logfiles": 0, "auto_start_csharp_server": 1, "auto_stop_csharp_server": 1, "csharp_server_port": 2000 diff --git a/python/ycm/server/handlers.py b/python/ycm/server/handlers.py index 9c53fcde..6640511a 100644 --- a/python/ycm/server/handlers.py +++ b/python/ycm/server/handlers.py @@ -197,7 +197,7 @@ def _GetCompleterForRequestData( request_data ): @atexit.register -def _ServerShutdown(): +def ServerShutdown(): LOGGER.info( 'Server shutting down' ) if SERVER_STATE: SERVER_STATE.Shutdown() diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 1173e792..036712b1 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -25,6 +25,7 @@ import logging import json import argparse import waitress +import signal from ycm import user_options_store from ycm import extra_conf_store @@ -34,6 +35,20 @@ def YcmCoreSanityCheck(): raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' ) +# We need to manually call ServerShutdown for the signals that turn down ycmd +# because atexit won't handle them. +def SetUpSignalHandler(): + def SignalHandler(): + import handlers + handlers.ServerShutdown() + + for sig in [ signal.SIGTERM, + signal.SIGINT, + signal.SIGHUP, + signal.SIGQUIT ]: + signal.signal( sig, SignalHandler ) + + def Main(): parser = argparse.ArgumentParser() parser.add_argument( '--host', type = str, default = 'localhost', @@ -69,6 +84,7 @@ def Main(): # preload has executed. import handlers handlers.UpdateUserOptions( options ) + SetUpSignalHandler() waitress.serve( handlers.app, host = args.host, port = args.port, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 211d43de..8d8fee92 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -205,6 +205,12 @@ class YouCompleteMe( object ): self._server_popen.terminate() os.remove( self._temp_options_filename ) + if not self._user_options[ 'server_keep_logfiles' ]: + if self._server_stderr: + os.remove( self._server_stderr ) + if self._server_stdout: + os.remove( self._server_stdout ) + def OnCurrentIdentifierFinished( self ): if not self._IsServerAlive(): From 40464f6e0da4825817e85a021ce9618b00043c64 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 14:21:37 -0700 Subject: [PATCH 132/149] Resolving issues with event requests timing out It appears that the issue comes from sending a None timeout to Requests. It seems it's a bug in Requests/urllib3. So we just pick an arbitrary long timeout of 30s as the default. --- python/ycm/client/base_request.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 45ce8926..78ac368d 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -29,6 +29,8 @@ from ycm.server.responses import ServerError, UnknownExtraConf HEADERS = {'content-type': 'application/json'} EXECUTOR = ThreadPoolExecutor( max_workers = 10 ) +# Setting this to None seems to screw up the Requests/urllib3 libs. +DEFAULT_TIMEOUT_SEC = 30 class BaseRequest( object ): def __init__( self ): @@ -51,7 +53,7 @@ class BaseRequest( object ): # |timeout| is num seconds to tolerate no response from server before giving # up; see Requests docs for details (we just pass the param along). @staticmethod - def PostDataToHandler( data, handler, timeout = None ): + def PostDataToHandler( data, handler, timeout = DEFAULT_TIMEOUT_SEC ): return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data, handler, timeout ) ) @@ -61,7 +63,7 @@ class BaseRequest( object ): # |timeout| is num seconds to tolerate no response from server before giving # up; see Requests docs for details (we just pass the param along). @staticmethod - def PostDataToHandlerAsync( data, handler, timeout = None ): + def PostDataToHandlerAsync( data, handler, timeout = DEFAULT_TIMEOUT_SEC ): def PostData( data, handler, timeout ): return BaseRequest.session.post( _BuildUri( handler ), data = json.dumps( data ), From acae8e4e9d5e47c2cc081ff331dcaebde77bd417 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 14:45:16 -0700 Subject: [PATCH 133/149] Tweaked the request retries logic. We now send more retry requests that are less spaced apart. --- python/ycm/client/base_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 78ac368d..ad3f1fbf 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -70,7 +70,7 @@ class BaseRequest( object ): headers = HEADERS, timeout = timeout ) - @retries( 3, delay = 0.5 ) + @retries( 5, delay = 0.5, backoff = 1.5 ) def DelayedPostData( data, handler ): return requests.post( _BuildUri( handler ), data = json.dumps( data ), From 6e9a16e90ec6ba09e96561f8e3a7fbefa2d366cc Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 20:14:56 -0700 Subject: [PATCH 134/149] Signal handler must take 2 params Otherwise we get a TypeError --- python/ycm/server/ycmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 036712b1..4d75f351 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -38,7 +38,7 @@ def YcmCoreSanityCheck(): # We need to manually call ServerShutdown for the signals that turn down ycmd # because atexit won't handle them. def SetUpSignalHandler(): - def SignalHandler(): + def SignalHandler( signum, frame ): import handlers handlers.ServerShutdown() From a5fb6b7509237e55324e52b1ae081c54d073eef2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 20:16:44 -0700 Subject: [PATCH 135/149] Removing unsupported signal() calls on Windows SIGQUIT and SIGHUP are not supported. For details, see Python docs: http://docs.python.org/2/library/signal.html#signal.signal --- python/ycm/server/ycmd.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 4d75f351..350eabe6 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -43,9 +43,7 @@ def SetUpSignalHandler(): handlers.ServerShutdown() for sig in [ signal.SIGTERM, - signal.SIGINT, - signal.SIGHUP, - signal.SIGQUIT ]: + signal.SIGINT ]: signal.signal( sig, SignalHandler ) From f6ca040cf726ba7845051eb74f452cfa9333b237 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 22:12:28 -0700 Subject: [PATCH 136/149] Nth attempt at correct shutdown procedure. If we install an explicit signal handler for SIGTERM and SIGINT and then call sys.exit ourselves, atexit handlers are run. If we don't call sys.exit from the handler, ycmd never shuts down. So fixed... I think. We'll see. Fixes #577... again. --- python/ycm/server/ycmd.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 350eabe6..65e9214c 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -35,12 +35,11 @@ def YcmCoreSanityCheck(): raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' ) -# We need to manually call ServerShutdown for the signals that turn down ycmd -# because atexit won't handle them. +# We manually call sys.exit() on SIGTERM and SIGINT so that atexit handlers are +# properly executed. def SetUpSignalHandler(): def SignalHandler( signum, frame ): - import handlers - handlers.ServerShutdown() + sys.exit() for sig in [ signal.SIGTERM, signal.SIGINT ]: From 9d8fdac51872c0df9df5baa047ebc1d7c4459f5e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 18 Oct 2013 12:35:40 -0700 Subject: [PATCH 137/149] Minor cleanup of Completer comments --- python/ycm/completers/completer.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 3e248531..efd8feff 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -71,7 +71,7 @@ class Completer( object ): return a list of strings, where the strings are Vim filetypes your completer supports. - clang_completer.py is a good example of a "complicated" completer that A good + clang_completer.py is a good example of a "complicated" completer. A good example of a simple completer is ultisnips_completer.py. The On* functions are provided for your convenience. They are called when @@ -82,8 +82,7 @@ class Completer( object ): One special function is OnUserCommand. It is called when the user uses the command :YcmCompleter and is passed all extra arguments used on command invocation (e.g. OnUserCommand(['first argument', 'second'])). This can be - used for completer-specific commands such as reloading external - configuration. + used for completer-specific commands such as reloading external configuration. When the command is called with no arguments you should print a short summary of the supported commands or point the user to the help section where this information can be found. From 5719a5e0806703b2c51084c3a53507556aedaa48 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 21 Oct 2013 12:49:34 -0700 Subject: [PATCH 138/149] Updating to latest upstream Jedi (dev branch) This fixes #599. --- third_party/jedi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/jedi b/third_party/jedi index 78f1ae5e..099fe4ee 160000 --- a/third_party/jedi +++ b/third_party/jedi @@ -1 +1 @@ -Subproject commit 78f1ae5e7163bda737433f1d551b3180cf03e1d1 +Subproject commit 099fe4eeb3544005a8e2ffdaae43fd8a12c82f16 From 6d9969fa9c70fbd5761ebecbc5eb6f598d586dce Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 22 Oct 2013 10:51:37 -0700 Subject: [PATCH 139/149] Surfacing ycmd server PID to user scripts To get the PID, call function youcompleteme#ServerPid(). --- autoload/youcompleteme.vim | 5 +++++ python/ycm/vimsupport.py | 1 + python/ycm/youcompleteme.py | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 64d43c91..4aa6feab 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -588,6 +588,11 @@ function! youcompleteme#OmniComplete( findstart, base ) endfunction +function! youcompleteme#ServerPid() + return pyeval( 'ycm_state.ServerPid()' ) +endfunction + + function! s:RestartServer() py ycm_state.RestartServer() endfunction diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 7138cf49..6ee36e3a 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -217,3 +217,4 @@ def GetBoolValue( variable ): def GetIntValue( variable ): return int( vim.eval( variable ) ) + diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 8d8fee92..0cc60340 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -109,6 +109,12 @@ class YouCompleteMe( object ): vimsupport.PostVimMessage( SERVER_CRASH_MESSAGE_SAME_STDERR ) + def ServerPid( self ): + if not self._server_popen: + return -1 + return self._server_popen.pid + + def RestartServer( self ): vimsupport.PostVimMessage( 'Restarting ycmd server...' ) self.OnVimLeave() From ce96a47098179d78c2c5229a20daf2b051e9504a Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 22 Oct 2013 10:53:31 -0700 Subject: [PATCH 140/149] Printing server PID in YcmDebugInfo --- python/ycm/youcompleteme.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 0cc60340..3a238572 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -261,6 +261,7 @@ class YouCompleteMe( object ): debug_info = 'Server crashed, no debug info from server' debug_info += '\nServer running at: {0}'.format( BaseRequest.server_location ) + debug_info += '\nServer process ID: {0}'.format( self._server_popen.pid ) if self._server_stderr or self._server_stdout: debug_info += '\nServer logfiles:\n {0}\n {1}'.format( self._server_stdout, From 5b76bcf8b7f926d5a11c695e5ed72acaec3d75af Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 22 Oct 2013 13:46:24 -0700 Subject: [PATCH 141/149] GetDetailedDiagnostic actually takes 2 params The Completer class version of the func only took 1 by mistake. --- python/ycm/completers/completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index efd8feff..780f1f78 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -242,7 +242,7 @@ class Completer( object ): return [] - def GetDetailedDiagnostic( self ): + def GetDetailedDiagnostic( self, request_data ): pass From 78107361b3e907a7a15af17a88fa3cbdf6d443e5 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 22 Oct 2013 13:48:15 -0700 Subject: [PATCH 142/149] Killing some dead code --- python/ycm/completers/completer.py | 4 ---- python/ycm/completers/cpp/clang_completer.py | 5 ----- 2 files changed, 9 deletions(-) diff --git a/python/ycm/completers/completer.py b/python/ycm/completers/completer.py index 780f1f78..63ccffa7 100644 --- a/python/ycm/completers/completer.py +++ b/python/ycm/completers/completer.py @@ -246,10 +246,6 @@ class Completer( object ): pass - def GettingCompletions( self ): - return False - - def _CurrentFiletype( self, filetypes ): supported = self.SupportedFiletypes() diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index f892b304..fb034cee 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -206,11 +206,6 @@ class ClangCompleter( Completer ): ToUtf8IfNeeded( request_data[ 'unloaded_buffer' ] ) ) - def GettingCompletions( self, request_data ): - return self._completer.UpdatingTranslationUnit( - ToUtf8IfNeeded( request_data[ 'filepath' ] ) ) - - def GetDetailedDiagnostic( self, request_data ): current_line = request_data[ 'line_num' ] + 1 current_column = request_data[ 'column_num' ] + 1 From 262d8aad033a59ba7da93b047debdacdc5cc0c15 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 23 Oct 2013 11:02:25 -0700 Subject: [PATCH 143/149] New ycmd watchdog kills server when idle too long Defaults are kill server after 12 hours of inactivity; the reason why it's 12 hours and not less is because we don't want to kill the server when the user just left his machine (and Vim) on during the night. --- python/ycm/server/default_settings.json | 1 + python/ycm/server/watchdog_plugin.py | 78 +++++++++++++++++++++++++ python/ycm/server/ycmd.py | 6 +- python/ycm/youcompleteme.py | 8 ++- 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 python/ycm/server/watchdog_plugin.py diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 4ee627a7..3cfdbc65 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -29,6 +29,7 @@ "server_use_vim_stdout": 0, "server_log_level": "info", "server_keep_logfiles": 0, + "server_idle_shutdown_seconds": 43200, "auto_start_csharp_server": 1, "auto_stop_csharp_server": 1, "csharp_server_port": 2000 diff --git a/python/ycm/server/watchdog_plugin.py b/python/ycm/server/watchdog_plugin.py new file mode 100644 index 00000000..82be070f --- /dev/null +++ b/python/ycm/server/watchdog_plugin.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013 Strahinja Val Markovic +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import time +import os +import copy +from ycm import utils +from threading import Thread, Lock + +# This class implements the Bottle plugin API: +# http://bottlepy.org/docs/dev/plugindev.html +# +# The idea here is to decorate every route handler automatically so that on +# every request, we log when the request was made. Then a watchdog thread checks +# every check_interval_seconds whether the server has been idle for a time +# greater that the passed-in idle_shutdown_seconds. If it has, we kill the +# server. +# +# We want to do this so that if something goes bonkers in Vim and the server +# never gets killed by the client, we don't end up with lots of zombie servers. +class WatchdogPlugin( object ): + name = 'watchdog' + api = 2 + + + def __init__( self, + idle_shutdown_seconds, + check_interval_seconds = 60 * 10 ): + self._check_interval_seconds = check_interval_seconds + self._idle_shutdown_seconds = idle_shutdown_seconds + self._last_request_time = time.time() + self._last_request_time_lock = Lock() + if idle_shutdown_seconds <= 0: + return + self._watchdog_thread = Thread( target = self._WatchdogMain ) + self._watchdog_thread.daemon = True + self._watchdog_thread.start() + + + def _GetLastRequestTime( self ): + with self._last_request_time_lock: + return copy.deepcopy( self._last_request_time ) + + + def _SetLastRequestTime( self, new_value ): + with self._last_request_time_lock: + self._last_request_time = new_value + + + def _WatchdogMain( self ): + while True: + time.sleep( self._check_interval_seconds ) + if time.time() - self._GetLastRequestTime() > self._idle_shutdown_seconds: + utils.TerminateProcess( os.getpid() ) + + + def __call__( self, callback ): + def wrapper( *args, **kwargs ): + self._SetLastRequestTime( time.time() ) + return callback( *args, **kwargs ) + return wrapper + diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 65e9214c..a80e1cbc 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -28,6 +28,7 @@ import waitress import signal from ycm import user_options_store from ycm import extra_conf_store +from ycm.server.watchdog_plugin import WatchdogPlugin def YcmCoreSanityCheck(): @@ -55,6 +56,8 @@ def Main(): parser.add_argument( '--log', type = str, default = 'info', help = 'log level, one of ' '[debug|info|warning|error|critical]' ) + parser.add_argument( '--idle_shutdown_seconds', type = int, default = 0, + help = 'num idle seconds before server shuts down') parser.add_argument( '--options_file', type = str, default = '', help = 'file with user options, in JSON format' ) args = parser.parse_args() @@ -79,9 +82,10 @@ def Main(): # This can't be a top-level import because it transitively imports # ycm_core which we want to be imported ONLY after extra conf # preload has executed. - import handlers + from ycm.server import handlers handlers.UpdateUserOptions( options ) SetUpSignalHandler() + handlers.app.install( WatchdogPlugin( args.idle_shutdown_seconds ) ) waitress.serve( handlers.app, host = args.host, port = args.port, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 3a238572..6967c807 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -41,9 +41,9 @@ try: except ImportError: USE_ULTISNIPS_DATA = False -SERVER_CRASH_MESSAGE_STDERR_FILE = 'The ycmd server crashed with output:\n' +SERVER_CRASH_MESSAGE_STDERR_FILE = 'The ycmd server SHUT DOWN with output:\n' SERVER_CRASH_MESSAGE_SAME_STDERR = ( - 'The ycmd server crashed, check console output for logs!' ) + 'The ycmd server shut down, check console output for logs!' ) class YouCompleteMe( object ): @@ -69,7 +69,9 @@ class YouCompleteMe( object ): _PathToServerScript(), '--port={0}'.format( server_port ), '--options_file={0}'.format( options_file.name ), - '--log={0}'.format( self._user_options[ 'server_log_level' ] ) ] + '--log={0}'.format( self._user_options[ 'server_log_level' ] ), + '--idle_shutdown_seconds={0}'.format( + self._user_options[ 'server_idle_shutdown_seconds' ] ) ] BaseRequest.server_location = 'http://localhost:' + str( server_port ) From b2aa5e3d3f578042374f47f8259b15b6d92b9c4f Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 23 Oct 2013 12:30:13 -0700 Subject: [PATCH 144/149] Moving client-only settings out of settings JSON Since these options are only used on the client, they shouldn't be in default_settings.json. --- plugin/youcompleteme.vim | 13 +++++++++++++ python/ycm/server/default_settings.json | 4 ---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 24f9f504..f8465da0 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -100,6 +100,19 @@ let g:ycm_key_detailed_diagnostics = let g:ycm_cache_omnifunc = \ get( g:, 'ycm_cache_omnifunc', 1 ) +let g:ycm_server_use_vim_stdout = + \ get( g:, 'ycm_server_use_vim_stdout', 0 ) + +let g:ycm_server_log_level = + \ get( g:, 'ycm_server_log_level', 'info' ) + +let g:ycm_server_keep_logfiles = + \ get( g:, 'ycm_server_keep_logfiles', 0 ) + +let g:ycm_server_idle_shutdown_seconds = + \ get( g:, 'ycm_server_idle_shutdown_seconds', 43200 ) + + " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. augroup youcompletemeStart diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 3cfdbc65..df4b3204 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -26,10 +26,6 @@ "unite": 1, "text": 1 }, - "server_use_vim_stdout": 0, - "server_log_level": "info", - "server_keep_logfiles": 0, - "server_idle_shutdown_seconds": 43200, "auto_start_csharp_server": 1, "auto_stop_csharp_server": 1, "csharp_server_port": 2000 From 4af2ba0faacfd111d28384b52a183bab02e7662b Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 23 Oct 2013 12:33:27 -0700 Subject: [PATCH 145/149] Option "shutdown_secs" is now "suicide_secs" Calling the option server_idle_suicide_seconds should be easier to understand. --- plugin/youcompleteme.vim | 4 ++-- python/ycm/server/watchdog_plugin.py | 10 +++++----- python/ycm/server/ycmd.py | 4 ++-- python/ycm/youcompleteme.py | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index f8465da0..f8e9b3e9 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -109,8 +109,8 @@ let g:ycm_server_log_level = let g:ycm_server_keep_logfiles = \ get( g:, 'ycm_server_keep_logfiles', 0 ) -let g:ycm_server_idle_shutdown_seconds = - \ get( g:, 'ycm_server_idle_shutdown_seconds', 43200 ) +let g:ycm_server_idle_suicide_seconds = + \ get( g:, 'ycm_server_idle_suicide_seconds', 43200 ) " On-demand loading. Let's use the autoload folder and not slow down vim's diff --git a/python/ycm/server/watchdog_plugin.py b/python/ycm/server/watchdog_plugin.py index 82be070f..23c48ae6 100644 --- a/python/ycm/server/watchdog_plugin.py +++ b/python/ycm/server/watchdog_plugin.py @@ -29,7 +29,7 @@ from threading import Thread, Lock # The idea here is to decorate every route handler automatically so that on # every request, we log when the request was made. Then a watchdog thread checks # every check_interval_seconds whether the server has been idle for a time -# greater that the passed-in idle_shutdown_seconds. If it has, we kill the +# greater that the passed-in idle_suicide_seconds. If it has, we kill the # server. # # We want to do this so that if something goes bonkers in Vim and the server @@ -40,13 +40,13 @@ class WatchdogPlugin( object ): def __init__( self, - idle_shutdown_seconds, + idle_suicide_seconds, check_interval_seconds = 60 * 10 ): self._check_interval_seconds = check_interval_seconds - self._idle_shutdown_seconds = idle_shutdown_seconds + self._idle_suicide_seconds = idle_suicide_seconds self._last_request_time = time.time() self._last_request_time_lock = Lock() - if idle_shutdown_seconds <= 0: + if idle_suicide_seconds <= 0: return self._watchdog_thread = Thread( target = self._WatchdogMain ) self._watchdog_thread.daemon = True @@ -66,7 +66,7 @@ class WatchdogPlugin( object ): def _WatchdogMain( self ): while True: time.sleep( self._check_interval_seconds ) - if time.time() - self._GetLastRequestTime() > self._idle_shutdown_seconds: + if time.time() - self._GetLastRequestTime() > self._idle_suicide_seconds: utils.TerminateProcess( os.getpid() ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index a80e1cbc..07d34d68 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -56,7 +56,7 @@ def Main(): parser.add_argument( '--log', type = str, default = 'info', help = 'log level, one of ' '[debug|info|warning|error|critical]' ) - parser.add_argument( '--idle_shutdown_seconds', type = int, default = 0, + parser.add_argument( '--idle_suicide_seconds', type = int, default = 0, help = 'num idle seconds before server shuts down') parser.add_argument( '--options_file', type = str, default = '', help = 'file with user options, in JSON format' ) @@ -85,7 +85,7 @@ def Main(): from ycm.server import handlers handlers.UpdateUserOptions( options ) SetUpSignalHandler() - handlers.app.install( WatchdogPlugin( args.idle_shutdown_seconds ) ) + handlers.app.install( WatchdogPlugin( args.idle_suicide_seconds ) ) waitress.serve( handlers.app, host = args.host, port = args.port, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 6967c807..bf9d4d99 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -70,8 +70,8 @@ class YouCompleteMe( object ): '--port={0}'.format( server_port ), '--options_file={0}'.format( options_file.name ), '--log={0}'.format( self._user_options[ 'server_log_level' ] ), - '--idle_shutdown_seconds={0}'.format( - self._user_options[ 'server_idle_shutdown_seconds' ] ) ] + '--idle_suicide_seconds={0}'.format( + self._user_options[ 'server_idle_suicide_seconds' ] ) ] BaseRequest.server_location = 'http://localhost:' + str( server_port ) From a0f85f0b6c500f9bd545fc2b4e18fb84f5d32fc9 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 24 Oct 2013 10:26:55 -0700 Subject: [PATCH 146/149] The ycm_temp dir is now accessible by all Fixes #606 --- python/ycm/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 0f36dca0..1e63f4f3 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -23,6 +23,7 @@ import sys import signal import functools import socket +import stat WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' @@ -46,9 +47,20 @@ def PathToTempDir(): tempdir = os.path.join( tempfile.gettempdir(), 'ycm_temp' ) if not os.path.exists( tempdir ): os.makedirs( tempdir ) + # Needed to support multiple users working on the same machine; + # see issue 606. + MakeFolderAccessibleToAll( tempdir ) return tempdir +def MakeFolderAccessibleToAll( path_to_folder ): + current_stat = os.stat( path_to_folder ) + # readable, writable and executable by everyone + flags = ( current_stat.st_mode | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH + | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP ) + os.chmod( path_to_folder, flags ) + + def GetUnusedLocalhostPort(): sock = socket.socket() # This tells the OS to give us any free port in the range [1024 - 65535] From cb939dd8e26b53ba135587d150cfa4982f71f53d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 24 Oct 2013 11:16:36 -0700 Subject: [PATCH 147/149] Adding a check for third_party libs present If the user forgot to checkout the submodules, then we bork during install.sh. --- install.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/install.sh b/install.sh index 3a5a2fe1..c771e4a5 100755 --- a/install.sh +++ b/install.sh @@ -106,6 +106,22 @@ function usage { exit 0 } +function check_third_party_libs { + libs_present=true + for folder in third_party/*; do + num_files_in_folder=$(find $folder -maxdepth 1 -mindepth 1 | wc -l) + if [[ $num_files_in_folder -eq 0 ]]; then + libs_present=false + fi + done + + if ! $libs_present; then + echo "Some folders in ./third_party are empty; you probably forgot to run:" + printf "\n\tgit submodule update --init --recursive\n\n" + exit 1 + fi +} + cmake_args="" omnisharp_completer=false for flag in $@; do @@ -130,6 +146,8 @@ if [[ $cmake_args == *-DUSE_SYSTEM_LIBCLANG=ON* ]] && \ usage fi +check_third_party_libs + if ! command_exists cmake; then echo "CMake is required to build YouCompleteMe." cmake_install From d35832da619ffcdc1ecd56476dc2d6f10bbc4e0e Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 24 Oct 2013 12:31:38 -0700 Subject: [PATCH 148/149] Updating README with ycmd info Lots of things changed on the ycmd branch! --- CONTRIBUTING.md | 14 ++++++-- README.md | 91 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 98e67062..c6bc2fb5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -44,14 +44,22 @@ Here are the things you should do when creating an issue: 1. **Write a step-by-step procedure that when performed repeatedly reproduces your issue.** If we can't reproduce the issue, then we can't fix it. It's that simple. -2. **Create a test case for your issue**. This is critical. Don't talk about how +2. Put the following options in your vimrc: + ``` + let g:ycm_server_use_vim_stdout = 1 + let g:ycm_server_log_level = 'debug' + ``` + Then start gvim/macvim (not console vim) from the console. As you use Vim, + you'll see the `ycmd` debug output stream in the console. Attach that to you + issue. +3. **Create a test case for your issue**. This is critical. Don't talk about how "when I have X in my file" or similar, _create a file with X in it_ and put the contents inside code blocks in your issue description. Try to make this test file _as small as possible_. Don't just paste a huge, 500 line source file you were editing and present that as a test. _Minimize_ the file so that the problem is reproduced with the smallest possible amount of test data. -3. **Include your OS and OS version.** -4. **Include the output of `vim --version`.** +4. **Include your OS and OS version.** +5. **Include the output of `vim --version`.** Creating good pull requests diff --git a/README.md b/README.md index 6c26afc8..8451d0ea 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,9 @@ local binary folder (for example `/usr/local/bin/mvim`) and then symlink it: Install YouCompleteMe with [Vundle][]. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM will -notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. It's recommended that you have the latest Xcode installed along with the latest Command Line Tools (that you install from within Xcode). @@ -136,8 +137,9 @@ from source][vim-build] (don't worry, it's easy). Install YouCompleteMe with [Vundle][]. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM will -notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. Install development tools and CMake: `sudo apt-get install build-essential cmake` @@ -184,8 +186,9 @@ that platform). See the _FAQ_ if you have any issues. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM will -notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. **Please follow the instructions carefully. Read EVERY WORD.** @@ -221,8 +224,8 @@ notify you to recompile it. You should then rerun the install process. binaries from llvm.org][clang-download] if at all possible. Make sure you download the correct archive file for your OS. -4. **Compile the `ycm_core` plugin plugin** (ha!) that YCM needs. This is the - C++ engine that YCM uses to get fast completions. +4. **Compile the `ycm_support_libs` libraries** that YCM needs. These libs + are the C++ engines that YCM uses to get fast completions. You will need to have `cmake` installed in order to generate the required makefiles. Linux users can install cmake with their package manager (`sudo @@ -261,7 +264,7 @@ notify you to recompile it. You should then rerun the install process. Now that makefiles have been generated, simply run: - make ycm_core + make ycm_support_libs For those who want to use the system version of libclang, you would pass `-DUSE_SYSTEM_LIBCLANG=ON` to cmake _instead of_ the @@ -323,6 +326,13 @@ YCM automatically detects which completion engine would be the best in any situation. On occasion, it queries several of them at once, merges the outputs and presents the results to you. +### Client-server architecture + +YCM has a client-server architecture; the Vim part of YCM is only a thin client +that talks to the `ycmd` HTTP+JSON server that has the vast majority of YCM +logic and functionality. The server is started and stopped automatically as you +start and stop Vim. + ### Completion string ranking The subsequence filter removes any completions that do not match the input, but @@ -498,6 +508,11 @@ yours truly. Commands -------- +### The `:YcmRestartServer` command + +If the `ycmd` completion server suddenly stops for some reason, you can restart +it with this command. + ### The `:YcmForceCompileAndDiagnostics` command Calling this command will force YCM to immediately recompile your file @@ -832,6 +847,64 @@ Default: `0` let g:ycm_seed_identifiers_with_syntax = 0 +### The `g:ycm_server_use_vim_stdout` option + +By default, the `ycmd` completion server writes logs to logfiles. When this +option is set to `1`, the server writes logs to Vim's stdout (so you'll see them +in the console). + +Default: `0` + + let g:ycm_server_use_vim_stdout = 0 + +### The `g:ycm_server_keep_logfiles` option + +When this option is set to `1`, the `ycmd` completion server will keep the +logfiles around after shutting down (they are deleted on shutdown by default). + +To see where the logfiles are, call `:YcmDebugInfo`. + +Default: `0` + + let g:ycm_server_keep_logfiles = 0 + +### The `g:ycm_server_log_level` option + +The logging level that the `ycmd` completion server uses. Valid values are the +following, from most verbose to least verbose: +- `debug` +- `info` +- `warning` +- `error` +- `critical` + +Note that `debug` is _very_ verbose. + +Default: `info` + + let g:ycm_server_log_level = 'info' + +### The `g:ycm_server_idle_suicide_seconds` option + +This option sets the number of seconds of `ycmd` server idleness (no requests +received) after which the server stops itself. NOTE: the YCM Vim client sends a +shutdown request to the server when Vim is shutting down. + +If your Vim crashes for instance, `ycmd` never gets the shutdown command and +becomes a zombie process. This option prevents such zombies from sticking around +forever. + +The default option is `43200` seconds which is 12 hours. The reason for the +interval being this long is to prevent the server from shutting down if you +leave your computer (and Vim) turned on during the night. + +The server "heartbeat" that checks whether this interval has passed occurs every +10 minutes. + +Default: `43200` + + let g:ycm_server_idle_suicide_seconds = 43200 + ### The `g:ycm_csharp_server_port` option The port number (on `localhost`) on which the OmniSharp server should be From 1aa3a12293b8b15c41c37ddfe6baf236e5e202d9 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 24 Oct 2013 12:41:56 -0700 Subject: [PATCH 149/149] Updating vimdoc from README --- doc/youcompleteme.txt | 172 +++++++++++++++++++++++++++++++----------- 1 file changed, 129 insertions(+), 43 deletions(-) diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index ad5a4af4..50cc69c1 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -1,4 +1,4 @@ -*youcompleteme* YouCompleteMe: a code-completion engine for Vim +*youcompleteme.txt* YouCompleteMe: a code-completion engine for Vim =============================================================================== Contents ~ @@ -10,20 +10,22 @@ Contents ~ 5. Full Installation Guide |youcompleteme-full-installation-guide| 6. User Guide |youcompleteme-user-guide| 1. General Usage |youcompleteme-general-usage| - 2. Completion string ranking |youcompleteme-completion-string-ranking| - 3. General Semantic Completion Engine Usage |youcompleteme-general-semantic-completion-engine-usage| - 4. C-family Semantic Completion Engine Usage |youcompleteme-c-family-semantic-completion-engine-usage| - 5. Python semantic completion |youcompleteme-python-semantic-completion| - 6. C# semantic completion |youcompleteme-c-semantic-completion| - 7. Semantic completion for other languages |youcompleteme-semantic-completion-for-other-languages| - 8. Writing New Semantic Completers |youcompleteme-writing-new-semantic-completers| - 9. Syntastic integration |youcompleteme-syntastic-integration| + 2. Client-server architecture |youcompleteme-client-server-architecture| + 3. Completion string ranking |youcompleteme-completion-string-ranking| + 4. General Semantic Completion Engine Usage |youcompleteme-general-semantic-completion-engine-usage| + 5. C-family Semantic Completion Engine Usage |youcompleteme-c-family-semantic-completion-engine-usage| + 6. Python semantic completion |youcompleteme-python-semantic-completion| + 7. C# semantic completion |youcompleteme-c-semantic-completion| + 8. Semantic completion for other languages |youcompleteme-semantic-completion-for-other-languages| + 9. Writing New Semantic Completers |youcompleteme-writing-new-semantic-completers| + 10. Syntastic integration |youcompleteme-syntastic-integration| 7. Commands |youcompleteme-commands| - 1. The |:YcmForceCompileAndDiagnostics| command - 2. The |:YcmDiags| command - 3. The |:YcmShowDetailedDiagnostic| command - 4. The |:YcmDebugInfo| command - 5. The |:YcmCompleter| command + 1. The |:YcmRestartServer| command + 2. The |:YcmForceCompileAndDiagnostics| command + 3. The |:YcmDiags| command + 4. The |:YcmShowDetailedDiagnostic| command + 5. The |:YcmDebugInfo| command + 6. The |:YcmCompleter| command 8. YcmCompleter subcommands |youcompleteme-ycmcompleter-subcommands| 1. The |GoToDeclaration| subcommand 2. The |GoToDefinition| subcommand @@ -45,23 +47,27 @@ Contents ~ 10. The |g:ycm_collect_identifiers_from_comments_and_strings| option 11. The |g:ycm_collect_identifiers_from_tags_files| option 12. The |g:ycm_seed_identifiers_with_syntax| option - 13. The |g:ycm_csharp_server_port| option - 14. The |g:ycm_auto_start_csharp_server| option - 15. The |g:ycm_auto_stop_csharp_server| option - 16. The |g:ycm_add_preview_to_completeopt| option - 17. The |g:ycm_autoclose_preview_window_after_completion| option - 18. The |g:ycm_autoclose_preview_window_after_insertion| option - 19. The |g:ycm_max_diagnostics_to_display| option - 20. The |g:ycm_key_list_select_completion| option - 21. The |g:ycm_key_list_previous_completion| option - 22. The |g:ycm_key_invoke_completion| option - 23. The |g:ycm_key_detailed_diagnostics| option - 24. The |g:ycm_global_ycm_extra_conf| option - 25. The |g:ycm_confirm_extra_conf| option - 26. The |g:ycm_extra_conf_globlist| option - 27. The |g:ycm_filepath_completion_use_working_dir| option - 28. The |g:ycm_semantic_triggers| option - 29. The |g:ycm_cache_omnifunc| option + 13. The |g:ycm_server_use_vim_stdout| option + 14. The |g:ycm_server_keep_logfiles| option + 15. The |g:ycm_server_log_level| option + 16. The |g:ycm_server_idle_suicide_seconds| option + 17. The |g:ycm_csharp_server_port| option + 18. The |g:ycm_auto_start_csharp_server| option + 19. The |g:ycm_auto_stop_csharp_server| option + 20. The |g:ycm_add_preview_to_completeopt| option + 21. The |g:ycm_autoclose_preview_window_after_completion| option + 22. The |g:ycm_autoclose_preview_window_after_insertion| option + 23. The |g:ycm_max_diagnostics_to_display| option + 24. The |g:ycm_key_list_select_completion| option + 25. The |g:ycm_key_list_previous_completion| option + 26. The |g:ycm_key_invoke_completion| option + 27. The |g:ycm_key_detailed_diagnostics| option + 28. The |g:ycm_global_ycm_extra_conf| option + 29. The |g:ycm_confirm_extra_conf| option + 30. The |g:ycm_extra_conf_globlist| option + 31. The |g:ycm_filepath_completion_use_working_dir| option + 32. The |g:ycm_semantic_triggers| option + 33. The |g:ycm_cache_omnifunc| option 10. FAQ |youcompleteme-faq| 1. I get a linker warning regarding |libpython| on Mac when compiling YCM 2. I get a weird window at the top of my file when I use the semantic engine |youcompleteme-i-get-weird-window-at-top-of-my-file-when-i-use-semantic-engine| @@ -182,8 +188,9 @@ local binary folder (for example '/usr/local/bin/mvim') and then symlink it: Install YouCompleteMe with Vundle [11]. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM -will notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. It's recommended that you have the latest Xcode installed along with the latest Command Line Tools (that you install from within Xcode). @@ -230,8 +237,9 @@ from source [14] (don't worry, it's easy). Install YouCompleteMe with Vundle [11]. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM -will notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. Install development tools and CMake: 'sudo apt-get install build-essential cmake' @@ -281,8 +289,9 @@ that platform). See the _FAQ_ if you have any issues. **Remember:** YCM is a plugin with a compiled component. If you **update** YCM -using Vundle and the ycm_core library API has changed (happens rarely), YCM -will notify you to recompile it. You should then rerun the install process. +using Vundle and the ycm_support_libs library APIs have changed (happens +rarely), YCM will notify you to recompile it. You should then rerun the install +process. **Please follow the instructions carefully. Read EVERY WORD.** @@ -319,8 +328,8 @@ will notify you to recompile it. You should then rerun the install process. official binaries from llvm.org [18] if at all possible. Make sure you download the correct archive file for your OS. -4. **Compile the 'ycm_core' plugin plugin** (ha!) that YCM needs. This is - the C++ engine that YCM uses to get fast completions. +4. **Compile the 'ycm_support_libs' libraries** that YCM needs. These libs + are the C++ engines that YCM uses to get fast completions. You will need to have 'cmake' installed in order to generate the required makefiles. Linux users can install cmake with their package manager @@ -359,7 +368,7 @@ will notify you to recompile it. You should then rerun the install process. < Now that makefiles have been generated, simply run: > - make ycm_core + make ycm_support_libs < For those who want to use the system version of libclang, you would pass '-DUSE_SYSTEM_LIBCLANG=ON' to cmake _instead of_ the @@ -427,6 +436,15 @@ YCM automatically detects which completion engine would be the best in any situation. On occasion, it queries several of them at once, merges the outputs and presents the results to you. +------------------------------------------------------------------------------- + *youcompleteme-client-server-architecture* +Client-server architecture ~ + +YCM has a client-server architecture; the Vim part of YCM is only a thin client +that talks to the 'ycmd' HTTP+JSON server that has the vast majority of YCM +logic and functionality. The server is started and stopped automatically as you +start and stop Vim. + ------------------------------------------------------------------------------- *youcompleteme-completion-string-ranking* Completion string ranking ~ @@ -624,6 +642,12 @@ yours truly. *youcompleteme-commands* Commands ~ +------------------------------------------------------------------------------- +The *:YcmRestartServer* command + +If the 'ycmd' completion server suddenly stops for some reason, you can restart +it with this command. + ------------------------------------------------------------------------------- The *:YcmForceCompileAndDiagnostics* command @@ -694,7 +718,7 @@ The *GoToDeclaration* subcommand Looks up the symbol under the cursor and jumps to its declaration. -Supported in filetypes: 'c, cpp, objc, objcpp, python' +Supported in filetypes: 'c, cpp, objc, objcpp, python, cs' ------------------------------------------------------------------------------- The *GoToDefinition* subcommand @@ -706,7 +730,7 @@ when the definition of the symbol is in the current translation unit. A translation unit consists of the file you are editing and all the files you are including with '#include' directives (directly or indirectly) in that file. -Supported in filetypes: 'c, cpp, objc, objcpp, python' +Supported in filetypes: 'c, cpp, objc, objcpp, python, cs' ------------------------------------------------------------------------------- The *GoToDefinitionElseDeclaration* subcommand @@ -715,7 +739,7 @@ Looks up the symbol under the cursor and jumps to its definition if possible; if the definition is not accessible from the current translation unit, jumps to the symbol's declaration. -Supported in filetypes: 'c, cpp, objc, objcpp, python' +Supported in filetypes: 'c, cpp, objc, objcpp, python, cs' ------------------------------------------------------------------------------- The *ClearCompilationFlagCache* subcommand @@ -982,6 +1006,64 @@ Default: '0' let g:ycm_seed_identifiers_with_syntax = 0 < ------------------------------------------------------------------------------- +The *g:ycm_server_use_vim_stdout* option + +By default, the 'ycmd' completion server writes logs to logfiles. When this +option is set to '1', the server writes logs to Vim's stdout (so you'll see +them in the console). + +Default: '0' +> + let g:ycm_server_use_vim_stdout = 0 +< +------------------------------------------------------------------------------- +The *g:ycm_server_keep_logfiles* option + +When this option is set to '1', the 'ycmd' completion server will keep the +logfiles around after shutting down (they are deleted on shutdown by default). + +To see where the logfiles are, call |:YcmDebugInfo|. + +Default: '0' +> + let g:ycm_server_keep_logfiles = 0 +< +------------------------------------------------------------------------------- +The *g:ycm_server_log_level* option + +The logging level that the 'ycmd' completion server uses. Valid values are the +following, from most verbose to least verbose: - 'debug' - 'info' - 'warning' - +'error' - 'critical' + +Note that 'debug' is _very_ verbose. + +Default: 'info' +> + let g:ycm_server_log_level = 'info' +< +------------------------------------------------------------------------------- +The *g:ycm_server_idle_suicide_seconds* option + +This option sets the number of seconds of 'ycmd' server idleness (no requests +received) after which the server stops itself. NOTE: the YCM Vim client sends a +shutdown request to the server when Vim is shutting down. + +If your Vim crashes for instance, 'ycmd' never gets the shutdown command and +becomes a zombie process. This option prevents such zombies from sticking +around forever. + +The default option is '43200' seconds which is 12 hours. The reason for the +interval being this long is to prevent the server from shutting down if you +leave your computer (and Vim) turned on during the night. + +The server "heartbeat" that checks whether this interval has passed occurs +every 10 minutes. + +Default: '43200' +> + let g:ycm_server_idle_suicide_seconds = 43200 +< +------------------------------------------------------------------------------- The *g:ycm_csharp_server_port* option The port number (on 'localhost') on which the OmniSharp server should be @@ -1575,6 +1657,8 @@ License ~ This software is licensed under the GPL v3 license [31]. © 2012 Strahinja Val Markovic . + Image: Bitdeli Badge [32] + =============================================================================== *youcompleteme-references* References ~ @@ -1610,5 +1694,7 @@ References ~ [29] https://groups.google.com/forum/?hl=en#!forum/ycm-users [30] https://github.com/Valloric/YouCompleteMe/issues?state=open [31] http://www.gnu.org/copyleft/gpl.html +[32] https://bitdeli.com/free +[33] https://d2weczhvl823v0.cloudfront.net/Valloric/youcompleteme/trend.png vim: ft=help