diff --git a/README.md b/README.md index cdaa37aa..139f6f8d 100644 --- a/README.md +++ b/README.md @@ -1122,6 +1122,35 @@ provides a list of implementations to choose from. Supported in filetypes: `cs` +Functions +-------- + +### The `youcompleteme#GetErrorCount` function + +Get the number of YCM Diagnostic errors. If no errors are present, this function +returns 0. + +For example: +```viml + call youcompleteme#GetErrorCount() +``` + +Both this function and `youcompleteme#GetWarningCount` can be useful when +integrating YCM with other Vim plugins. For example, a [lightline][] user could +add a diagnostics section to their statusline which would display the number of +errors and warnings. + +### The `youcompleteme#GetWarningCount` function + +Get the number of YCM Diagnostic warnings. If no warnings are present, this +function returns 0. + +For example: +```viml + call youcompleteme#GetWarningCount() +``` + + Options ------- @@ -2349,6 +2378,7 @@ This software is licensed under the [GPL v3 license][gpl]. [gpl]: http://www.gnu.org/copyleft/gpl.html [vim]: http://www.vim.org/ [syntastic]: https://github.com/scrooloose/syntastic +[lightline]: https://github.com/itchyny/lightline.vim [flags_example]: https://github.com/Valloric/ycmd/blob/master/cpp/ycm/.ycm_extra_conf.py [compdb]: http://clang.llvm.org/docs/JSONCompilationDatabase.html [subsequence]: https://en.wikipedia.org/wiki/Subsequence diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 0714cdae..9158331f 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -111,6 +111,16 @@ function! youcompleteme#DisableCursorMovedAutocommands() endfunction +function! youcompleteme#GetErrorCount() + return pyeval( 'ycm_state.GetErrorCount()' ) +endfunction + + +function! youcompleteme#GetWarningCount() + return pyeval( 'ycm_state.GetWarningCount()' ) +endfunction + + function! s:SetUpPython() abort python << EOF import sys diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 10dbce73..6c0ce1f1 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -55,7 +55,10 @@ Contents ~ 14. The |ReloadSolution| subcommand 15. The |GoToImplementation| subcommand 16. The |GoToImplementationElseDeclaration| subcommand - 8. Options |youcompleteme-options| + 8. Functions |youcompleteme-functions| + 1. The |youcompleteme#GetErrorCount| function + 2. The |youcompleteme#GetWarningCount| function + 9. Options |youcompleteme-options| 1. The |g:ycm_min_num_of_chars_for_completion| option 2. The |g:ycm_min_num_identifier_candidate_chars| option 3. The |g:ycm_auto_trigger| option @@ -101,7 +104,7 @@ Contents ~ 43. The |g:ycm_use_ultisnips_completer| option 44. The |g:ycm_goto_buffer_command| option 45. The |g:ycm_disable_for_files_larger_than_kb| option - 9. FAQ |youcompleteme-faq| + 10. FAQ |youcompleteme-faq| 1. I used to be able to 'import vim' in '.ycm_extra_conf.py', but now can't |import-vim| 2. On very rare occasions Vim crashes when I tab through the completion menu |youcompleteme-on-very-rare-occasions-vim-crashes-when-i-tab-through-completion-menu| 3. I get a linker warning regarding |libpython| on Mac when compiling YCM @@ -1228,9 +1231,9 @@ For example: class C { void f(); }; - + void C::f() { - + } < In the out-of-line definition of 'C::f', the semantic parent is the class 'C', @@ -1346,6 +1349,35 @@ provides a list of implementations to choose from. Supported in filetypes: 'cs' +=============================================================================== + *youcompleteme-functions* +Functions ~ + +------------------------------------------------------------------------------- +The *youcompleteme#GetErrorCount* function + +Get the number of YCM Diagnostic errors. If no errors are present, this +function returns 0. + +For example: +> + call youcompleteme#GetErrorCount() +< +Both this function and |youcompleteme#GetWarningCount| can be useful when +integrating YCM with other Vim plugins. For example, a lightline [48] user +could add a diagnostics section to their statusline which would display the +number of errors and warnings. + +------------------------------------------------------------------------------- +The *youcompleteme#GetWarningCount* function + +Get the number of YCM Diagnostic warnings. If no warnings are present, this +function returns 0. + +For example: +> + call youcompleteme#GetWarningCount() +< =============================================================================== *youcompleteme-options* Options ~ @@ -2600,5 +2632,6 @@ References ~ [45] http://www.gnu.org/copyleft/gpl.html [46] https://bitdeli.com/free [47] https://d2weczhvl823v0.cloudfront.net/Valloric/youcompleteme/trend.png +[48] https://github.com/itchyny/lightline.vim vim: ft=help diff --git a/python/ycm/diagnostic_interface.py b/python/ycm/diagnostic_interface.py index 52afae28..317c6095 100644 --- a/python/ycm/diagnostic_interface.py +++ b/python/ycm/diagnostic_interface.py @@ -43,6 +43,15 @@ class DiagnosticInterface( object ): if self._user_options[ 'echo_current_diagnostic' ]: self._EchoDiagnosticForLine( line ) + + def GetErrorCount( self ): + return len( self._FilterDiagnostics( _DiagnosticIsError ) ) + + + def GetWarningCount( self ): + return len( self._FilterDiagnostics( _DiagnosticIsWarning ) ) + + def UpdateWithNewDiagnostics( self, diags ): normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags ] self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( @@ -79,6 +88,16 @@ class DiagnosticInterface( object ): self._diag_message_needs_clearing = True + def _FilterDiagnostics( self, predicate ): + matched_diags = [] + line_to_diags = self._buffer_number_to_line_to_diags[ + vim.current.buffer.number ] + + for diags in line_to_diags.itervalues(): + matched_diags.extend( filter( predicate, diags ) ) + return matched_diags + + def _UpdateSquiggles( buffer_number_to_line_to_diags ): vimsupport.ClearYcmSyntaxMatches() line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ] @@ -209,6 +228,10 @@ def _DiagnosticIsError( diag ): return diag[ 'kind' ] == 'ERROR' +def _DiagnosticIsWarning( diag ): + return diag[ 'kind' ] == 'WARNING' + + def _NormalizeDiagnostic( diag ): def ClampToOne( value ): return value if value > 0 else 1 diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 77b81a0a..78544a8c 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -451,6 +451,11 @@ class YouCompleteMe( object ): return None return completion[ "extra_data" ][ "required_namespace_import" ] + def GetErrorCount( self ): + return self._diag_interface.GetErrorCount() + + def GetWarningCount( self ): + return self._diag_interface.GetWarningCount() def DiagnosticsForCurrentFileReady( self ): return bool( self._latest_file_parse_request and