diff --git a/README.md b/README.md index 63279a2b..8418cd39 100644 --- a/README.md +++ b/README.md @@ -532,6 +532,16 @@ Default: `0` let g:ycm_complete_in_comments_and_strings = 0 +### The `g:ycm_collect_identifiers_from_comments_and_strings` option + +When this option is set to `1`, YCM's identifier completer will also collect +identifiers from strings and comments. Otherwise, the text in comments and +strings will be ignored. + +Default: `0` + + let g:ycm_collect_identifiers_from_comments_and_strings = 0 + ### The `g:ycm_add_preview_to_completeopt` option When this option is set to `1`, YCM will add the `preview` string to Vim's diff --git a/cpp/ycm/IdentifierCompleter.cpp b/cpp/ycm/IdentifierCompleter.cpp index 8f3b2c21..05683d8c 100644 --- a/cpp/ycm/IdentifierCompleter.cpp +++ b/cpp/ycm/IdentifierCompleter.cpp @@ -137,20 +137,26 @@ void IdentifierCompleter::AddCandidatesToDatabase( void IdentifierCompleter::AddCandidatesToDatabaseFromBuffer( const std::string &buffer_contents, const std::string &filetype, - const std::string &filepath ) { + const std::string &filepath, + bool collect_from_comments_and_strings) { ClearCandidatesStoredForFile( filetype, filepath ); - AddCandidatesToDatabase( - ExtractIdentifiersFromText( RemoveIdentifierFreeText( buffer_contents ) ), - filetype, - filepath ); + std::string new_contents = + collect_from_comments_and_strings ? + buffer_contents : + RemoveIdentifierFreeText( buffer_contents ); + + AddCandidatesToDatabase( ExtractIdentifiersFromText( new_contents ), + filetype, + filepath ); } void IdentifierCompleter::AddCandidatesToDatabaseFromBufferAsync( std::string buffer_contents, std::string filetype, - std::string filepath ) { + 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; @@ -160,7 +166,8 @@ void IdentifierCompleter::AddCandidatesToDatabaseFromBufferAsync( boost::ref( *this ), boost::move( buffer_contents ), boost::move( filetype ), - boost::move( filepath ) ); + boost::move( filepath ), + collect_from_comments_and_strings ); buffer_identifiers_task_stack_.Push( make_shared< packaged_task< void > >( boost::move( functor ) ) ); diff --git a/cpp/ycm/IdentifierCompleter.h b/cpp/ycm/IdentifierCompleter.h index 1f635f54..ad7787dd 100644 --- a/cpp/ycm/IdentifierCompleter.h +++ b/cpp/ycm/IdentifierCompleter.h @@ -56,9 +56,11 @@ public: const std::string &filetype, const std::string &filepath ); - void AddCandidatesToDatabaseFromBuffer( const std::string &buffer_contents, - const std::string &filetype, - const std::string &filepath ); + void AddCandidatesToDatabaseFromBuffer( + 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 @@ -66,7 +68,8 @@ public: void AddCandidatesToDatabaseFromBufferAsync( std::string buffer_contents, std::string filetype, - std::string filepath ); + std::string filepath, + bool collect_from_comments_and_strings ); // Only provided for tests! std::vector< std::string > CandidatesForQuery( diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp index 51e64825..4ee8bdd8 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 1; + return 2; } diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 98a40907..225f6f7f 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -77,6 +77,9 @@ let g:ycm_add_preview_to_completeopt = let g:ycm_complete_in_comments_and_strings = \ get( g:, 'ycm_complete_in_comments_and_strings', 0 ) +let g:ycm_collect_identifiers_from_comments_and_strings = + \ get( g:, 'ycm_collect_identifiers_from_comments_and_strings', 0 ) + let g:ycm_autoclose_preview_window_after_completion = \ get( g:, 'ycm_autoclose_preview_window_after_completion', 0 ) diff --git a/python/completers/all/identifier_completer.py b/python/completers/all/identifier_completer.py index 2c3abeac..cd181fda 100644 --- a/python/completers/all/identifier_completer.py +++ b/python/completers/all/identifier_completer.py @@ -87,14 +87,18 @@ class IdentifierCompleter( Completer ): def AddBufferIdentifiers( self ): filetype = vim.eval( "&filetype" ) filepath = vim.eval( "expand('%:p')" ) + collect_from_comments_and_strings = bool( int( vimsupport.GetVariableValue( + "g:ycm_collect_identifiers_from_comments_and_strings" ) ) ) if not filetype or not filepath: return text = "\n".join( vim.current.buffer ) - self.completer.AddCandidatesToDatabaseFromBufferAsync( text, - filetype, - filepath ) + self.completer.AddCandidatesToDatabaseFromBufferAsync( + text, + filetype, + filepath, + collect_from_comments_and_strings ) def OnFileReadyToParse( self ): diff --git a/python/ycm.py b/python/ycm.py index 09219049..c47cf754 100644 --- a/python/ycm.py +++ b/python/ycm.py @@ -238,7 +238,7 @@ def CurrentIdentifierFinished(): return line[ : current_column ].isspace() -COMPATIBLE_WITH_CORE_VERSION = 1 +COMPATIBLE_WITH_CORE_VERSION = 2 def CompatibleWithYcmCore(): try: