Skip to content

Commit e321b99

Browse files
committed
more ensure active
1 parent 8f9d52f commit e321b99

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

ide-common/src/main/kotlin/org/digma/intellij/plugin/discovery/AbstractNavigationDiscoveryManager.kt

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.openapi.project.DumbService
88
import com.intellij.openapi.project.Project
99
import com.intellij.openapi.project.waitForSmartMode
1010
import com.intellij.openapi.roots.ProjectFileIndex
11+
import com.intellij.openapi.vfs.AsyncFileListener
1112
import com.intellij.openapi.vfs.VirtualFile
1213
import com.intellij.openapi.vfs.VirtualFileManager
1314
import com.intellij.openapi.vfs.VirtualFileManagerListener
@@ -104,25 +105,26 @@ abstract class AbstractNavigationDiscoveryManager(protected val project: Project
104105
}
105106
})
106107

107-
//this listener send too many events
108-
// VirtualFileManager.getInstance().addAsyncFileListener({ it ->
109-
// val count = it.size
110-
// object : AsyncFileListener.ChangeApplier {
111-
// override fun beforeVfsChange() {
112-
//// if(count > 10) {
113-
// Log.trace(logger, project, "Event: beforeVfsChange, count=$count")
114-
// jonManager.stop("stop because beforeVfsChange")
115-
//// }
116-
// }
117-
//
118-
// override fun afterVfsChange() {
119-
//// if (count > 10) {
120-
// Log.trace(logger, project, "Event: afterVfsChange, count=$count")
121-
// jonManager.startWithDelay()
122-
//// }
123-
// }
124-
// }
125-
// }, this)
108+
VirtualFileManager.getInstance().addAsyncFileListener({ it ->
109+
val minSizeToActivate = 50
110+
val count = it.size
111+
object : AsyncFileListener.ChangeApplier {
112+
private val isInBulkUpdate = AtomicBoolean(false)
113+
override fun beforeVfsChange() {
114+
if (isInBulkUpdate.compareAndSet(false, true) && count > minSizeToActivate) {
115+
Log.trace(logger, project, "Event: beforeVfsChange, count=$count")
116+
jonManager.stop("stop because beforeVfsChange")
117+
}
118+
}
119+
120+
override fun afterVfsChange() {
121+
if (isInBulkUpdate.compareAndSet(true, false) && count > minSizeToActivate) {
122+
Log.trace(logger, project, "Event: afterVfsChange, count=$count")
123+
jonManager.startWithDelay()
124+
}
125+
}
126+
}
127+
}, this)
126128

127129

