From f07d2db742fc5cea728ffbbb0eef0a983e270413 Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Wed, 25 Apr 2012 18:20:10 +0400 Subject: [PATCH] IDEA-71952 Generating a sub module (with parents) from intellij creates a module without relativePath in it --- .../references/MavenModulePsiReference.java | 1 + .../jetbrains/idea/maven/utils/MavenUtil.java | 23 +++++ .../wizards/MavenModuleBuilderHelper.java | 2 +- .../fileTemplates/j2ee/Maven Project.xml.ft | 3 + .../idea/maven/dom/MavenDomTestCase.java | 6 +- ...avenModuleCompletionAndResolutionTest.java | 90 +++++++++++++++++++ 6 files changed, 123 insertions(+), 2 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenModulePsiReference.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenModulePsiReference.java index 5e94047a700e..124941a432e5 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenModulePsiReference.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenModulePsiReference.java @@ -130,6 +130,7 @@ public class MavenModulePsiReference extends MavenPsiReference implements LocalQ modulePom, new MavenId(groupId, artifactId, version), myWithParent ? id : null, + myPsiFile.getVirtualFile(), true); } catch (IOException e) { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java index 9f6faff1080e..b71ddcff0e5d 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java @@ -285,10 +285,18 @@ public class MavenUtil { return " "; } + public static void runOrApplyMavenProjectFileTemplate(Project project, + VirtualFile file, + MavenId projectId, + boolean interactive) throws IOException { + runOrApplyMavenProjectFileTemplate(project, file, projectId, null, null, interactive); + } + public static void runOrApplyMavenProjectFileTemplate(Project project, VirtualFile file, MavenId projectId, MavenId parentId, + VirtualFile parentFile, boolean interactive) throws IOException { Properties properties = new Properties(); Properties conditions = new Properties(); @@ -300,6 +308,21 @@ public class MavenUtil { properties.setProperty("PARENT_GROUP_ID", parentId.getGroupId()); properties.setProperty("PARENT_ARTIFACT_ID", parentId.getArtifactId()); properties.setProperty("PARENT_VERSION", parentId.getVersion()); + + if (parentFile != null) { + VirtualFile modulePath = file.getParent(); + VirtualFile parentModulePath = parentFile.getParent(); + + if (modulePath.getParent() != parentModulePath) { + String relativePath = VfsUtil.getPath(file, parentModulePath, '/'); + if (relativePath != null) { + if (relativePath.endsWith("/")) relativePath = relativePath.substring(0, relativePath.length() - 1); + + conditions.setProperty("HAS_RELATIVE_PATH", "true"); + properties.setProperty("PARENT_RELATIVE_PATH", relativePath); + } + } + } } runOrApplyFileTemplate(project, file, MavenFileTemplateGroupFactory.MAVEN_PROJECT_XML_TEMPLATE, properties, conditions, interactive); } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/MavenModuleBuilderHelper.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/MavenModuleBuilderHelper.java index c194329c73ac..3e40d8780479 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/MavenModuleBuilderHelper.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/MavenModuleBuilderHelper.java @@ -94,7 +94,7 @@ public class MavenModuleBuilderHelper { VirtualFile file; try { file = root.createChildData(this, MavenConstants.POM_XML); - MavenUtil.runOrApplyMavenProjectFileTemplate(project, file, myProjectId, null, isInteractive); + MavenUtil.runOrApplyMavenProjectFileTemplate(project, file, myProjectId, isInteractive); result.setResult(file); } catch (IOException e) { diff --git a/plugins/maven/src/main/resources/fileTemplates/j2ee/Maven Project.xml.ft b/plugins/maven/src/main/resources/fileTemplates/j2ee/Maven Project.xml.ft index 5370d53f20f7..7fef5aa28d92 100644 --- a/plugins/maven/src/main/resources/fileTemplates/j2ee/Maven Project.xml.ft +++ b/plugins/maven/src/main/resources/fileTemplates/j2ee/Maven Project.xml.ft @@ -9,6 +9,9 @@ ${PARENT_GROUP_ID} ${PARENT_ARTIFACT_ID} ${PARENT_VERSION} +#if (${HAS_RELATIVE_PATH}) + ${PARENT_RELATIVE_PATH} +#end #end diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenDomTestCase.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenDomTestCase.java index 41c0cd7e753c..36b037431930 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenDomTestCase.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenDomTestCase.java @@ -303,7 +303,11 @@ public abstract class MavenDomTestCase extends MavenImportingTestCase { } protected IntentionAction getIntentionAtCaret(String intentionName) throws IOException { - configTest(myProjectPom); + return getIntentionAtCaret(myProjectPom, intentionName); + } + + protected IntentionAction getIntentionAtCaret(VirtualFile pomFile, String intentionName) throws IOException { + configTest(pomFile); try { List intentions = myFixture.getAvailableIntentions(); diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModuleCompletionAndResolutionTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModuleCompletionAndResolutionTest.java index b8b2f30c672f..c6544c60b1ec 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModuleCompletionAndResolutionTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModuleCompletionAndResolutionTest.java @@ -20,6 +20,7 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileEditor.FileEditorManager; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; public class MavenModuleCompletionAndResolutionTest extends MavenDomWithIndicesTestCase { @@ -467,6 +468,95 @@ public class MavenModuleCompletionAndResolutionTest extends MavenDomWithIndicesT ""); } + public void testCreateModuleWithParentQuickFix2() throws Throwable { + createProjectPom("test" + + "project" + + "1" + + "pom"); + importProject(); + + createProjectPom("test" + + "project" + + "1" + + "pom" + + + "" + + " ppp/newModule" + + ""); + + IntentionAction i = getIntentionAtCaret(CREATE_MODULE_WITH_PARENT_INTENTION); + assertNotNull(i); + myFixture.launchAction(i); + + assertCreateModuleFixResult( + "ppp/newModule/pom.xml", + "\n" + + "\n" + + " 4.0.0\n" + + "\n" + + " \n" + + " test\n" + + " project\n" + + " 1\n" + + " ../..\n" + + " \n" + + "\n" + + " test\n" + + " newModule\n" + + " 1\n" + + "\n" + + " \n" + + ""); + } + + public void testCreateModuleWithParentQuickFix3() throws Throwable { + VirtualFile parentPom = createModulePom("parent", + "test" + + "project" + + "1" + + "pom"); + + importProject(parentPom); + + VfsUtil.saveText(parentPom, createPomXml( + "test" + + "project" + + "1" + + "pom" + + + "" + + " ../ppp/newModule" + + "")); + + IntentionAction i = getIntentionAtCaret(parentPom, CREATE_MODULE_WITH_PARENT_INTENTION); + assertNotNull(i); + myFixture.launchAction(i); + + assertCreateModuleFixResult( + "ppp/newModule/pom.xml", + "\n" + + "\n" + + " 4.0.0\n" + + "\n" + + " \n" + + " test\n" + + " project\n" + + " 1\n" + + " ../../parent\n" + + " \n" + + "\n" + + " test\n" + + " newModule\n" + + " 1\n" + + "\n" + + " \n" + + ""); + } + public void testDoesNotShowCreatePomQuickFixForEmptyModuleTag() throws Throwable { createProjectPom("test" + "project" +