Minor, simplify FirDiagnosticsCompilerResultsReporter.reportByFile

This commit is contained in:
Alexander Udalov
2023-06-26 17:02:24 +02:00
parent f2ea2021d7
commit b81c637fed
@@ -35,25 +35,16 @@ object FirDiagnosticsCompilerResultsReporter {
} }
} }
fun reportByFile( private fun reportByFile(
diagnosticsCollector: BaseDiagnosticsCollector, report: (KtDiagnostic, CompilerMessageSourceLocation) -> Unit diagnosticsCollector: BaseDiagnosticsCollector, report: (KtDiagnostic, CompilerMessageSourceLocation) -> Unit
): Boolean { ): Boolean {
var hasErrors = false var hasErrors = false
for (filePath in diagnosticsCollector.diagnosticsByFilePath.keys) { for (filePath in diagnosticsCollector.diagnosticsByFilePath.keys) {
// emulating lazy because of the usage pattern in finally block below (should not initialize in finally) val positionFinder = lazy {
var positionFinderInitialized = false val file = filePath?.let(::File)
var positionFinder: SequentialFilePositionFinder? = null if (file != null && file.isFile) SequentialFilePositionFinder(file) else null
}
fun getPositionFinder() =
if (positionFinderInitialized) positionFinder
else {
positionFinderInitialized = true
filePath?.let(::File)?.takeIf { it.isFile }?.let(::SequentialFilePositionFinder)?.also {
positionFinder = it
}
}
@Suppress("ConvertTryFinallyToUseCall")
try { try {
for (diagnostic in diagnosticsCollector.diagnosticsByFilePath[filePath].orEmpty().sortedWith(InFileDiagnosticsComparator)) { for (diagnostic in diagnosticsCollector.diagnosticsByFilePath[filePath].orEmpty().sortedWith(InFileDiagnosticsComparator)) {
when (diagnostic) { when (diagnostic) {
@@ -70,7 +61,7 @@ object FirDiagnosticsCompilerResultsReporter {
// NOTE: SequentialPositionFinder relies on the ascending order of the input offsets, so the code relies // NOTE: SequentialPositionFinder relies on the ascending order of the input offsets, so the code relies
// on the the appropriate sorting above // on the the appropriate sorting above
// Also the end offset is ignored, as it is irrelevant for the CLI reporting // Also the end offset is ignored, as it is irrelevant for the CLI reporting
getPositionFinder()?.findNextPosition(DiagnosticUtils.firstRange(diagnostic.textRanges).startOffset) positionFinder.value?.findNextPosition(DiagnosticUtils.firstRange(diagnostic.textRanges).startOffset)
?.let { pos -> ?.let { pos ->
MessageUtil.createMessageLocation(filePath, pos.lineContent, pos.line, pos.column, -1, -1) MessageUtil.createMessageLocation(filePath, pos.lineContent, pos.line, pos.column, -1, -1)
} }
@@ -81,7 +72,9 @@ object FirDiagnosticsCompilerResultsReporter {
} }
} }
} finally { } finally {
positionFinder?.close() if (positionFinder.isInitialized()) {
positionFinder.value?.close()
}
} }
} }
// TODO: for uncommenting, see comment in reportSpecialErrors // TODO: for uncommenting, see comment in reportSpecialErrors