128130
VirtualFileManager.getInstance().addVirtualFileManagerListener(object : VirtualFileManagerListener {
@@ -169,7 +171,7 @@ abstract class AbstractNavigationDiscoveryManager(protected val project: Project
169171
private var stuckTimerJob: Job? = null
170172

171173
init {
172-
managementJob = cs.launchWithErrorReporting("${this::class.java.simpleName}.JobManager", logger){
174+
managementJob = cs.launchWithErrorReporting("${this::class.java.simpleName}.JobManager", logger) {
173175
Log.trace(logger, project, "JobManager: managementJob started")
174176
jobManagementChannel.consumeEach { command: JobCommand ->
175177
Log.trace(logger, project, "JobManager: processing command: {}", command::class.java.simpleName)
@@ -208,7 +210,7 @@ abstract class AbstractNavigationDiscoveryManager(protected val project: Project
208210
// is not 0, then restart the jobs and hope for the best.
209211
private fun startStuckTimer() {
210212
stuckTimerJob?.cancel()
211-
stuckTimerJob = cs.launchWithErrorReporting("$ {this::class.java.simpleName}.JobManager.startStuckTimer", logger){
213+
stuckTimerJob = cs.launchWithErrorReporting("$ {this::class.java.simpleName}.JobManager.startStuckTimer", logger) {
212214
delay(5.minutes)
213215
@Suppress("UnstableApiUsage")
214216
project.waitForSmartMode()
@@ -227,7 +229,7 @@ abstract class AbstractNavigationDiscoveryManager(protected val project: Project
227229
}
228230

229231
fun startup() {
230-
cs.launchWithErrorReporting("${this::class.java.simpleName}.JobManager.startup", logger){
232+
cs.launchWithErrorReporting("${this::class.java.simpleName}.JobManager.startup", logger) {
231233
//on startup currentState is not relevant, if its 0 the jobs will start, if it was changed by any listener,
232234
// the jobs will not start until the currentState is 0 again when a listener calls startWithDelay
233235
Log.trace(logger, project, "JobManager: startup called, sending RestartWithDelay command")
@@ -256,7 +258,10 @@ abstract class AbstractNavigationDiscoveryManager(protected val project: Project
256258
} else {
257259
Log.warn(logger, project, "JobManager: RestartWithDelay command failed with ${result.exceptionOrNull()}")
258260
ErrorReporter.getInstance()
259-
.reportError("AbstractNavigationDiscoveryManager.JobManager.startWithDelay", result.exceptionOrNull() ?: Exception("Unknown error"))
261+
.reportError(
262+
"AbstractNavigationDiscoveryManager.JobManager.startWithDelay",
263+
result.exceptionOrNull() ?: Exception("Unknown error")
264+
)
260265
}
261266
}
262267
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/kotlin/ext/CoroutineScopeExtensions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ fun <T> CoroutineScope.asyncWithErrorReporting(
102102
} catch (e: Throwable) {
103103
Log.warnWithException(logger, e, "Error in coroutine {}", e)
104104
ErrorReporter.getInstance().reportError("asyncWithErrorReporting.$name", e)
105-
//todo: will jetbrains consider it as unhandled exception? if yes swallow the exceptions and return empty Deferred
106105
throw e
107106
}
108107
}

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/AbstractCodeObjectDiscovery.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
9696
}
9797

9898
//maybe uFile is null,there is nothing to do without a UFile.
99-
val fileData = readAction {
100-
FileData.buildFileData(psiFile)
101-
} ?: return null
99+
val fileData = FileData.buildFileData(psiFile) ?: return null
102100
coroutineContext.ensureActive()
103101

104102
val packageName = fileData.packageName
@@ -208,10 +206,12 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
208206

209207
private class FileData(val uFile: UFile, val packageName: String) {
210208
companion object {
211-
fun buildFileData(psiFile: PsiFile): FileData? {
212-
val uFile: UFile? = psiFile.toUElementOfType<UFile>()
213-
return uFile?.let {
214-
val packageName = it.packageName
209+
suspend fun buildFileData(psiFile: PsiFile): FileData? {
210+
coroutineContext.ensureActive()
211+
val uFile: UFile? = readAction { psiFile.takeIf { it.isValid }?.toUElementOfType<UFile>() }
212+
return uFile?.takeIf { it.isPsiValid }?.let {
213+
coroutineContext.ensureActive()
214+
val packageName = readAction { it.packageName }
215215
FileData(it, packageName)
216216
}
217217
}

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/span/AbstractSpanDiscovery.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.digma.intellij.plugin.idea.discovery.span
22

3+
import com.intellij.openapi.application.readAction
34
import com.intellij.openapi.application.smartReadAction
45
import com.intellij.openapi.components.service
56
import com.intellij.openapi.diagnostic.Logger
@@ -159,10 +160,16 @@ abstract class AbstractSpanDiscovery {
159160
searchScope: SearchScopeProvider,
160161
): Collection<UReferenceExpression>? {
161162

162-
return smartReadAction(project) {
163+
coroutineContext.ensureActive()
164+
val methodReferences = smartReadAction(project) {
163165
startSpanMethodPointer.element?.let { startSpanMethod ->
164-
val methodReferences = MethodReferencesSearch.search(startSpanMethod, searchScope.get(), true)
165-
methodReferences.findAll().mapNotNull { psiReference: PsiReference -> psiReference.element.toUElementOfType<UReferenceExpression>() }
166+
MethodReferencesSearch.search(startSpanMethod, searchScope.get(), true)
167+
}
168+
}
169+
coroutineContext.ensureActive()
170+
return methodReferences?.let {
171+
readAction {
172+
it.findAll().mapNotNull { psiReference: PsiReference -> psiReference.element.toUElementOfType<UReferenceExpression>() }
166173
}
167174
}
168175
}

0 commit comments

Comments
 (0)