File: inlay_hints.py

package info (click to toggle)
vim-youcompleteme 0%2B20230109%2Bgit7620d87%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,404 kB
  • sloc: python: 10,569; sh: 203; cpp: 121; makefile: 24; f90: 5; xml: 1
file content (192 lines) | stat: -rw-r--r-- 6,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright (C) 2022, YouCompleteMe Contributors
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe.  If not, see <http://www.gnu.org/licenses/>.


from ycm.client.inlay_hints_request import InlayHintsRequest
from ycm.client.base_request import BuildRequestData
from ycm import vimsupport
from ycm import text_properties as tp


HIGHLIGHT_GROUP = {
  'Type':      'YcmInlayHint',
  'Parameter': 'YcmInlayHint',
  'Enum':      'YcmInlayHint',
}
REPORTED_MISSING_TYPES = set()


def Initialise():
  if not vimsupport.VimSupportsVirtualText():
    return False

  props = tp.GetTextPropertyTypes()
  if 'YCM_INLAY_UNKNOWN' not in props:
    tp.AddTextPropertyType( 'YCM_INLAY_UNKNOWN',
                            highlight = 'YcmInlayHint',
                            start_incl = 1 )
  if 'YCM_INLAY_PADDING' not in props:
    tp.AddTextPropertyType( 'YCM_INLAY_PADDING',
                            highlight = 'YcmInvisible',
                            start_incl = 1 )

  for token_type, group in HIGHLIGHT_GROUP.items():
    prop = f'YCM_INLAY_{ token_type }'
    if prop not in props and vimsupport.GetIntValue(
        f"hlexists( '{ vimsupport.EscapeForVim( group ) }' )" ):
      tp.AddTextPropertyType( prop,
                              highlight = group,
                              start_incl = 1 )

  return True


class InlayHints:
  """Stores the inlay hints state for a Vim buffer"""

  # FIXME: Send a request per-disjoint range for this buffer rather than the
  # maximal range. then collaate the results when all responses are returned
  def __init__( self, bufnr, user_options ):
    self._request = None
    self._bufnr = bufnr
    self.tick = -1
    self._latest_inlay_hints = []
    self._last_requested_range = None


  def Request( self, force=False ):
    if self._request and not self.Ready():
      return True

    # Check to see if the buffer ranges would actually change anything visible.
    # This avoids a round-trip for every single line scroll event
    if ( not force and
         self.tick == vimsupport.GetBufferChangedTick( self._bufnr ) and
         vimsupport.VisibleRangeOfBufferOverlaps(
           self._bufnr,
           self._last_requested_range ) ):
      return False # don't poll

    # We're requesting changes, so the existing results are now invalid
    self._latest_inlay_hints = []
    # FIXME: This call is duplicated in the call to VisibleRangeOfBufferOverlaps
    #  - remove the expansion param
    #  - look up the actual visible range, then call this function
    #  - if not overlapping, do the factor expansion and request
    self._last_requested_range = vimsupport.RangeVisibleInBuffer( self._bufnr )
    self.tick = vimsupport.GetBufferChangedTick( self._bufnr )

    request_data = BuildRequestData( self._bufnr )
    request_data.update( {
      'range': self._last_requested_range
    } )
    self._request = InlayHintsRequest( request_data )
    self._request.Start()
    return True


  def Ready( self ):
    return self._request is not None and self._request.Done()


  def Clear( self ):
    # ClearTextProperties is slow as it must scan the whole buffer
    # we shouldn't use _last_requested_range, because the server is free to
    # return a larger range, so we pick the first/last from the latest results
    types = [ 'YCM_INLAY_UNKNOWN', 'YCM_INLAY_PADDING' ] + [
      f'YCM_INLAY_{ prop_type }' for prop_type in HIGHLIGHT_GROUP.keys()
    ]

    tp.ClearTextProperties( self._bufnr, prop_types = types )

  def Update( self ):
    if not self._request:
      # Nothing to update
      return True

    assert self.Ready()

    # We're ready to use this response. Clear it (to avoid repeatedly
    # re-polling).
    self._latest_inlay_hints = self._request.Response()
    self._request = None

    if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
      # Buffer has changed, we should ignore the data and retry
      self.Request( force=True )
      return False # poll again

    self._Draw()

    # No need to re-poll
    return True


  def Refresh( self ):
    if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
      # stale data
      return

    if self._request is not None:
      # request in progress; we''l handle refreshing when it's done.
      return

    self._Draw()


  def _Draw( self ):
    self.Clear()

    for inlay_hint in self._latest_inlay_hints:
      if 'kind' not in inlay_hint:
        prop_type = 'YCM_INLAY_UNKNOWN'
      elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
        prop_type = 'YCM_INLAY_UNKNOWN'
      else:
        prop_type = 'YCM_INLAY_' + inlay_hint[ 'kind' ]

      if inlay_hint.get( 'paddingLeft', False ):
        tp.AddTextProperty( self._bufnr,
                            None,
                            'YCM_INLAY_PADDING',
                            {
                              'start': inlay_hint[ 'position' ],
                            },
                            {
                              'text': ' '
                            } )

      tp.AddTextProperty( self._bufnr,
                          None,
                          prop_type,
                          {
                            'start': inlay_hint[ 'position' ],
                          },
                          {
                            'text': inlay_hint[ 'label' ]
                          } )

      if inlay_hint.get( 'paddingRight', False ):
        tp.AddTextProperty( self._bufnr,
                            None,
                            'YCM_INLAY_PADDING',
                            {
                              'start': inlay_hint[ 'position' ],
                            },
                            {
                              'text': ' '
                            } )