Properly positioning compilation exceptions in the IDE

This commit is contained in:
Andrey Breslav
2012-04-25 12:25:00 +04:00
parent 3eb6b3d0f5
commit d0bd5cf9c6
5 changed files with 60 additions and 14 deletions
@@ -24,6 +24,9 @@ public interface CompilationErrorHandler {
CompilationErrorHandler THROW_EXCEPTION = new CompilationErrorHandler() { CompilationErrorHandler THROW_EXCEPTION = new CompilationErrorHandler() {
@Override @Override
public void reportException(Throwable exception, String fileUrl) { public void reportException(Throwable exception, String fileUrl) {
if (exception instanceof RuntimeException) {
throw (RuntimeException)exception;
}
throw new IllegalStateException(exception); throw new IllegalStateException(exception);
} }
}; };
@@ -23,20 +23,22 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
/** /**
* @author alex.tkachman * @author alex.tkachman
* @author abreslav
*/ */
public class CompilationException extends RuntimeException { public class CompilationException extends RuntimeException {
private PsiElement element; private final PsiElement element;
CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) { CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) {
super(message, cause); super(message, cause);
this.element = element; this.element = element;
} }
@Override @NotNull
public String toString() { public PsiElement getElement() {
return getMessage(); return element;
} }
private String where() { private String where() {
Throwable cause = getCause(); Throwable cause = getCause();
Throwable throwable = cause != null ? cause : this; Throwable throwable = cause != null ? cause : this;
@@ -56,7 +58,7 @@ public class CompilationException extends RuntimeException {
message.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n"); message.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n");
} }
message.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n"); message.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n");
message.append("The root cause was thrown from: ").append(where()); message.append("The root cause was thrown at: ").append(where());
return message.toString(); return message.toString();
} }
@@ -25,11 +25,9 @@ import com.intellij.openapi.util.Disposer;
import com.sampullara.cli.Args; import com.sampullara.cli.Args;
import jet.modules.Module; import jet.modules.Module;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.CompilationException;
import org.jetbrains.jet.compiler.*; import org.jetbrains.jet.compiler.*;
import org.jetbrains.jet.compiler.messages.CompilerMessageLocation; import org.jetbrains.jet.compiler.messages.*;
import org.jetbrains.jet.compiler.messages.CompilerMessageSeverity;
import org.jetbrains.jet.compiler.messages.MessageCollector;
import org.jetbrains.jet.compiler.messages.MessageRenderer;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies; import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.utils.PathUtil; import org.jetbrains.jet.utils.PathUtil;
@@ -147,7 +145,8 @@ public class KotlinCompiler {
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, dependencies); JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, dependencies);
CompileEnvironmentConfiguration configuration = new CompileEnvironmentConfiguration(environment, dependencies, messageCollector); CompileEnvironmentConfiguration configuration = new CompileEnvironmentConfiguration(environment, dependencies, messageCollector);
configuration.getMessageCollector().report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment", CompilerMessageLocation.NO_LOCATION); messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
CompilerMessageLocation.NO_LOCATION);
try { try {
configureEnvironment(configuration, arguments); configureEnvironment(configuration, arguments);
@@ -172,8 +171,13 @@ public class KotlinCompiler {
} }
return noErrors ? OK : COMPILATION_ERROR; return noErrors ? OK : COMPILATION_ERROR;
} }
catch (CompilationException e) {
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
MessageUtil.psiElementToMessageLocation(e.getElement()));
return INTERNAL_ERROR;
}
catch (Throwable t) { catch (Throwable t) {
errStream.println(messageRenderer.renderException(t)); messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(t), CompilerMessageLocation.NO_LOCATION);
return INTERNAL_ERROR; return INTERNAL_ERROR;
} }
finally { finally {
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2012 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.jet.compiler.messages;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
/**
* @author abreslav
*/
public class MessageUtil {
public static CompilerMessageLocation psiElementToMessageLocation(PsiElement element) {
PsiFile file = element.getContainingFile();
DiagnosticUtils.LineAndColumn lineAndColumn = DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange());
return CompilerMessageLocation.create(file.getVirtualFile().getPath(), lineAndColumn.getLine(), lineAndColumn.getColumn());
}
}
@@ -80,11 +80,16 @@ public class DiagnosticUtils {
@NotNull @NotNull
public static LineAndColumn getLineAndColumn(@NotNull Diagnostic diagnostic) { public static LineAndColumn getLineAndColumn(@NotNull Diagnostic diagnostic) {
PsiFile file = diagnostic.getPsiFile(); PsiFile file = diagnostic.getPsiFile();
Document document = file.getViewProvider().getDocument();
List<TextRange> textRanges = diagnostic.getTextRanges(); List<TextRange> textRanges = diagnostic.getTextRanges();
if (textRanges.isEmpty()) return LineAndColumn.NONE; if (textRanges.isEmpty()) return LineAndColumn.NONE;
TextRange firstRange = textRanges.iterator().next(); TextRange firstRange = textRanges.iterator().next();
return offsetToLineAndColumn(document, firstRange.getStartOffset()); return getLineAndColumnInPsiFile(file, firstRange);
}
@NotNull
public static LineAndColumn getLineAndColumnInPsiFile(PsiFile file, TextRange range) {
Document document = file.getViewProvider().getDocument();
return offsetToLineAndColumn(document, range.getStartOffset());
} }
@NotNull @NotNull