inlay_hints.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. HIGHLIGHT_GROUP = {
  22. # 1-based inedexes
  23. 0: '',
  24. 1: 'YcmInlayHint', # Type
  25. 2: 'YcmInlayHint' # Parameter
  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:
  49. """Stores the inlay hints state for a Vim buffer"""
  50. # FIXME: Send a request per-disjoint range for this buffer rather than the
  51. # maximal range. then collaate the results when all responses are returned
  52. def __init__( self, bufnr, user_options ):
  53. self._request = None
  54. self._bufnr = bufnr
  55. self.tick = -1
  56. self._latest_inlay_hints = []
  57. self._last_requested_range = None
  58. def Request( self, force=False ):
  59. if self._request and not self.Ready():
  60. return True
  61. # Check to see if the buffer ranges would actually change anything visible.
  62. # This avoids a round-trip for every single line scroll event
  63. if ( not force and
  64. self.tick == vimsupport.GetBufferChangedTick( self._bufnr ) and
  65. vimsupport.VisibleRangeOfBufferOverlaps(
  66. self._bufnr,
  67. self._last_requested_range ) ):
  68. return False # don't poll
  69. # We're requesting changes, so the existing results are now invalid
  70. self._latest_inlay_hints = []
  71. # FIXME: This call is duplicated in the call to VisibleRangeOfBufferOverlaps
  72. # - remove the expansion param
  73. # - look up the actual visible range, then call this function
  74. # - if not overlapping, do the factor expansion and request
  75. self._last_requested_range = vimsupport.RangeVisibleInBuffer( self._bufnr )
  76. self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
  77. # TODO: How to determine the range to display ? Should we do the range
  78. # visible in "all" windows? We're doing this per-buffer, but perhaps it
  79. # should actually be per-window; that might ultimately be a better model
  80. # but the resulting properties are per-buffer, not per-window.
  81. #
  82. # Perhaps the maximal range of visible windows or something.
  83. request_data = BuildRequestData( self._bufnr )
  84. request_data.update( {
  85. 'range': self._last_requested_range
  86. } )
  87. self._request = InlayHintsRequest( request_data )
  88. self._request.Start()
  89. return True
  90. def Ready( self ):
  91. return self._request is not None and self._request.Done()
  92. def Clear( self ):
  93. # FIXME: ClearTextProperties is slow as it must scan the whole buffer
  94. # We should use _last_requested_range to specify the range to clear
  95. for type in HIGHLIGHT_GROUP.keys():
  96. if type == 0:
  97. continue
  98. tp.ClearTextProperties( self._bufnr, type=f'YCM_INLAY_{ type }' )
  99. tp.ClearTextProperties( self._bufnr, type='YCM_INLAY_UNKNOWN' )
  100. tp.ClearTextProperties( self._bufnr, type='YCM_INLAY_PADDING' )
  101. def Update( self ):
  102. if not self._request:
  103. # Nothing to update
  104. return True
  105. assert self.Ready()
  106. # We're ready to use this response. Clear it (to avoid repeatedly
  107. # re-polling).
  108. self._latest_inlay_hints = self._request.Response()
  109. self._request = None
  110. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  111. # Buffer has changed, we should ignore the data and retry
  112. self.Request( force=True )
  113. return False # poll again
  114. self._Draw()
  115. # No need to re-poll
  116. return True
  117. def Refresh( self ):
  118. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  119. # state data
  120. return
  121. if self._request is not None:
  122. # request in progress; we''l handle refreshing when it's done.
  123. return
  124. self._Draw()
  125. def _Draw( self ):
  126. self.Clear()
  127. for inlay_hint in self._latest_inlay_hints:
  128. if 'kind' not in inlay_hint:
  129. prop_type = 'YCM_INLAY_UNKNOWN'
  130. elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
  131. prop_type = 'YCM_INLAY_UNKNOWN'
  132. else:
  133. prop_type = 'YCM_INLAY_' + str( inlay_hint[ 'kind' ] )
  134. if inlay_hint.get( 'paddingLeft', False ):
  135. tp.AddTextProperty( self._bufnr,
  136. None,
  137. 'YCM_INLAY_PADDING',
  138. {
  139. 'start': inlay_hint[ 'position' ],
  140. 'end': inlay_hint[ 'position' ],
  141. },
  142. {
  143. 'text': ' '
  144. } )
  145. tp.AddTextProperty( self._bufnr,
  146. None,
  147. prop_type,
  148. {
  149. 'start': inlay_hint[ 'position' ],
  150. 'end': inlay_hint[ 'position' ],
  151. },
  152. {
  153. 'text': inlay_hint[ 'label' ]
  154. } )
  155. if inlay_hint.get( 'paddingRight', False ):
  156. tp.AddTextProperty( self._bufnr,
  157. None,
  158. 'YCM_INLAY_PADDING',
  159. {
  160. 'start': inlay_hint[ 'position' ],
  161. 'end': inlay_hint[ 'position' ],
  162. },
  163. {
  164. 'text': ' '
  165. } )