inlay_hints.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (C) 2022, YouCompleteMe Contributors
  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 ycm.client.inlay_hints_request import InlayHintsRequest
  18. from ycm.client.base_request import BuildRequestData
  19. from ycm import vimsupport
  20. from ycm import text_properties as tp
  21. from ycm import scrolling_range as sr
  22. HIGHLIGHT_GROUP = {
  23. 'Type': 'YcmInlayHint',
  24. 'Parameter': 'YcmInlayHint',
  25. 'Enum': 'YcmInlayHint',
  26. }
  27. REPORTED_MISSING_TYPES = set()
  28. def Initialise():
  29. if not vimsupport.VimSupportsVirtualText():
  30. return False
  31. props = tp.GetTextPropertyTypes()
  32. if 'YCM_INLAY_UNKNOWN' not in props:
  33. tp.AddTextPropertyType( 'YCM_INLAY_UNKNOWN',
  34. highlight = 'YcmInlayHint',
  35. start_incl = 1 )
  36. if 'YCM_INLAY_PADDING' not in props:
  37. tp.AddTextPropertyType( 'YCM_INLAY_PADDING',
  38. highlight = 'YcmInvisible',
  39. start_incl = 1 )
  40. for token_type, group in HIGHLIGHT_GROUP.items():
  41. prop = f'YCM_INLAY_{ token_type }'
  42. if prop not in props and vimsupport.GetIntValue(
  43. f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
  44. tp.AddTextPropertyType( prop,
  45. highlight = group,
  46. start_incl = 1 )
  47. return True
  48. class InlayHints( sr.ScrollingBufferRange ):
  49. """Stores the inlay hints state for a Vim buffer"""
  50. def _NewRequest( self, request_range ):
  51. request_data = BuildRequestData( self._bufnr )
  52. request_data[ 'range' ] = request_range
  53. return InlayHintsRequest( request_data )
  54. def Clear( self ):
  55. types = [ 'YCM_INLAY_UNKNOWN', 'YCM_INLAY_PADDING' ] + [
  56. f'YCM_INLAY_{ prop_type }' for prop_type in HIGHLIGHT_GROUP.keys()
  57. ]
  58. tp.ClearTextProperties( self._bufnr, prop_types = types )
  59. def _Draw( self ):
  60. self.Clear()
  61. for inlay_hint in self._latest_response:
  62. if 'kind' not in inlay_hint:
  63. prop_type = 'YCM_INLAY_UNKNOWN'
  64. elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
  65. prop_type = 'YCM_INLAY_UNKNOWN'
  66. else:
  67. prop_type = 'YCM_INLAY_' + inlay_hint[ 'kind' ]
  68. self.GrowRangeIfNeeded( {
  69. 'start': inlay_hint[ 'position' ],
  70. 'end': {
  71. 'line_num': inlay_hint[ 'position' ][ 'line_num' ],
  72. 'column_num': inlay_hint[ 'position' ][ 'column_num' ] + len(
  73. inlay_hint[ 'label' ] )
  74. }
  75. } )
  76. if inlay_hint.get( 'paddingLeft', False ):
  77. tp.AddTextProperty( self._bufnr,
  78. None,
  79. 'YCM_INLAY_PADDING',
  80. {
  81. 'start': inlay_hint[ 'position' ],
  82. },
  83. {
  84. 'text': ' '
  85. } )
  86. tp.AddTextProperty( self._bufnr,
  87. None,
  88. prop_type,
  89. {
  90. 'start': inlay_hint[ 'position' ],
  91. },
  92. {
  93. 'text': inlay_hint[ 'label' ]
  94. } )
  95. if inlay_hint.get( 'paddingRight', False ):
  96. tp.AddTextProperty( self._bufnr,
  97. None,
  98. 'YCM_INLAY_PADDING',
  99. {
  100. 'start': inlay_hint[ 'position' ],
  101. },
  102. {
  103. 'text': ' '
  104. } )