semantic_highlighting.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 ycmd import utils
  21. import vim
  22. import json
  23. HIGHLIGHT_GROUP = {
  24. 'namespace': 'Type',
  25. 'type': 'Type',
  26. 'class': 'Structure',
  27. 'enum': 'Structure',
  28. 'interface': 'Structure',
  29. 'struct': 'Structure',
  30. 'typeParameter': 'Identifier',
  31. 'parameter': 'Identifier',
  32. 'variable': 'Identifier',
  33. 'property': 'Identifier',
  34. 'enumMember': 'Identifier',
  35. 'enumConstant': 'Constant',
  36. 'event': 'Identifier',
  37. 'function': 'Function',
  38. 'member': 'Identifier',
  39. 'macro': 'Macro',
  40. 'keyword': 'Keyword',
  41. 'modifier': 'Keyword',
  42. 'comment': 'Comment',
  43. 'string': 'String',
  44. 'number': 'Number',
  45. 'regexp': 'String',
  46. 'operator': 'Operator',
  47. }
  48. def Initialise():
  49. props = GetTextPropertyTypes()
  50. if 'YCM_HL_UNKNOWN' not in props:
  51. AddTextPropertyType( 'YCM_HL_UNKNOWN', highlight = 'WarningMsg' )
  52. for token_type, group in HIGHLIGHT_GROUP.items():
  53. prop = f'YCM_HL_{ token_type }'
  54. if prop not in props:
  55. AddTextPropertyType( prop, highlight = group )
  56. # "arbitrary" base id
  57. NEXT_TEXT_PROP_ID = 70784
  58. def NextPropID():
  59. global NEXT_TEXT_PROP_ID
  60. try:
  61. return NEXT_TEXT_PROP_ID
  62. finally:
  63. NEXT_TEXT_PROP_ID += 1
  64. class SemanticHighlighting:
  65. """Stores the semantic highlighting state for a Vim buffer"""
  66. def __init__( self, bufnr, user_options ):
  67. self._request = None
  68. self._bufnr = bufnr
  69. self._prop_id = NextPropID()
  70. self.tick = -1
  71. def SendRequest( self ):
  72. if self._request and not self.IsResponseReady():
  73. return
  74. self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
  75. self._request = SemanticTokensRequest( BuildRequestData() )
  76. self._request.Start()
  77. def IsResponseReady( self ):
  78. return self._request is not None and self._request.Done()
  79. def Update( self ):
  80. if not self.IsResponseReady():
  81. # Not ready - poll
  82. return False
  83. if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
  84. # Buffer has changed, we should ignore the data and retry
  85. # self.SendRequest()
  86. return False
  87. # We requested a snapshot
  88. response = self._request.Response()
  89. self._request = None
  90. tokens = response.get( 'tokens', [] )
  91. prev_prop_id = self._prop_id
  92. self._prop_id = NextPropID()
  93. for token in tokens:
  94. if token[ 'type' ] not in HIGHLIGHT_GROUP:
  95. continue
  96. prop_type = f"YCM_HL_{ token[ 'type' ] }"
  97. AddTextProperty( self._bufnr, self._prop_id, prop_type, token[ 'range' ] )
  98. ClearTextProperties( self._bufnr, prev_prop_id )
  99. # No need to re-poll
  100. return False
  101. # FIXME/TODO: Merge this with vimsupport funcitons, added after these were
  102. # writted for Diagnostics
  103. if not vimsupport.VimSupportsTextProperties():
  104. def AddTextPropertyType( *args, **kwargs ):
  105. pass
  106. def GetTextPropertyTypes( *args, **kwargs ):
  107. return []
  108. def AddTextProperty( *args, **kwargs ):
  109. pass
  110. def ClearTextProperties( *args, **kwargs ):
  111. pass
  112. else:
  113. def AddTextPropertyType( name, **kwargs ):
  114. props = {
  115. 'highlight': 'Ignore',
  116. 'combine': False,
  117. 'start_incl': False,
  118. 'end_incl': False,
  119. 'priority': 10
  120. }
  121. props.update( kwargs )
  122. vim.eval( f"prop_type_add( '{ vimsupport.EscapeForVim( name ) }', "
  123. f" { json.dumps( kwargs ) } )" )
  124. def GetTextPropertyTypes( *args, **kwargs ):
  125. return [ utils.ToUnicode( p ) for p in vim.eval( 'prop_type_list()' ) ]
  126. def AddTextProperty( bufnr, prop_id, prop_type, range ):
  127. props = {
  128. 'end_lnum': range[ 'end' ][ 'line_num' ],
  129. 'end_col': range[ 'end' ][ 'column_num' ],
  130. 'bufnr': bufnr,
  131. 'id': prop_id,
  132. 'type': prop_type
  133. }
  134. vim.eval( f"prop_add( { range[ 'start' ][ 'line_num' ] },"
  135. f" { range[ 'start' ][ 'column_num' ] },"
  136. f" { json.dumps( props ) } )" )
  137. def ClearTextProperties( bufnr, prop_id ):
  138. props = {
  139. 'id': prop_id,
  140. 'bufnr': bufnr,
  141. 'all': 1,
  142. }
  143. vim.eval( f"prop_remove( { json.dumps( props ) } )" )