2018-05-14 08:18:18 -04:00
|
|
|
# Copyright (C) 2013-2018 YouCompleteMe contributors
|
2014-01-04 17:28:27 -05:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2017-03-09 09:57:27 -05:00
|
|
|
# Not installing aliases from python-future; it's unreliable and slow.
|
2016-02-27 19:12:24 -05:00
|
|
|
from builtins import * # noqa
|
|
|
|
|
|
|
|
from future.utils import itervalues, iteritems
|
2018-02-13 17:21:12 -05:00
|
|
|
from collections import defaultdict
|
2014-01-04 17:28:27 -05:00
|
|
|
from ycm import vimsupport
|
2016-10-23 10:29:54 -04:00
|
|
|
from ycm.diagnostic_filter import DiagnosticFilter, CompileLevel
|
2014-01-04 17:28:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DiagnosticInterface( object ):
|
2017-06-05 14:14:51 -04:00
|
|
|
def __init__( self, bufnr, user_options ):
|
|
|
|
self._bufnr = bufnr
|
2014-01-08 21:43:17 -05:00
|
|
|
self._user_options = user_options
|
2017-04-30 17:17:20 -04:00
|
|
|
self._diagnostics = []
|
2016-10-22 18:37:13 -04:00
|
|
|
self._diag_filter = DiagnosticFilter.CreateFromOptions( user_options )
|
2014-01-04 19:45:34 -05:00
|
|
|
# Line and column numbers are 1-based
|
2017-06-05 14:14:51 -04:00
|
|
|
self._line_to_diags = defaultdict( list )
|
2017-08-18 17:27:15 -04:00
|
|
|
self._previous_diag_line_number = -1
|
2014-01-10 15:18:24 -05:00
|
|
|
self._diag_message_needs_clearing = False
|
2014-01-04 19:45:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnCursorMoved( self ):
|
2017-08-18 17:27:15 -04:00
|
|
|
if self._user_options[ 'echo_current_diagnostic' ]:
|
|
|
|
line, _ = vimsupport.CurrentLineAndColumn()
|
|
|
|
line += 1 # Convert to 1-based
|
|
|
|
if line != self._previous_diag_line_number:
|
2014-01-08 21:43:17 -05:00
|
|
|
self._EchoDiagnosticForLine( line )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
def GetErrorCount( self ):
|
2017-06-05 14:14:51 -04:00
|
|
|
return self._DiagnosticsCount( _DiagnosticIsError )
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
def GetWarningCount( self ):
|
2017-06-05 14:14:51 -04:00
|
|
|
return self._DiagnosticsCount( _DiagnosticIsWarning )
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
|
2017-04-30 17:17:20 -04:00
|
|
|
def PopulateLocationList( self ):
|
|
|
|
# Do nothing if loc list is already populated by diag_interface
|
|
|
|
if not self._user_options[ 'always_populate_location_list' ]:
|
2018-05-17 08:52:40 -04:00
|
|
|
self._UpdateLocationLists()
|
2017-04-30 17:17:20 -04:00
|
|
|
return bool( self._diagnostics )
|
2016-01-11 14:16:49 -05:00
|
|
|
|
|
|
|
|
2014-01-04 17:28:27 -05:00
|
|
|
def UpdateWithNewDiagnostics( self, diags ):
|
2017-04-30 17:17:20 -04:00
|
|
|
self._diagnostics = [ _NormalizeDiagnostic( x ) for x in
|
|
|
|
self._ApplyDiagnosticFilter( diags ) ]
|
2017-06-05 14:14:51 -04:00
|
|
|
self._ConvertDiagListToDict()
|
2014-01-08 21:43:17 -05:00
|
|
|
|
2017-08-18 17:27:15 -04:00
|
|
|
if self._user_options[ 'echo_current_diagnostic' ]:
|
|
|
|
self._EchoDiagnostic()
|
|
|
|
|
2014-01-08 22:43:21 -05:00
|
|
|
if self._user_options[ 'enable_diagnostic_signs' ]:
|
2017-06-05 14:14:51 -04:00
|
|
|
self._UpdateSigns()
|
2014-01-08 21:43:17 -05:00
|
|
|
|
2018-02-15 15:38:58 -05:00
|
|
|
self.UpdateMatches()
|
2014-01-04 19:45:34 -05:00
|
|
|
|
2014-01-08 22:43:21 -05:00
|
|
|
if self._user_options[ 'always_populate_location_list' ]:
|
2018-05-17 08:52:40 -04:00
|
|
|
self._UpdateLocationLists()
|
2014-01-08 22:09:40 -05:00
|
|
|
|
2016-10-22 18:37:13 -04:00
|
|
|
|
2017-06-05 14:14:51 -04:00
|
|
|
def _ApplyDiagnosticFilter( self, diags ):
|
|
|
|
filetypes = vimsupport.GetBufferFiletypes( self._bufnr )
|
2016-10-22 18:37:13 -04:00
|
|
|
diag_filter = self._diag_filter.SubsetForTypes( filetypes )
|
2017-06-05 14:14:51 -04:00
|
|
|
return filter( diag_filter.IsAllowed, diags )
|
2016-10-22 18:37:13 -04:00
|
|
|
|
|
|
|
|
2017-08-18 17:27:15 -04:00
|
|
|
def _EchoDiagnostic( self ):
|
|
|
|
line, _ = vimsupport.CurrentLineAndColumn()
|
|
|
|
line += 1 # Convert to 1-based
|
|
|
|
self._EchoDiagnosticForLine( line )
|
|
|
|
|
|
|
|
|
2014-01-04 19:45:34 -05:00
|
|
|
def _EchoDiagnosticForLine( self, line_num ):
|
2017-08-18 17:27:15 -04:00
|
|
|
self._previous_diag_line_number = line_num
|
|
|
|
|
2017-06-05 14:14:51 -04:00
|
|
|
diags = self._line_to_diags[ line_num ]
|
2014-01-04 19:45:34 -05:00
|
|
|
if not diags:
|
2014-01-10 15:18:24 -05:00
|
|
|
if self._diag_message_needs_clearing:
|
|
|
|
# Clear any previous diag echo
|
2016-08-28 02:34:09 -04:00
|
|
|
vimsupport.PostVimMessage( '', warning = False )
|
2014-01-10 15:18:24 -05:00
|
|
|
self._diag_message_needs_clearing = False
|
2014-01-04 19:45:34 -05:00
|
|
|
return
|
2015-08-05 17:09:07 -04:00
|
|
|
|
2017-03-28 15:37:58 -04:00
|
|
|
first_diag = diags[ 0 ]
|
|
|
|
text = first_diag[ 'text' ]
|
|
|
|
if first_diag.get( 'fixit_available', False ):
|
2015-08-05 17:09:07 -04:00
|
|
|
text += ' (FixIt)'
|
|
|
|
|
2016-08-28 02:34:09 -04:00
|
|
|
vimsupport.PostVimMessage( text, warning = False, truncate = True )
|
2014-01-10 15:18:24 -05:00
|
|
|
self._diag_message_needs_clearing = True
|
2014-01-04 19:45:34 -05:00
|
|
|
|
|
|
|
|
2017-06-05 14:14:51 -04:00
|
|
|
def _DiagnosticsCount( self, predicate ):
|
|
|
|
count = 0
|
|
|
|
for diags in itervalues( self._line_to_diags ):
|
|
|
|
count += sum( 1 for d in diags if predicate( d ) )
|
|
|
|
return count
|
2015-12-04 18:59:18 -05:00
|
|
|
|
|
|
|
|
2018-05-17 08:52:40 -04:00
|
|
|
def _UpdateLocationLists( self ):
|
|
|
|
vimsupport.SetLocationListsForBuffer(
|
2017-12-21 18:23:21 -05:00
|
|
|
self._bufnr,
|
2017-04-30 17:17:20 -04:00
|
|
|
vimsupport.ConvertDiagnosticsToQfList( self._diagnostics ) )
|
|
|
|
|
|
|
|
|
2018-02-15 15:38:58 -05:00
|
|
|
def UpdateMatches( self ):
|
|
|
|
if not self._user_options[ 'enable_diagnostic_highlighting' ]:
|
|
|
|
return
|
2017-06-05 14:14:51 -04:00
|
|
|
|
2018-05-17 08:52:40 -04:00
|
|
|
with vimsupport.CurrentWindow():
|
|
|
|
for window in vimsupport.GetWindowsForBufferNumber( self._bufnr ):
|
|
|
|
vimsupport.SwitchWindow( window )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
2018-05-17 08:52:40 -04:00
|
|
|
matches_to_remove = vimsupport.GetDiagnosticMatchesInCurrentWindow()
|
|
|
|
|
|
|
|
for diags in itervalues( self._line_to_diags ):
|
|
|
|
# Insert squiggles in reverse order so that errors overlap warnings.
|
|
|
|
for diag in reversed( diags ):
|
|
|
|
group = ( 'YcmErrorSection' if _DiagnosticIsError( diag ) else
|
|
|
|
'YcmWarningSection' )
|
|
|
|
|
|
|
|
for pattern in _ConvertDiagnosticToMatchPatterns( diag ):
|
|
|
|
# The id doesn't matter for matches that we may add.
|
|
|
|
match = vimsupport.DiagnosticMatch( 0, group, pattern )
|
|
|
|
try:
|
|
|
|
matches_to_remove.remove( match )
|
|
|
|
except ValueError:
|
|
|
|
vimsupport.AddDiagnosticMatch( match )
|
|
|
|
|
|
|
|
for match in matches_to_remove:
|
|
|
|
vimsupport.RemoveDiagnosticMatch( match )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _UpdateSigns( self ):
|
2018-02-13 17:21:12 -05:00
|
|
|
signs_to_unplace = vimsupport.GetSignsInBuffer( self._bufnr )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
|
|
|
for line, diags in iteritems( self._line_to_diags ):
|
2017-08-18 17:27:15 -04:00
|
|
|
if not diags:
|
|
|
|
continue
|
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
# We always go for the first diagnostic on the line because diagnostics
|
|
|
|
# are sorted by errors in priority and Vim can only display one sign by
|
|
|
|
# line.
|
|
|
|
name = 'YcmError' if _DiagnosticIsError( diags[ 0 ] ) else 'YcmWarning'
|
2018-05-14 08:18:18 -04:00
|
|
|
sign = vimsupport.CreateSign( line, name, self._bufnr )
|
2018-02-13 17:21:12 -05:00
|
|
|
|
2017-06-05 14:14:51 -04:00
|
|
|
try:
|
2018-02-13 17:21:12 -05:00
|
|
|
signs_to_unplace.remove( sign )
|
2017-06-05 14:14:51 -04:00
|
|
|
except ValueError:
|
2018-02-13 17:21:12 -05:00
|
|
|
vimsupport.PlaceSign( sign )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
for sign in signs_to_unplace:
|
|
|
|
vimsupport.UnplaceSign( sign )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _ConvertDiagListToDict( self ):
|
|
|
|
self._line_to_diags = defaultdict( list )
|
|
|
|
for diag in self._diagnostics:
|
|
|
|
location = diag[ 'location' ]
|
|
|
|
bufnr = vimsupport.GetBufferNumberForFilename( location[ 'filepath' ] )
|
2017-06-11 13:46:09 -04:00
|
|
|
if bufnr == self._bufnr:
|
|
|
|
line_number = location[ 'line_num' ]
|
|
|
|
self._line_to_diags[ line_number ].append( diag )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
|
|
|
for diags in itervalues( self._line_to_diags ):
|
|
|
|
# We also want errors to be listed before warnings so that errors aren't
|
2017-06-10 01:53:18 -04:00
|
|
|
# hidden by the warnings; Vim won't place a sign over an existing one.
|
2017-03-28 15:37:58 -04:00
|
|
|
diags.sort( key = lambda diag: ( diag[ 'kind' ],
|
|
|
|
diag[ 'location' ][ 'column_num' ] ) )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2014-01-04 21:32:42 -05:00
|
|
|
|
2016-10-23 10:29:54 -04:00
|
|
|
_DiagnosticIsError = CompileLevel( 'error' )
|
|
|
|
_DiagnosticIsWarning = CompileLevel( 'warning' )
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
|
2014-10-29 13:16:38 -04:00
|
|
|
def _NormalizeDiagnostic( diag ):
|
|
|
|
def ClampToOne( value ):
|
|
|
|
return value if value > 0 else 1
|
|
|
|
|
|
|
|
location = diag[ 'location' ]
|
|
|
|
location[ 'column_num' ] = ClampToOne( location[ 'column_num' ] )
|
|
|
|
location[ 'line_num' ] = ClampToOne( location[ 'line_num' ] )
|
|
|
|
return diag
|
2018-05-17 08:52:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _ConvertDiagnosticToMatchPatterns( diagnostic ):
|
|
|
|
patterns = []
|
|
|
|
|
|
|
|
location_extent = diagnostic[ 'location_extent' ]
|
|
|
|
if location_extent[ 'start' ][ 'line_num' ] <= 0:
|
|
|
|
location = diagnostic[ 'location' ]
|
|
|
|
patterns.append( vimsupport.GetDiagnosticMatchPattern(
|
|
|
|
location[ 'line_num' ],
|
|
|
|
location[ 'column_num' ] ) )
|
|
|
|
else:
|
|
|
|
patterns.append( vimsupport.GetDiagnosticMatchPattern(
|
|
|
|
location_extent[ 'start' ][ 'line_num' ],
|
|
|
|
location_extent[ 'start' ][ 'column_num' ],
|
|
|
|
location_extent[ 'end' ][ 'line_num' ],
|
|
|
|
location_extent[ 'end' ][ 'column_num' ] ) )
|
|
|
|
|
|
|
|
for diagnostic_range in diagnostic[ 'ranges' ]:
|
|
|
|
patterns.append( vimsupport.GetDiagnosticMatchPattern(
|
|
|
|
diagnostic_range[ 'start' ][ 'line_num' ],
|
|
|
|
diagnostic_range[ 'start' ][ 'column_num' ],
|
|
|
|
diagnostic_range[ 'end' ][ 'line_num' ],
|
|
|
|
diagnostic_range[ 'end' ][ 'column_num' ] ) )
|
|
|
|
|
|
|
|
return patterns
|