diagnostic_interface.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (C) 2013 Google Inc.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from future.utils import itervalues, iteritems
  24. from collections import defaultdict
  25. from ycm import vimsupport
  26. from ycm.diagnostic_filter import DiagnosticFilter, CompileLevel
  27. class DiagnosticInterface( object ):
  28. def __init__( self, bufnr, user_options ):
  29. self._bufnr = bufnr
  30. self._user_options = user_options
  31. self._diagnostics = []
  32. self._diag_filter = DiagnosticFilter.CreateFromOptions( user_options )
  33. # Line and column numbers are 1-based
  34. self._line_to_diags = defaultdict( list )
  35. self._next_sign_id = vimsupport.SIGN_BUFFER_ID_INITIAL_VALUE
  36. self._previous_diag_line_number = -1
  37. self._diag_message_needs_clearing = False
  38. def OnCursorMoved( self ):
  39. if self._user_options[ 'echo_current_diagnostic' ]:
  40. line, _ = vimsupport.CurrentLineAndColumn()
  41. line += 1 # Convert to 1-based
  42. if line != self._previous_diag_line_number:
  43. self._EchoDiagnosticForLine( line )
  44. def GetErrorCount( self ):
  45. return self._DiagnosticsCount( _DiagnosticIsError )
  46. def GetWarningCount( self ):
  47. return self._DiagnosticsCount( _DiagnosticIsWarning )
  48. def PopulateLocationList( self ):
  49. # Do nothing if loc list is already populated by diag_interface
  50. if not self._user_options[ 'always_populate_location_list' ]:
  51. self._UpdateLocationList()
  52. return bool( self._diagnostics )
  53. def UpdateWithNewDiagnostics( self, diags ):
  54. self._diagnostics = [ _NormalizeDiagnostic( x ) for x in
  55. self._ApplyDiagnosticFilter( diags ) ]
  56. self._ConvertDiagListToDict()
  57. if self._user_options[ 'echo_current_diagnostic' ]:
  58. self._EchoDiagnostic()
  59. if self._user_options[ 'enable_diagnostic_signs' ]:
  60. self._UpdateSigns()
  61. self.UpdateMatches()
  62. if self._user_options[ 'always_populate_location_list' ]:
  63. self._UpdateLocationList()
  64. def _ApplyDiagnosticFilter( self, diags ):
  65. filetypes = vimsupport.GetBufferFiletypes( self._bufnr )
  66. diag_filter = self._diag_filter.SubsetForTypes( filetypes )
  67. return filter( diag_filter.IsAllowed, diags )
  68. def _EchoDiagnostic( self ):
  69. line, _ = vimsupport.CurrentLineAndColumn()
  70. line += 1 # Convert to 1-based
  71. self._EchoDiagnosticForLine( line )
  72. def _EchoDiagnosticForLine( self, line_num ):
  73. self._previous_diag_line_number = line_num
  74. diags = self._line_to_diags[ line_num ]
  75. if not diags:
  76. if self._diag_message_needs_clearing:
  77. # Clear any previous diag echo
  78. vimsupport.PostVimMessage( '', warning = False )
  79. self._diag_message_needs_clearing = False
  80. return
  81. first_diag = diags[ 0 ]
  82. text = first_diag[ 'text' ]
  83. if first_diag.get( 'fixit_available', False ):
  84. text += ' (FixIt)'
  85. vimsupport.PostVimMessage( text, warning = False, truncate = True )
  86. self._diag_message_needs_clearing = True
  87. def _DiagnosticsCount( self, predicate ):
  88. count = 0
  89. for diags in itervalues( self._line_to_diags ):
  90. count += sum( 1 for d in diags if predicate( d ) )
  91. return count
  92. def _UpdateLocationList( self ):
  93. vimsupport.SetLocationListForBuffer(
  94. self._bufnr,
  95. vimsupport.ConvertDiagnosticsToQfList( self._diagnostics ) )
  96. def UpdateMatches( self ):
  97. if not self._user_options[ 'enable_diagnostic_highlighting' ]:
  98. return
  99. matches_to_remove = vimsupport.GetDiagnosticMatchesInCurrentWindow()
  100. for diags in itervalues( self._line_to_diags ):
  101. # Insert squiggles in reverse order so that errors overlap warnings.
  102. for diag in reversed( diags ):
  103. patterns = []
  104. group = ( 'YcmErrorSection' if _DiagnosticIsError( diag ) else
  105. 'YcmWarningSection' )
  106. location_extent = diag[ 'location_extent' ]
  107. if location_extent[ 'start' ][ 'line_num' ] <= 0:
  108. location = diag[ 'location' ]
  109. patterns.append( vimsupport.GetDiagnosticMatchPattern(
  110. location[ 'line_num' ],
  111. location[ 'column_num' ] ) )
  112. else:
  113. patterns.append( vimsupport.GetDiagnosticMatchPattern(
  114. location_extent[ 'start' ][ 'line_num' ],
  115. location_extent[ 'start' ][ 'column_num' ],
  116. location_extent[ 'end' ][ 'line_num' ],
  117. location_extent[ 'end' ][ 'column_num' ] ) )
  118. for diag_range in diag[ 'ranges' ]:
  119. patterns.append( vimsupport.GetDiagnosticMatchPattern(
  120. diag_range[ 'start' ][ 'line_num' ],
  121. diag_range[ 'start' ][ 'column_num' ],
  122. diag_range[ 'end' ][ 'line_num' ],
  123. diag_range[ 'end' ][ 'column_num' ] ) )
  124. for pattern in patterns:
  125. # The id doesn't matter for matches that we may add.
  126. match = vimsupport.DiagnosticMatch( 0, group, pattern )
  127. try:
  128. matches_to_remove.remove( match )
  129. except ValueError:
  130. vimsupport.AddDiagnosticMatch( match )
  131. for match in matches_to_remove:
  132. vimsupport.RemoveDiagnosticMatch( match )
  133. def _UpdateSigns( self ):
  134. signs_to_unplace = vimsupport.GetSignsInBuffer( self._bufnr )
  135. for line, diags in iteritems( self._line_to_diags ):
  136. if not diags:
  137. continue
  138. # We always go for the first diagnostic on the line because diagnostics
  139. # are sorted by errors in priority and Vim can only display one sign by
  140. # line.
  141. name = 'YcmError' if _DiagnosticIsError( diags[ 0 ] ) else 'YcmWarning'
  142. sign = vimsupport.DiagnosticSign( self._next_sign_id,
  143. line,
  144. name,
  145. self._bufnr )
  146. try:
  147. signs_to_unplace.remove( sign )
  148. except ValueError:
  149. vimsupport.PlaceSign( sign )
  150. self._next_sign_id += 1
  151. for sign in signs_to_unplace:
  152. vimsupport.UnplaceSign( sign )
  153. def _ConvertDiagListToDict( self ):
  154. self._line_to_diags = defaultdict( list )
  155. for diag in self._diagnostics:
  156. location = diag[ 'location' ]
  157. bufnr = vimsupport.GetBufferNumberForFilename( location[ 'filepath' ] )
  158. if bufnr == self._bufnr:
  159. line_number = location[ 'line_num' ]
  160. self._line_to_diags[ line_number ].append( diag )
  161. for diags in itervalues( self._line_to_diags ):
  162. # We also want errors to be listed before warnings so that errors aren't
  163. # hidden by the warnings; Vim won't place a sign over an existing one.
  164. diags.sort( key = lambda diag: ( diag[ 'kind' ],
  165. diag[ 'location' ][ 'column_num' ] ) )
  166. _DiagnosticIsError = CompileLevel( 'error' )
  167. _DiagnosticIsWarning = CompileLevel( 'warning' )
  168. def _NormalizeDiagnostic( diag ):
  169. def ClampToOne( value ):
  170. return value if value > 0 else 1
  171. location = diag[ 'location' ]
  172. location[ 'column_num' ] = ClampToOne( location[ 'column_num' ] )
  173. location[ 'line_num' ] = ClampToOne( location[ 'line_num' ] )
  174. return diag