From dafc36ba37f6f1699f6c3786e0c193687a0650b8 Mon Sep 17 00:00:00 2001 From: Kenny Kaye Date: Fri, 4 Dec 2015 14:45:11 -0800 Subject: [PATCH 1/5] Expose functions to get error and warning counts --- autoload/youcompleteme.vim | 10 ++++++++++ python/ycm/diagnostic_interface.py | 29 +++++++++++++++++++++++++++++ python/ycm/youcompleteme.py | 5 +++++ 3 files changed, 44 insertions(+) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 0714cdae..586e9584 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -870,6 +870,16 @@ function! s:ShowDiagnostics() endfunction +function! g:YcmGetErrorCount() + return pyeval( 'ycm_state.GetErrorCount()' ) +endfunction + + +function! g:YcmGetWarningCount() + return pyeval( 'ycm_state.GetWarningCount()' ) +endfunction + + " This is basic vim plugin boilerplate let &cpo = s:save_cpo unlet s:save_cpo diff --git a/python/ycm/diagnostic_interface.py b/python/ycm/diagnostic_interface.py index 52afae28..1082a14f 100644 --- a/python/ycm/diagnostic_interface.py +++ b/python/ycm/diagnostic_interface.py @@ -43,6 +43,31 @@ class DiagnosticInterface( object ): if self._user_options[ 'echo_current_diagnostic' ]: self._EchoDiagnosticForLine( line ) + + def GetErrorCount( self ): + errors = 0 + line_to_diags = self._buffer_number_to_line_to_diags[ + vim.current.buffer.number ] + + for diags in line_to_diags.itervalues(): + for diag in diags: + if _DiagnosticIsError( diag ): + errors += 1 + return errors + + + def GetWarningCount( self ): + warnings = 0 + line_to_diags = self._buffer_number_to_line_to_diags[ + vim.current.buffer.number ] + + for diags in line_to_diags.itervalues(): + for diag in diags: + if _DiagnosticIsWarning( diag ): + warnings += 1 + return warnings + + def UpdateWithNewDiagnostics( self, diags ): normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags ] self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( @@ -209,6 +234,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 From 163238992cc5f2656a135c35e63ca4c11325e803 Mon Sep 17 00:00:00 2001 From: Kenny Kaye Date: Fri, 4 Dec 2015 15:59:18 -0800 Subject: [PATCH 2/5] Document YcmGetErrorCount and YcmGetWarningCount functions --- README.md | 24 +++++++++++++++++++++++ doc/youcompleteme.txt | 31 ++++++++++++++++++++++++++++-- python/ycm/diagnostic_interface.py | 30 ++++++++++++----------------- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cdaa37aa..4b2f6c39 100644 --- a/README.md +++ b/README.md @@ -1122,6 +1122,30 @@ provides a list of implementations to choose from. Supported in filetypes: `cs` +Functions +-------- + +### The `YcmGetErrorCount` function + +Get the number of YCM Diagnostic errors. If no errors are present, this function +returns 0. + +For example: +```viml + call YcmGetErrorCount() +``` + +### The `YcmGetWarningCount` function + +Get the number of YCM Diagnostic warnings. If no warnings are present, this +function returns 0. + +For example: +```viml + call YcmGetWarningCount() +``` + + Options ------- diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 10dbce73..c7b4228d 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 |YcmGetErrorCount| function + 2. The |YcmGetWarningCount| 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 @@ -1346,6 +1349,30 @@ provides a list of implementations to choose from. Supported in filetypes: 'cs' +=============================================================================== + *youcompleteme-functions* +Functions ~ + +------------------------------------------------------------------------------- +The *YcmGetErrorCount* function + +Get the number of YCM Diagnostic errors. If no errors are present, this function +returns 0. + +For example: +> + call YcmGetErrorCount() +< +------------------------------------------------------------------------------- +The *YcmGetWarningCount* function + +Get the number of YCM Diagnostic warnings. If no warnings are present, this +function returns 0. + +For example: +> + call YcmGetWarningCount() +< =============================================================================== *youcompleteme-options* Options ~ diff --git a/python/ycm/diagnostic_interface.py b/python/ycm/diagnostic_interface.py index 1082a14f..317c6095 100644 --- a/python/ycm/diagnostic_interface.py +++ b/python/ycm/diagnostic_interface.py @@ -45,27 +45,11 @@ class DiagnosticInterface( object ): def GetErrorCount( self ): - errors = 0 - line_to_diags = self._buffer_number_to_line_to_diags[ - vim.current.buffer.number ] - - for diags in line_to_diags.itervalues(): - for diag in diags: - if _DiagnosticIsError( diag ): - errors += 1 - return errors + return len( self._FilterDiagnostics( _DiagnosticIsError ) ) def GetWarningCount( self ): - warnings = 0 - line_to_diags = self._buffer_number_to_line_to_diags[ - vim.current.buffer.number ] - - for diags in line_to_diags.itervalues(): - for diag in diags: - if _DiagnosticIsWarning( diag ): - warnings += 1 - return warnings + return len( self._FilterDiagnostics( _DiagnosticIsWarning ) ) def UpdateWithNewDiagnostics( self, diags ): @@ -104,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 ] From bb8490e6c7aed511e605aa3679394cf262dfdc9f Mon Sep 17 00:00:00 2001 From: Kenny Kaye Date: Sat, 5 Dec 2015 09:24:15 -0800 Subject: [PATCH 3/5] Properly namespace vim warning/error getters --- README.md | 8 ++++---- autoload/youcompleteme.vim | 20 ++++++++++---------- doc/youcompleteme.txt | 13 +++++++------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4b2f6c39..ca4d0b78 100644 --- a/README.md +++ b/README.md @@ -1125,24 +1125,24 @@ Supported in filetypes: `cs` Functions -------- -### The `YcmGetErrorCount` function +### The `youcompleteme#GetErrorCount` function Get the number of YCM Diagnostic errors. If no errors are present, this function returns 0. For example: ```viml - call YcmGetErrorCount() + call youcompleteme#GetErrorCount() ``` -### The `YcmGetWarningCount` function +### The `youcompleteme#GetWarningCount` function Get the number of YCM Diagnostic warnings. If no warnings are present, this function returns 0. For example: ```viml - call YcmGetWarningCount() + call youcompleteme#GetWarningCount() ``` diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 586e9584..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 @@ -870,16 +880,6 @@ function! s:ShowDiagnostics() endfunction -function! g:YcmGetErrorCount() - return pyeval( 'ycm_state.GetErrorCount()' ) -endfunction - - -function! g:YcmGetWarningCount() - return pyeval( 'ycm_state.GetWarningCount()' ) -endfunction - - " This is basic vim plugin boilerplate let &cpo = s:save_cpo unlet s:save_cpo diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index c7b4228d..445ccb00 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -30,6 +30,7 @@ Contents ~ 8. Diagnostic display |youcompleteme-diagnostic-display| 1. C# Diagnostic Support |youcompleteme-c-diagnostic-support| 2. Diagnostic highlighting groups |youcompleteme-diagnostic-highlighting-groups| + 6. Commands |youcompleteme-commands| 1. The |:YcmRestartServer| command 2. The |:YcmForceCompileAndDiagnostics| command @@ -1231,9 +1232,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', @@ -1354,24 +1355,24 @@ Supported in filetypes: 'cs' Functions ~ ------------------------------------------------------------------------------- -The *YcmGetErrorCount* function +The *youcompleteme#GetErrorCount* function Get the number of YCM Diagnostic errors. If no errors are present, this function returns 0. For example: > - call YcmGetErrorCount() + call youcompleteme#GetErrorCount() < ------------------------------------------------------------------------------- -The *YcmGetWarningCount* function +The *youcompleteme#GetWarningCount* function Get the number of YCM Diagnostic warnings. If no warnings are present, this function returns 0. For example: > - call YcmGetWarningCount() + call youcompleteme#GetWarningCount() < =============================================================================== *youcompleteme-options* From 908fd68548b6bbe79499e6a2ea9e102aeb45f8b1 Mon Sep 17 00:00:00 2001 From: Kenny Kaye Date: Sat, 5 Dec 2015 10:34:05 -0800 Subject: [PATCH 4/5] Update help doc formatting --- doc/youcompleteme.txt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 445ccb00..1721140a 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -30,7 +30,6 @@ Contents ~ 8. Diagnostic display |youcompleteme-diagnostic-display| 1. C# Diagnostic Support |youcompleteme-c-diagnostic-support| 2. Diagnostic highlighting groups |youcompleteme-diagnostic-highlighting-groups| - 6. Commands |youcompleteme-commands| 1. The |:YcmRestartServer| command 2. The |:YcmForceCompileAndDiagnostics| command @@ -57,8 +56,8 @@ Contents ~ 15. The |GoToImplementation| subcommand 16. The |GoToImplementationElseDeclaration| subcommand 8. Functions |youcompleteme-functions| - 1. The |YcmGetErrorCount| function - 2. The |YcmGetWarningCount| function + 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 @@ -1357,8 +1356,8 @@ Functions ~ ------------------------------------------------------------------------------- The *youcompleteme#GetErrorCount* function -Get the number of YCM Diagnostic errors. If no errors are present, this function -returns 0. +Get the number of YCM Diagnostic errors. If no errors are present, this +function returns 0. For example: > From 0c55527a38f3ae2111a0071ec85385a15b23a3c8 Mon Sep 17 00:00:00 2001 From: Kenny Kaye Date: Sat, 5 Dec 2015 11:06:29 -0800 Subject: [PATCH 5/5] Included usage example for error/warning getters --- README.md | 6 ++++++ doc/youcompleteme.txt | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index ca4d0b78..139f6f8d 100644 --- a/README.md +++ b/README.md @@ -1135,6 +1135,11 @@ 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][] 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 @@ -2373,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/doc/youcompleteme.txt b/doc/youcompleteme.txt index 1721140a..6c0ce1f1 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -1363,6 +1363,11 @@ 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 @@ -2627,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