From 54386022e2d1cfad0d2f4ec8d4454785d1c62ba5 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Wed, 15 Feb 2023 13:13:29 +0100 Subject: [PATCH] [kotlin] Show expression script nodes #KTIJ-12844 #KTIJ-24328 GitOrigin-RevId: ba129af8351283565d57354de7d8852af53629eb --- .../messages/KotlinBundle.properties | 1 + .../kotlin/idea/KotlinIconProvider.kt | 1 + .../idea/projectView/KtDeclarationTreeNode.kt | 22 +++++++++---------- .../KotlinStructureViewElement.kt | 3 +-- .../testData/projectView/ScriptMembers.txt | 1 + .../testData/projectView/ScriptMembers2.test | 2 +- .../testData/projectView/ScriptMembers2.txt | 1 + 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties b/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties index 0935be4d6302..99e0b56ab199 100644 --- a/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties +++ b/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties @@ -488,6 +488,7 @@ presentation.text.for.receiver.in.container.paren.no.brackets=for {0} in {1} presentation.text.object.in.container=object in {0} project.view.class.initializer=class initializer +project.view.expression=expression project.view.class.error.name=no name provided copy.text.adding.imports=Adding imports\u2026 diff --git a/plugins/kotlin/base/resources/src/org/jetbrains/kotlin/idea/KotlinIconProvider.kt b/plugins/kotlin/base/resources/src/org/jetbrains/kotlin/idea/KotlinIconProvider.kt index 65c21fe29db9..b0dbd091d136 100644 --- a/plugins/kotlin/base/resources/src/org/jetbrains/kotlin/idea/KotlinIconProvider.kt +++ b/plugins/kotlin/base/resources/src/org/jetbrains/kotlin/idea/KotlinIconProvider.kt @@ -134,6 +134,7 @@ abstract class KotlinIconProvider : IconProvider(), DumbAware { fun PsiElement.getBaseIcon(): Icon? = when (this) { is KtPackageDirective -> AllIcons.Nodes.Package is KtFile, is KtLightClassForFacade -> FILE + is KtScript -> (parent as? KtFile)?.scriptIcon() is KtLightClassForSourceDeclaration -> navigationElement.getBaseIcon() is KtNamedFunction -> when { receiverTypeReference != null -> diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.kt index 5ac9d1df60ac..57c439bd6aa5 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.kt @@ -31,6 +31,7 @@ internal class KtDeclarationTreeNode private constructor( internal companion object { private val CLASS_INITIALIZER = "<" + KotlinBundle.message("project.view.class.initializer") + ">" + private val EXPRESSION = "<" + KotlinBundle.message("project.view.expression") + ">" private val ERROR_NAME = "<" + KotlinBundle.message("project.view.class.error.name") + ">" private fun String?.orErrorName() = if (!isNullOrBlank()) this else ERROR_NAME @@ -100,10 +101,16 @@ internal class KtDeclarationTreeNode private constructor( declaration.referenceExpression() val referencedNameAsName = nameReferenceExpression?.getReferencedNameAsName() - referencedNameAsName?.asString() ?: CLASS_INITIALIZER + referencedNameAsName?.asString()?.let { return it } + return if (declaration.body is KtExpression) { + EXPRESSION + } else { + CLASS_INITIALIZER + } } is KtAnonymousInitializer -> CLASS_INITIALIZER + is KtScript -> (declaration.parent as? KtFile)?.name ?: declaration.name.orErrorName() else -> declaration.name.orErrorName() } } @@ -117,16 +124,7 @@ internal class KtDeclarationTreeNode private constructor( } as? KtNameReferenceExpression } - fun KtDeclaration.isApplicableNode(): Boolean = - this !is KtScriptInitializer || referenceExpression() != null - - fun create(project: Project?, - ktDeclaration: KtDeclaration, - viewSettings: ViewSettings): KtDeclarationTreeNode? = - if (ktDeclaration.isApplicableNode()) { - KtDeclarationTreeNode(project, ktDeclaration, viewSettings) - } else { - null - } + fun create(project: Project?, ktDeclaration: KtDeclaration, viewSettings: ViewSettings): KtDeclarationTreeNode = + KtDeclarationTreeNode(project, ktDeclaration, viewSettings) } } diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt index d11dd88399ad..1643857d622d 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny -import org.jetbrains.kotlin.idea.projectView.KtDeclarationTreeNode.Companion.isApplicableNode import org.jetbrains.kotlin.psi.* import javax.swing.Icon import kotlin.properties.ReadWriteProperty @@ -73,7 +72,7 @@ class KotlinStructureViewElement( (declarations.singleOrNull() as? KtScript) ?: element } else { element - }.declarations.filter { it.isApplicableNode() } + }.declarations } is KtClass -> element.getStructureDeclarations() is KtClassOrObject -> element.declarations diff --git a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers.txt b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers.txt index 97db21ca227a..ad3210b9484b 100644 --- a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers.txt +++ b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers.txt @@ -11,6 +11,7 @@ Navigation item file #0: src/build.gradle.kts -Project -PsiDirectory: src -[build.gradle.kts] + application dependencies plugins diff --git a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.test b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.test index e261aa399a44..433ff9ae7aef 100644 --- a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.test +++ b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.test @@ -2,6 +2,6 @@ // FILE: settings.gradle.kts // PATH: settings.gradle.kts // SHOW_MEMBERS: TRUE -rootProject.name = "simple2" +"".length + 1 include() \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.txt b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.txt index 968da0297314..2a8012e1ff40 100644 --- a/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.txt +++ b/plugins/kotlin/idea/tests/testData/projectView/ScriptMembers2.txt @@ -11,5 +11,6 @@ Navigation item file #0: src/settings.gradle.kts -Project -PsiDirectory: src -[settings.gradle.kts] + include +External Libraries \ No newline at end of file