Drop CompilerMessageLocation.NO_LOCATION, use null everywhere instead

This commit is contained in:
Alexander Udalov
2017-03-31 20:39:22 +03:00
parent 861d9a1620
commit cb4d2994a3
12 changed files with 31 additions and 47 deletions
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.cli.common.messages
import java.io.Serializable
data class CompilerMessageLocation private constructor(
val path: String?,
val path: String,
val line: Int,
val column: Int,
val lineContent: String?
@@ -28,16 +28,13 @@ data class CompilerMessageLocation private constructor(
path + (if (line != -1 || column != -1) " ($line:$column)" else "")
companion object {
@JvmField
val NO_LOCATION: CompilerMessageLocation = create(null)
@JvmStatic
fun create(path: String?): CompilerMessageLocation? =
create(path, -1, -1, null)
@JvmStatic
fun create(path: String?): CompilerMessageLocation =
CompilerMessageLocation(path, -1, -1, null)
@JvmStatic
fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation =
if (path == null) NO_LOCATION else CompilerMessageLocation(path, line, column, lineContent)
fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation? =
if (path == null) null else CompilerMessageLocation(path, line, column, lineContent)
private val serialVersionUID: Long = 8228357578L
}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cli.common.repl
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import java.io.Reader
import java.util.concurrent.locks.ReentrantReadWriteLock
import javax.script.*
@@ -109,6 +108,5 @@ abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEn
}
private fun ReplCompileResult.Error.locationString() =
if (location == CompilerMessageLocation.NO_LOCATION) ""
if (location == null) ""
else " at ${location.line}:${location.column}"
@@ -62,9 +62,8 @@ sealed class ReplCheckResult : Serializable {
class Incomplete : ReplCheckResult()
class Error(val message: String,
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCheckResult() {
override fun toString(): String = "Error(message = \"$message\""
class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCheckResult() {
override fun toString(): String = "Error(message = \"$message\")"
}
companion object {
@@ -88,8 +87,7 @@ sealed class ReplCompileResult : Serializable {
class Incomplete : ReplCompileResult()
class Error(val message: String,
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCompileResult() {
class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCompileResult() {
override fun toString(): String = "Error(message = \"$message\""
}
@@ -125,8 +123,7 @@ sealed class ReplEvalResult : Serializable {
sealed class Error(val message: String) : ReplEvalResult() {
class Runtime(message: String, val cause: Exception? = null) : Error(message)
class CompileTime(message: String,
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : Error(message)
class CompileTime(message: String, val location: CompilerMessageLocation? = null) : Error(message)
override fun toString(): String = "${this::class.simpleName}Error(message = \"$message\""
}
@@ -183,4 +180,4 @@ enum class ReplRepeatingMode {
interface InvokeWrapper {
operator fun <T> invoke(body: () -> T): T // e.g. for capturing io
}
}
@@ -77,7 +77,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
Usage.print(errStream, createArguments(), false);
}
catch (Throwable t) {
errStream.println(messageRenderer.render(EXCEPTION, OutputMessageUtil.renderException(t), CompilerMessageLocation.NO_LOCATION));
errStream.println(messageRenderer.render(EXCEPTION, OutputMessageUtil.renderException(t), null));
}
return null;
}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cli.common.messages;
import kotlin.io.FilesKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.utils.fileUtils.FileUtilsKt;
@@ -36,7 +35,7 @@ public interface MessageRenderer {
};
MessageRenderer PLAIN_FULL_PATHS = new PlainTextMessageRenderer() {
@Nullable
@NotNull
@Override
protected String getPath(@NotNull CompilerMessageLocation location) {
return location.getPath();
@@ -44,20 +43,18 @@ public interface MessageRenderer {
};
MessageRenderer PLAIN_RELATIVE_PATHS = new PlainTextMessageRenderer() {
@NotNull
private final File cwd = new File(".").getAbsoluteFile();
@Nullable
@NotNull
@Override
protected String getPath(@NotNull CompilerMessageLocation location) {
String path = location.getPath();
return path == null ? path : FileUtilsKt.descendantRelativeTo(new File(path), cwd).getPath();
return FileUtilsKt.descendantRelativeTo(new File(location.getPath()), cwd).getPath();
}
};
String renderPreamble();
String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location);
String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location);
String renderConclusion();
}
@@ -37,7 +37,7 @@ public class MessageUtil {
return psiFileToMessageLocation(file, "<no path>", DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange()));
}
@NotNull
@Nullable
public static CompilerMessageLocation psiFileToMessageLocation(
@NotNull PsiFile file,
@Nullable String defaultValue,
@@ -57,16 +57,14 @@ public abstract class PlainTextMessageRenderer implements MessageRenderer {
}
@Override
public String render(
@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location
) {
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
StringBuilder result = new StringBuilder();
int line = location.getLine();
int column = location.getColumn();
String lineContent = location.getLineContent();
int line = location != null ? location.getLine() : -1;
int column = location != null ? location.getColumn() : -1;
String lineContent = location != null ? location.getLineContent() : null;
String path = getPath(location);
String path = location != null ? getPath(location) : null;
if (path != null) {
result.append(path);
result.append(":");
@@ -48,7 +48,7 @@ public class PrintingMessageCollector implements MessageCollector {
hasErrors |= severity.isError();
errStream.println(messageRenderer.render(severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION));
errStream.println(messageRenderer.render(severity, message, location));
}
@Override
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.common.messages;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class XmlMessageRenderer implements MessageRenderer {
@Override
@@ -26,11 +27,11 @@ public class XmlMessageRenderer implements MessageRenderer {
}
@Override
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
StringBuilder out = new StringBuilder();
String tagName = severity.getPresentableName();
out.append("<").append(tagName);
if (location.getPath() != null) {
if (location != null) {
out.append(" path=\"").append(e(location.getPath())).append("\"");
out.append(" line=\"").append(location.getLine()).append("\"");
out.append(" column=\"").append(location.getColumn()).append("\"");
@@ -98,8 +98,7 @@ open class KotlinJvmReplService(
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult {
operationsTracer?.before("check")
try {
return replCompiler?.check(state, codeLine)
?: ReplCheckResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION)
return replCompiler?.check(state, codeLine) ?: ReplCheckResult.Error("Initialization error")
}
finally {
operationsTracer?.after("check")
@@ -109,8 +108,7 @@ open class KotlinJvmReplService(
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult {
operationsTracer?.before("compile")
try {
return replCompiler?.compile(state, codeLine)
?: ReplCompileResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION)
return replCompiler?.compile(state, codeLine) ?: ReplCompileResult.Error("Initialization error")
}
finally {
operationsTracer?.after("compile")
@@ -35,7 +35,7 @@ internal class CompileServicesFacadeMessageCollector(
}
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
log.info("Message: " + MessageRenderer.WITHOUT_PATHS.render(severity, message, location ?: CompilerMessageLocation.NO_LOCATION))
log.info("Message: " + MessageRenderer.WITHOUT_PATHS.render(severity, message, location))
when (severity) {
CompilerMessageSeverity.OUTPUT -> {
servicesFacade.report(ReportCategory.OUTPUT_MESSAGE, ReportSeverity.ERROR, message)
@@ -42,9 +42,7 @@ public abstract class AbstractModuleXmlParserTest extends TestCase {
public void report(
@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location
) {
throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render(
severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION
));
throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location));
}
@Override