diagnostic_interface.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 operator import itemgetter
  21. from ycm import vimsupport
  22. import vim
  23. class DiagnosticInterface( object ):
  24. def __init__( self ):
  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. self._EchoDiagnosticForLine( line )
  36. def UpdateWithNewDiagnostics( self, diags ):
  37. self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( diags )
  38. self._next_sign_id = _UpdateSigns( self._buffer_number_to_line_to_diags,
  39. self._next_sign_id )
  40. def _EchoDiagnosticForLine( self, line_num ):
  41. buffer_num = vim.current.buffer.number
  42. diags = self._buffer_number_to_line_to_diags[ buffer_num ][ line_num ]
  43. if not diags:
  44. # Clear any previous diag echo
  45. vimsupport.EchoText( '', False )
  46. return
  47. vimsupport.EchoTextVimWidth( diags[ 0 ][ 'text' ] )
  48. def _UpdateSigns( buffer_number_to_line_to_diags, next_sign_id ):
  49. for buffer_number, line_to_diags in buffer_number_to_line_to_diags.iteritems():
  50. if not vimsupport.BufferIsVisible( buffer_number ):
  51. continue
  52. vimsupport.UnplaceAllSignsInBuffer( buffer_number )
  53. for line, diags in line_to_diags.iteritems():
  54. for diag in diags:
  55. vimsupport.PlaceSign( next_sign_id,
  56. line,
  57. buffer_number,
  58. diag[ 'type' ] == 'E' )
  59. next_sign_id += 1
  60. return next_sign_id
  61. def _ConvertDiagListToDict( diag_list ):
  62. buffer_to_line_to_diags = defaultdict( lambda: defaultdict( list ) )
  63. for diag in diag_list:
  64. buffer_to_line_to_diags[ diag[ 'bufnr' ] ][ diag[ 'lnum' ] ].append( diag )
  65. for line_to_diags in buffer_to_line_to_diags.itervalues():
  66. for diags in line_to_diags.itervalues():
  67. # We also want errors to be listed before warnings so that errors aren't
  68. # hidden by the warnings; Vim won't place a sign oven an existing one.
  69. diags.sort( key = lambda diag: itemgetter( 'col', 'type' ) )
  70. return buffer_to_line_to_diags