diagnostic_interface.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. def OnCursorMoved( self ):
  31. line, _ = vimsupport.CurrentLineAndColumn()
  32. line += 1 # Convert to 1-based
  33. if line != self._previous_line_number:
  34. self._previous_line_number = line
  35. if self._user_options[ 'echo_current_diagnostic' ]:
  36. self._EchoDiagnosticForLine( line )
  37. def UpdateWithNewDiagnostics( self, diags ):
  38. self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( diags )
  39. if self._user_options[ 'enable_signs' ]:
  40. self._next_sign_id = _UpdateSigns( self._buffer_number_to_line_to_diags,
  41. self._next_sign_id )
  42. if self._user_options[ 'enable_highlighting' ]:
  43. _UpdateSquiggles( self._buffer_number_to_line_to_diags )
  44. if self._user_options[ 'always_populate_loc_list' ]:
  45. vimsupport.SetLocationList(
  46. vimsupport.ConvertDiagnosticsToQfList( diags ) )
  47. def _EchoDiagnosticForLine( self, line_num ):
  48. buffer_num = vim.current.buffer.number
  49. diags = self._buffer_number_to_line_to_diags[ buffer_num ][ line_num ]
  50. if not diags:
  51. # Clear any previous diag echo
  52. vimsupport.EchoText( '', False )
  53. return
  54. vimsupport.EchoTextVimWidth( diags[ 0 ][ 'text' ] )
  55. def _UpdateSquiggles( buffer_number_to_line_to_diags ):
  56. vimsupport.ClearYcmSyntaxMatches()
  57. line_to_diags = buffer_number_to_line_to_diags[ vim.current.buffer.number ]
  58. for diags in line_to_diags.itervalues():
  59. for diag in diags:
  60. location_extent = diag[ 'location_extent' ]
  61. is_error = _DiagnosticIsError( diag )
  62. vimsupport.AddDiagnosticSyntaxMatch(
  63. location_extent[ 'start' ][ 'line_num' ] + 1,
  64. location_extent[ 'start' ][ 'column_num' ] + 1,
  65. location_extent[ 'end' ][ 'line_num' ] + 1,
  66. location_extent[ 'end' ][ 'column_num' ] + 1,
  67. is_error = is_error )
  68. for diag_range in diag[ 'ranges' ]:
  69. vimsupport.AddDiagnosticSyntaxMatch(
  70. diag_range[ 'start' ][ 'line_num' ] + 1,
  71. diag_range[ 'start' ][ 'column_num' ] + 1,
  72. diag_range[ 'end' ][ 'line_num' ] + 1,
  73. diag_range[ 'end' ][ 'column_num' ] + 1,
  74. is_error = is_error )
  75. def _UpdateSigns( buffer_number_to_line_to_diags, next_sign_id ):
  76. vimsupport.UnplaceAllSignsInBuffer( vim.current.buffer.number )
  77. for buffer_number, line_to_diags in buffer_number_to_line_to_diags.iteritems():
  78. if not vimsupport.BufferIsVisible( buffer_number ):
  79. continue
  80. vimsupport.UnplaceAllSignsInBuffer( buffer_number )
  81. for line, diags in line_to_diags.iteritems():
  82. for diag in diags:
  83. vimsupport.PlaceSign( next_sign_id,
  84. line,
  85. buffer_number,
  86. _DiagnosticIsError( diag ) )
  87. next_sign_id += 1
  88. return next_sign_id
  89. def _ConvertDiagListToDict( diag_list ):
  90. buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
  91. for diag in diag_list:
  92. location = diag[ 'location' ]
  93. buffer_number = vimsupport.GetBufferNumberForFilename(
  94. location[ 'filepath' ] )
  95. line_number = location[ 'line_num' ] + 1
  96. buffer_to_line_to_diags[ buffer_number ][ line_number ].append( diag )
  97. for line_to_diags in buffer_to_line_to_diags.itervalues():
  98. for diags in line_to_diags.itervalues():
  99. # We also want errors to be listed before warnings so that errors aren't
  100. # hidden by the warnings; Vim won't place a sign oven an existing one.
  101. diags.sort( key = lambda diag: ( diag[ 'location' ][ 'column_num' ],
  102. diag[ 'kind' ] ) )
  103. return buffer_to_line_to_diags
  104. def _DiagnosticIsError( diag ):
  105. return diag[ 'kind' ] == 'E'