From 346cc40f442c2190bd59e196796ad9a289287c8e Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 20 Apr 2020 17:38:09 +0200 Subject: [PATCH] Convert KotlinCompilerManager into a startup activity Relates to #KT-38407 --- .../idea/compiler/KotlinCompilerManager.kt | 45 ++++----- .../compiler/KotlinCompilerManager.kt.192 | 99 +++++++++++++++++++ idea/resources/META-INF/jvm-common.xml | 3 - idea/resources/META-INF/jvm.xml | 1 + idea/resources/META-INF/jvm.xml.192 | 4 + idea/resources/META-INF/jvm.xml.201 | 1 + 6 files changed, 122 insertions(+), 31 deletions(-) create mode 100644 idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt.192 diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt index 6482dc65b0e..869e92463e7 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt @@ -1,43 +1,30 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ + package org.jetbrains.kotlin.idea.compiler import com.intellij.diagnostic.PluginException import com.intellij.ide.plugins.PluginManagerCore -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.compiler.CompilationStatusListener -import com.intellij.openapi.compiler.CompileContext -import com.intellij.openapi.compiler.CompilerManager -import com.intellij.openapi.compiler.CompilerMessageCategory -import com.intellij.openapi.components.ProjectComponent +import com.intellij.openapi.compiler.* import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.StartupActivity import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.LocalFileSystem -import com.intellij.util.containers.ContainerUtil import org.jetbrains.kotlin.config.CompilerRunnerConstants import org.jetbrains.kotlin.idea.KotlinFileType +import org.jetbrains.kotlin.idea.util.application.isUnitTestMode import org.jetbrains.kotlin.js.JavaScript import java.io.PrintStream import java.io.PrintWriter -class KotlinCompilerManager(project: Project, manager: CompilerManager) : ProjectComponent { +// BUNCH: 192 +class KotlinCompilerStartupActivity : StartupActivity { // Extending PluginException ensures that Exception Analyzer recognizes this as a Kotlin exception private class KotlinCompilerException(private val text: String) : - PluginException("", PluginManagerCore.getPluginByClassName(KotlinCompilerManager::class.java.name)) { + PluginException("", PluginManagerCore.getPluginByClassName(KotlinCompilerStartupActivity::class.java.name)) { override fun printStackTrace(s: PrintWriter) { s.print(text) } @@ -63,17 +50,18 @@ class KotlinCompilerManager(project: Project, manager: CompilerManager) : Projec } companion object { - private val LOG = Logger.getInstance(KotlinCompilerManager::class.java) + private val LOG = Logger.getInstance(KotlinCompilerStartupActivity::class.java) // Comes from external make private const val PREFIX_WITH_COMPILER_NAME = CompilerRunnerConstants.KOTLIN_COMPILER_NAME + ": " + CompilerRunnerConstants.INTERNAL_ERROR_PREFIX - private val FILE_EXTS_WHICH_NEEDS_REFRESH = ContainerUtil.immutableSet(JavaScript.DOT_EXTENSION, ".map") + private val FILE_EXTS_WHICH_NEEDS_REFRESH = setOf(JavaScript.DOT_EXTENSION, ".map") } - init { + override fun runActivity(project: Project) { + val manager = CompilerManager.getInstance(project) manager.addCompilableFileType(KotlinFileType.INSTANCE) - manager.addCompilationStatusListener(object : CompilationStatusListener { + project.messageBus.connect().subscribe(CompilerTopics.COMPILATION_STATUS, object : CompilationStatusListener { override fun compilationFinished(aborted: Boolean, errors: Int, warnings: Int, compileContext: CompileContext) { for (error in compileContext.getMessages(CompilerMessageCategory.ERROR)) { val message = error.message @@ -84,7 +72,8 @@ class KotlinCompilerManager(project: Project, manager: CompilerManager) : Projec } override fun fileGenerated(outputRoot: String, relativePath: String) { - if (ApplicationManager.getApplication().isUnitTestMode) return + if (isUnitTestMode()) return + val ext = FileUtilRt.getExtension(relativePath).toLowerCase() if (FILE_EXTS_WHICH_NEEDS_REFRESH.contains(ext)) { val outFile = "$outputRoot/$relativePath" @@ -93,6 +82,6 @@ class KotlinCompilerManager(project: Project, manager: CompilerManager) : Projec virtualFile.refresh( /*async =*/false, /*recursive =*/false) } } - }, project) + }) } } \ No newline at end of file diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt.192 b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt.192 new file mode 100644 index 00000000000..ef6c77c5506 --- /dev/null +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/compiler/KotlinCompilerManager.kt.192 @@ -0,0 +1,99 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.kotlin.idea.compiler + +import com.intellij.diagnostic.PluginException +import com.intellij.ide.plugins.PluginManagerCore +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.compiler.CompilationStatusListener +import com.intellij.openapi.compiler.CompileContext +import com.intellij.openapi.compiler.CompilerManager +import com.intellij.openapi.compiler.CompilerMessageCategory +import com.intellij.openapi.components.ProjectComponent +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.io.FileUtilRt +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.util.containers.ContainerUtil +import org.jetbrains.kotlin.config.CompilerRunnerConstants +import org.jetbrains.kotlin.idea.KotlinFileType +import org.jetbrains.kotlin.js.JavaScript +import java.io.PrintStream +import java.io.PrintWriter + +// BUNCH: 192 +class KotlinCompilerManager(project: Project, manager: CompilerManager) : ProjectComponent { + // Extending PluginException ensures that Exception Analyzer recognizes this as a Kotlin exception + private class KotlinCompilerException(private val text: String) : + PluginException("", PluginManagerCore.getPluginByClassName(KotlinCompilerManager::class.java.name)) { + override fun printStackTrace(s: PrintWriter) { + s.print(text) + } + + override fun printStackTrace(s: PrintStream) { + s.print(text) + } + + @Synchronized + override fun fillInStackTrace(): Throwable { + return this + } + + override fun getStackTrace(): Array { + LOG.error("Somebody called getStackTrace() on KotlinCompilerException") + // Return some stack trace that originates in Kotlin + return UnsupportedOperationException().stackTrace + } + + override val message: String + get() = "" + + } + + companion object { + private val LOG = Logger.getInstance(KotlinCompilerManager::class.java) + + // Comes from external make + private const val PREFIX_WITH_COMPILER_NAME = + CompilerRunnerConstants.KOTLIN_COMPILER_NAME + ": " + CompilerRunnerConstants.INTERNAL_ERROR_PREFIX + private val FILE_EXTS_WHICH_NEEDS_REFRESH = ContainerUtil.immutableSet(JavaScript.DOT_EXTENSION, ".map") + } + + init { + manager.addCompilableFileType(KotlinFileType.INSTANCE) + manager.addCompilationStatusListener(object : CompilationStatusListener { + override fun compilationFinished(aborted: Boolean, errors: Int, warnings: Int, compileContext: CompileContext) { + for (error in compileContext.getMessages(CompilerMessageCategory.ERROR)) { + val message = error.message + if (message.startsWith(CompilerRunnerConstants.INTERNAL_ERROR_PREFIX) || message.startsWith(PREFIX_WITH_COMPILER_NAME)) { + LOG.error(KotlinCompilerException(message)) + } + } + } + + override fun fileGenerated(outputRoot: String, relativePath: String) { + if (ApplicationManager.getApplication().isUnitTestMode) return + val ext = FileUtilRt.getExtension(relativePath).toLowerCase() + if (FILE_EXTS_WHICH_NEEDS_REFRESH.contains(ext)) { + val outFile = "$outputRoot/$relativePath" + val virtualFile = LocalFileSystem.getInstance().findFileByPath(outFile) + ?: error("Virtual file not found for generated file path: $outFile") + virtualFile.refresh( /*async =*/false, /*recursive =*/false) + } + } + }, project) + } +} \ No newline at end of file diff --git a/idea/resources/META-INF/jvm-common.xml b/idea/resources/META-INF/jvm-common.xml index 48ab2a379f9..2e1687132c0 100644 --- a/idea/resources/META-INF/jvm-common.xml +++ b/idea/resources/META-INF/jvm-common.xml @@ -1,8 +1,5 @@ - - org.jetbrains.kotlin.idea.compiler.KotlinCompilerManager - org.jetbrains.kotlin.idea.scratch.ScratchFileModuleInfoProvider diff --git a/idea/resources/META-INF/jvm.xml b/idea/resources/META-INF/jvm.xml index 7a55680b1f2..9941b8cb5ef 100644 --- a/idea/resources/META-INF/jvm.xml +++ b/idea/resources/META-INF/jvm.xml @@ -3,6 +3,7 @@ + diff --git a/idea/resources/META-INF/jvm.xml.192 b/idea/resources/META-INF/jvm.xml.192 index a35904ac9ec..9181ff60274 100644 --- a/idea/resources/META-INF/jvm.xml.192 +++ b/idea/resources/META-INF/jvm.xml.192 @@ -6,6 +6,10 @@ + + org.jetbrains.kotlin.idea.compiler.KotlinCompilerManager + + org.jetbrains.kotlin.idea.configuration.ui.KotlinConfigurationCheckerComponent diff --git a/idea/resources/META-INF/jvm.xml.201 b/idea/resources/META-INF/jvm.xml.201 index 390e643e7e4..7b7605ae063 100644 --- a/idea/resources/META-INF/jvm.xml.201 +++ b/idea/resources/META-INF/jvm.xml.201 @@ -7,6 +7,7 @@ +