inlay_hints.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: 'Comment', # Type
  25. 2: 'Comment' # 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', highlight = 'Comment' )
  34. for token_type, group in HIGHLIGHT_GROUP.items():
  35. prop = f'YCM_INLAY_{ token_type }'
  36. if prop not in props and vimsupport.GetIntValue(
  37. f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
  38. tp.AddTextPropertyType( prop, highlight = group )
  39. return True
  40. class InlayHints:
  41. """Stores the inlay hints state for a Vim buffer"""
  42. # FIXME: Send a request per-disjoint range for this buffer rather than the
  43. # maximal range. then collaate the results when all responses are returned
  44. def __init__( self, bufnr, user_options ):
  45. self._request = None
  46. self._bufnr = bufnr
  47. self._prop_ids = set()
  48. self.tick = -1
  49. self._latest_inlay_hints = []
  50. def Request( self ):
  51. if self._request and not self.Ready():
  52. return
  53. # We're requesting changes, so the existing results are now invalid
  54. self._latest_inlay_hints = []
  55. self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
  56. # TODO: How to determine the range to display ? Should we do the range
  57. # visible in "all" windows? We're doing this per-buffer, but perhaps it
  58. # should actually be per-window; that might ultimately be a better model
  59. # but the resulting properties are per-buffer, not per-window.
  60. #
  61. # Perhaps the maximal range of visible windows or something.
  62. request_data = BuildRequestData( self._bufnr )
  63. request_data.update( {
  64. 'range': vimsupport.RangeVisibleInBuffer( self._bufnr )
  65. } )
  66. self._request = InlayHintsRequest( request_data )
  67. self._request.Start()
  68. def Ready( self ):
  69. return self._request is not None and self._request.Done()
  70. def Clear( self ):
  71. for prop_id in self._prop_ids:
  72. tp.ClearTextProperties( self._bufnr, prop_id )
  73. self._prop_ids.clear()
  74. def Update( self ):
  75. if not self._request:
  76. # Nothing to update
  77. return True
  78. assert self.Ready()
  79. # We're ready to use this response. Clear it (to avoid repeatedly
  80. # re-polling).
  81. self._latest_inlay_hints = [] # in case there was an error in request
  82. self._latest_inlay_hints = self._request.Response()
  83. self._request = None
  84. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  85. # Buffer has changed, we should ignore the data and retry
  86. self.Request()
  87. return False # poll again
  88. self._Draw()
  89. # No need to re-poll
  90. return True
  91. def Refresh( self ):
  92. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  93. # state data
  94. return
  95. if self._request is not None:
  96. # request in progress; we''l handle refreshing when it's done.
  97. return
  98. self._Draw()
  99. def _Draw( self ):
  100. self.Clear()
  101. for inlay_hint in self._latest_inlay_hints:
  102. if 'kind' not in inlay_hint:
  103. prop_type = 'YCM_INLAY_UNKNOWN'
  104. elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
  105. prop_type = 'YCM_INLAY_UNKNOWN'
  106. else:
  107. prop_type = 'YCM_INLAY_' + str( inlay_hint[ 'kind' ] )
  108. self._prop_ids.add(
  109. tp.AddTextProperty( self._bufnr,
  110. None,
  111. prop_type,
  112. {
  113. 'start': inlay_hint[ 'position' ],
  114. 'end': inlay_hint[ 'position' ],
  115. },
  116. {
  117. 'text': inlay_hint[ 'label' ]
  118. } ) )