CLI: output relative paths in diagnostic messages

This commit is contained in:
Alexander Udalov
2014-07-23 16:38:29 +04:00
parent 807cf1dcc0
commit 5a792ca11f
10 changed files with 67 additions and 43 deletions
@@ -17,10 +17,11 @@
package org.jetbrains.jet.cli.common.messages;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.io.IoPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.File;
public interface MessageRenderer {
@@ -57,24 +58,48 @@ public interface MessageRenderer {
}
};
MessageRenderer PLAIN = new MessageRenderer() {
MessageRenderer PLAIN = new PlainText() {
@Nullable
@Override
protected String getPath(@NotNull CompilerMessageLocation location) {
return location.getPath();
}
};
MessageRenderer PLAIN_WITH_RELATIVE_PATH = new PlainText() {
private final File cwd = new File(".").getAbsoluteFile();
@Nullable
@Override
protected String getPath(@NotNull CompilerMessageLocation location) {
String path = location.getPath();
return cwd == null || path == null ? path : IoPackage.relativePath(cwd, new File(path));
}
};
abstract class PlainText implements MessageRenderer {
@Override
public String renderPreamble() {
return "";
}
@Override
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
String path = location.getPath();
public String render(
@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location
) {
String path = getPath(location);
String position = path == null ? "" : path + ": (" + (location.getLine() + ", " + location.getColumn()) + ") ";
return severity + ": " + position + message;
}
@Nullable
protected abstract String getPath(@NotNull CompilerMessageLocation location);
@Override
public String renderConclusion() {
return "";
}
};
}
String renderPreamble();
String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location);
@@ -34,7 +34,6 @@ import java.util.List;
import static org.jetbrains.jet.cli.common.ExitCode.*;
public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
private List<CompilerPlugin> compilerPlugins = Lists.newArrayList();
@@ -49,7 +48,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@NotNull
public ExitCode exec(@NotNull PrintStream errStream, @NotNull String... args) {
return exec(errStream, MessageRenderer.PLAIN, args);
return exec(errStream, MessageRenderer.PLAIN_WITH_RELATIVE_PATH, args);
}
@SuppressWarnings("UnusedDeclaration") // Used via reflection in CompilerRunnerUtil#invokeExecMethod