[kotlin] Drop KotlinPackageModificationListener

##KTIJ-20658

GitOrigin-RevId: 0a2285a7157e8ad9053f716077bfafdfec6f8f91
This commit is contained in:
Vladimir Dolzhenko
2022-03-24 13:13:20 +00:00
committed by intellij-monorepo-bot
parent e96d3d9f95
commit 5cf0de795f
10 changed files with 0 additions and 153 deletions
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.idea.caches.project.IdeaModuleInfo
import org.jetbrains.kotlin.idea.caches.project.ModuleSourceInfo
import org.jetbrains.kotlin.idea.caches.project.projectSourceModules
import org.jetbrains.kotlin.idea.caches.trackers.KotlinCodeBlockModificationListener
import org.jetbrains.kotlin.idea.caches.trackers.KotlinPackageModificationListener
import org.jetbrains.kotlin.idea.stubindex.PackageIndexUtil
import org.jetbrains.kotlin.idea.util.application.getServiceSafe
import org.jetbrains.kotlin.name.FqName
@@ -86,7 +85,6 @@ class PluginDeclarationProviderFactory(
oldPackageExists = $oldPackageExists,
SPI.packageExists = $spiPackageExists,
OOCB count = ${KotlinCodeBlockModificationListener.getInstance(project).kotlinOutOfCodeBlockTracker.modificationCount}
PT count = ${KotlinPackageModificationListener.getInstance(project).packageTracker.modificationCount}
moduleModificationCount = $moduleModificationCount
""".trimIndent()
@@ -1,128 +0,0 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.caches.trackers
import com.intellij.ide.plugins.DynamicPluginListener
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.lang.ASTNode
import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.util.ModificationTracker
import com.intellij.openapi.util.SimpleModificationTracker
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
import com.intellij.pom.PomManager
import com.intellij.pom.PomModelAspect
import com.intellij.pom.event.PomModelEvent
import com.intellij.pom.event.PomModelListener
import com.intellij.pom.tree.TreeAspect
import com.intellij.pom.tree.events.TreeChangeEvent
import com.intellij.pom.tree.events.impl.ChangeInfoImpl
import com.intellij.psi.PsiElement
import com.intellij.psi.util.findTopmostParentOfType
import com.intellij.vfs.AsyncVfsEventsListener
import com.intellij.vfs.AsyncVfsEventsPostProcessor
import org.jetbrains.kotlin.idea.util.application.getServiceSafe
import org.jetbrains.kotlin.idea.util.isKotlinFileType
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
/**
* Tracks potential package changes
* - In Kotlin package directives
* - VFS changes line created new file/folder
* - Plugins load/unload
*
* Tested in [org.jetbrains.kotlin.idea.codeInsight.OutOfBlockModificationTestGenerated]
*/
class KotlinPackageModificationListener(project: Project): Disposable {
private val trackerImpl = SimpleModificationTracker()
val packageTracker: ModificationTracker = trackerImpl
init {
val messageBusConnection = project.messageBus.connect(this)
messageBusConnection.subscribe(DynamicPluginListener.TOPIC, object : DynamicPluginListener {
override fun beforePluginUnload(pluginDescriptor: IdeaPluginDescriptor, isUpdate: Boolean) {
incModificationCount()
}
override fun pluginLoaded(pluginDescriptor: IdeaPluginDescriptor) {
incModificationCount()
}
})
val fileIndex = ProjectFileIndex.SERVICE.getInstance(project)
val vfsEventsListener = AsyncVfsEventsListener { events ->
val relatedVfsFileChange = events.any { event ->
event.takeIf { it.isFromRefresh || it is VFileContentChangeEvent }?.file?.let {
it.isKotlinFileType() && fileIndex.isInContent(it)
} ?: false
}
if (relatedVfsFileChange) {
incModificationCount()
}
}
AsyncVfsEventsPostProcessor.getInstance().addListener(vfsEventsListener, this)
val treeAspect: TreeAspect = TreeAspect.getInstance(project)
val model = PomManager.getModel(project)
model.addModelListener(
object : PomModelListener {
override fun isAspectChangeInteresting(aspect: PomModelAspect): Boolean = aspect == treeAspect
private fun PsiElement.findPackageDirectiveFqName(): FqName? {
return findTopmostParentOfType<KtPackageDirective>(false)?.let {
return it.fqName
}
}
private inline fun ASTNode?.packageDirectiveChange() =
this?.elementType == KtStubElementTypes.PACKAGE_DIRECTIVE
override fun modelChanged(event: PomModelEvent) {
val changeSet = event.getChangeSet(treeAspect).safeAs<TreeChangeEvent>() ?: return
// track only Kotlin
changeSet.rootElement.psi.containingFile.takeIf { it is KtFile } ?: return
val packageChange = changeSet.changedElements.any { changedElement ->
if (changedElement.packageDirectiveChange()) return@any true
val changesByElement = changeSet.getChangesByElement(changedElement)
changesByElement.affectedChildren.any child@ { affectedChild ->
if (affectedChild.packageDirectiveChange()) return@child true
val oldChildNode = changesByElement.getChangeByChild(affectedChild).safeAs<ChangeInfoImpl>()?.oldChildNode
// if oldChildNode is null and new child is Kotlin file - it means file was not exist
if (oldChildNode == null && changedElement.elementType == KtFileElementType.INSTANCE ||
oldChildNode?.packageDirectiveChange() == true
) return@child true
affectedChild.psi.findPackageDirectiveFqName()?.let { return@child true }
oldChildNode?.psi?.findPackageDirectiveFqName() != null
}
}
if (packageChange) {
incModificationCount()
}
}
}
)
}
private fun incModificationCount() {
trackerImpl.incModificationCount()
}
override fun dispose() = Unit
companion object {
fun getInstance(project: Project): KotlinPackageModificationListener = project.getServiceSafe()
}
}
@@ -14,7 +14,6 @@ import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import org.jetbrains.kotlin.idea.FrontendInternals
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.trackers.KotlinPackageModificationListener
import org.jetbrains.kotlin.idea.caches.trackers.outOfBlockModificationCount
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
@@ -36,7 +35,6 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
}
val expectedOutOfBlock = expectedOutOfBlockResult
val text = psiFile.text
val expectedPackageTrackerChange = InTextDirectivesUtils.isDirectiveDefined(text, PACKAGE_CHANGE_DIRECTIVE)
val isErrorChecksDisabled = InTextDirectivesUtils.isDirectiveDefined(text, DISABLE_ERROR_CHECKS_DIRECTIVE)
val isSkipCheckDefined = InTextDirectivesUtils.isDirectiveDefined(text, SKIP_ANALYZE_CHECK_DIRECTIVE)
val project = myFixture.project
@@ -44,8 +42,6 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
PsiManager.getInstance(project).modificationTracker as PsiModificationTrackerImpl
val element = psiFile.findElementAt(myFixture.caretOffset)
assertNotNull("Should be valid element", element)
val packageTracker = KotlinPackageModificationListener.getInstance(project).packageTracker
val ptcBeforeType = packageTracker.modificationCount
val oobBeforeType = ktFile?.outOfBlockModificationCount
val modificationCountBeforeType = tracker.modificationCount
@@ -55,7 +51,6 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
myFixture.type(stringToType)
PsiDocumentManager.getInstance(project).commitDocument(myFixture.getDocument(myFixture.file))
val oobAfterCount = ktFile?.outOfBlockModificationCount
val ptcAfterType = packageTracker.modificationCount
val modificationCountAfterType = tracker.modificationCount
assertTrue(
"Modification tracker should always be changed after type",
@@ -67,11 +62,6 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
+ FileUtil.loadFile(testDataFile()),
expectedOutOfBlock, oobBeforeType != oobAfterCount
)
assertEquals(
"package modification tracker differs from expected:\n"
+ FileUtil.loadFile(testDataFile()),
expectedPackageTrackerChange, ptcBeforeType != ptcAfterType
)
ktFile?.let {
if (!isErrorChecksDisabled) {
checkForUnexpectedErrors(it)
@@ -149,7 +139,6 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
companion object {
const val OUT_OF_CODE_BLOCK_DIRECTIVE = "OUT_OF_CODE_BLOCK:"
const val PACKAGE_CHANGE_DIRECTIVE = "PACKAGE_CHANGE"
const val DISABLE_ERROR_CHECKS_DIRECTIVE = "DISABLE_ERROR_CHECKS"
const val SKIP_ANALYZE_CHECK_DIRECTIVE = "SKIP_ANALYZE_CHECK"
const val TYPE_DIRECTIVE = "TYPE:"
@@ -1,8 +1,5 @@
// OUT_OF_CODE_BLOCK: TRUE
// It is kind of false positive as `}` breaks entire PSI and all PSI elements are recreated, even (implicit) package directive
// PACKAGE_CHANGE
// ERROR: Function 'test' must have a body
// TYPE: \b
@@ -1,6 +1,5 @@
// OUT_OF_CODE_BLOCK: TRUE
// DISABLE_ERROR_CHECKS
// PACKAGE_CHANGE
package foo.b<caret>
class Some
@@ -1,5 +1,4 @@
// OUT_OF_CODE_BLOCK: TRUE
// PACKAGE_CHANGE
// TYPE: '\n'
package foo.<caret>b
@@ -1,5 +1,4 @@
// OUT_OF_CODE_BLOCK: TRUE
// PACKAGE_CHANGE
// TYPE: '\n'
package <caret>foo.b
@@ -1,5 +1,4 @@
// OUT_OF_CODE_BLOCK: TRUE
// PACKAGE_CHANGE
// TYPE: '\b\b\b\b\b\b\b\b\b'
package a<caret>
@@ -2,10 +2,6 @@
// TYPE: }
// SKIP_ANALYZE_CHECK
// ERROR: Function declaration must have a name
// It is kind of false positive as `}` breaks entire PSI and all PSI elements are recreated, even (implicit) package directive
// PACKAGE_CHANGE
fun test() {<caret>
val someThing: Any? = null
@@ -13,7 +13,6 @@
<applicationService serviceImplementation="org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache"/>
<projectService serviceImplementation="org.jetbrains.kotlin.idea.caches.trackers.KotlinPackageModificationListener"/>
<projectService serviceImplementation="org.jetbrains.kotlin.idea.caches.trackers.KotlinCodeBlockModificationListener"/>
<projectService serviceImplementation="org.jetbrains.kotlin.idea.caches.trackers.PureKotlinCodeBlockModificationListener"/>
<projectService serviceImplementation="org.jetbrains.kotlin.idea.caches.project.LibraryModificationTracker"/>