From f4cada802d5e90fe4c0d2036f4ce6fc4b18caef7 Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Wed, 4 Apr 2012 20:01:11 +0400 Subject: [PATCH] IDEA-53817 (Environment variable is red (not set) despite the fact it is used in profile .) --- .../references/MavenPropertyPsiReference.java | 34 ++++++++++- .../idea/maven/dom/MavenDomTestCase.java | 18 +++++- ...avenPropertyInActivationSectionTest.groovy | 56 +++++++++++++++++++ 3 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyInActivationSectionTest.groovy diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java index ea987636dded..0147445d81a1 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java @@ -23,6 +23,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.Navigatable; import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.xml.XmlDocument; import com.intellij.psi.xml.XmlFile; import com.intellij.psi.xml.XmlTag; @@ -62,12 +63,12 @@ public class MavenPropertyPsiReference extends MavenPsiReference { protected final MavenDomProjectModel myProjectDom; protected final MavenProject myMavenProject; - private final boolean mySoft; + private Boolean mySoft; public MavenPropertyPsiReference(MavenProject mavenProject, PsiElement element, String text, TextRange range, boolean isSoft) { super(element, text, range); myMavenProject = mavenProject; - mySoft = isSoft; + mySoft = isSoft ? true : null; myProjectDom = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile()); } @@ -430,7 +431,34 @@ public class MavenPropertyPsiReference extends MavenPsiReference { @Override public boolean isSoft() { - return mySoft; + Boolean res = mySoft; + if (res == null) { + XmlTag xmlTag = PsiTreeUtil.getParentOfType(getElement(), XmlTag.class); + while (xmlTag != null) { + if (xmlTag.getName().equals("profile")) { + XmlTag activation = xmlTag.findFirstSubTag("activation"); + if (activation != null) { + for (XmlTag propertyTag : activation.findSubTags("property")) { + XmlTag nameTag = propertyTag.findFirstSubTag("name"); + if (nameTag != null) { + if (nameTag.getValue().getTrimmedText().equals(myText)) { + res = true; + break; + } + } + } + } + break; + } + + xmlTag = xmlTag.getParentTag(); + } + + if (res == null) res = false; + mySoft = res; + } + + return res; } private interface SchemaProcessor { 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 3dcbe20edc8f..41c0cd7e753c 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 @@ -194,11 +194,25 @@ public abstract class MavenDomTestCase extends MavenImportingTestCase { String text = VfsUtilCore.loadText(file); int index = text.indexOf(referenceText); assert index >= 0; - + assert text.indexOf(referenceText, index + referenceText.length()) == -1 : "Reference text '" + referenceText + "' occurs more than one times"; - + return getReferenceAt(file, index); } + + @Nullable + protected PsiReference getReference(VirtualFile file, @NotNull String referenceText, int index) throws IOException { + String text = VfsUtilCore.loadText(file); + int k = -1; + + do { + k = text.indexOf(referenceText, k + 1); + assert k >= 0 : index; + } + while (--index >= 0); + + return getReferenceAt(file, k); + } @Nullable protected PsiElement resolveReference(VirtualFile file, @NotNull String referenceText) throws IOException { diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyInActivationSectionTest.groovy b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyInActivationSectionTest.groovy new file mode 100644 index 000000000000..6017e8a1d164 --- /dev/null +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyInActivationSectionTest.groovy @@ -0,0 +1,56 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.idea.maven.dom + +/** + * @author Sergey Evdokimov + */ +class MavenPropertyInActivationSectionTest extends MavenDomTestCase { + + public void testResolvePropertyFromActivationSection() throws IOException { + importProject(""" + example + parent + jar + 1.0 + example + + + + glassfish-env-path + + + env.GLASSFISH_HOME_123 + + + + + \${env.GLASSFISH_HOME_123} + + + + + + + \${env.GLASSFISH_HOME_123} + +"""); + + assert getReference(myProjectPom, "env.GLASSFISH_HOME_123", 1).isSoft() + assert !getReference(myProjectPom, "env.GLASSFISH_HOME_123", 2).isSoft() + } + +}