Minor: cleanup and reformat KotlinCodeBlockModificationListener.kt

This commit is contained in:
Nikolay Krasko
2019-05-29 14:56:49 +03:00
parent 2029d16062
commit 2ddcd9f0af
@@ -58,20 +58,18 @@ class KotlinCodeBlockModificationListener(
// All modifications since that count are known to be single-module modifications reflected in // All modifications since that count are known to be single-module modifications reflected in
// perModuleModCount map // perModuleModCount map
private var perModuleChangesHighwatermark: Long? = null private var perModuleChangesHighWatermark: Long? = null
fun getModificationCount(module: Module): Long { fun getModificationCount(module: Module): Long {
return perModuleModCount[module] ?: return perModuleModCount[module] ?: perModuleChangesHighWatermark ?: modificationTrackerImpl.outOfCodeBlockModificationCount
perModuleChangesHighwatermark ?:
modificationTrackerImpl.outOfCodeBlockModificationCount
} }
fun hasPerModuleModificationCounts() = perModuleChangesHighwatermark != null fun hasPerModuleModificationCounts() = perModuleChangesHighWatermark != null
init { init {
val model = PomManager.getModel(project) val model = PomManager.getModel(project)
val messageBusConnection = project.messageBus.connect() val messageBusConnection = project.messageBus.connect()
model.addModelListener(object: PomModelListener { model.addModelListener(object : PomModelListener {
override fun isAspectChangeInteresting(aspect: PomModelAspect): Boolean { override fun isAspectChangeInteresting(aspect: PomModelAspect): Boolean {
return aspect == treeAspect return aspect == treeAspect
} }
@@ -80,7 +78,7 @@ class KotlinCodeBlockModificationListener(
val changeSet = event.getChangeSet(treeAspect) as TreeChangeEvent? ?: return val changeSet = event.getChangeSet(treeAspect) as TreeChangeEvent? ?: return
val file = changeSet.rootElement.psi.containingFile as? KtFile ?: return val file = changeSet.rootElement.psi.containingFile as? KtFile ?: return
val changedElements = changeSet.changedElements val changedElements = changeSet.changedElements
// When a code fragment is reparsed, IntelliJ doesn't do an AST diff and considers the entire // When a code fragment is reparsed, Intellij doesn't do an AST diff and considers the entire
// contents to be replaced, which is represented in a POM event as an empty list of changed elements // contents to be replaced, which is represented in a POM event as an empty list of changed elements
if (changedElements.any { getInsideCodeBlockModificationScope(it.psi) == null } || changedElements.isEmpty()) { if (changedElements.any { getInsideCodeBlockModificationScope(it.psi) == null } || changedElements.isEmpty()) {
messageBusConnection.deliverImmediately() messageBusConnection.deliverImmediately()
@@ -98,13 +96,12 @@ class KotlinCodeBlockModificationListener(
val newModCount = modificationTrackerImpl.outOfCodeBlockModificationCount val newModCount = modificationTrackerImpl.outOfCodeBlockModificationCount
val affectedModule = lastAffectedModule val affectedModule = lastAffectedModule
if (affectedModule != null && newModCount == lastAffectedModuleModCount + 1) { if (affectedModule != null && newModCount == lastAffectedModuleModCount + 1) {
if (perModuleChangesHighwatermark == null) { if (perModuleChangesHighWatermark == null) {
perModuleChangesHighwatermark = lastAffectedModuleModCount perModuleChangesHighWatermark = lastAffectedModuleModCount
} }
perModuleModCount[affectedModule] = newModCount perModuleModCount[affectedModule] = newModCount
} } else {
else { perModuleChangesHighWatermark = null
perModuleChangesHighwatermark = null
perModuleModCount.clear() perModuleModCount.clear()
} }
}) })
@@ -134,8 +131,7 @@ class KotlinCodeBlockModificationListener(
is KtNamedFunction -> { is KtNamedFunction -> {
if (blockDeclaration.hasBlockBody()) { if (blockDeclaration.hasBlockBody()) {
return blockDeclaration.bodyExpression?.takeIf { it.isAncestor(element) } return blockDeclaration.bodyExpression?.takeIf { it.isAncestor(element) }
} } else if (blockDeclaration.hasDeclaredReturnType()) {
else if (blockDeclaration.hasDeclaredReturnType()) {
return blockDeclaration.initializer?.takeIf { it.isAncestor(element) } return blockDeclaration.initializer?.takeIf { it.isAncestor(element) }
} }
} }
@@ -183,7 +179,7 @@ private val FILE_OUT_OF_BLOCK_MODIFICATION_COUNT = Key<Long>("FILE_OUT_OF_BLOCK_
val KtFile.outOfBlockModificationCount: Long val KtFile.outOfBlockModificationCount: Long
get() = getUserData(FILE_OUT_OF_BLOCK_MODIFICATION_COUNT) ?: 0 get() = getUserData(FILE_OUT_OF_BLOCK_MODIFICATION_COUNT) ?: 0
class KotlinModuleModificationTracker(val module: Module): ModificationTracker { class KotlinModuleModificationTracker(val module: Module) : ModificationTracker {
private val kotlinModCountListener = KotlinCodeBlockModificationListener.getInstance(module.project) private val kotlinModCountListener = KotlinCodeBlockModificationListener.getInstance(module.project)
private val psiModificationTracker = PsiModificationTracker.SERVICE.getInstance(module.project) private val psiModificationTracker = PsiModificationTracker.SERVICE.getInstance(module.project)
private val dependencies by lazy { private val dependencies by lazy {
@@ -191,7 +187,8 @@ class KotlinModuleModificationTracker(val module: Module): ModificationTracker {
CachedValueProvider.Result.create( CachedValueProvider.Result.create(
HashSet<Module>().apply { HashSet<Module>().apply {
ModuleRootManager.getInstance(module).orderEntries().recursively().forEachModule( ModuleRootManager.getInstance(module).orderEntries().recursively().forEachModule(
CommonProcessors.CollectProcessor(this)) CommonProcessors.CollectProcessor(this)
)
}, },
ProjectRootModificationTracker.getInstance(module.project) ProjectRootModificationTracker.getInstance(module.project)
) )