Skip to content
← Back

src/analysis/narrowing_inlay_merger.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use Logging
5
use Collections
6
7
// Merges editor-only narrowing-introduction inlays that share a source
8
// location and construct into a single hint. The stored carriers hold
9
// only the narrowed-to type in `detail`; the hover body is assembled
10
// here so a true/false edge pair renders as one marker rather than two
11
// stacked at one column. The body is presentation-neutral text — one
12
// `► <type>` line for the matched narrow, plus a `> <type>` line for
13
// the complementary else narrow when there is one. A client wraps it
14
// for display (the VS Code hover fences it as a ghul code block so the
15
// types syntax-highlight and the sigils stay neutral).
16
//
17
// The matched edge carries the bare `narrowing-<kind>` slug; the
18
// complementary edge carries `narrowing-<kind>-complement`, at the same
19
// location. Kill hints (a different sigil) and any other carrier pass
20
// through unchanged.
21
class NARROWING_INLAY_MERGER is
22
init() is si
23
24
merge(carriers: Iterable[INLAY]) -> LIST[INLAY] is
25
let groups = MAP[string, LIST[INLAY]]()
26
let order = LIST[string]()
27
28
let other_index mut = 0
29
30
for c in carriers do
31
let key: string mut = ""
32
33
if _is_introduction(c) then
34
key = "I\t{c.location.start_line}\t{c.location.start_column}\t{_base_code(c.code)}"
35
else
36
key = "X\t{other_index}"
37
other_index = other_index + 1
38
fi
39
40
if !groups.contains_key(key) then
41
order.add(key)
42
groups[key] = LIST[INLAY]()
43
fi
44
45
groups[key].add(c)
46
od
47
48
let result = LIST[INLAY]()
49
50
for key in order do
51
let group = groups[key]
52
53
if group.count > 0 /\ _is_introduction(group[0]) then
54
result.add(_render(group))
55
else
56
for c in group do
57
result.add(c)
58
od
59
fi
60
od
61
62
return result
63
si
64
65
_render(group: LIST[INLAY]) -> INLAY is
66
let head = group[0]
67
let base = _base_code(head.code)
68
69
let true_detail mut = ""
70
let false_detail: string? mut = null
71
72
for g in group do
73
if _is_complement(g.code) then
74
false_detail = g.detail
75
else
76
true_detail = g.detail ?? ""
77
fi
78
od
79
80
let body = System.Text.StringBuilder()
81
82
body.append("► ").append(_body_text(true_detail))
83
84
if false_detail? then
85
body.append("\n> ").append(_body_text(false_detail))
86
fi
87
88
return INLAY(
89
head.is_analysis,
90
head.is_compile_expressions,
91
head.location,
92
base,
93
head.text,
94
body.to_string()
95
)
96
si
97
98
// A path narrow whose static type couldn't be resolved stores an
99
// empty type; fall back to a bare phrase so the line is not left
100
// dangling after its sigil.
101
_body_text(detail: string?) -> string =>
102
if detail? /\ detail.length > 0 then detail else "holds a value" fi
103
104
_is_introduction(c: INLAY) -> bool =>
105
c.text =~ "►"
106
107
_is_complement(code: string?) -> bool =>
108
code? /\ code.ends_with("-complement")
109
110
_base_code(code: string?) -> string =>
111
if !code? then
112
""
113
elif code.ends_with("-complement") then
114
code.substring(0, code.length - "-complement".length)
115
else
116
code
117
fi
118
si
119
si