semantic_highlighting.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (C) 2020, 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.semantic_tokens_request import SemanticTokensRequest
  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. 'namespace': 'Type',
  23. 'type': 'Type',
  24. 'class': 'Structure',
  25. 'enum': 'Structure',
  26. 'interface': 'Structure',
  27. 'struct': 'Structure',
  28. 'typeParameter': 'Identifier',
  29. 'parameter': 'Identifier',
  30. 'variable': 'Identifier',
  31. 'property': 'Identifier',
  32. 'enumMember': 'Identifier',
  33. 'enumConstant': 'Constant',
  34. 'event': 'Identifier',
  35. 'function': 'Function',
  36. 'member': 'Identifier',
  37. 'macro': 'Macro',
  38. 'method': 'Function',
  39. 'keyword': 'Keyword',
  40. 'modifier': 'Keyword',
  41. 'comment': 'Comment',
  42. 'string': 'String',
  43. 'number': 'Number',
  44. 'regexp': 'String',
  45. 'operator': 'Operator',
  46. 'unknown': 'Normal',
  47. }
  48. REPORTED_MISSING_TYPES = set()
  49. def Initialise():
  50. if vimsupport.VimIsNeovim():
  51. return
  52. props = tp.GetTextPropertyTypes()
  53. if 'YCM_HL_UNKNOWN' not in props:
  54. tp.AddTextPropertyType( 'YCM_HL_UNKNOWN', highlight = 'WarningMsg' )
  55. for token_type, group in HIGHLIGHT_GROUP.items():
  56. prop = f'YCM_HL_{ token_type }'
  57. if prop not in props and vimsupport.GetIntValue(
  58. f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
  59. tp.AddTextPropertyType( prop, highlight = group )
  60. # "arbitrary" base id
  61. NEXT_TEXT_PROP_ID = 70784
  62. def NextPropID():
  63. global NEXT_TEXT_PROP_ID
  64. try:
  65. return NEXT_TEXT_PROP_ID
  66. finally:
  67. NEXT_TEXT_PROP_ID += 1
  68. class SemanticHighlighting:
  69. """Stores the semantic highlighting state for a Vim buffer"""
  70. def __init__( self, bufnr, user_options ):
  71. self._request = None
  72. self._bufnr = bufnr
  73. self._prop_id = NextPropID()
  74. self.tick = -1
  75. def SendRequest( self ):
  76. if self._request and not self.IsResponseReady():
  77. return
  78. self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
  79. request: dict = BuildRequestData( self._bufnr )
  80. request.update( {
  81. 'range': vimsupport.RangeVisibleInBuffer( self._bufnr )
  82. } )
  83. self._request = SemanticTokensRequest( request )
  84. self._request.Start()
  85. def IsResponseReady( self ):
  86. return self._request is not None and self._request.Done()
  87. def Update( self ):
  88. if not self._request:
  89. # Nothing to update
  90. return True
  91. assert self.IsResponseReady()
  92. # We're ready to use this response. Clear it (to avoid repeatedly
  93. # re-polling).
  94. response = self._request.Response()
  95. self._request = None
  96. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  97. # Buffer has changed, we should ignore the data and retry
  98. self.SendRequest()
  99. return False # poll again
  100. # We requested a snapshot
  101. tokens = response.get( 'tokens', [] )
  102. prev_prop_id = self._prop_id
  103. self._prop_id = NextPropID()
  104. for token in tokens:
  105. if token[ 'type' ] not in HIGHLIGHT_GROUP:
  106. if token[ 'type' ] not in REPORTED_MISSING_TYPES:
  107. REPORTED_MISSING_TYPES.add( token[ 'type' ] )
  108. vimsupport.PostVimMessage(
  109. f"Missing property type for { token[ 'type' ] }" )
  110. continue
  111. prop_type = f"YCM_HL_{ token[ 'type' ] }"
  112. tp.AddTextProperty( self._bufnr,
  113. self._prop_id,
  114. prop_type,
  115. token[ 'range' ] )
  116. tp.ClearTextProperties( self._bufnr, prev_prop_id )
  117. # No need to re-poll
  118. return True