Skip to content
← Back

src/analysis/inlay_range.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use Logging
5
use Protocol = Analysis.Protocol
6
7
use Collections.Iterable
8
use Ghul.Pipes
9
10
// Limits inlay hints to the range the client asked about - the
11
// editor's viewport - so a kind with a hint on nearly every line does
12
// not pay encode, transport and parse for a whole file. Any range
13
// field absent on the request means whole file, so an older client's
14
// frame is served exactly as before.
15
//
16
// The filter runs before the mergers, which is sound only because
17
// both group on exact position: a range either contains a whole
18
// group or none of it. A merger that grouped by enclosing scope
19
// instead could see a group cut in half by this filter - the
20
// property is pinned by the unit tests.
21
class INLAY_RANGE is
22
init() is si
23
24
filter(inlays: Iterable[Logging.INLAY], request: Protocol.Request.INLAY_HINTS) -> Collections.List[Logging.INLAY] static is
25
let range = bounds(request)
26
27
if !range? then
28
return inlays |> collect()
29
fi
30
31
let (start_line, start_column, end_line, end_column) = range
32
33
return inlays |> filter(i => within(i, start_line, start_column, end_line, end_column)) |> collect()
34
si
35
36
// A hint's position is inside the range, comparing line then
37
// column. Start-inclusive, end-exclusive, so a hint sitting at
38
// the end position belongs to the next viewport rather than
39
// flickering between two.
40
within(
41
inlay: Logging.INLAY,
42
start_line: int,
43
start_column: int,
44
end_line: int,
45
end_column: int
46
) -> bool static is
47
let line = inlay.location.start_line
48
let column = inlay.location.start_column
49
50
return (line > start_line \/ (line == start_line /\ column >= start_column)) /\
51
(line < end_line \/ (line == end_line /\ column < end_column))
52
si
53
54
// The request's range with absent fields defaulted inwards, so a
55
// partially-specified range behaves as the whole file from that
56
// edge. Null when no range was given at all. The defaults stay
57
// inside what LOCATION's 12-bit packed form can hold: the
58
// terminator boundaries compare as packed ints, and a default
59
// that overflowed the packing would silently match nothing.
60
bounds(request: Protocol.Request.INLAY_HINTS) -> (int, int, int, int)? static is
61
if !request.start_line? then
62
return null
63
fi
64
65
return (
66
request.start_line ?? 1,
67
request.start_column ?? 1,
68
request.end_line ?? _MAX_LINES,
69
request.end_column ?? _MAX_COLUMNS
70
)
71
si
72
73
_MAX_LINES: int static => 524_287
74
_MAX_COLUMNS: int static => 4_095
75
si
76
si