semantic_highlighting.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. from ycm import scrolling_range as sr
  22. import vim
  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. 'method': 'Function',
  41. 'keyword': 'Keyword',
  42. 'modifier': 'Keyword',
  43. 'comment': 'Comment',
  44. 'string': 'String',
  45. 'number': 'Number',
  46. 'regexp': 'String',
  47. 'operator': 'Operator',
  48. 'decorator': 'Special',
  49. 'unknown': 'Normal',
  50. # These are not part of the spec, but are used by clangd
  51. 'bracket': 'Normal',
  52. 'concept': 'Type',
  53. # These are not part of the spec, but are used by jdt.ls
  54. 'annotation': 'Macro',
  55. }
  56. REPORTED_MISSING_TYPES = set()
  57. def Initialise():
  58. if vimsupport.VimIsNeovim():
  59. return
  60. props = tp.GetTextPropertyTypes()
  61. if 'YCM_HL_UNKNOWN' not in props:
  62. tp.AddTextPropertyType( 'YCM_HL_UNKNOWN',
  63. highlight = 'WarningMsg',
  64. priority = 0 )
  65. for token_type, group in HIGHLIGHT_GROUP.items():
  66. prop = f'YCM_HL_{ token_type }'
  67. if prop not in props and vimsupport.GetIntValue(
  68. f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
  69. tp.AddTextPropertyType( prop,
  70. highlight = group,
  71. priority = 0 )
  72. # "arbitrary" base id
  73. NEXT_TEXT_PROP_ID = 70784
  74. def NextPropID():
  75. global NEXT_TEXT_PROP_ID
  76. try:
  77. return NEXT_TEXT_PROP_ID
  78. finally:
  79. NEXT_TEXT_PROP_ID += 1
  80. class SemanticHighlighting( sr.ScrollingBufferRange ):
  81. """Stores the semantic highlighting state for a Vim buffer"""
  82. def __init__( self, bufnr ):
  83. self._prop_id = NextPropID()
  84. super().__init__( bufnr )
  85. def _NewRequest( self, request_range ):
  86. request: dict = BuildRequestData( self._bufnr )
  87. request[ 'range' ] = request_range
  88. return SemanticTokensRequest( request )
  89. def _Draw( self ):
  90. # We requested a snapshot
  91. tokens = self._latest_response.get( 'tokens', [] )
  92. prev_prop_id = self._prop_id
  93. self._prop_id = NextPropID()
  94. for token in tokens:
  95. prop_type = f"YCM_HL_{ token[ 'type' ] }"
  96. rng = token[ 'range' ]
  97. self.GrowRangeIfNeeded( rng )
  98. try:
  99. tp.AddTextProperty( self._bufnr, self._prop_id, prop_type, rng )
  100. except vim.error as e:
  101. if 'E971:' in str( e ): # Text property doesn't exist
  102. if token[ 'type' ] not in REPORTED_MISSING_TYPES:
  103. REPORTED_MISSING_TYPES.add( token[ 'type' ] )
  104. vimsupport.PostVimMessage(
  105. f"Token type { token[ 'type' ] } not supported. "
  106. f"Define property type { prop_type }. "
  107. f"See :help youcompleteme-customising-highlight-groups" )
  108. else:
  109. raise e
  110. tp.ClearTextProperties( self._bufnr, prop_id = prev_prop_id )