-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsPsiRenderer.kt
822 lines (756 loc) · 30.5 KB
/
RsPsiRenderer.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.presentation
import org.rust.ide.utils.import.ImportCandidate
import org.rust.ide.utils.import.ImportCandidatesCollector2
import org.rust.ide.utils.import.ImportContext2
import org.rust.lang.core.parser.RustParserUtil
import org.rust.lang.core.psi.*
import org.rust.lang.core.psi.ext.*
import org.rust.lang.core.resolve.*
import org.rust.lang.core.stubs.RsStubLiteralKind
import org.rust.lang.core.types.RsPsiSubstitution
import org.rust.lang.core.types.Substitution
import org.rust.lang.core.types.consts.CtConstParameter
import org.rust.lang.core.types.consts.CtValue
import org.rust.lang.core.types.emptySubstitution
import org.rust.lang.core.types.infer.resolve
import org.rust.lang.core.types.infer.substitute
import org.rust.lang.core.types.regions.ReEarlyBound
import org.rust.lang.core.types.ty.Ty
import org.rust.lang.core.types.ty.TyInteger
import org.rust.lang.core.types.ty.TyPrimitive
import org.rust.lang.core.types.ty.TyTypeParameter
import org.rust.lang.core.types.type
import org.rust.lang.utils.escapeRust
import org.rust.lang.utils.evaluation.evaluate
import org.rust.stdext.exhaustive
import org.rust.stdext.joinToWithBuffer
/** Return text of the element without switching to AST (loses non-stubbed parts of PSI) */
fun RsTypeReference.getStubOnlyText(
subst: Substitution = emptySubstitution,
renderLifetimes: Boolean = true
): String = TypeSubstitutingPsiRenderer(PsiRenderingOptions(renderLifetimes), subst).renderTypeReference(this)
/** Return text of the element without switching to AST (loses non-stubbed parts of PSI) */
fun RsValueParameterList.getStubOnlyText(
subst: Substitution = emptySubstitution,
renderLifetimes: Boolean = true
): String = TypeSubstitutingPsiRenderer(PsiRenderingOptions(renderLifetimes), subst).renderValueParameterList(this)
/** Return text of the element without switching to AST (loses non-stubbed parts of PSI) */
fun RsTraitRef.getStubOnlyText(subst: Substitution = emptySubstitution, renderLifetimes: Boolean = true): String =
buildString { TypeSubstitutingPsiRenderer(PsiRenderingOptions(renderLifetimes), subst).appendPath(this, path) }
fun RsPsiRenderer.renderTypeReference(ref: RsTypeReference): String =
buildString { appendTypeReference(this, ref) }
fun RsPsiRenderer.renderTraitRef(ref: RsTraitRef): String =
buildString { appendPath(this, ref.path) }
fun RsPsiRenderer.renderValueParameterList(list: RsValueParameterList): String =
buildString { appendValueParameterList(this, list) }
fun RsPsiRenderer.renderFunctionSignature(fn: RsFunction): String =
buildString { appendFunctionSignature(this, fn) }
fun RsPsiRenderer.renderTypeAliasSignature(ta: RsTypeAlias, renderBounds: Boolean): String =
buildString { appendTypeAliasSignature(this, ta, renderBounds) }
data class PsiRenderingOptions(
val renderLifetimes: Boolean = true,
/** Related to [RsPsiRenderer.appendFunctionSignature] */
val renderGenericsAndWhere: Boolean = true,
/** if `true`, renders `Bar` instead of `foo::Bar` */
val shortPaths: Boolean = true,
)
@Suppress("MemberVisibilityCanBePrivate", "DuplicatedCode")
open class RsPsiRenderer(
protected val options: PsiRenderingOptions
) {
protected val renderLifetimes: Boolean get() = options.renderLifetimes
protected val renderGenericsAndWhere: Boolean get() = options.renderGenericsAndWhere
protected val shortPaths: Boolean get() = options.shortPaths
open fun appendFunctionSignature(sb: StringBuilder, fn: RsFunction) {
if (fn.isAsync) {
sb.append("async ")
}
if (fn.isConst) {
sb.append("const ")
}
if (fn.isUnsafe) {
sb.append("unsafe ")
}
if (fn.isActuallyExtern) {
sb.append("extern ")
val abiName = fn.abiName
if (abiName != null) {
sb.append("\"")
sb.append(abiName)
sb.append("\" ")
}
}
sb.append("fn ")
sb.append(fn.escapedName ?: "")
val typeParameterList = fn.typeParameterList
if (typeParameterList != null && renderGenericsAndWhere) {
appendTypeParameterList(sb, typeParameterList)
}
val valueParameterList = fn.valueParameterList
if (valueParameterList != null) {
appendValueParameterList(sb, valueParameterList)
}
val retType = fn.retType
if (retType != null) {
sb.append(" -> ")
val retTypeReference = retType.typeReference
if (retTypeReference != null) {
appendTypeReference(sb, retTypeReference)
}
}
val whereClause = fn.whereClause
if (whereClause != null && renderGenericsAndWhere) {
appendWhereClause(sb, whereClause)
}
}
open fun appendTypeAliasSignature(sb: StringBuilder, ta: RsTypeAlias, renderBounds: Boolean) {
sb.append("type ")
sb.append(ta.escapedName ?: "")
val typeParameterList = ta.typeParameterList
if (typeParameterList != null && renderGenericsAndWhere) {
appendTypeParameterList(sb, typeParameterList)
}
val whereClause = ta.whereClause
if (whereClause != null && renderGenericsAndWhere) {
appendWhereClause(sb, whereClause)
}
val typeParamBounds = ta.typeParamBounds
if (typeParamBounds != null && renderBounds) {
appendTypeParamBounds(sb, typeParamBounds)
}
}
private fun appendWherePred(sb: StringBuilder, pred: RsWherePred) {
val lifetime = pred.lifetime
val type = pred.typeReference
if (lifetime != null) {
sb.append(lifetime.name)
val bounds = pred.lifetimeParamBounds
if (bounds != null) {
appendLifetimeBounds(sb, bounds)
}
} else if (type != null) {
val forLifetimes = pred.forLifetimes
if (renderLifetimes && forLifetimes != null) {
appendForLifetimes(sb, forLifetimes)
}
appendTypeReference(sb, type)
val typeParamBounds = pred.typeParamBounds
if (typeParamBounds != null) {
appendTypeParamBounds(sb, typeParamBounds)
}
}
}
private fun appendWhereClause(sb: StringBuilder, whereClause: RsWhereClause) {
sb.append(" where ")
whereClause.wherePredList.joinToWithBuffer(sb, separator = ", ") {
appendWherePred(sb, this)
}
}
private fun appendTypeParamBounds(sb: StringBuilder, bounds: RsTypeParamBounds) {
sb.append(": ")
bounds.polyboundList.joinToWithBuffer(sb, " + ") {
appendPolybound(sb, this)
}
}
open fun appendTypeParameterList(sb: StringBuilder, list: RsTypeParameterList) {
sb.append("<")
list.stubChildrenOfType<RsElement>().joinToWithBuffer(sb, separator = ", ") {
when (this) {
is RsLifetimeParameter -> {
sb.append(name)
val bounds = lifetimeParamBounds
if (bounds != null) {
appendLifetimeBounds(sb, bounds)
}
}
is RsTypeParameter -> {
sb.append(name)
val bounds = typeParamBounds
if (bounds != null) {
sb.append(": ")
bounds.polyboundList.joinToWithBuffer(sb, " + ") {
appendPolybound(sb, this)
}
}
val defaultValue = typeReference
if (defaultValue != null) {
sb.append(" = ")
appendTypeReference(sb, defaultValue)
}
}
is RsConstParameter -> {
sb.append("const ")
sb.append(name ?: "_")
val type = typeReference
if (type != null) {
sb.append(": ")
appendTypeReference(sb, type)
}
}
}
}
sb.append(">")
}
private fun appendLifetimeBounds(sb: StringBuilder, bounds: RsLifetimeParamBounds) {
sb.append(": ")
bounds.lifetimeList.joinToWithBuffer(sb, separator = " + ") { it.append(name) }
}
open fun appendValueParameterList(
sb: StringBuilder,
list: RsValueParameterList
) {
sb.append("(")
val selfParameter = list.selfParameter
val valueParameterList = list.valueParameterList
if (selfParameter != null) {
appendSelfParameter(sb, selfParameter)
if (valueParameterList.isNotEmpty()) {
sb.append(", ")
}
}
valueParameterList.joinToWithBuffer(sb, separator = ", ") { sb1 ->
sb1.append(patText ?: "_")
sb1.append(": ")
val typeReference = typeReference
if (typeReference != null) {
appendTypeReference(sb1, typeReference)
} else {
sb1.append("()")
}
}
sb.append(")")
}
open fun appendSelfParameter(
sb: StringBuilder,
selfParameter: RsSelfParameter
) {
val typeReference = selfParameter.typeReference
if (typeReference != null) {
sb.append("self: ")
appendTypeReference(sb, typeReference)
} else {
if (selfParameter.isRef) {
sb.append("&")
val lifetime = selfParameter.lifetime
if (renderLifetimes && lifetime != null) {
appendLifetime(sb, lifetime)
sb.append(" ")
}
sb.append(if (selfParameter.mutability.isMut) "mut " else "")
}
sb.append("self")
}
}
open fun appendTypeReference(sb: StringBuilder, type: RsTypeReference) {
when (type) {
is RsParenType -> {
sb.append("(")
type.typeReference?.let { appendTypeReference(sb, it) }
sb.append(")")
}
is RsTupleType -> {
val types = type.typeReferenceList
if (types.size == 1) {
sb.append("(")
appendTypeReference(sb, types.single())
sb.append(",)")
} else {
types.joinToWithBuffer(sb, ", ", "(", ")") { appendTypeReference(it, this) }
}
}
is RsBaseType -> when (val kind = type.kind) {
RsBaseTypeKind.Unit -> sb.append("()")
RsBaseTypeKind.Never -> sb.append("!")
RsBaseTypeKind.Underscore -> sb.append("_")
is RsBaseTypeKind.Path -> appendPath(sb, kind.path)
}
is RsRefLikeType -> {
if (type.isPointer) {
sb.append(if (type.mutability.isMut) "*mut " else "*const ")
} else if (type.isRef) {
sb.append("&")
val lifetime = type.lifetime
if (renderLifetimes && lifetime != null) {
appendLifetime(sb, lifetime)
sb.append(" ")
}
if (type.mutability.isMut) sb.append("mut ")
}
type.typeReference?.let { appendTypeReference(sb, it) }
}
is RsArrayType -> {
sb.append("[")
type.typeReference?.let { appendTypeReference(sb, it) }
if (!type.isSlice) {
val arraySizeExpr = type.expr
sb.append("; ")
if (arraySizeExpr != null) {
appendConstExpr(sb, arraySizeExpr, TyInteger.USize.INSTANCE)
} else {
sb.append("{}")
}
}
sb.append("]")
}
is RsFnPointerType -> {
if (type.isUnsafe) {
sb.append("unsafe ")
}
if (type.isExtern) {
sb.append("extern ")
val abiName = type.abiName
if (abiName != null) {
sb.append("\"")
sb.append(abiName)
sb.append("\" ")
}
}
sb.append("fn")
appendValueParameterListTypes(sb, type.valueParameters)
appendRetType(sb, type.retType)
}
is RsTraitType -> {
sb.append(if (type.isImpl) "impl " else "dyn ")
type.polyboundList.joinToWithBuffer(sb, " + ") {
appendPolybound(sb, this)
}
}
is RsMacroType -> {
appendPath(sb, type.macroCall.path)
sb.append("!(")
type.macroCall.macroBody?.let { sb.append(it) }
sb.append(")")
}
}
}
private fun appendPolybound(sb: StringBuilder, polyBound: RsPolybound) {
val forLifetimes = polyBound.forLifetimes
if (renderLifetimes && forLifetimes != null) {
appendForLifetimes(sb, forLifetimes)
}
if (polyBound.hasQ) {
sb.append("?")
}
val bound = polyBound.bound
val lifetime = bound.lifetime
if (renderLifetimes && lifetime != null) {
sb.append(lifetime.referenceName)
} else {
bound.traitRef?.path?.let { appendPath(sb, it) }
}
}
private fun appendForLifetimes(sb: StringBuilder, forLifetimes: RsForLifetimes) {
sb.append("for<")
forLifetimes.lifetimeParameterList.joinTo(sb, ", ") {
it.name ?: "'_"
}
sb.append("> ")
}
open fun appendLifetime(sb: StringBuilder, lifetime: RsLifetime) {
sb.append(lifetime.referenceName)
}
open fun appendPath(
sb: StringBuilder,
path: RsPath
) {
appendPathWithoutArgs(sb, path)
appendPathArgs(sb, path)
}
protected open fun appendPathWithoutArgs(sb: StringBuilder, path: RsPath) {
val qualifier = path.path
if (!shortPaths && qualifier != null) {
appendPath(sb, qualifier)
}
val typeQual = path.typeQual
if (typeQual != null) {
appendTypeQual(sb, typeQual)
}
if (path.hasColonColon) {
sb.append("::")
}
sb.append(path.referenceName.orEmpty())
}
protected open fun appendTypeQual(sb: StringBuilder, typeQual: RsTypeQual) {
sb.append("<")
appendTypeReference(sb, typeQual.typeReference)
val traitRef = typeQual.traitRef
if (traitRef != null) {
sb.append(" as ")
appendPath(sb, traitRef.path)
}
sb.append(">")
sb.append("::")
}
private fun appendPathArgs(sb: StringBuilder, path: RsPath) {
val inAngles = path.typeArgumentList // Foo<...>
val fnSugar = path.valueParameterList // &dyn FnOnce(...) -> i32
if (inAngles != null) {
val lifetimeArguments = inAngles.lifetimeList
val typeArguments = inAngles.typeReferenceList
val constArguments = inAngles.exprList
val assocTypeBindings = inAngles.assocTypeBindingList
val hasLifetimes = renderLifetimes && lifetimeArguments.isNotEmpty()
val hasTypeReferences = typeArguments.isNotEmpty()
val hasConstArguments = constArguments.isNotEmpty()
val hasAssocTypeBindings = assocTypeBindings.isNotEmpty()
if (hasLifetimes || hasTypeReferences || hasConstArguments || hasAssocTypeBindings) {
sb.append("<")
if (hasLifetimes) {
lifetimeArguments.joinToWithBuffer(sb, ", ") { appendLifetime(it, this) }
if (hasTypeReferences || hasConstArguments || hasAssocTypeBindings) {
sb.append(", ")
}
}
if (hasTypeReferences) {
typeArguments.joinToWithBuffer(sb, ", ") { appendTypeReference(it, this) }
if (hasConstArguments || hasAssocTypeBindings) {
sb.append(", ")
}
}
if (hasConstArguments) {
constArguments.joinToWithBuffer(sb, ", ") { appendConstExpr(it, this) }
if (hasAssocTypeBindings) {
sb.append(", ")
}
}
assocTypeBindings.joinToWithBuffer(sb, ", ") { sb ->
appendPath(sb, this.path)
sb.append("=")
typeReference?.let { appendTypeReference(sb, it) }
}
sb.append(">")
}
} else if (fnSugar != null) {
appendValueParameterListTypes(sb, fnSugar.valueParameterList)
appendRetType(sb, path.retType)
}
}
protected open fun appendRetType(sb: StringBuilder, retType: RsRetType?) {
val retTypeRef = retType?.typeReference
if (retTypeRef != null) {
sb.append(" -> ")
appendTypeReference(sb, retTypeRef)
}
}
protected open fun appendValueParameterListTypes(
sb: StringBuilder,
list: List<RsValueParameter>
) {
list.joinToWithBuffer(sb, separator = ", ", prefix = "(", postfix = ")") { sb ->
typeReference?.let { appendTypeReference(sb, it) }
}
}
protected open fun appendConstExpr(
sb: StringBuilder,
expr: RsExpr,
expectedTy: Ty = expr.type
) {
when (expr) {
is RsPathExpr -> appendPath(sb, expr.path)
is RsLitExpr -> appendLitExpr(sb, expr)
is RsBlockExpr -> appendBlockExpr(sb, expr)
is RsUnaryExpr -> appendUnaryExpr(sb, expr)
is RsBinaryExpr -> appendBinaryExpr(sb, expr)
else -> sb.append("{}")
}
}
protected open fun appendLitExpr(sb: StringBuilder, expr: RsLitExpr) {
when (val kind = expr.stubKind) {
is RsStubLiteralKind.Boolean -> sb.append(kind.value.toString())
is RsStubLiteralKind.Integer -> sb.append(kind.value?.toString() ?: "")
is RsStubLiteralKind.Float -> sb.append(kind.value?.toString() ?: "")
is RsStubLiteralKind.Char -> {
if (kind.isByte) {
sb.append("b")
}
sb.append("'")
sb.append(kind.value.orEmpty().escapeRust())
sb.append("'")
}
is RsStubLiteralKind.String -> {
if (kind.isByte) {
sb.append("b")
}
sb.append('"')
sb.append(kind.value.orEmpty().escapeRust())
sb.append('"')
}
null -> "{}"
}.exhaustive
}
protected open fun appendBlockExpr(sb: StringBuilder, expr: RsBlockExpr) {
val isTry = expr.isTry
val isUnsafe = expr.isUnsafe
val isAsync = expr.isAsync
val isConst = expr.isConst
val tailExpr = expr.block.expandedTailExpr
if (isTry) {
sb.append("try ")
}
if (isUnsafe) {
sb.append("unsafe ")
}
if (isAsync) {
sb.append("async ")
}
if (isConst) {
sb.append("const ")
}
if (tailExpr == null) {
sb.append("{}")
} else {
sb.append("{ ")
appendConstExpr(sb, tailExpr)
sb.append(" }")
}
}
protected open fun appendUnaryExpr(sb: StringBuilder, expr: RsUnaryExpr) {
val sign = when (expr.operatorType) {
UnaryOperator.REF -> "&"
UnaryOperator.REF_MUT -> "&mut "
UnaryOperator.DEREF -> "*"
UnaryOperator.MINUS -> "-"
UnaryOperator.NOT -> "!"
UnaryOperator.BOX -> "box "
UnaryOperator.RAW_REF_CONST -> "&raw const "
UnaryOperator.RAW_REF_MUT -> "&raw mut "
}
sb.append(sign)
val innerExpr = expr.expr
if (innerExpr != null) {
appendConstExpr(sb, innerExpr)
}
}
protected open fun appendBinaryExpr(sb: StringBuilder, expr: RsBinaryExpr) {
val sign = when (val op = expr.operatorType) {
is ArithmeticOp -> op.sign
is ArithmeticAssignmentOp -> op.sign
AssignmentOp.EQ -> "="
is ComparisonOp -> op.sign
is EqualityOp -> op.sign
LogicOp.AND -> "&&"
LogicOp.OR -> "||"
}
appendConstExpr(sb, expr.left)
sb.append(" ")
sb.append(sign)
sb.append(" ")
val right = expr.right
if (right != null) {
appendConstExpr(sb, right)
}
}
}
open class TypeSubstitutingPsiRenderer(
options: PsiRenderingOptions,
private val subst: Substitution
) : RsPsiRenderer(options) {
override fun appendTypeReference(sb: StringBuilder, ref: RsTypeReference) {
val ty = ref.type
if (ty is TyTypeParameter && subst[ty] != null) {
sb.append(ty.substAndGetText(subst))
} else {
super.appendTypeReference(sb, ref)
}
}
override fun appendLifetime(sb: StringBuilder, lifetime: RsLifetime) {
val resolvedLifetime = lifetime.resolve()
val substitutedLifetime = if (resolvedLifetime is ReEarlyBound) subst[resolvedLifetime] else null
if (substitutedLifetime is ReEarlyBound) {
sb.append(substitutedLifetime.parameter.name)
} else {
sb.append(lifetime.referenceName)
}
}
override fun appendConstExpr(
sb: StringBuilder,
expr: RsExpr,
expectedTy: Ty
) {
when (val const = expr.evaluate(expectedTy).substitute(subst)) { // may trigger resolve
is CtValue -> sb.append(const)
is CtConstParameter -> {
val wrapParameterInBraces = expr.stubParent is RsTypeArgumentList
if (wrapParameterInBraces) {
sb.append("{ ")
}
sb.append(const.toString())
if (wrapParameterInBraces) {
sb.append(" }")
}
}
else -> sb.append("{}")
}
}
}
open class PsiSubstitutingPsiRenderer(
options: PsiRenderingOptions,
private val substitutions: List<RsPsiSubstitution>
) : RsPsiRenderer(options) {
override fun appendPathWithoutArgs(sb: StringBuilder, path: RsPath) {
val replaced = when (val resolved = path.reference?.resolve()) {
is RsTypeParameter -> when (val s = typeSubst(resolved)) {
is RsPsiSubstitution.Value.Present -> when (s.value) {
is RsPsiSubstitution.TypeValue.InAngles -> {
super.appendTypeReference(sb, s.value.value)
true
}
is RsPsiSubstitution.TypeValue.FnSugar -> false
}
is RsPsiSubstitution.Value.DefaultValue -> {
super.appendTypeReference(sb, s.value.value)
true
}
else -> false
}
is RsConstParameter -> when (val s = constSubst(resolved)) {
is RsPsiSubstitution.Value.Present -> {
when (s.value) {
is RsExpr -> appendConstExpr(sb, s.value)
is RsTypeReference -> appendTypeReference(sb, s.value)
}
true
}
is RsPsiSubstitution.Value.DefaultValue -> {
appendConstExpr(sb, s.value)
true
}
else -> false
}
else -> false
}
if (!replaced) {
super.appendPathWithoutArgs(sb, path)
}
}
override fun appendLifetime(sb: StringBuilder, lifetime: RsLifetime) {
val resolvedLifetime = lifetime.reference.resolve()
val substitutedLifetime = if (resolvedLifetime is RsLifetimeParameter) {
regionSubst(resolvedLifetime)
} else {
null
}
when (substitutedLifetime) {
is RsPsiSubstitution.Value.Present -> sb.append(substitutedLifetime.value.name)
else -> sb.append(lifetime.referenceName)
}
}
private fun regionSubst(lifetime: RsLifetimeParameter?): RsPsiSubstitution.Value<RsLifetime, Nothing>? {
return substitutions.firstNotNullOfOrNull { it.regionSubst[lifetime] }
}
private fun constSubst(const: RsConstParameter?): RsPsiSubstitution.Value<RsElement, RsExpr>? {
return substitutions.firstNotNullOfOrNull { it.constSubst[const] }
}
private fun typeSubst(type: RsTypeParameter?): RsPsiSubstitution.Value<RsPsiSubstitution.TypeValue, RsPsiSubstitution.TypeDefault>? {
return substitutions.firstNotNullOfOrNull { it.typeSubst[type] }
}
}
class ImportingPsiRenderer(
options: PsiRenderingOptions,
substitutions: List<RsPsiSubstitution>,
private val context: RsElement
) : PsiSubstitutingPsiRenderer(options, substitutions) {
private val importContext = ImportContext2.from(context, ImportContext2.Type.OTHER)
private val visibleNames: Pair<MutableMap<Pair<String, Namespace>, RsElement>, MutableMap<RsElement, String>> by lazy(LazyThreadSafetyMode.PUBLICATION) {
val nameToElement = mutableMapOf<Pair<String, Namespace>, RsElement>()
val elementToName = mutableMapOf<RsElement, String>()
processNestedScopesUpwards(context, TYPES_N_VALUES, createProcessor {
val element = it.element as? RsNamedElement ?: return@createProcessor false
for (namespace in element.namespaces) {
nameToElement[it.name to namespace] = element
if (it.name != "_" && element !in elementToName) {
elementToName[element] = it.name
}
}
false
})
nameToElement to elementToName
}
private val visibleNameToElement: MutableMap<Pair<String, Namespace>, RsElement> get() = visibleNames.first
private val visibleElementToName: MutableMap<RsElement, String> get() = visibleNames.second
private val itemsToImportMut: MutableSet<ImportCandidate> = mutableSetOf()
val itemsToImport: Set<ImportCandidate> get() = itemsToImportMut
override fun appendPathWithoutArgs(sb: StringBuilder, path: RsPath) {
val pathReferenceName = path.referenceName
val tryImportPath1 = path.parent !is RsPath &&
TyPrimitive.fromPath(path) == null &&
path.basePath().referenceName != "Self" &&
path.basePath().typeQual == null
if (tryImportPath1 && pathReferenceName != null) {
val resolved = path.reference?.resolve()
val tryImportPath2 = resolved !is RsTypeParameter
&& resolved !is RsConstParameter
&& resolved !is RsMacroDefinitionBase
&& resolved !is RsMod
if (tryImportPath2 && resolved is RsQualifiedNamedElement) {
val visibleElementName = visibleElementToName[resolved]
if (visibleElementName != null) {
sb.append(visibleElementName)
} else {
val importCandidate = importContext?.let {
ImportCandidatesCollector2.findImportCandidate(it, resolved)
}
if (importCandidate == null) {
val resolvedCrate = resolved.containingCrate
if (resolvedCrate == null || resolvedCrate == context.containingCrate) {
sb.append("crate")
} else {
sb.append(resolvedCrate.normName)
}
sb.append(resolved.crateRelativePath)
} else {
val ns = if (path.parent is RsExpr) {
Namespace.Values
} else {
Namespace.Types
}
val elementInScopeWithSameName = visibleNameToElement[pathReferenceName to ns]
val isNameConflict = elementInScopeWithSameName != null && elementInScopeWithSameName != resolved
if (isNameConflict) {
val qualifiedPath = importCandidate.info.usePath
sb.append(trySimplifyPath(path, qualifiedPath) ?: qualifiedPath)
} else {
itemsToImportMut += importCandidate
visibleElementToName[resolved] = pathReferenceName
for (namespace in resolved.namespaces) {
visibleNameToElement[pathReferenceName to namespace] = resolved
}
sb.append(pathReferenceName)
}
}
}
return
}
}
super.appendPathWithoutArgs(sb, path)
}
private fun trySimplifyPath(originPath: RsPath, qualifiedPath: String): String? {
val newPath = RsCodeFragmentFactory(originPath.project).createPath(
qualifiedPath,
context,
RustParserUtil.PathParsingMode.TYPE,
originPath.allowedNamespaces()
) ?: return null
val segmentsReversed = generateSequence(newPath) { it.path }.toList()
var simplifiedSegmentCount = 1
var firstSegmentName: String? = null
for (s in segmentsReversed.asSequence().drop(1)) {
val resolved = s.reference?.resolve() ?: return null
simplifiedSegmentCount++
firstSegmentName = visibleElementToName[resolved]
if (firstSegmentName != null) break
}
return if (firstSegmentName == null || simplifiedSegmentCount >= segmentsReversed.size) {
null
} else {
"$firstSegmentName::" + segmentsReversed
.take(simplifiedSegmentCount - 1)
.asReversed()
.joinToString(separator = "::") { it.referenceName.orEmpty() }
}
}
}