11package com.sourcegraph.semanticdb_kotlinc
22
3+ import java.io.PrintWriter
4+ import java.io.Writer
35import java.nio.file.Files
46import java.nio.file.Path
57import java.nio.file.Paths
68import kotlin.contracts.ExperimentalContracts
79import org.jetbrains.kotlin.analyzer.AnalysisResult
10+ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
11+ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
12+ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
813import org.jetbrains.kotlin.com.intellij.openapi.project.Project
14+ import org.jetbrains.kotlin.config.CompilerConfiguration
915import org.jetbrains.kotlin.descriptors.ModuleDescriptor
1016import org.jetbrains.kotlin.psi.*
1117import org.jetbrains.kotlin.resolve.BindingTrace
1218import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
19+ import org.jetbrains.kotlin.utils.rethrow
20+ import java.io.StringWriter
1321
1422@ExperimentalContracts
1523class Analyzer (
@@ -19,24 +27,37 @@ class Analyzer(
1927) : AnalysisHandlerExtension {
2028 private val globals = GlobalSymbolsCache ()
2129
30+ private val messageCollector =
31+ CompilerConfiguration ()
32+ .get(CLIConfigurationKeys .MESSAGE_COLLECTOR_KEY , MessageCollector .NONE )
33+
2234 override fun analysisCompleted (
2335 project : Project ,
2436 module : ModuleDescriptor ,
2537 bindingTrace : BindingTrace ,
2638 files : Collection <KtFile >
27- ): AnalysisResult ? {
28- val resolver = DescriptorResolver (bindingTrace).also { globals.resolver = it }
29- for (file in files) {
30- val lineMap = LineMap (project, file)
31- val document = SemanticdbVisitor (sourceroot, resolver, file, lineMap, globals).build()
32- semanticdbOutPathForFile(file)?.apply {
33- Files .write(this , TextDocuments { addDocuments(document) }.toByteArray())
39+ ): AnalysisResult ? =
40+ try {
41+ val resolver = DescriptorResolver (bindingTrace).also { globals.resolver = it }
42+ for (file in files) {
43+ try {
44+ val lineMap = LineMap (project, file)
45+ val document =
46+ SemanticdbVisitor (sourceroot, resolver, file, lineMap, globals).build()
47+ semanticdbOutPathForFile(file)?.apply {
48+ Files .write(this , TextDocuments { addDocuments(document) }.toByteArray())
49+ }
50+ callback(document)
51+ } catch (e: Exception ) {
52+ handleException(e)
53+ }
3454 }
35- callback(document)
36- }
3755
38- return super .analysisCompleted(project, module, bindingTrace, files)
39- }
56+ super .analysisCompleted(project, module, bindingTrace, files)
57+ } catch (e: Exception ) {
58+ handleException(e)
59+ super .analysisCompleted(project, module, bindingTrace, files)
60+ }
4061
4162 private fun semanticdbOutPathForFile (file : KtFile ): Path ? {
4263 val normalizedPath = Paths .get(file.virtualFilePath).normalize()
@@ -57,4 +78,24 @@ class Analyzer(
5778 " given file is not under the sourceroot.\n\t Sourceroot: $sourceroot \n\t File path: ${file.virtualFilePath} \n\t Normalized file path: $normalizedPath " )
5879 return null
5980 }
81+
82+ private fun handleException (e : Exception ) {
83+ val writer =
84+ PrintWriter (
85+ object : Writer () {
86+ val buf = StringBuffer ()
87+ override fun close () =
88+ messageCollector.report(
89+ CompilerMessageSeverity .EXCEPTION , buf.toString())
90+ override fun flush () = Unit
91+ override fun write (data : CharArray , offset : Int , len : Int ) {
92+ buf.append(data, offset, len)
93+ }
94+ },
95+ false )
96+ e.printStackTrace(writer)
97+ writer.println (
98+ " Please report a bug to https://github.com/sourcegraph/lsif-kotlin with the stack trace above." )
99+ writer.close()
100+ }
60101}
0 commit comments