Minor: cleanup and reformat KotlinCodeBlockModificationListener.kt
This commit is contained in:
+26
-29
@@ -46,9 +46,9 @@ val KOTLIN_CONSOLE_KEY = Key.create<Boolean>("kotlin.console")
|
|||||||
* Tested in OutOfBlockModificationTestGenerated
|
* Tested in OutOfBlockModificationTestGenerated
|
||||||
*/
|
*/
|
||||||
class KotlinCodeBlockModificationListener(
|
class KotlinCodeBlockModificationListener(
|
||||||
modificationTracker: PsiModificationTracker,
|
modificationTracker: PsiModificationTracker,
|
||||||
project: Project,
|
project: Project,
|
||||||
private val treeAspect: TreeAspect
|
private val treeAspect: TreeAspect
|
||||||
) {
|
) {
|
||||||
private val perModuleModCount = mutableMapOf<Module, Long>()
|
private val perModuleModCount = mutableMapOf<Module, Long>()
|
||||||
private val modificationTrackerImpl = modificationTracker as PsiModificationTrackerImpl
|
private val modificationTrackerImpl = modificationTracker as PsiModificationTrackerImpl
|
||||||
@@ -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()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -124,7 +121,7 @@ class KotlinCodeBlockModificationListener(
|
|||||||
val lambda = element.getTopmostParentOfType<KtLambdaExpression>()
|
val lambda = element.getTopmostParentOfType<KtLambdaExpression>()
|
||||||
if (lambda is KtLambdaExpression) {
|
if (lambda is KtLambdaExpression) {
|
||||||
lambda.getTopmostParentOfType<KtSuperTypeCallEntry>()
|
lambda.getTopmostParentOfType<KtSuperTypeCallEntry>()
|
||||||
?.let { return it }
|
?.let { return it }
|
||||||
}
|
}
|
||||||
|
|
||||||
val blockDeclaration = KtPsiUtil.getTopmostParentOfTypes(element, *BLOCK_DECLARATION_TYPES) as? KtDeclaration ?: return null
|
val blockDeclaration = KtPsiUtil.getTopmostParentOfTypes(element, *BLOCK_DECLARATION_TYPES) as? KtDeclaration ?: return null
|
||||||
@@ -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) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,8 +140,8 @@ class KotlinCodeBlockModificationListener(
|
|||||||
if (blockDeclaration.typeReference != null) {
|
if (blockDeclaration.typeReference != null) {
|
||||||
for (accessor in blockDeclaration.accessors) {
|
for (accessor in blockDeclaration.accessors) {
|
||||||
(accessor.initializer ?: accessor.bodyExpression)
|
(accessor.initializer ?: accessor.bodyExpression)
|
||||||
?.takeIf { it.isAncestor(element) }
|
?.takeIf { it.isAncestor(element) }
|
||||||
?.let { return it }
|
?.let { return it }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,9 +165,9 @@ class KotlinCodeBlockModificationListener(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val BLOCK_DECLARATION_TYPES = arrayOf<Class<out KtDeclaration>>(
|
private val BLOCK_DECLARATION_TYPES = arrayOf<Class<out KtDeclaration>>(
|
||||||
KtProperty::class.java,
|
KtProperty::class.java,
|
||||||
KtNamedFunction::class.java,
|
KtNamedFunction::class.java,
|
||||||
KtScriptInitializer::class.java
|
KtScriptInitializer::class.java
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getInstance(project: Project) = project.getComponent(KotlinCodeBlockModificationListener::class.java)
|
fun getInstance(project: Project) = project.getComponent(KotlinCodeBlockModificationListener::class.java)
|
||||||
@@ -183,17 +179,18 @@ 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 {
|
||||||
module.cached(CachedValueProvider {
|
module.cached(CachedValueProvider {
|
||||||
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)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user