|
| 1 | +package org.tonstudio.tact.ide.hints |
| 2 | + |
| 3 | +import com.intellij.codeInsight.hints.declarative.* |
| 4 | +import com.intellij.openapi.editor.Editor |
| 5 | +import com.intellij.psi.PsiElement |
| 6 | +import com.intellij.psi.PsiFile |
| 7 | +import org.tonstudio.tact.lang.psi.* |
| 8 | +import org.tonstudio.tact.lang.psi.types.TactBaseTypeEx.Companion.toEx |
| 9 | +import org.tonstudio.tact.lang.psi.types.TactPrimitiveTypeEx |
| 10 | +import org.tonstudio.tact.lang.psi.types.TactPrimitiveTypes |
| 11 | +import org.tonstudio.tact.lang.psi.types.TactTypeEx |
| 12 | + |
| 13 | +class TactImplicitAsInt257HintsProvider : InlayHintsProvider { |
| 14 | + override fun createCollector(file: PsiFile, editor: Editor) = Collector() |
| 15 | + |
| 16 | + class Collector : SharedBypassCollector { |
| 17 | + override fun collectFromElement(element: PsiElement, sink: InlayTreeSink) { |
| 18 | + if (element !is TactFieldDeclaration) return |
| 19 | + |
| 20 | + val typeNode = element.type ?: return |
| 21 | + val type = element.fieldDefinition.getType(null) ?: return |
| 22 | + |
| 23 | + // foo: Int |
| 24 | + if (needImplicitHint(type)) { |
| 25 | + addHint(sink, typeNode) |
| 26 | + } |
| 27 | + |
| 28 | + // foo: map<Int, Int> |
| 29 | + if (typeNode is TactMapType) { |
| 30 | + val key = typeNode.keyType.toEx() |
| 31 | + val value = typeNode.valueType.toEx() |
| 32 | + |
| 33 | + if (needImplicitHint(key)) { |
| 34 | + addHint(sink, typeNode.keyType!!) |
| 35 | + } |
| 36 | + if (needImplicitHint(value)) { |
| 37 | + addHint(sink, typeNode.valueType!!) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private fun needImplicitHint(type: TactTypeEx): Boolean = |
| 43 | + type is TactPrimitiveTypeEx && type.name == TactPrimitiveTypes.INT && type.tlbType == null |
| 44 | + |
| 45 | + private fun addHint(sink: InlayTreeSink, typeNode: TactType) { |
| 46 | + sink.addPresentation( |
| 47 | + InlineInlayPosition(typeNode.textRange.endOffset, true), |
| 48 | + listOf(), |
| 49 | + "Default TL-B serialization type for Int type.<br>Learn more in documentation: <a href=\"https://docs.tact-lang.org/book/integers/#common-serialization-types\">https://docs.tact-lang.org/book/integers/#common-serialization-types</a>", |
| 50 | + HintFormat.default |
| 51 | + .withColorKind(HintColorKind.TextWithoutBackground) |
| 52 | + .withHorizontalMargin(HintMarginPadding.MarginAndSmallerPadding) |
| 53 | + .withFontSize(HintFontSize.AsInEditor) |
| 54 | + ) { |
| 55 | + text("as int257") |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments