Auto merge of #1814 - kennykaye:error-warning-count, r=vheon

Expose functions to get error and warning counts

@Valloric I've implemented two functions which resolve #1011

The interface exposed to Vim is:

```viml
youcompleteme#GetErrorCount()
youcompleteme#GetWarningCount()
```

Below is a rough example of how these methods can be used to integrate with other plugins. In this case Lightline has been extended to show the error and warning count as red and yellow sections on the end of the status line, respectively.

![animation](https://cloud.githubusercontent.com/assets/3267574/11604073/078cb98e-9a9d-11e5-9fa3-c9b4bb6a6e82.gif)

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/1814)
<!-- Reviewable:end -->
This commit is contained in:
Homu 2015-12-06 06:37:39 +09:00
commit cc623d43ea
5 changed files with 105 additions and 4 deletions

View File

@ -1122,6 +1122,35 @@ provides a list of implementations to choose from.
Supported in filetypes: `cs` 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 Options
------- -------
@ -2349,6 +2378,7 @@ This software is licensed under the [GPL v3 license][gpl].
[gpl]: http://www.gnu.org/copyleft/gpl.html [gpl]: http://www.gnu.org/copyleft/gpl.html
[vim]: http://www.vim.org/ [vim]: http://www.vim.org/
[syntastic]: https://github.com/scrooloose/syntastic [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 [flags_example]: https://github.com/Valloric/ycmd/blob/master/cpp/ycm/.ycm_extra_conf.py
[compdb]: http://clang.llvm.org/docs/JSONCompilationDatabase.html [compdb]: http://clang.llvm.org/docs/JSONCompilationDatabase.html
[subsequence]: https://en.wikipedia.org/wiki/Subsequence [subsequence]: https://en.wikipedia.org/wiki/Subsequence

View File

@ -111,6 +111,16 @@ function! youcompleteme#DisableCursorMovedAutocommands()
endfunction endfunction
function! youcompleteme#GetErrorCount()
return pyeval( 'ycm_state.GetErrorCount()' )
endfunction
function! youcompleteme#GetWarningCount()
return pyeval( 'ycm_state.GetWarningCount()' )
endfunction
function! s:SetUpPython() abort function! s:SetUpPython() abort
python << EOF python << EOF
import sys import sys

View File

@ -55,7 +55,10 @@ Contents ~
14. The |ReloadSolution| subcommand 14. The |ReloadSolution| subcommand
15. The |GoToImplementation| subcommand 15. The |GoToImplementation| subcommand
16. The |GoToImplementationElseDeclaration| 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 1. The |g:ycm_min_num_of_chars_for_completion| option
2. The |g:ycm_min_num_identifier_candidate_chars| option 2. The |g:ycm_min_num_identifier_candidate_chars| option
3. The |g:ycm_auto_trigger| option 3. The |g:ycm_auto_trigger| option
@ -101,7 +104,7 @@ Contents ~
43. The |g:ycm_use_ultisnips_completer| option 43. The |g:ycm_use_ultisnips_completer| option
44. The |g:ycm_goto_buffer_command| option 44. The |g:ycm_goto_buffer_command| option
45. The |g:ycm_disable_for_files_larger_than_kb| 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| 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| 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 3. I get a linker warning regarding |libpython| on Mac when compiling YCM
@ -1228,9 +1231,9 @@ For example:
class C { class C {
void f(); void f();
}; };
void C::f() { void C::f() {
} }
< <
In the out-of-line definition of 'C::f', the semantic parent is the class 'C', 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' 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* *youcompleteme-options*
Options ~ Options ~
@ -2600,5 +2632,6 @@ References ~
[45] http://www.gnu.org/copyleft/gpl.html [45] http://www.gnu.org/copyleft/gpl.html
[46] https://bitdeli.com/free [46] https://bitdeli.com/free
[47] https://d2weczhvl823v0.cloudfront.net/Valloric/youcompleteme/trend.png [47] https://d2weczhvl823v0.cloudfront.net/Valloric/youcompleteme/trend.png
[48] https://github.com/itchyny/lightline.vim
vim: ft=help vim: ft=help

View File

@ -43,6 +43,15 @@ class DiagnosticInterface( object ):
if self._user_options[ 'echo_current_diagnostic' ]: if self._user_options[ 'echo_current_diagnostic' ]:
self._EchoDiagnosticForLine( line ) self._EchoDiagnosticForLine( line )
def GetErrorCount( self ):
return len( self._FilterDiagnostics( _DiagnosticIsError ) )
def GetWarningCount( self ):
return len( self._FilterDiagnostics( _DiagnosticIsWarning ) )
def UpdateWithNewDiagnostics( self, diags ): def UpdateWithNewDiagnostics( self, diags ):
normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags ] normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags ]
self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( self._buffer_number_to_line_to_diags = _ConvertDiagListToDict(
@ -79,6 +88,16 @@ class DiagnosticInterface( object ):
self._diag_message_needs_clearing = True 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 ): def _UpdateSquiggles( buffer_number_to_line_to_diags ):
vimsupport.ClearYcmSyntaxMatches() vimsupport.ClearYcmSyntaxMatches()
line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ] line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
@ -209,6 +228,10 @@ def _DiagnosticIsError( diag ):
return diag[ 'kind' ] == 'ERROR' return diag[ 'kind' ] == 'ERROR'
def _DiagnosticIsWarning( diag ):
return diag[ 'kind' ] == 'WARNING'
def _NormalizeDiagnostic( diag ): def _NormalizeDiagnostic( diag ):
def ClampToOne( value ): def ClampToOne( value ):
return value if value > 0 else 1 return value if value > 0 else 1

View File

@ -451,6 +451,11 @@ class YouCompleteMe( object ):
return None return None
return completion[ "extra_data" ][ "required_namespace_import" ] 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 ): def DiagnosticsForCurrentFileReady( self ):
return bool( self._latest_file_parse_request and return bool( self._latest_file_parse_request and