inlay_hints.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. import vim
  22. from ycmd.utils import ToBytes
  23. HIGHLIGHT_GROUP = {
  24. # 1-based inedexes
  25. 0: '',
  26. 1: 'Comment', # Type
  27. 2: 'Comment' # Parameter
  28. }
  29. REPORTED_MISSING_TYPES = set()
  30. def Initialise():
  31. if vimsupport.VimIsNeovim():
  32. return
  33. props = tp.GetTextPropertyTypes()
  34. if 'YCM_INLAY_UNKNOWN' not in props:
  35. tp.AddTextPropertyType( 'YCM_INLAY_UNKNOWN', highlight = 'Comment' )
  36. for token_type, group in HIGHLIGHT_GROUP.items():
  37. prop = f'YCM_INLAY_{ token_type }'
  38. if prop not in props and vimsupport.GetIntValue(
  39. f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
  40. tp.AddTextPropertyType( prop, highlight = group )
  41. class InlayHints:
  42. """Stores the inlay hints state for a Vim buffer"""
  43. def __init__( self, bufnr, user_options ):
  44. self._request = None
  45. self._bufnr = bufnr
  46. self._prop_ids = set()
  47. self.tick = -1
  48. def SendRequest( self ):
  49. if self._request and not self.IsResponseReady():
  50. return
  51. self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
  52. # TODO: How to determine the range to display ? Should we do the range
  53. # visible in "all" windows? We're doing this per-buffer, but perhaps it
  54. # should actually be per-window; that might ultimately be a better model
  55. # but the resulting properties are per-buffer, not per-window.
  56. #
  57. # Perhaps the maximal range of visible windows or something.
  58. request_data = BuildRequestData( self._bufnr )
  59. request_data.update( {
  60. 'range': vimsupport.RangeVisibleInBuffer( self._bufnr )
  61. } )
  62. self._request = InlayHintsRequest( request_data )
  63. self._request.Start()
  64. def IsResponseReady( self ):
  65. return self._request is not None and self._request.Done()
  66. def Clear( self ):
  67. for prop_id in self._prop_ids:
  68. tp.ClearTextProperties( self._bufnr, prop_id )
  69. self._prop_ids.clear()
  70. def Update( self ):
  71. if not self._request:
  72. # Nothing to update
  73. return True
  74. assert self.IsResponseReady()
  75. # We're ready to use this response. Clear it (to avoid repeatedly
  76. # re-polling).
  77. inlay_hints = self._request.Response()
  78. self._request = None
  79. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  80. # Buffer has changed, we should ignore the data and retry
  81. self.SendRequest()
  82. return False # poll again
  83. self.Clear()
  84. for inlay_hint in inlay_hints:
  85. if 'kind' not in inlay_hint:
  86. prop_type = 'YCM_INLAY_UNKNOWN'
  87. elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
  88. prop_type = 'YCM_INLAY_UNKNOWN'
  89. else:
  90. prop_type = 'YCM_INLAY_' + str( inlay_hint[ 'kind' ] )
  91. self._prop_ids.add(
  92. tp.AddTextProperty( self._bufnr,
  93. None,
  94. prop_type,
  95. {
  96. 'start': inlay_hint[ 'position' ],
  97. 'end': inlay_hint[ 'position' ],
  98. },
  99. {
  100. 'text': inlay_hint[ 'label' ]
  101. } ) )
  102. # No need to re-poll
  103. return True