Minor: extract method

This commit is contained in:
Natalia Ukhorskaya
2015-09-01 13:16:18 +03:00
parent 73946050c1
commit 4980780268
@@ -44,42 +44,23 @@ public class KotlinSteppingCommandProvider: JvmSteppingCommandProvider() {
): DebugProcessImpl.ResumeCommand? { ): DebugProcessImpl.ResumeCommand? {
if (suspendContext == null) return null if (suspendContext == null) return null
val semaphore = Semaphore() val result: Pair<SourcePosition, List<ReferenceType>>? = computeInManagerThread(suspendContext) {
semaphore.down() val sp = runReadAction { ContextUtil.getSourcePosition(it) } ?: return@computeInManagerThread null
val cl = it.debugProcess.positionManager.getAllClasses(sp)
var sourcePosition : SourcePosition? = null sp to cl
var allClasses: List<ReferenceType>? = null
val worker = object : DebuggerCommandImpl() {
override fun action() {
try {
sourcePosition = runReadAction { ContextUtil.getSourcePosition(suspendContext) }
if (sourcePosition != null) {
allClasses = suspendContext.debugProcess.positionManager.getAllClasses(sourcePosition!!)
}
}
finally {
semaphore.up()
}
}
} }
suspendContext.debugProcess.managerThread?.invoke(worker) val (sourcePosition, allClasses) = result ?: return null
val computedReferenceType = allClasses.firstOrNull() ?: return null
for (i in 0..25) { val file = sourcePosition.file as? JetFile ?: return null
if (semaphore.waitFor(20)) break
}
val computedSourcePosition = sourcePosition ?: return null val inlinedArguments = getInlinedArgumentsIfAny(sourcePosition) ?: return null
val computedReferenceType = allClasses?.firstOrNull() ?: return null
val file = computedSourcePosition.file as? JetFile ?: return null
val inlinedArguments = getInlinedArgumentsIfAny(computedSourcePosition) ?: return null
val locations = computedReferenceType.allLineLocations() val locations = computedReferenceType.allLineLocations()
val countOfLinesInFile = file.getLineCount() val countOfLinesInFile = file.getLineCount()
val nextLine = computedSourcePosition.line + 2 /* +1 - because of locations are counted from 1 and +1 - because we want next line */ val nextLine = sourcePosition.line + 2 /* +1 - because of locations are counted from 1 and +1 - because we want next line */
for (lineNumber in nextLine..countOfLinesInFile) { for (lineNumber in nextLine..countOfLinesInFile) {
val location = locations.firstOrNull { it.lineNumber() == lineNumber } val location = locations.firstOrNull { it.lineNumber() == lineNumber }
@@ -97,6 +78,31 @@ public class KotlinSteppingCommandProvider: JvmSteppingCommandProvider() {
return null return null
} }
private fun <T: Any> computeInManagerThread(suspendContext: SuspendContextImpl, action: (SuspendContextImpl) -> T?): T? {
val semaphore = Semaphore()
semaphore.down()
var result : T? = null
val worker = object : DebuggerCommandImpl() {
override fun action() {
try {
result = action(suspendContext)
}
finally {
semaphore.up()
}
}
}
suspendContext.debugProcess.managerThread?.invoke(worker)
for (i in 0..25) {
if (semaphore.waitFor(20)) break
}
return result
}
private fun getInlinedArgumentsIfAny(sourcePosition: SourcePosition): List<JetFunction>? { private fun getInlinedArgumentsIfAny(sourcePosition: SourcePosition): List<JetFunction>? {
val file = sourcePosition.file as? JetFile ?: return null val file = sourcePosition.file as? JetFile ?: return null
val lineNumber = sourcePosition.line val lineNumber = sourcePosition.line