Fallback to simple oocb when per-language trackers are disabled (KT-32364)

This commit is contained in:
Nikolay Krasko
2019-06-06 17:00:43 +03:00
parent f605d4e73b
commit ba2ca5ae3c
2 changed files with 46 additions and 19 deletions
@@ -41,15 +41,27 @@ class KotlinCodeBlockModificationListener(
modificationTracker: PsiModificationTracker, modificationTracker: PsiModificationTracker,
project: Project, project: Project,
private val treeAspect: TreeAspect private val treeAspect: TreeAspect
): PsiTreeChangePreprocessor { ) : PsiTreeChangePreprocessor {
private val modificationTrackerImpl = modificationTracker as PsiModificationTrackerImpl private val modificationTrackerImpl = modificationTracker as PsiModificationTrackerImpl
@Suppress("UnstableApiUsage")
private val isLanguageTrackerEnabled = modificationTrackerImpl.isEnableLanguageTracker
// BUNCH: 183
// When there're we no per-language trackers we had to increment global tracker first and process result afterward
private val customIncrement = if (isLanguageTrackerEnabled) 0 else 1
@Volatile @Volatile
private var kotlinModificationTracker: Long = 0 private var kotlinModificationTracker: Long = 0
private val kotlinOutOfCodeBlockTrackerImpl = object : SimpleModificationTracker() { private val kotlinOutOfCodeBlockTrackerImpl: SimpleModificationTracker = if (isLanguageTrackerEnabled) {
override fun incModificationCount() { SimpleModificationTracker()
super.incModificationCount() } else {
object : SimpleModificationTracker() {
override fun getModificationCount(): Long {
@Suppress("DEPRECATION")
return modificationTracker.outOfCodeBlockModificationCount
}
} }
} }
@@ -61,7 +73,9 @@ class KotlinCodeBlockModificationListener(
val model = PomManager.getModel(project) val model = PomManager.getModel(project)
val messageBusConnection = project.messageBus.connect() val messageBusConnection = project.messageBus.connect()
(PsiManager.getInstance(project) as PsiManagerImpl).addTreeChangePreprocessor(this) if (isLanguageTrackerEnabled) {
(PsiManager.getInstance(project) as PsiManagerImpl).addTreeChangePreprocessor(this)
}
model.addModelListener(object : PomModelListener { model.addModelListener(object : PomModelListener {
override fun isAspectChangeInteresting(aspect: PomModelAspect): Boolean { override fun isAspectChangeInteresting(aspect: PomModelAspect): Boolean {
@@ -79,8 +93,14 @@ class KotlinCodeBlockModificationListener(
messageBusConnection.deliverImmediately() messageBusConnection.deliverImmediately()
if (ktFile.isPhysical && !isReplLine(ktFile.virtualFile)) { if (ktFile.isPhysical && !isReplLine(ktFile.virtualFile)) {
kotlinOutOfCodeBlockTrackerImpl.incModificationCount() if (isLanguageTrackerEnabled) {
perModuleOutOfCodeBlockTrackerUpdater.onKotlinPhysicalFileOutOfBlockChange(ktFile) kotlinOutOfCodeBlockTrackerImpl.incModificationCount()
perModuleOutOfCodeBlockTrackerUpdater.onKotlinPhysicalFileOutOfBlockChange(ktFile, true)
} else {
perModuleOutOfCodeBlockTrackerUpdater.onKotlinPhysicalFileOutOfBlockChange(ktFile, false)
// Increment counter and process changes in PsiModificationTracker.Listener
modificationTrackerImpl.incCounter()
}
} }
incOutOfBlockModificationCount(ktFile) incOutOfBlockModificationCount(ktFile)
@@ -88,21 +108,26 @@ class KotlinCodeBlockModificationListener(
} }
}) })
@Suppress("UnstableApiUsage")
messageBusConnection.subscribe(PsiModificationTracker.TOPIC, PsiModificationTracker.Listener { messageBusConnection.subscribe(PsiModificationTracker.TOPIC, PsiModificationTracker.Listener {
@Suppress("UnstableApiUsage") val kotlinTrackerInternalIDECount = if (isLanguageTrackerEnabled) {
modificationTrackerImpl.forLanguage(KotlinLanguage.INSTANCE).modificationCount val kotlinTrackerInternalIDECount =
if (kotlinModificationTracker == kotlinTrackerInternalIDECount) { modificationTrackerImpl.forLanguage(KotlinLanguage.INSTANCE).modificationCount
// Some update that we are not sure is from Kotlin language, as Kotlin language tracker wasn't changed if (kotlinModificationTracker == kotlinTrackerInternalIDECount) {
kotlinOutOfCodeBlockTrackerImpl.incModificationCount() // Some update that we are not sure is from Kotlin language, as Kotlin language tracker wasn't changed
} else { kotlinOutOfCodeBlockTrackerImpl.incModificationCount()
kotlinModificationTracker = kotlinTrackerInternalIDECount } else {
kotlinModificationTracker = kotlinTrackerInternalIDECount
}
} }
perModuleOutOfCodeBlockTrackerUpdater.onPsiModificationTrackerUpdate() perModuleOutOfCodeBlockTrackerUpdater.onPsiModificationTrackerUpdate(customIncrement)
}) })
} }
override fun treeChanged(event: PsiTreeChangeEventImpl) { override fun treeChanged(event: PsiTreeChangeEventImpl) {
assert(isLanguageTrackerEnabled)
if (!PsiModificationTrackerImpl.canAffectPsi(event)) { if (!PsiModificationTrackerImpl.canAffectPsi(event)) {
return return
} }
@@ -89,17 +89,19 @@ class KotlinModuleOutOfCodeBlockModificationTracker private constructor(private
internal fun hasPerModuleModificationCounts() = perModuleChangesHighWatermark != null internal fun hasPerModuleModificationCounts() = perModuleChangesHighWatermark != null
internal fun onKotlinPhysicalFileOutOfBlockChange(ktFile: KtFile) { internal fun onKotlinPhysicalFileOutOfBlockChange(ktFile: KtFile, immediateUpdatesProcess: Boolean) {
lastAffectedModule = ModuleUtil.findModuleForPsiElement(ktFile) lastAffectedModule = ModuleUtil.findModuleForPsiElement(ktFile)
lastAffectedModuleModCount = kotlinOfOfCodeBlockTracker.modificationCount lastAffectedModuleModCount = kotlinOfOfCodeBlockTracker.modificationCount
onPsiModificationTrackerUpdate() if (immediateUpdatesProcess) {
onPsiModificationTrackerUpdate(0)
}
} }
internal fun onPsiModificationTrackerUpdate() { internal fun onPsiModificationTrackerUpdate(customIncrement: Int) {
val newModCount = kotlinOfOfCodeBlockTracker.modificationCount val newModCount = kotlinOfOfCodeBlockTracker.modificationCount
val affectedModule = lastAffectedModule val affectedModule = lastAffectedModule
if (affectedModule != null && newModCount == lastAffectedModuleModCount) { if (affectedModule != null && newModCount == lastAffectedModuleModCount + customIncrement) {
if (perModuleChangesHighWatermark == null) { if (perModuleChangesHighWatermark == null) {
perModuleChangesHighWatermark = lastAffectedModuleModCount perModuleChangesHighWatermark = lastAffectedModuleModCount
} }