From 5a89552d6f50b28f90830969191897a9631f5d9c Mon Sep 17 00:00:00 2001 From: Yi Cheng Date: Fri, 18 Nov 2016 11:11:23 -0800 Subject: [PATCH] Fix import on directory Earler https://rbcommons.com/s/twitter/r/4386 removed the usage of `com.twitter.intellij.pants.util.PantsUtil#convertToTargetSpecs` which implicitly convert an empty target spec (in the case where user chooses a directory instead of a specifc target) to `::`. This RB makes the directory case explicit as one of the GUI options. Other fix: * Target in GUI will show up correctly as `a/b/c::` instead of `Optional[a/b/c::]` Testing Done: https://travis-ci.org/pantsbuild/intellij-pants-plugin/builds/176898711 Reviewed at https://rbcommons.com/s/twitter/r/4389/ --- common/com/twitter/intellij/pants/util/PantsUtil.java | 6 +++--- .../pants/settings/PantsProjectSettingsControl.java | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/common/com/twitter/intellij/pants/util/PantsUtil.java b/common/com/twitter/intellij/pants/util/PantsUtil.java index 1dfb96bd6..e6be5c647 100644 --- a/common/com/twitter/intellij/pants/util/PantsUtil.java +++ b/common/com/twitter/intellij/pants/util/PantsUtil.java @@ -509,8 +509,8 @@ public static Map filterByValue(Map map, Condition conditi return result; } - public static Optional getRelativeProjectPath(@NotNull File projectFile) { - final Optional buildRoot = findBuildRoot(projectFile); + public static Optional getRelativeProjectPath(@NotNull String projectFile) { + final Optional buildRoot = findBuildRoot(new File(projectFile)); return buildRoot.flatMap(file -> getRelativeProjectPath(file, projectFile)); } @@ -861,7 +861,7 @@ public static List convertToTargetSpecs(String importPath, List File importPathFile = new File(importPath); final String projectDir = isBUILDFileName(importPathFile.getName()) ? importPathFile.getParent() : importPathFile.getPath(); - final Optional relativeProjectDir = getRelativeProjectPath(new File(projectDir)); + final Optional relativeProjectDir = getRelativeProjectPath(projectDir); // If relativeProjectDir is null, that means the projectDir is already relative. String relativePath = relativeProjectDir.orElse(projectDir); if (targetNames.isEmpty()) { diff --git a/src/com/twitter/intellij/pants/settings/PantsProjectSettingsControl.java b/src/com/twitter/intellij/pants/settings/PantsProjectSettingsControl.java index ee00ecdbb..56ae0e51f 100644 --- a/src/com/twitter/intellij/pants/settings/PantsProjectSettingsControl.java +++ b/src/com/twitter/intellij/pants/settings/PantsProjectSettingsControl.java @@ -23,6 +23,7 @@ import com.intellij.util.ui.StatusText; import com.intellij.util.ui.UIUtil; import com.twitter.intellij.pants.PantsBundle; +import com.twitter.intellij.pants.PantsException; import com.twitter.intellij.pants.util.PantsUtil; import org.jetbrains.annotations.NotNull; @@ -97,7 +98,12 @@ public void onProjectPathChanged(@NotNull final String projectPath) { if (file.isDirectory()) { myTargetSpecs.setEnabled(false); - myTargetSpecs.setEmptyText(PantsUtil.getRelativeProjectPath(new File(file.getPath())) + "/::"); + String text = PantsUtil.getRelativeProjectPath(file.getPath()) + .orElseThrow(() -> new PantsException(String.format( + "Fail to find relative path from %s to build root.", file.getPath() + ))) + "/::"; + myTargetSpecs.setEmptyText(text); + myTargetSpecs.addItem(text, text, true); myWithDependeesCheckBox.setSelected(false); myWithDependeesCheckBox.setEnabled(true);