From 165bbb355910996ad1d2eb87bd035ec2cec99afa Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 29 Feb 2024 16:56:29 +0100 Subject: [PATCH] remove illegal dependency kotlin.code-insight.k2 -> kotlin.core because turned out kotlin.core is k1-specific followup to jetbrains.team/p/ij/reviews/125638 (part of KTIJ-28926 K2: Create Function From Usage Fix) GitOrigin-RevId: a2bdeeffea16b9ca049ee397f95069b4f88c6e4b --- intellij.idea.community.main.iml | 1 + .../kotlin/idea/core/generateUtils.kt | 60 +++++++++---------- .../kotlin/idea/core/oldGenerateUtil.kt | 15 ++--- .../createFromUsage/CreateFromUsageUtil.kt} | 49 ++++++++------- .../kotlin.code-insight.k2.iml | 1 - ...lin.code-insight.override-implement.k1.iml | 1 + ...lin.code-insight.override-implement.k2.iml | 1 + .../impl-k1/kotlin.completion.impl.k1.iml | 1 + .../kotlin.completion.impl.shared.iml | 1 + 9 files changed, 67 insertions(+), 63 deletions(-) rename plugins/kotlin/{frontend-independent => code-insight/impl-base}/src/org/jetbrains/kotlin/idea/core/generateUtils.kt (81%) rename plugins/kotlin/{core => code-insight/impl-base}/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt (72%) rename plugins/kotlin/{core/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createFromUsageUtil.kt => code-insight/impl-base/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/CreateFromUsageUtil.kt} (85%) diff --git a/intellij.idea.community.main.iml b/intellij.idea.community.main.iml index 97b5cdeeeb10..dc54e9b376b0 100644 --- a/intellij.idea.community.main.iml +++ b/intellij.idea.community.main.iml @@ -8,6 +8,7 @@ + diff --git a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/core/generateUtils.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/generateUtils.kt similarity index 81% rename from plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/core/generateUtils.kt rename to plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/generateUtils.kt index 8d36da21e147..2e69ab1a1025 100644 --- a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/core/generateUtils.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/generateUtils.kt @@ -211,42 +211,42 @@ fun insertMembersAfter( ) } - if (otherMembers.isNotEmpty()) { - val psiFactory = KtPsiFactory(project) - val tailComments = classOrObject.allChildren.toList() - .takeLastWhile { it is PsiComment || it is PsiWhiteSpace } - .map { commentOrSpace -> - if (commentOrSpace is PsiWhiteSpace) { - psiFactory.createWhiteSpace(commentOrSpace.text) - } else { - commentOrSpace.copy().also { commentOrSpace.delete() } - } + if (otherMembers.isNotEmpty()) { + val psiFactory = KtPsiFactory(project) + val tailComments = classOrObject.allChildren.toList() + .takeLastWhile { it is PsiComment || it is PsiWhiteSpace } + .map { commentOrSpace -> + if (commentOrSpace is PsiWhiteSpace) { + psiFactory.createWhiteSpace(commentOrSpace.text) + } else { + commentOrSpace.copy().also { commentOrSpace.delete() } } - val body = classOrObject.getOrCreateBody() - val lBrace = body.lBrace - if (lBrace != null) { - tailComments.reversed().map { body.addAfter(it, lBrace) } } + val body = classOrObject.getOrCreateBody() + val lBrace = body.lBrace + if (lBrace != null) { + tailComments.reversed().map { body.addAfter(it, lBrace) } + } - var afterAnchor = anchor ?: findInsertAfterAnchor(editor, body) ?: return emptyList() - otherMembers.mapTo(insertedMembers) { - afterAnchor = getAnchor(it) ?: afterAnchor + var afterAnchor = anchor ?: findInsertAfterAnchor(editor, body) ?: return emptyList() + otherMembers.mapTo(insertedMembers) { + afterAnchor = getAnchor(it) ?: afterAnchor - if (classOrObject is KtClass && classOrObject.isEnum()) { - val enumEntries = classOrObject.declarations.filterIsInstance() - val bound = (enumEntries.lastOrNull() ?: classOrObject.allChildren.firstOrNull { element -> - element.node.elementType == KtTokens.SEMICOLON - }) - if (it !is KtEnumEntry) { - if (bound != null && afterAnchor.startOffset <= bound.startOffset) { - afterAnchor = bound - } - } else if (bound == null && body.declarations.isNotEmpty()) { - afterAnchor = body.lBrace!! - } else if (bound != null && afterAnchor.startOffset > bound.startOffset) { - afterAnchor = bound.prevSibling!! + if (classOrObject is KtClass && classOrObject.isEnum()) { + val enumEntries = classOrObject.declarations.filterIsInstance() + val bound = (enumEntries.lastOrNull() ?: classOrObject.allChildren.firstOrNull { element -> + element.node.elementType == KtTokens.SEMICOLON + }) + if (it !is KtEnumEntry) { + if (bound != null && afterAnchor.startOffset <= bound.startOffset) { + afterAnchor = bound } + } else if (bound == null && body.declarations.isNotEmpty()) { + afterAnchor = body.lBrace!! + } else if (bound != null && afterAnchor.startOffset > bound.startOffset) { + afterAnchor = bound.prevSibling!! } + } it.removeModifier(KtTokens.EXTERNAL_KEYWORD) diff --git a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt similarity index 72% rename from plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt rename to plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt index c9b600a353d5..a679f1d78ae2 100644 --- a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/core/oldGenerateUtil.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.kotlin.idea.core @@ -6,6 +6,7 @@ import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement import com.intellij.psi.codeStyle.CodeStyleManager +import org.jetbrains.kotlin.idea.base.codeInsight.ShortenReferencesFacility import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtDeclaration @@ -21,18 +22,18 @@ fun insertMembersAfterAndReformat( val insertedMembersElementPointers = insertMembersAfter(editor, classOrObject, members, anchor, getAnchor) val firstElement = insertedMembersElementPointers.firstOrNull() ?: return@runWriteAction emptyList() - fun insertedMembersElements() = insertedMembersElementPointers.mapNotNull { it.element } - - // TODO make shorten references work in both K1 and K2 - if (!org.jetbrains.kotlin.idea.base.plugin.KotlinPluginModeProvider.Companion.isK2Mode()) { - ShortenReferences.DEFAULT.process(insertedMembersElements()) + for (pointer in insertedMembersElementPointers) { + val element = pointer.element + if (element != null) { + ShortenReferencesFacility.getInstance().shorten(element) + } } if (editor != null) { firstElement.element?.let { moveCaretIntoGeneratedElement(editor, it) } } insertedMembersElementPointers.onEach { it.element?.let { element -> codeStyleManager.reformat(element) } } - insertedMembersElements() + insertedMembersElementPointers.mapNotNull { it.element } } } diff --git a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createFromUsageUtil.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/CreateFromUsageUtil.kt similarity index 85% rename from plugins/kotlin/core/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createFromUsageUtil.kt rename to plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/CreateFromUsageUtil.kt index ef30331970c2..d140a7bf5270 100644 --- a/plugins/kotlin/core/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createFromUsageUtil.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/CreateFromUsageUtil.kt @@ -9,7 +9,6 @@ import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.isAncestor import org.jetbrains.kotlin.idea.base.psi.isMultiLine import org.jetbrains.kotlin.idea.base.psi.replaced -import org.jetbrains.kotlin.idea.core.insertMembersAfterAndReformat import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* @@ -22,10 +21,10 @@ object CreateFromUsageUtil { // TODO: Simplify and use formatter as much as possible @Suppress("UNCHECKED_CAST") fun placeDeclarationInContainer( - declaration: D, - container: PsiElement, - anchor: PsiElement, - fileToEdit: KtFile = container.containingFile as KtFile + declaration: D, + container: PsiElement, + anchor: PsiElement, + fileToEdit: KtFile = container.containingFile as KtFile ): D { val psiFactory = KtPsiFactory(container.project) val newLine = psiFactory.createNewLine() @@ -47,8 +46,8 @@ object CreateFromUsageUtil { val neighborType = neighbor?.node?.elementType val lineBreaksNeeded = when { - neighborType == KtTokens.LBRACE || neighborType == KtTokens.RBRACE -> 1 - neighbor is KtDeclaration && (neighbor !is KtProperty || decl !is KtProperty) -> 2 + neighborType == KtTokens.LBRACE || neighborType == KtTokens.RBRACE -> 1 + neighbor is KtDeclaration && (neighbor !is KtProperty || decl !is KtProperty) -> 2 else -> 1 } @@ -58,14 +57,14 @@ object CreateFromUsageUtil { val actualContainer = (container as? KtClassOrObject)?.getOrCreateBody() ?: container fun addDeclarationToClassOrObject( - classOrObject: KtClassOrObject, - declaration: KtNamedDeclaration + classOrObject: KtClassOrObject, + declaration: KtNamedDeclaration ): KtNamedDeclaration { val classBody = classOrObject.getOrCreateBody() return if (declaration is KtNamedFunction) { val neighbor = PsiTreeUtil.skipSiblingsBackward( - classBody.rBrace ?: classBody.lastChild!!, - PsiWhiteSpace::class.java + classBody.rBrace ?: classBody.lastChild!!, + PsiWhiteSpace::class.java ) classBody.addAfter(declaration, neighbor) as KtNamedDeclaration } else classBody.addAfter(declaration, classBody.lBrace!!) as KtNamedDeclaration @@ -83,10 +82,10 @@ object CreateFromUsageUtil { val declarationInPlace = when { declaration is KtPrimaryConstructor -> { - (container as KtClass).createPrimaryConstructorIfAbsent().replaced(declaration) + (container as KtClass).createPrimaryConstructorIfAbsent().replaced(declaration) } - declaration is KtProperty && container !is KtBlockExpression -> { + declaration is KtProperty && container !is KtBlockExpression -> { val sibling = actualContainer.getChildOfType() ?: when (actualContainer) { is KtClassBody -> actualContainer.declarations.firstOrNull() ?: actualContainer.rBrace is KtFile -> actualContainer.declarations.first() @@ -127,7 +126,7 @@ object CreateFromUsageUtil { sibling = container.body?.lBrace } - insertMembersAfterAndReformat(null, container, declaration, sibling) + org.jetbrains.kotlin.idea.core.insertMembersAfterAndReformat(null, container, declaration, sibling) } else -> throw KotlinExceptionWithAttachments("Invalid containing element: ${container::class.java}") .withPsiAttachment("container", container) @@ -148,7 +147,7 @@ object CreateFromUsageUtil { } val semicolon = prevEnumEntry.allChildren.firstOrNull { it.node?.elementType == KtTokens.SEMICOLON } if (semicolon != null) { - (semicolon.prevSibling as? PsiWhiteSpace)?.text?.let { + (semicolon.prevSibling as? PsiWhiteSpace)?.text?.let { declarationInPlace.add(psiFactory.createWhiteSpace(it)) } declarationInPlace.add(psiFactory.createSemicolon()) @@ -183,11 +182,11 @@ object CreateFromUsageUtil { } fun computeDefaultVisibilityAsString( - containingElement: PsiElement, - isAbstract: Boolean, - isExtension: Boolean, - isConstructor: Boolean, - originalElement: PsiElement + containingElement: PsiElement, + isAbstract: Boolean, + isExtension: Boolean, + isConstructor: Boolean, + originalElement: PsiElement ): String { val modifier = if (isAbstract) null else if (containingElement is KtClassOrObject @@ -202,12 +201,12 @@ object CreateFromUsageUtil { } private val modifierToKotlinToken: Map = mapOf( - JvmModifier.PRIVATE to KtTokens.PRIVATE_KEYWORD, - JvmModifier.PACKAGE_LOCAL to KtTokens.INTERNAL_KEYWORD, - JvmModifier.PROTECTED to KtTokens.PROTECTED_KEYWORD, - JvmModifier.PUBLIC to KtTokens.PUBLIC_KEYWORD + JvmModifier.PRIVATE to KtTokens.PRIVATE_KEYWORD, + JvmModifier.PACKAGE_LOCAL to KtTokens.INTERNAL_KEYWORD, + JvmModifier.PROTECTED to KtTokens.PROTECTED_KEYWORD, + JvmModifier.PUBLIC to KtTokens.PUBLIC_KEYWORD ) fun modifierToString(modifier: JvmModifier?):String { - return modifierToKotlinToken[modifier]?.let { if (it == KtTokens.PUBLIC_KEYWORD) "" else it.value } ?:"" + return modifierToKotlinToken[modifier]?.let { if (it == KtTokens.PUBLIC_KEYWORD) "" else it.value } ?: "" } } \ No newline at end of file diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/kotlin.code-insight.k2.iml b/plugins/kotlin/code-insight/kotlin.code-insight.k2/kotlin.code-insight.k2.iml index c04499772496..6fa3ee728dbc 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/kotlin.code-insight.k2.iml +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/kotlin.code-insight.k2.iml @@ -55,7 +55,6 @@ - diff --git a/plugins/kotlin/code-insight/override-implement-k1/kotlin.code-insight.override-implement.k1.iml b/plugins/kotlin/code-insight/override-implement-k1/kotlin.code-insight.override-implement.k1.iml index 2491fe892f86..870ebdee004e 100644 --- a/plugins/kotlin/code-insight/override-implement-k1/kotlin.code-insight.override-implement.k1.iml +++ b/plugins/kotlin/code-insight/override-implement-k1/kotlin.code-insight.override-implement.k1.iml @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/plugins/kotlin/code-insight/override-implement-k2/kotlin.code-insight.override-implement.k2.iml b/plugins/kotlin/code-insight/override-implement-k2/kotlin.code-insight.override-implement.k2.iml index eefaef273a19..29fad0e83117 100644 --- a/plugins/kotlin/code-insight/override-implement-k2/kotlin.code-insight.override-implement.k2.iml +++ b/plugins/kotlin/code-insight/override-implement-k2/kotlin.code-insight.override-implement.k2.iml @@ -38,5 +38,6 @@ + \ No newline at end of file diff --git a/plugins/kotlin/completion/impl-k1/kotlin.completion.impl.k1.iml b/plugins/kotlin/completion/impl-k1/kotlin.completion.impl.k1.iml index 32194f03db00..760b88da3283 100644 --- a/plugins/kotlin/completion/impl-k1/kotlin.completion.impl.k1.iml +++ b/plugins/kotlin/completion/impl-k1/kotlin.completion.impl.k1.iml @@ -56,5 +56,6 @@ + \ No newline at end of file diff --git a/plugins/kotlin/completion/impl-shared/kotlin.completion.impl.shared.iml b/plugins/kotlin/completion/impl-shared/kotlin.completion.impl.shared.iml index b1bde7ce827d..415fcb385f9c 100644 --- a/plugins/kotlin/completion/impl-shared/kotlin.completion.impl.shared.iml +++ b/plugins/kotlin/completion/impl-shared/kotlin.completion.impl.shared.iml @@ -37,5 +37,6 @@ + \ No newline at end of file