From d728a1a6c7221e86d75514c662b456efbc3bc3fd Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 20 Jan 2016 01:01:37 +0300 Subject: [PATCH] Replace File.relativePath from stdlib with File.descendantRelativeTo in fileUtils. IO utils: cleanup java usages. --- .../cli/common/messages/MessageRenderer.java | 4 +++- .../jvm/compiler/CompileEnvironmentUtil.java | 3 ++- .../org/jetbrains/kotlin/utils/fileUtils.kt | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java index 04ad3d623d1..0e29862870c 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java @@ -19,6 +19,7 @@ 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; import java.io.File; @@ -43,13 +44,14 @@ public interface MessageRenderer { }; MessageRenderer PLAIN_RELATIVE_PATHS = new PlainTextMessageRenderer() { + @NotNull 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 : FilesKt.relativePath(cwd, new File(path)); + return path == null ? path : FileUtilsKt.descendantRelativeTo(new File(path), cwd).getPath(); } }; diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java index 88c12469328..e28788c9c30 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java @@ -30,6 +30,7 @@ import com.intellij.psi.PsiManager; import kotlin.Unit; import kotlin.io.FilesKt; import kotlin.jvm.functions.Function1; +import kotlin.sequences.SequencesKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.backend.common.output.OutputFile; @@ -162,7 +163,7 @@ public class CompileEnvironmentUtil { continue; } - FilesKt.recurse(new File(sourceRootPath), new Function1() { + SequencesKt.forEach(FilesKt.walkTopDown(new File(sourceRootPath)), new Function1() { @Override public Unit invoke(File file) { if (file.isFile()) { diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/fileUtils.kt b/compiler/util/src/org/jetbrains/kotlin/utils/fileUtils.kt index 9aa4ab155be..1834638cd4b 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/fileUtils.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/fileUtils.kt @@ -31,4 +31,24 @@ fun File.withReplacedExtensionOrNull(oldExt: String, newExt: String): File? { } return null +} + +/** + * Calculates the relative path to this file from [base] file. + * Note that the [base] file is treated as a directory. + * + * If this file matches the [base] directory an empty path is returned. + * If this file does not belong to the [base] directory, it is returned unchanged. + */ +public fun File.descendantRelativeTo(base: File): File { + val prefix = base.canonicalPath + val answer = this.canonicalPath + return if (answer.startsWith(prefix)) { + val prefixSize = prefix.length + if (answer.length > prefixSize) { + File(answer.substring(prefixSize + 1)) + } else File("") + } else { + this + } } \ No newline at end of file