mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[maven] IDEA-132097 Resolve properties like ${parsedVersion.majorVersion} to a PsiElement of the <version> tag
GitOrigin-RevId: 0163710ed249bb92dbebc659e593ab238225881e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
4c9fabd32b
commit
fe1653dea9
+27
-7
@@ -273,8 +273,9 @@ public class MavenPropertyPsiReference extends MavenPsiReference implements Loca
|
||||
return resolveSettingsModelProperty();
|
||||
}
|
||||
|
||||
if (couldBeResolvedByBuildHelper(myText)) {
|
||||
return myElement;
|
||||
PsiElement resolved = resolveAsParsedVersion(myText, mavenProject);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -610,17 +611,36 @@ public class MavenPropertyPsiReference extends MavenPsiReference implements Loca
|
||||
}
|
||||
|
||||
/**
|
||||
* Some properties could be resolved by "build-helper-maven-plugin" (e.g., `${parsedVersion.majorVersion}` property)
|
||||
* If "build-helper-maven-plugin" has `parse-version` goal, probably it could resolve properties starting with a defined prefix
|
||||
* to something related to the version from the `version` tag (e.g., `${parsedVersion.majorVersion}`)
|
||||
* @see <a href="https://www.mojohaus.org/build-helper-maven-plugin/parse-version-mojo.html#propertyPrefix">mojohaus documentation</a>
|
||||
*/
|
||||
private boolean couldBeResolvedByBuildHelper(@NotNull String propertyText) {
|
||||
@Nullable
|
||||
private PsiElement resolveAsParsedVersion(@NotNull String propertyText, @NotNull MavenProject mavenProject) {
|
||||
String prefix = getBuildHelperParseablePrefix();
|
||||
if (prefix == null || !propertyText.startsWith(prefix + ".")) return null;
|
||||
|
||||
MavenDomProjectModel domProjectModel = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
|
||||
if (domProjectModel == null) {
|
||||
return myElement;
|
||||
}
|
||||
XmlTag versionTag = MavenDomUtil.findTag(domProjectModel, "project.version");
|
||||
if (versionTag == null) {
|
||||
return myElement;
|
||||
}
|
||||
else {
|
||||
return versionTag;
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable String getBuildHelperParseablePrefix() {
|
||||
MavenPlugin buildHelperPlugin = myMavenProject.findPlugin("org.codehaus.mojo", "build-helper-maven-plugin");
|
||||
if (buildHelperPlugin == null) return false;
|
||||
if (buildHelperPlugin == null) return null;
|
||||
|
||||
Optional<MavenPlugin.Execution> execution = buildHelperPlugin.getExecutions().stream()
|
||||
.filter(it -> it.getGoals().contains("parse-version"))
|
||||
.findFirst();
|
||||
if (execution.isEmpty()) return false;
|
||||
if (execution.isEmpty()) return null;
|
||||
|
||||
String propertyPrefix = "parsedVersion"; // default value
|
||||
Element configuration = execution.get().getConfigurationElement();
|
||||
@@ -630,6 +650,6 @@ public class MavenPropertyPsiReference extends MavenPsiReference implements Loca
|
||||
propertyPrefix = customPrefix.getText();
|
||||
}
|
||||
}
|
||||
return propertyText.startsWith(propertyPrefix + ".");
|
||||
return propertyPrefix;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-7
@@ -6,7 +6,6 @@ import com.intellij.lang.properties.IProperty
|
||||
import com.intellij.maven.testFramework.MavenDomTestCase
|
||||
import com.intellij.openapi.application.readAction
|
||||
import com.intellij.openapi.util.SystemInfo
|
||||
import com.intellij.openapi.util.registry.Registry
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.psi.xml.XmlTag
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@@ -926,10 +925,10 @@ class MavenPropertyCompletionAndResolutionTest : MavenDomTestCase() {
|
||||
</plugins>
|
||||
</build>
|
||||
""".trimIndent())
|
||||
val ref = getReferenceAtCaret(projectPom)!!
|
||||
// reimport to add build-helper-maven-plugin into the MavenProject. The property should be resolved if this plugin exists in pom.
|
||||
fixture.configureFromExistingVirtualFile(projectPom)
|
||||
// Resolving this property depends on the presence of the build-helper-maven-plugin in pom. Reimport to add the plugin in MavenProject.
|
||||
runBlocking { importProjectAsync() }
|
||||
assertResolved(projectPom, ref.element)
|
||||
assertResolved(projectPom, findTag(projectPom, "project.version"))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -958,10 +957,10 @@ class MavenPropertyCompletionAndResolutionTest : MavenDomTestCase() {
|
||||
</plugins>
|
||||
</build>
|
||||
""".trimIndent())
|
||||
val ref = getReferenceAtCaret(projectPom)!!
|
||||
// reimport to add build-helper-maven-plugin into the MavenProject. The property should be resolved if this plugin exists in pom.
|
||||
fixture.configureFromExistingVirtualFile(projectPom)
|
||||
// Resolving this property depends on the presence of the build-helper-maven-plugin in pom. Reimport to add the plugin in MavenProject.
|
||||
runBlocking { importProjectAsync() }
|
||||
assertResolved(projectPom, ref.element)
|
||||
assertResolved(projectPom, findTag(projectPom, "project.version"))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user