Skip to content

Commit 724218e

Browse files
committed
Grow the requested range based on what we receive
This means that if the servers return a wider range of semantic tokens or inlay hints than we requested, we don't constantly re-request them whenever the viewport moves. Importantly, clangd doesn't support range tokens, so we previously requested and re-parsed, deleted and re-added every single token on every single scroll event. This meant that scrolling was very slow and lots of wasted effort. This should help reduce that. We should essentially re-use the tokens on scolling and pretty much never re-request them unless the buffer changes or we have a file-ready-to-parse.
1 parent 25296a6 commit 724218e

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

python/ycm/inlay_hints.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ def _Draw( self ):
8585
else:
8686
prop_type = 'YCM_INLAY_' + inlay_hint[ 'kind' ]
8787

88+
self.GrowRangeIfNeeded( {
89+
'start': inlay_hint[ 'position' ],
90+
'end': {
91+
'line_num': inlay_hint[ 'position' ][ 'line_num' ],
92+
'column_num': inlay_hint[ 'position' ][ 'column_num' ] + len(
93+
inlay_hint[ 'label' ] )
94+
}
95+
} )
96+
8897
if inlay_hint.get( 'paddingLeft', False ):
8998
tp.AddTextProperty( self._bufnr,
9099
None,

python/ycm/scrolling_range.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ def Refresh( self ):
9999
self._Draw()
100100

101101

102+
def GrowRangeIfNeeded( self, rng ):
103+
"""When processing results, we may receive a wider range than requested. In
104+
that case, grow our 'last requested' range to minimise requesting more
105+
frequently than we need to."""
106+
# Note: references (pointers) so no need to re-assign
107+
rmin = self._last_requested_range[ 'start' ]
108+
rmax = self._last_requested_range[ 'end' ]
109+
110+
start = rng[ 'start' ]
111+
end = rng[ 'end' ]
112+
113+
if rmin[ 'line_num' ] is None or start[ 'line_num' ] < rmin[ 'line_num' ]:
114+
rmin[ 'line_num' ] = start[ 'line_num' ]
115+
rmin[ 'column_num' ] = start[ 'column_num' ]
116+
elif start[ 'line_num' ] == rmin[ 'line_num' ]:
117+
rmin[ 'column_num' ] = min( start[ 'column_num' ],
118+
rmin[ 'column_num' ] )
119+
120+
if rmax[ 'line_num' ] is None or end[ 'line_num' ] > rmax[ 'line_num' ]:
121+
rmax[ 'line_num' ] = end[ 'line_num' ]
122+
rmax[ 'column_num' ] = end[ 'column_num' ]
123+
elif end[ 'line_num' ] == rmax[ 'line_num' ]:
124+
rmax[ 'column_num' ] = max( end[ 'column_num' ], rmax[ 'column_num' ] )
125+
126+
102127
# API; just implement the following, using self._bufnr and
103128
# self._latest_response as required
104129

python/ycm/semantic_highlighting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ def _Draw( self ):
114114
f"Missing property type for { token[ 'type' ] }" )
115115
continue
116116
prop_type = f"YCM_HL_{ token[ 'type' ] }"
117-
tp.AddTextProperty( self._bufnr,
118-
self._prop_id,
119-
prop_type,
120-
token[ 'range' ] )
117+
118+
rng = token[ 'range' ]
119+
self.GrowRangeIfNeeded( rng )
120+
tp.AddTextProperty( self._bufnr, self._prop_id, prop_type, rng )
121121

122122
tp.ClearTextProperties( self._bufnr, prop_id = prev_prop_id )

0 commit comments

Comments
 (0)