diagnostic_interface.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Google Inc.
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from collections import defaultdict, namedtuple
  20. from ycm import vimsupport
  21. import vim
  22. class DiagnosticInterface( object ):
  23. def __init__( self, user_options ):
  24. self._user_options = user_options
  25. # Line and column numbers are 1-based
  26. self._buffer_number_to_line_to_diags = defaultdict(
  27. lambda: defaultdict( list ) )
  28. self._next_sign_id = 1
  29. self._previous_line_number = -1
  30. self._diag_message_needs_clearing = False
  31. self._placed_signs = []
  32. def OnCursorMoved( self ):
  33. line, _ = vimsupport.CurrentLineAndColumn()
  34. line += 1 # Convert to 1-based
  35. if line != self._previous_line_number:
  36. self._previous_line_number = line
  37. if self._user_options[ 'echo_current_diagnostic' ]:
  38. self._EchoDiagnosticForLine( line )
  39. def UpdateWithNewDiagnostics( self, diags ):
  40. self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( diags )
  41. if self._user_options[ 'enable_diagnostic_signs' ]:
  42. self._placed_signs, self._next_sign_id = _UpdateSigns(
  43. self._placed_signs,
  44. self._buffer_number_to_line_to_diags,
  45. self._next_sign_id )
  46. if self._user_options[ 'enable_diagnostic_highlighting' ]:
  47. _UpdateSquiggles( self._buffer_number_to_line_to_diags )
  48. if self._user_options[ 'always_populate_location_list' ]:
  49. vimsupport.SetLocationList(
  50. vimsupport.ConvertDiagnosticsToQfList( diags ) )
  51. def _EchoDiagnosticForLine( self, line_num ):
  52. buffer_num = vim.current.buffer.number
  53. diags = self._buffer_number_to_line_to_diags[ buffer_num ][ line_num ]
  54. if not diags:
  55. if self._diag_message_needs_clearing:
  56. # Clear any previous diag echo
  57. vimsupport.EchoText( '', False )
  58. self._diag_message_needs_clearing = False
  59. return
  60. vimsupport.EchoTextVimWidth( diags[ 0 ][ 'text' ] )
  61. self._diag_message_needs_clearing = True
  62. def _UpdateSquiggles( buffer_number_to_line_to_diags ):
  63. vimsupport.ClearYcmSyntaxMatches()
  64. line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
  65. for diags in line_to_diags.itervalues():
  66. for diag in diags:
  67. location_extent = diag[ 'location_extent' ]
  68. is_error = _DiagnosticIsError( diag )
  69. if location_extent[ 'start' ][ 'line_num' ] < 0:
  70. location = diag[ 'location' ]
  71. vimsupport.AddDiagnosticSyntaxMatch(
  72. location[ 'line_num' ],
  73. location[ 'column_num' ] )
  74. else:
  75. vimsupport.AddDiagnosticSyntaxMatch(
  76. location_extent[ 'start' ][ 'line_num' ],
  77. location_extent[ 'start' ][ 'column_num' ],
  78. location_extent[ 'end' ][ 'line_num' ],
  79. location_extent[ 'end' ][ 'column_num' ],
  80. is_error = is_error )
  81. for diag_range in diag[ 'ranges' ]:
  82. vimsupport.AddDiagnosticSyntaxMatch(
  83. diag_range[ 'start' ][ 'line_num' ],
  84. diag_range[ 'start' ][ 'column_num' ],
  85. diag_range[ 'end' ][ 'line_num' ],
  86. diag_range[ 'end' ][ 'column_num' ],
  87. is_error = is_error )
  88. def _UpdateSigns( placed_signs, buffer_number_to_line_to_diags, next_sign_id ):
  89. new_signs, kept_signs, next_sign_id = _GetKeptAndNewSigns(
  90. placed_signs, buffer_number_to_line_to_diags, next_sign_id
  91. )
  92. # Dummy sign used to prevent "flickering" in vim when last mark gets
  93. # deleted from buffer. Dummy sign prevent vim to collapse sign column in
  94. # that case.
  95. # There also a vim "bug", which cause whole window to redraw in some
  96. # conditions (vim redraw logic is very complex). But, somehow, if we place
  97. # dummy sign before placing other real visible sign, it will not redraw the
  98. # buffer (patch to vim pending).
  99. dummy_sign_needed = False
  100. if kept_signs == [] and new_signs != []:
  101. dummy_sign_needed = True
  102. if dummy_sign_needed:
  103. vimsupport.PlaceDummySign( next_sign_id + 1, vim.current.buffer.number,
  104. new_signs[0].line )
  105. # Now we can place only that signs that are not placed yet.
  106. new_placed_signs = _PlaceNewSigns( kept_signs, new_signs )
  107. # We use incremental placement, so signs that already placed on right lines
  108. # will not be deleted and placed again, which should improve performance in
  109. # case of many diags.
  110. # Signs which are not exist in current diag should be deleted.
  111. _UnplaceRottenSigns( kept_signs, placed_signs )
  112. if dummy_sign_needed:
  113. vimsupport.UnPlaceDummySign( next_sign_id + 1, vim.current.buffer.number )
  114. return new_placed_signs, next_sign_id
  115. def _GetKeptAndNewSigns( placed_signs, buffer_number_to_line_to_diags,
  116. next_sign_id ):
  117. new_signs = []
  118. kept_signs = []
  119. for buffer_number, line_to_diags in buffer_number_to_line_to_diags.iteritems():
  120. if not vimsupport.BufferIsVisible( buffer_number ):
  121. continue
  122. for line, diags in line_to_diags.iteritems():
  123. for diag in diags:
  124. sign = _DiagSignPlacement( next_sign_id,
  125. line,
  126. buffer_number,
  127. _DiagnosticIsError( diag ) )
  128. if sign not in placed_signs:
  129. new_signs += [ sign ]
  130. next_sign_id += 1
  131. else:
  132. # We should use .index here because of `sign` contains new id, but
  133. # we need old id to unplace sign in future.
  134. kept_signs += [ placed_signs[ placed_signs.index( sign ) ] ]
  135. return new_signs, kept_signs, next_sign_id
  136. def _PlaceNewSigns( kept_signs, new_signs ):
  137. placed_signs = kept_signs
  138. for sign in new_signs:
  139. # Do not add two signs at the same line, it will screw signs remembering.
  140. if sign in placed_signs:
  141. continue
  142. vimsupport.PlaceSign( sign.id, sign.line, sign.buffer, sign.is_err )
  143. placed_signs += [ sign ]
  144. return placed_signs
  145. def _UnplaceRottenSigns( kept_signs, placed_signs ):
  146. for sign in placed_signs:
  147. if sign not in kept_signs:
  148. print("rotten", sign)
  149. vimsupport.UnplaceSignInBuffer( sign.buffer, sign.id )
  150. def _ConvertDiagListToDict( diag_list ):
  151. buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
  152. for diag in diag_list:
  153. location = diag[ 'location' ]
  154. buffer_number = vimsupport.GetBufferNumberForFilename(
  155. location[ 'filepath' ] )
  156. line_number = location[ 'line_num' ]
  157. buffer_to_line_to_diags[ buffer_number ][ line_number ].append( diag )
  158. for line_to_diags in buffer_to_line_to_diags.itervalues():
  159. for diags in line_to_diags.itervalues():
  160. # We also want errors to be listed before warnings so that errors aren't
  161. # hidden by the warnings; Vim won't place a sign oven an existing one.
  162. diags.sort( key = lambda diag: ( diag[ 'location' ][ 'column_num' ],
  163. diag[ 'kind' ] ) )
  164. return buffer_to_line_to_diags
  165. def _DiagnosticIsError( diag ):
  166. return diag[ 'kind' ] == 'ERROR'
  167. class _DiagSignPlacement(namedtuple( "_DiagSignPlacement",
  168. [ 'id', 'line', 'buffer', 'is_err' ])):
  169. def __eq__(self, another_sign):
  170. if self.line != another_sign.line:
  171. return False
  172. if self.buffer != another_sign.buffer:
  173. return False
  174. if self.is_err != another_sign.is_err:
  175. return False
  176. return True