Skip to content
← Back

src/analysis/definition_virtuality_merger.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use Logging
5
use Collections
6
7
// Folds the two accessor carriers a readable-and-assignable property
8
// produces into a single hint. Both accessors are declared at the
9
// property's own name, so both carriers land at one column; left
10
// alone they would stack two markers there.
11
//
12
// The merged label is the union of the two halves' glyphs, in the
13
// pass's own order, and the hover keeps both halves' notes with the
14
// getter's first — so a property that overrides on one half and is
15
// overridden on the other still says which half did which.
16
//
17
// Anything else — a method carrier, a type carrier, a narrowing
18
// inlay, a kill hint — passes through untouched.
19
class DEFINITION_VIRTUALITY_MERGER is
20
_GET_CODE: string static => "{Syntax.Process.DEFINITION_VIRTUALITY.CODE}-get"
21
_SET_CODE: string static => "{Syntax.Process.DEFINITION_VIRTUALITY.CODE}-set"
22
23
// The pass's emission order, so a union of two halves' glyphs
24
// reads the same way a single half's does.
25
_ORDER: string static =>
26
"{Syntax.Process.DEFINITION_VIRTUALITY.ABSTRACT}"
27
"{Syntax.Process.DEFINITION_VIRTUALITY.OVERRIDES}"
28
"{Syntax.Process.DEFINITION_VIRTUALITY.OVERRIDDEN}"
29
"{Syntax.Process.DEFINITION_VIRTUALITY.OPEN}"
30
31
init() is si
32
33
merge(carriers: Iterable[INLAY]) -> LIST[INLAY] is
34
let groups = MAP[string, LIST[INLAY]]()
35
let order = LIST[string]()
36
37
let other_index mut = 0
38
39
for c in carriers do
40
let key: string mut = ""
41
42
if _is_accessor(c) then
43
key = "A\t{c.location.file_name}\t{c.location.start_line}\t{c.location.start_column}"
44
else
45
key = "X\t{other_index}"
46
other_index = other_index + 1
47
fi
48
49
if !groups.contains_key(key) then
50
order.add(key)
51
groups[key] = LIST[INLAY]()
52
fi
53
54
groups[key].add(c)
55
od
56
57
let result = LIST[INLAY]()
58
59
for key in order do
60
let group = groups[key]
61
62
if group.count > 1 then
63
result.add(_render(group))
64
else
65
for c in group do
66
result.add(c)
67
od
68
fi
69
od
70
71
return result
72
si
73
74
_render(group: LIST[INLAY]) -> INLAY is
75
let head = group[0]
76
77
let glyphs = System.Text.StringBuilder()
78
79
for g in _ORDER do
80
let present mut = false
81
82
for c in group do
83
if c.text.contains(g) then
84
present = true
85
fi
86
od
87
88
if present then
89
glyphs.append(g)
90
fi
91
od
92
93
// The subject line is the property's name, which both halves
94
// carry, so it is taken once from whichever came first.
95
let body = System.Text.StringBuilder()
96
97
body.append(_subject(head))
98
99
for c in _get_first(group) do
100
body.append("\n\n").append(_notes(c))
101
od
102
103
return INLAY(
104
head.is_analysis,
105
head.is_compile_expressions,
106
head.location,
107
Syntax.Process.DEFINITION_VIRTUALITY.CODE,
108
"{glyphs}",
109
"{body}"
110
)
111
si
112
113
// The getter's notes first, whatever order the carriers arrived
114
// in: a property reads before it is assigned.
115
_get_first(group: LIST[INLAY]) -> LIST[INLAY] is
116
let ordered = LIST[INLAY]()
117
118
for c in group do
119
if c.code? /\ c.code =~ _GET_CODE then
120
ordered.add(c)
121
fi
122
od
123
124
for c in group do
125
if !c.code? \/ c.code !~ _GET_CODE then
126
ordered.add(c)
127
fi
128
od
129
130
return ordered
131
si
132
133
_subject(c: INLAY) -> string is
134
let detail = c.detail
135
136
if !detail? then
137
return ""
138
fi
139
140
let separator = detail.index_of("\n\n")
141
142
return if separator >= 0 then detail.substring(0, separator) else detail fi
143
si
144
145
_notes(c: INLAY) -> string is
146
let detail = c.detail
147
148
if !detail? then
149
return ""
150
fi
151
152
let separator = detail.index_of("\n\n")
153
154
return if separator >= 0 then detail.substring(separator + 2) else "" fi
155
si
156
157
_is_accessor(c: INLAY) -> bool =>
158
c.code? /\ (c.code =~ _GET_CODE \/ c.code =~ _SET_CODE)
159
si
160
si