diagnostic_interface.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  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
  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._buffer_number_to_sign_ids = defaultdict( set )
  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._UpdateSigns()
  43. if self._user_options[ 'enable_diagnostic_highlighting' ]:
  44. _UpdateSquiggles( self._buffer_number_to_line_to_diags )
  45. if self._user_options[ 'always_populate_location_list' ]:
  46. vimsupport.SetLocationList(
  47. vimsupport.ConvertDiagnosticsToQfList( diags ) )
  48. def _EchoDiagnosticForLine( self, line_num ):
  49. buffer_num = vim.current.buffer.number
  50. diags = self._buffer_number_to_line_to_diags[ buffer_num ][ line_num ]
  51. if not diags:
  52. if self._diag_message_needs_clearing:
  53. # Clear any previous diag echo
  54. vimsupport.EchoText( '', False )
  55. self._diag_message_needs_clearing = False
  56. return
  57. vimsupport.EchoTextVimWidth( diags[ 0 ][ 'text' ] )
  58. self._diag_message_needs_clearing = True
  59. def _UnplaceSignsInBuffer( self, buffer_number ):
  60. vimsupport.UnplaceSignsInBuffer(
  61. buffer_number,
  62. self._buffer_number_to_sign_ids[ buffer_number ] )
  63. self._buffer_number_to_sign_ids[ buffer_number ].clear()
  64. def _PlaceSignInBuffer( self, line, buffer_number, is_error ):
  65. vimsupport.PlaceSign( self._next_sign_id,
  66. line,
  67. buffer_number,
  68. is_error )
  69. self._buffer_number_to_sign_ids[ buffer_number ].add( self._next_sign_id )
  70. self._next_sign_id += 1
  71. def _UpdateSigns( self ):
  72. self._UnplaceSignsInBuffer( vim.current.buffer.number )
  73. for buffer_number, line_to_diags in \
  74. self._buffer_number_to_line_to_diags.iteritems():
  75. if not vimsupport.BufferIsVisible( buffer_number ):
  76. continue
  77. self._UnplaceSignsInBuffer( buffer_number )
  78. for line, diags in line_to_diags.iteritems():
  79. for diag in diags:
  80. self._PlaceSignInBuffer( line,
  81. buffer_number,
  82. _DiagnosticIsError( diag ) )
  83. def _UpdateSquiggles( buffer_number_to_line_to_diags ):
  84. vimsupport.ClearYcmSyntaxMatches()
  85. line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
  86. for diags in line_to_diags.itervalues():
  87. for diag in diags:
  88. location_extent = diag[ 'location_extent' ]
  89. is_error = _DiagnosticIsError( diag )
  90. if location_extent[ 'start' ][ 'line_num' ] < 0:
  91. location = diag[ 'location' ]
  92. vimsupport.AddDiagnosticSyntaxMatch(
  93. location[ 'line_num' ] + 1,
  94. location[ 'column_num' ] + 1 )
  95. else:
  96. vimsupport.AddDiagnosticSyntaxMatch(
  97. location_extent[ 'start' ][ 'line_num' ] + 1,
  98. location_extent[ 'start' ][ 'column_num' ] + 1,
  99. location_extent[ 'end' ][ 'line_num' ] + 1,
  100. location_extent[ 'end' ][ 'column_num' ] + 1,
  101. is_error = is_error )
  102. for diag_range in diag[ 'ranges' ]:
  103. vimsupport.AddDiagnosticSyntaxMatch(
  104. diag_range[ 'start' ][ 'line_num' ] + 1,
  105. diag_range[ 'start' ][ 'column_num' ] + 1,
  106. diag_range[ 'end' ][ 'line_num' ] + 1,
  107. diag_range[ 'end' ][ 'column_num' ] + 1,
  108. is_error = is_error )
  109. def _ConvertDiagListToDict( diag_list ):
  110. buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
  111. for diag in diag_list:
  112. location = diag[ 'location' ]
  113. buffer_number = vimsupport.GetBufferNumberForFilename(
  114. location[ 'filepath' ] )
  115. line_number = location[ 'line_num' ] + 1
  116. buffer_to_line_to_diags[ buffer_number ][ line_number ].append( diag )
  117. for line_to_diags in buffer_to_line_to_diags.itervalues():
  118. for diags in line_to_diags.itervalues():
  119. # We also want errors to be listed before warnings so that errors aren't
  120. # hidden by the warnings; Vim won't place a sign oven an existing one.
  121. diags.sort( key = lambda diag: ( diag[ 'location' ][ 'column_num' ],
  122. diag[ 'kind' ] ) )
  123. return buffer_to_line_to_diags
  124. def _DiagnosticIsError( diag ):
  125. return diag[ 'kind' ] == 'E'