From c34286a3278693b1c1f209c24b578deab894e718 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sun, 9 Feb 2020 16:25:13 +0200 Subject: [PATCH] More usable rendering of usage context in Analyze Data Flow to/from Here --- .../slicer/KotlinSliceUsageCellRenderer.kt | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/slicer/KotlinSliceUsageCellRenderer.kt b/idea/src/org/jetbrains/kotlin/idea/slicer/KotlinSliceUsageCellRenderer.kt index 00d92a81766..7122fe883b7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/slicer/KotlinSliceUsageCellRenderer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/slicer/KotlinSliceUsageCellRenderer.kt @@ -10,20 +10,25 @@ import com.intellij.slicer.SliceUsageCellRendererBase import com.intellij.ui.JBColor import com.intellij.ui.SimpleTextAttributes import com.intellij.util.FontUtil +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor +import org.jetbrains.kotlin.descriptors.ConstructorDescriptor +import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.parents +import org.jetbrains.kotlin.renderer.ClassifierNamePolicy +import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy +import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject +import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension // Based on com.intellij.slicer.SliceUsageCellRenderer object KotlinSliceUsageCellRenderer : SliceUsageCellRendererBase() { - private val descriptorRenderer = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.withOptions { - withDefinedIn = true - withoutTypeParameters = true - parameterNameRenderingPolicy = ParameterNameRenderingPolicy.NONE - includeAdditionalModifiers = false - withoutSuperTypes = true + private val descriptorRenderer = DescriptorRenderer.ONLY_NAMES_WITH_SHORT_TYPES.withOptions { + withoutReturnType = true + renderConstructorKeyword = false } override fun customizeCellRendererFor(sliceUsage: SliceUsage) { @@ -45,9 +50,25 @@ object KotlinSliceUsageCellRenderer : SliceUsageCellRendererBase() { append(" (Tracking enclosing lambda)".repeat(sliceUsage.lambdaLevel), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES) val declaration = sliceUsage.element?.parents?.firstOrNull { - it is KtClass || it is KtObjectDeclaration && !it.isObjectLiteral() || it is KtDeclarationWithBody || it is KtProperty && it.isLocal + it is KtClass || + it is KtObjectDeclaration && !it.isObjectLiteral() || + it is KtNamedFunction && !it.isLocal || + it is KtProperty && !it.isLocal || + it is KtConstructor<*> } as? KtDeclaration ?: return + + append(" in ", SimpleTextAttributes.GRAY_ATTRIBUTES) + val descriptor = declaration.unsafeResolveToDescriptor() - append(" in ${descriptorRenderer.render(descriptor)}", SimpleTextAttributes.GRAY_ATTRIBUTES) + + if (!descriptor.isExtension && descriptor !is ConstructorDescriptor && !descriptor.isCompanionObject()) { + val containingClassifier = descriptor.containingDeclaration as? ClassifierDescriptor + if (containingClassifier != null) { + append(descriptorRenderer.render(containingClassifier), SimpleTextAttributes.GRAY_ATTRIBUTES) + append(".", SimpleTextAttributes.GRAY_ATTRIBUTES) + } + } + + append(descriptorRenderer.render(descriptor), SimpleTextAttributes.GRAY_ATTRIBUTES) } } \ No newline at end of file