2014-01-13 14:08:43 -05:00
|
|
|
# Copyright (C) 2013 Google Inc.
|
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
|
2014-09-13 14:15:40 -04:00
|
|
|
from collections import defaultdict, namedtuple
|
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 19:45:34 -05:00
|
|
|
import vim
|
2014-01-04 17:28:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DiagnosticInterface( object ):
|
2014-01-08 21:43:17 -05:00
|
|
|
def __init__( self, user_options ):
|
|
|
|
self._user_options = user_options
|
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
|
|
|
|
self._buffer_number_to_line_to_diags = defaultdict(
|
|
|
|
lambda: defaultdict( list ) )
|
2014-01-04 17:28:27 -05:00
|
|
|
self._next_sign_id = 1
|
2014-01-04 19:45:34 -05:00
|
|
|
self._previous_line_number = -1
|
2014-01-10 15:18:24 -05:00
|
|
|
self._diag_message_needs_clearing = False
|
2014-09-13 14:15:40 -04:00
|
|
|
self._placed_signs = []
|
2014-01-04 19:45:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
def OnCursorMoved( self ):
|
|
|
|
line, _ = vimsupport.CurrentLineAndColumn()
|
|
|
|
line += 1 # Convert to 1-based
|
|
|
|
if line != self._previous_line_number:
|
|
|
|
self._previous_line_number = line
|
2014-01-08 21:43:17 -05:00
|
|
|
|
|
|
|
if self._user_options[ 'echo_current_diagnostic' ]:
|
|
|
|
self._EchoDiagnosticForLine( line )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
def GetErrorCount( self ):
|
2015-12-04 18:59:18 -05:00
|
|
|
return len( self._FilterDiagnostics( _DiagnosticIsError ) )
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
def GetWarningCount( self ):
|
2015-12-04 18:59:18 -05:00
|
|
|
return len( self._FilterDiagnostics( _DiagnosticIsWarning ) )
|
2015-12-04 17:45:11 -05:00
|
|
|
|
|
|
|
|
2016-01-11 14:16:49 -05:00
|
|
|
def PopulateLocationList( self, diags ):
|
|
|
|
vimsupport.SetLocationList(
|
2016-10-22 18:37:13 -04:00
|
|
|
vimsupport.ConvertDiagnosticsToQfList(
|
|
|
|
self._ApplyDiagnosticFilter( diags ) ) )
|
2016-01-11 14:16:49 -05:00
|
|
|
|
|
|
|
|
2014-01-04 17:28:27 -05:00
|
|
|
def UpdateWithNewDiagnostics( self, diags ):
|
2016-10-22 18:37:13 -04:00
|
|
|
normalized_diags = [ _NormalizeDiagnostic( x ) for x in
|
|
|
|
self._ApplyDiagnosticFilter( diags ) ]
|
2014-10-29 13:16:38 -04:00
|
|
|
self._buffer_number_to_line_to_diags = _ConvertDiagListToDict(
|
|
|
|
normalized_diags )
|
2014-01-08 21:43:17 -05:00
|
|
|
|
2014-01-08 22:43:21 -05:00
|
|
|
if self._user_options[ 'enable_diagnostic_signs' ]:
|
2014-09-13 14:15:40 -04:00
|
|
|
self._placed_signs, self._next_sign_id = _UpdateSigns(
|
|
|
|
self._placed_signs,
|
|
|
|
self._buffer_number_to_line_to_diags,
|
|
|
|
self._next_sign_id )
|
2014-01-08 21:43:17 -05:00
|
|
|
|
2014-01-08 22:43:21 -05:00
|
|
|
if self._user_options[ 'enable_diagnostic_highlighting' ]:
|
2014-01-08 21:43:17 -05:00
|
|
|
_UpdateSquiggles( self._buffer_number_to_line_to_diags )
|
2014-01-04 19:45:34 -05:00
|
|
|
|
2014-01-08 22:43:21 -05:00
|
|
|
if self._user_options[ 'always_populate_location_list' ]:
|
2016-01-11 14:16:49 -05:00
|
|
|
self.PopulateLocationList( normalized_diags )
|
2014-01-08 22:09:40 -05:00
|
|
|
|
2016-10-22 18:37:13 -04:00
|
|
|
|
2016-10-23 10:29:54 -04:00
|
|
|
def _ApplyDiagnosticFilter( self, diags, extra_predicate = None ):
|
2016-10-22 18:37:13 -04:00
|
|
|
filetypes = vimsupport.CurrentFiletypes()
|
|
|
|
diag_filter = self._diag_filter.SubsetForTypes( filetypes )
|
2016-10-23 10:29:54 -04:00
|
|
|
predicate = diag_filter.IsAllowed
|
|
|
|
if extra_predicate is not None:
|
|
|
|
def Filter( diag ):
|
|
|
|
return extra_predicate( diag ) and diag_filter.IsAllowed( diag )
|
|
|
|
|
|
|
|
predicate = Filter
|
|
|
|
|
|
|
|
return filter( predicate, diags )
|
2016-10-22 18:37:13 -04:00
|
|
|
|
|
|
|
|
2014-01-04 19:45:34 -05:00
|
|
|
def _EchoDiagnosticForLine( self, line_num ):
|
|
|
|
buffer_num = vim.current.buffer.number
|
|
|
|
diags = self._buffer_number_to_line_to_diags[ buffer_num ][ line_num ]
|
|
|
|
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
|
|
|
|
|
|
|
|
2015-12-04 18:59:18 -05:00
|
|
|
def _FilterDiagnostics( self, predicate ):
|
|
|
|
matched_diags = []
|
|
|
|
line_to_diags = self._buffer_number_to_line_to_diags[
|
|
|
|
vim.current.buffer.number ]
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
for diags in itervalues( line_to_diags ):
|
2016-10-23 10:29:54 -04:00
|
|
|
matched_diags.extend( list(
|
|
|
|
self._ApplyDiagnosticFilter( diags, predicate ) ) )
|
2015-12-04 18:59:18 -05:00
|
|
|
return matched_diags
|
|
|
|
|
|
|
|
|
2014-01-04 21:32:42 -05:00
|
|
|
def _UpdateSquiggles( buffer_number_to_line_to_diags ):
|
|
|
|
vimsupport.ClearYcmSyntaxMatches()
|
|
|
|
line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
for diags in itervalues( line_to_diags ):
|
2017-03-28 15:37:58 -04:00
|
|
|
# Insert squiggles in reverse order so that errors overlap warnings.
|
|
|
|
for diag in reversed( diags ):
|
2014-01-05 19:09:40 -05:00
|
|
|
location_extent = diag[ 'location_extent' ]
|
2014-01-05 16:30:22 -05:00
|
|
|
is_error = _DiagnosticIsError( diag )
|
|
|
|
|
2017-04-05 20:22:04 -04:00
|
|
|
if location_extent[ 'start' ][ 'line_num' ] <= 0:
|
2014-01-09 18:48:48 -05:00
|
|
|
location = diag[ 'location' ]
|
|
|
|
vimsupport.AddDiagnosticSyntaxMatch(
|
2017-04-05 20:22:04 -04:00
|
|
|
location[ 'line_num' ],
|
|
|
|
location[ 'column_num' ],
|
|
|
|
is_error = is_error )
|
2014-01-09 18:48:48 -05:00
|
|
|
else:
|
|
|
|
vimsupport.AddDiagnosticSyntaxMatch(
|
2014-05-09 18:57:04 -04:00
|
|
|
location_extent[ 'start' ][ 'line_num' ],
|
|
|
|
location_extent[ 'start' ][ 'column_num' ],
|
|
|
|
location_extent[ 'end' ][ 'line_num' ],
|
|
|
|
location_extent[ 'end' ][ 'column_num' ],
|
2014-01-09 18:48:48 -05:00
|
|
|
is_error = is_error )
|
2014-01-05 16:30:22 -05:00
|
|
|
|
|
|
|
for diag_range in diag[ 'ranges' ]:
|
|
|
|
vimsupport.AddDiagnosticSyntaxMatch(
|
2014-05-09 18:57:04 -04:00
|
|
|
diag_range[ 'start' ][ 'line_num' ],
|
|
|
|
diag_range[ 'start' ][ 'column_num' ],
|
|
|
|
diag_range[ 'end' ][ 'line_num' ],
|
|
|
|
diag_range[ 'end' ][ 'column_num' ],
|
2014-01-05 16:30:22 -05:00
|
|
|
is_error = is_error )
|
2014-01-04 21:32:42 -05:00
|
|
|
|
|
|
|
|
2014-09-13 14:15:40 -04:00
|
|
|
def _UpdateSigns( placed_signs, buffer_number_to_line_to_diags, next_sign_id ):
|
|
|
|
new_signs, kept_signs, next_sign_id = _GetKeptAndNewSigns(
|
|
|
|
placed_signs, buffer_number_to_line_to_diags, next_sign_id
|
|
|
|
)
|
2014-09-19 15:30:15 -04:00
|
|
|
# Dummy sign used to prevent "flickering" in Vim when last mark gets
|
|
|
|
# deleted from buffer. Dummy sign prevents Vim to collapsing the sign column
|
|
|
|
# in that case.
|
|
|
|
# There's also a vim bug which causes the whole window to redraw in some
|
|
|
|
# conditions (vim redraw logic is very complex). But, somehow, if we place a
|
|
|
|
# dummy sign before placing other "real" signs, it will not redraw the
|
2014-09-13 14:15:40 -04:00
|
|
|
# buffer (patch to vim pending).
|
2014-09-19 15:30:15 -04:00
|
|
|
dummy_sign_needed = not kept_signs and new_signs
|
2014-09-13 14:15:40 -04:00
|
|
|
|
|
|
|
if dummy_sign_needed:
|
2014-09-19 15:30:15 -04:00
|
|
|
vimsupport.PlaceDummySign( next_sign_id + 1,
|
|
|
|
vim.current.buffer.number,
|
|
|
|
new_signs[ 0 ].line )
|
2014-09-13 14:15:40 -04:00
|
|
|
|
2014-09-19 15:30:15 -04:00
|
|
|
# We place only those signs that haven't been placed yet.
|
2014-09-13 14:15:40 -04:00
|
|
|
new_placed_signs = _PlaceNewSigns( kept_signs, new_signs )
|
|
|
|
|
2014-09-19 15:30:15 -04:00
|
|
|
# We use incremental placement, so signs that already placed on the correct
|
|
|
|
# lines will not be deleted and placed again, which should improve performance
|
|
|
|
# in case of many diags. Signs which don't exist in the current diag should be
|
|
|
|
# deleted.
|
|
|
|
_UnplaceObsoleteSigns( kept_signs, placed_signs )
|
2014-09-13 14:15:40 -04:00
|
|
|
|
|
|
|
if dummy_sign_needed:
|
|
|
|
vimsupport.UnPlaceDummySign( next_sign_id + 1, vim.current.buffer.number )
|
|
|
|
|
|
|
|
return new_placed_signs, next_sign_id
|
|
|
|
|
2014-09-19 15:30:15 -04:00
|
|
|
|
2014-09-13 14:15:40 -04:00
|
|
|
def _GetKeptAndNewSigns( placed_signs, buffer_number_to_line_to_diags,
|
|
|
|
next_sign_id ):
|
|
|
|
new_signs = []
|
|
|
|
kept_signs = []
|
2016-03-06 12:13:43 -05:00
|
|
|
for buffer_number, line_to_diags in iteritems(
|
|
|
|
buffer_number_to_line_to_diags ):
|
2014-01-10 17:39:52 -05:00
|
|
|
if not vimsupport.BufferIsVisible( buffer_number ):
|
|
|
|
continue
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
for line, diags in iteritems( line_to_diags ):
|
2017-03-28 15:37:58 -04:00
|
|
|
# Only one sign is visible by line.
|
|
|
|
first_diag = diags[ 0 ]
|
|
|
|
sign = _DiagSignPlacement( next_sign_id,
|
|
|
|
line,
|
|
|
|
buffer_number,
|
|
|
|
_DiagnosticIsError( first_diag ) )
|
|
|
|
if sign not in placed_signs:
|
|
|
|
new_signs.append( sign )
|
|
|
|
next_sign_id += 1
|
|
|
|
else:
|
|
|
|
# We use .index here because `sign` contains a new id, but
|
|
|
|
# we need the sign with the old id to unplace it later on.
|
|
|
|
# We won't be placing the new sign.
|
|
|
|
kept_signs.append( placed_signs[ placed_signs.index( sign ) ] )
|
2014-09-13 14:15:40 -04:00
|
|
|
return new_signs, kept_signs, next_sign_id
|
|
|
|
|
|
|
|
|
|
|
|
def _PlaceNewSigns( kept_signs, new_signs ):
|
2014-10-29 13:02:41 -04:00
|
|
|
placed_signs = kept_signs[:]
|
2014-09-13 14:15:40 -04:00
|
|
|
for sign in new_signs:
|
2014-09-19 15:30:15 -04:00
|
|
|
# Do not set two signs on the same line, it will screw up storing sign
|
|
|
|
# locations.
|
2014-09-13 14:15:40 -04:00
|
|
|
if sign in placed_signs:
|
|
|
|
continue
|
2014-09-19 15:30:15 -04:00
|
|
|
vimsupport.PlaceSign( sign.id, sign.line, sign.buffer, sign.is_error )
|
2017-03-28 15:37:58 -04:00
|
|
|
placed_signs.append( sign )
|
2014-09-13 14:15:40 -04:00
|
|
|
return placed_signs
|
|
|
|
|
|
|
|
|
2014-09-19 15:30:15 -04:00
|
|
|
def _UnplaceObsoleteSigns( kept_signs, placed_signs ):
|
2014-09-13 14:15:40 -04:00
|
|
|
for sign in placed_signs:
|
|
|
|
if sign not in kept_signs:
|
|
|
|
vimsupport.UnplaceSignInBuffer( sign.buffer, sign.id )
|
2014-01-10 17:39:52 -05:00
|
|
|
|
|
|
|
|
2014-01-04 19:45:34 -05:00
|
|
|
def _ConvertDiagListToDict( diag_list ):
|
|
|
|
buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
|
|
|
|
for diag in diag_list:
|
2014-01-05 15:58:13 -05:00
|
|
|
location = diag[ 'location' ]
|
|
|
|
buffer_number = vimsupport.GetBufferNumberForFilename(
|
|
|
|
location[ 'filepath' ] )
|
2014-05-09 18:57:04 -04:00
|
|
|
line_number = location[ 'line_num' ]
|
2014-01-05 15:58:13 -05:00
|
|
|
buffer_to_line_to_diags[ buffer_number ][ line_number ].append( diag )
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
for line_to_diags in itervalues( buffer_to_line_to_diags ):
|
|
|
|
for diags in itervalues( line_to_diags ):
|
2017-03-28 15:37:58 -04:00
|
|
|
# We want errors to be listed before warnings so that errors aren't hidden
|
|
|
|
# by the warnings.
|
|
|
|
diags.sort( key = lambda diag: ( diag[ 'kind' ],
|
|
|
|
diag[ 'location' ][ 'column_num' ] ) )
|
2014-01-04 19:45:34 -05:00
|
|
|
return buffer_to_line_to_diags
|
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
|
|
|
|
|
|
|
|
|
2016-03-06 12:13:43 -05:00
|
|
|
class _DiagSignPlacement(
|
|
|
|
namedtuple( "_DiagSignPlacement",
|
|
|
|
[ 'id', 'line', 'buffer', 'is_error' ] ) ):
|
2014-09-19 15:30:15 -04:00
|
|
|
# We want two signs that have different ids but the same location to compare
|
|
|
|
# equal. ID doesn't matter.
|
|
|
|
def __eq__( self, other ):
|
|
|
|
return ( self.line == other.line and
|
|
|
|
self.buffer == other.buffer and
|
|
|
|
self.is_error == other.is_error )
|