Kotlinify KotlinCompilerManager p.2
Relates to #KT-38407
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
bbd4116b67
commit
4f99ac406a
@@ -13,102 +13,86 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
package org.jetbrains.kotlin.idea.compiler
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
import com.intellij.diagnostic.PluginException;
|
class KotlinCompilerManager(project: Project, manager: CompilerManager) : ProjectComponent {
|
||||||
import com.intellij.ide.plugins.PluginManagerCore;
|
// Extending PluginException ensures that Exception Analyzer recognizes this as a Kotlin exception
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
private class KotlinCompilerException(private val text: String) :
|
||||||
import com.intellij.openapi.compiler.*;
|
PluginException("", PluginManagerCore.getPluginByClassName(KotlinCompilerManager::class.java.name)) {
|
||||||
import com.intellij.openapi.components.ProjectComponent;
|
override fun printStackTrace(s: PrintWriter) {
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
s.print(text)
|
||||||
import com.intellij.openapi.project.Project;
|
}
|
||||||
import com.intellij.openapi.util.io.FileUtilRt;
|
|
||||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinFileType;
|
|
||||||
import org.jetbrains.kotlin.js.JavaScript;
|
|
||||||
|
|
||||||
import java.io.PrintStream;
|
override fun printStackTrace(s: PrintStream) {
|
||||||
import java.io.PrintWriter;
|
s.print(text)
|
||||||
import java.util.Set;
|
}
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.config.CompilerRunnerConstants.INTERNAL_ERROR_PREFIX;
|
@Synchronized
|
||||||
import static org.jetbrains.kotlin.config.CompilerRunnerConstants.KOTLIN_COMPILER_NAME;
|
override fun fillInStackTrace(): Throwable {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
public class KotlinCompilerManager implements ProjectComponent {
|
override fun getStackTrace(): Array<StackTraceElement> {
|
||||||
private static final Logger LOG = Logger.getInstance(KotlinCompilerManager.class);
|
LOG.error("Somebody called getStackTrace() on KotlinCompilerException")
|
||||||
|
// Return some stack trace that originates in Kotlin
|
||||||
|
return UnsupportedOperationException().stackTrace
|
||||||
|
}
|
||||||
|
|
||||||
// Comes from external make
|
override val message: String
|
||||||
private static final String PREFIX_WITH_COMPILER_NAME = KOTLIN_COMPILER_NAME + ": " + INTERNAL_ERROR_PREFIX;
|
get() = "<Exception from standalone Kotlin compiler>"
|
||||||
private static final Set<String> FILE_EXTS_WHICH_NEEDS_REFRESH = ContainerUtil.immutableSet(JavaScript.DOT_EXTENSION, ".map");
|
|
||||||
|
|
||||||
public KotlinCompilerManager(Project project, CompilerManager manager) {
|
}
|
||||||
manager.addCompilableFileType(KotlinFileType.INSTANCE);
|
|
||||||
manager.addCompilationStatusListener(new CompilationStatusListener() {
|
companion object {
|
||||||
@Override
|
private val LOG = Logger.getInstance(KotlinCompilerManager::class.java)
|
||||||
public void compilationFinished(boolean aborted, int errors, int warnings, CompileContext compileContext) {
|
|
||||||
for (CompilerMessage error : compileContext.getMessages(CompilerMessageCategory.ERROR)) {
|
// Comes from external make
|
||||||
String message = error.getMessage();
|
private const val PREFIX_WITH_COMPILER_NAME =
|
||||||
if (message.startsWith(INTERNAL_ERROR_PREFIX) || message.startsWith(PREFIX_WITH_COMPILER_NAME)) {
|
CompilerRunnerConstants.KOTLIN_COMPILER_NAME + ": " + CompilerRunnerConstants.INTERNAL_ERROR_PREFIX
|
||||||
LOG.error(new KotlinCompilerException(message));
|
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
|
override fun fileGenerated(outputRoot: String, relativePath: String) {
|
||||||
public void fileGenerated(String outputRoot, String relativePath) {
|
if (ApplicationManager.getApplication().isUnitTestMode) return
|
||||||
if (ApplicationManager.getApplication().isUnitTestMode()) return;
|
val ext = FileUtilRt.getExtension(relativePath).toLowerCase()
|
||||||
|
|
||||||
String ext = FileUtilRt.getExtension(relativePath).toLowerCase();
|
|
||||||
|
|
||||||
if (FILE_EXTS_WHICH_NEEDS_REFRESH.contains(ext)) {
|
if (FILE_EXTS_WHICH_NEEDS_REFRESH.contains(ext)) {
|
||||||
String outFile = outputRoot + "/" + relativePath;
|
val outFile = "$outputRoot/$relativePath"
|
||||||
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(outFile);
|
val virtualFile = LocalFileSystem.getInstance().findFileByPath(outFile)
|
||||||
assert virtualFile != null : "Virtual file not found for generated file path: " + outFile;
|
?: error("Virtual file not found for generated file path: $outFile")
|
||||||
virtualFile.refresh(/*async =*/ false, /*recursive =*/ false);
|
virtualFile.refresh( /*async =*/false, /*recursive =*/false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, project);
|
}, project)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Extending PluginException ensures that Exception Analyzer recognizes this as a Kotlin exception
|
|
||||||
private static class KotlinCompilerException extends PluginException {
|
|
||||||
private final String text;
|
|
||||||
|
|
||||||
public KotlinCompilerException(String text) {
|
|
||||||
super("", PluginManagerCore.getPluginByClassName(KotlinCompilerManager.class.getName()));
|
|
||||||
this.text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void printStackTrace(PrintWriter s) {
|
|
||||||
s.print(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void printStackTrace(@NotNull PrintStream s) {
|
|
||||||
s.print(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public synchronized Throwable fillInStackTrace() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public StackTraceElement[] getStackTrace() {
|
|
||||||
LOG.error("Somebody called getStackTrace() on KotlinCompilerException");
|
|
||||||
// Return some stack trace that originates in Kotlin
|
|
||||||
return new UnsupportedOperationException().getStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return "<Exception from standalone Kotlin compiler>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user