提交 d0bd5cf9 编写于 作者: A Andrey Breslav

Properly positioning compilation exceptions in the IDE

上级 3eb6b3d0
......@@ -24,6 +24,9 @@ public interface CompilationErrorHandler {
CompilationErrorHandler THROW_EXCEPTION = new CompilationErrorHandler() {
@Override
public void reportException(Throwable exception, String fileUrl) {
if (exception instanceof RuntimeException) {
throw (RuntimeException)exception;
}
throw new IllegalStateException(exception);
}
};
......
......@@ -23,20 +23,22 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
/**
* @author alex.tkachman
* @author abreslav
*/
public class CompilationException extends RuntimeException {
private PsiElement element;
private final PsiElement element;
CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) {
super(message, cause);
this.element = element;
}
@Override
public String toString() {
return getMessage();
@NotNull
public PsiElement getElement() {
return element;
}
private String where() {
Throwable cause = getCause();
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("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();
}
......
......@@ -25,11 +25,9 @@ import com.intellij.openapi.util.Disposer;
import com.sampullara.cli.Args;
import jet.modules.Module;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.CompilationException;
import org.jetbrains.jet.compiler.*;
import org.jetbrains.jet.compiler.messages.CompilerMessageLocation;
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.compiler.messages.*;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.utils.PathUtil;
......@@ -147,7 +145,8 @@ public class KotlinCompiler {
JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, dependencies);
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 {
configureEnvironment(configuration, arguments);
......@@ -172,8 +171,13 @@ public class KotlinCompiler {
}
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) {
errStream.println(messageRenderer.renderException(t));
messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(t), CompilerMessageLocation.NO_LOCATION);
return INTERNAL_ERROR;
}
finally {
......
/*
* 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
public static LineAndColumn getLineAndColumn(@NotNull Diagnostic diagnostic) {
PsiFile file = diagnostic.getPsiFile();
Document document = file.getViewProvider().getDocument();
List<TextRange> textRanges = diagnostic.getTextRanges();
if (textRanges.isEmpty()) return LineAndColumn.NONE;
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册