-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsTypeHintsPresentationFactory.kt
288 lines (248 loc) · 11.4 KB
/
RsTypeHintsPresentationFactory.kt
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.hints
import com.intellij.codeInsight.hints.presentation.InlayPresentation
import com.intellij.codeInsight.hints.presentation.PresentationFactory
import org.rust.ide.presentation.shortPresentableText
import org.rust.lang.core.psi.RsTraitItem
import org.rust.lang.core.psi.RsTypeAlias
import org.rust.lang.core.psi.RsTypeParameter
import org.rust.lang.core.psi.ext.typeParameters
import org.rust.lang.core.types.BoundElement
import org.rust.lang.core.types.Kind
import org.rust.lang.core.types.consts.CtConstParameter
import org.rust.lang.core.types.consts.CtValue
import org.rust.lang.core.types.ty.*
import org.rust.lang.core.types.type
class RsTypeHintsPresentationFactory(private val factory: PresentationFactory, private val showObviousTypes: Boolean) {
fun typeHint(type: Ty): InlayPresentation = factory.roundWithBackground(
listOf(text(": "), hint(type, 1)).join()
)
private fun hint(kind: Kind, level: Int): InlayPresentation = when (kind) {
is TyTuple -> tupleTypeHint(kind, level)
is TyAdt -> adtTypeHint(kind, level)
is TyFunction -> functionTypeHint(kind, level)
is TyReference -> referenceTypeHint(kind, level)
is TyPointer -> pointerTypeHint(kind, level)
is TyProjection -> projectionTypeHint(kind, level)
is TyTypeParameter -> typeParameterTypeHint(kind)
is TyArray -> arrayTypeHint(kind, level)
is TySlice -> sliceTypeHint(kind, level)
is TyTraitObject -> traitObjectTypeHint(kind, level)
is TyAnon -> anonTypeHint(kind, level)
is CtConstParameter -> constParameterTypeHint(kind)
is CtValue -> text(kind.expr.toString())
is Ty -> text(kind.shortPresentableText)
else -> text(null)
}
private fun functionTypeHint(type: TyFunction, level: Int): InlayPresentation {
val parameters = type.paramTypes
val returnType = type.retType
val startWithPlaceholder = checkSize(level, parameters.size + 1)
val fn = if (parameters.isEmpty()) {
text("fn()")
} else {
factory.collapsible(
prefix = text("fn("),
collapsed = text(PLACEHOLDER),
expanded = { parametersHint(parameters, level + 1) },
suffix = text(")"),
startWithPlaceholder = startWithPlaceholder
)
}
if (returnType != TyUnit) {
val ret = factory.collapsible(
prefix = text(" → "),
collapsed = text(PLACEHOLDER),
expanded = { hint(returnType, level + 1) },
suffix = text(""),
startWithPlaceholder = startWithPlaceholder
)
return factory.seq(fn, ret)
}
return fn
}
private fun tupleTypeHint(type: TyTuple, level: Int): InlayPresentation =
factory.collapsible(
prefix = text("("),
collapsed = text(PLACEHOLDER),
expanded = { parametersHint(type.types, level + 1) },
suffix = text(")"),
startWithPlaceholder = checkSize(level, type.types.size)
)
private fun adtTypeHint(type: TyAdt, level: Int): InlayPresentation {
val aliasedBy = type.aliasedBy
val alias = aliasedBy?.element
val adtName = alias?.name ?: type.item.name
val typeDeclaration = alias ?: type.item
val typeNamePresentation = factory.psiSingleReference(text(adtName)) { typeDeclaration }
val typeArguments = alias?.typeParameters?.map { (aliasedBy.subst[it] ?: TyUnknown) to it }
?: type.typeArguments.zip(type.item.typeParameters)
val userVisibleKindArguments = mutableListOf<Kind>()
for ((argument, parameter) in typeArguments) {
if (!showObviousTypes && isDefaultTypeParameter(argument, parameter)) {
// don't show default types
continue
}
userVisibleKindArguments.add(argument)
}
for (argument in type.constArguments) {
userVisibleKindArguments.add(argument)
}
if (userVisibleKindArguments.isNotEmpty()) {
val collapsible = factory.collapsible(
prefix = text("<"),
collapsed = text(PLACEHOLDER),
expanded = { parametersHint(userVisibleKindArguments, level + 1) },
suffix = text(">"),
startWithPlaceholder = checkSize(level, userVisibleKindArguments.size)
)
return listOf(typeNamePresentation, collapsible).join()
}
return typeNamePresentation
}
private fun referenceTypeHint(type: TyReference, level: Int): InlayPresentation = listOf(
text("&" + if (type.mutability.isMut) "mut " else ""),
hint(type.referenced, level) // level is not incremented intentionally
).join()
private fun pointerTypeHint(type: TyPointer, level: Int): InlayPresentation = listOf(
text("*" + if (type.mutability.isMut) "mut " else "const "),
hint(type.referenced, level) // level is not incremented intentionally
).join()
private fun projectionTypeHint(type: TyProjection, level: Int): InlayPresentation {
val collapsible = factory.collapsible(
prefix = text("<"),
collapsed = text(PLACEHOLDER),
expanded = {
val typePresentation = hint(type.type, level + 1)
val traitPresentation = traitItemTypeHint(type.trait, level + 1, false)
listOf(typePresentation, traitPresentation).join(" as ")
},
suffix = text(">"),
startWithPlaceholder = checkSize(level, 2)
)
val targetDeclaration = type.target
val targetPresentation = factory.psiSingleReference(text(targetDeclaration.name)) { targetDeclaration }
return listOf(collapsible, targetPresentation).join("::")
}
private fun typeParameterTypeHint(type: TyTypeParameter): InlayPresentation {
val parameter = type.parameter
if (parameter is TyTypeParameter.Named) {
return factory.psiSingleReference(text(parameter.name)) { parameter.parameter }
}
return text(parameter.name)
}
private fun constParameterTypeHint(const: CtConstParameter): InlayPresentation =
factory.psiSingleReference(text(const.parameter.name)) { const.parameter }
private fun arrayTypeHint(type: TyArray, level: Int): InlayPresentation =
factory.collapsible(
prefix = text("["),
collapsed = text(PLACEHOLDER),
expanded = {
val basePresentation = hint(type.base, level + 1)
val sizePresentation = text(type.size?.toString())
listOf(basePresentation, sizePresentation).join("; ")
},
suffix = text("]"),
startWithPlaceholder = checkSize(level, 1)
)
private fun sliceTypeHint(type: TySlice, level: Int): InlayPresentation =
factory.collapsible(
prefix = text("["),
collapsed = text(PLACEHOLDER),
expanded = { hint(type.elementType, level + 1) },
suffix = text("]"),
startWithPlaceholder = checkSize(level, 1)
)
private fun traitObjectTypeHint(type: TyTraitObject, level: Int): InlayPresentation =
factory.collapsible(
prefix = text("dyn "),
collapsed = text(PLACEHOLDER),
expanded = { traitItemTypeHint(type.trait, level, true) },
suffix = text(""),
startWithPlaceholder = checkSize(level, 1)
)
private fun anonTypeHint(type: TyAnon, level: Int): InlayPresentation =
factory.collapsible(
prefix = text("impl "),
collapsed = text(PLACEHOLDER),
expanded = { type.traits.map { traitItemTypeHint(it, level + 1, true) }.join("+") },
suffix = text(""),
startWithPlaceholder = checkSize(level, type.traits.size)
)
private fun parametersHint(kinds: List<Kind>, level: Int): InlayPresentation =
kinds.map { hint(it, level) }.join(", ")
private fun traitItemTypeHint(
trait: BoundElement<RsTraitItem>,
level: Int,
includeAssoc: Boolean
): InlayPresentation {
val traitPresentation = factory.psiSingleReference(text(trait.element.name)) { trait.element }
val typeParametersPresentations = mutableListOf<InlayPresentation>()
for (parameter in trait.element.typeParameters) {
val argument = trait.subst[parameter] ?: continue
if (!showObviousTypes && isDefaultTypeParameter(argument, parameter)) {
// don't show default types
continue
}
val parameterPresentation = hint(argument, level + 1)
typeParametersPresentations.add(parameterPresentation)
}
val assocTypesPresentations = mutableListOf<InlayPresentation>()
if (includeAssoc) {
for (alias in trait.element.associatedTypesTransitively) {
val aliasName = alias.name ?: continue
val type = trait.assoc[alias] ?: continue
if (!showObviousTypes && isDefaultTypeAlias(type, alias)) {
// don't show default types
continue
}
val aliasPresentation = factory.psiSingleReference(text(aliasName)) { alias }
val presentation = listOf(aliasPresentation, text("="), hint(type, level + 1)).join()
assocTypesPresentations.add(presentation)
}
}
val innerPresentations = typeParametersPresentations + assocTypesPresentations
return if (innerPresentations.isEmpty()) {
traitPresentation
} else {
val expanded = innerPresentations.join(", ")
val traitTypesPresentation = factory.collapsible(
prefix = text("<"),
collapsed = text(PLACEHOLDER),
expanded = { expanded },
suffix = text(">"),
startWithPlaceholder = checkSize(level, innerPresentations.size)
)
listOf(traitPresentation, traitTypesPresentation).join()
}
}
private fun checkSize(level: Int, elementsCount: Int): Boolean =
level + elementsCount > FOLDING_THRESHOLD
private fun isDefaultTypeParameter(argument: Ty, parameter: RsTypeParameter): Boolean =
argument == parameter.typeReference?.type
private fun isDefaultTypeAlias(argument: Ty, alias: RsTypeAlias): Boolean =
argument == alias.typeReference?.type
private fun List<InlayPresentation>.join(separator: String = ""): InlayPresentation {
if (separator.isEmpty()) {
return factory.seq(*toTypedArray())
}
val presentations = mutableListOf<InlayPresentation>()
var first = true
for (presentation in this) {
if (!first) {
presentations.add(text(separator))
}
presentations.add(presentation)
first = false
}
return factory.seq(*presentations.toTypedArray())
}
private fun text(text: String?): InlayPresentation = factory.smallText(text ?: "?")
companion object {
private const val PLACEHOLDER: String = "…"
private const val FOLDING_THRESHOLD: Int = 3
}
}