From a0a81e35623deba73a247d20564bb6c7df12955a Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Thu, 3 Jan 2013 23:01:05 +0400 Subject: [PATCH] Refactoring of language injection --- ...enPluginConfigurationLanguageInjector.java | 53 ++++ .../plugins/api/MavenPluginDescriptor.java | 5 + .../plugins/api/MavenPluginParamInfo.java | 243 ++++++++++++++++++ .../MavenPluginParamReferenceContributor.java | 208 ++------------- .../plugins/groovy/MavenGroovyInjector.java | 31 --- .../maven/plugins/sql/MavenSqlInjector.java | 29 --- ...enPluginConfigurationLanguageInjector.java | 104 -------- .../resources/META-INF/groovy-support.xml | 11 +- .../src/main/resources/META-INF/plugin.xml | 6 +- 9 files changed, 335 insertions(+), 355 deletions(-) create mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginConfigurationLanguageInjector.java create mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamInfo.java delete mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/groovy/MavenGroovyInjector.java delete mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/sql/MavenSqlInjector.java delete mode 100644 plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenPluginConfigurationLanguageInjector.java diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginConfigurationLanguageInjector.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginConfigurationLanguageInjector.java new file mode 100644 index 000000000000..304d05d3db37 --- /dev/null +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginConfigurationLanguageInjector.java @@ -0,0 +1,53 @@ +/* + * 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.plugins.api; + +import com.intellij.lang.Language; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.InjectedLanguagePlaces; +import com.intellij.psi.LanguageInjector; +import com.intellij.psi.PsiLanguageInjectionHost; +import com.intellij.psi.xml.XmlText; +import com.intellij.util.PairProcessor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.maven.dom.model.MavenDomConfiguration; + +/** + * @author Sergey Evdokimov + */ +public final class MavenPluginConfigurationLanguageInjector implements LanguageInjector { + + @Override + public void getLanguagesToInject(@NotNull final PsiLanguageInjectionHost host, @NotNull final InjectedLanguagePlaces injectionPlacesRegistrar) { + if (!(host instanceof XmlText)) return; + + XmlText xmlText = (XmlText)host; + + if (!MavenPluginParamInfo.isSimpleText(xmlText)) return; + + MavenPluginParamInfo.processParamInfo(xmlText, new PairProcessor() { + @Override + public boolean process(MavenPluginParamInfo.ParamInfo info, MavenDomConfiguration configuration) { + Language language = info.getLanguage(); + if (language != null) { + injectionPlacesRegistrar.addPlace(language, TextRange.from(0, host.getTextLength()), null, null); + return false; + } + return true; + } + }); + } +} diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginDescriptor.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginDescriptor.java index cb99a4aef9b5..f3973b570095 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginDescriptor.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginDescriptor.java @@ -66,6 +66,11 @@ public class MavenPluginDescriptor extends AbstractExtensionPointBean { @Attribute("refProvider") public String refProvider; + /** + * Language to inject. + */ + @Attribute("language") + public String language; } public static Pair parsePluginId(String mavenId) { diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamInfo.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamInfo.java new file mode 100644 index 000000000000..a58458ad5fb4 --- /dev/null +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamInfo.java @@ -0,0 +1,243 @@ +package org.jetbrains.idea.maven.plugins.api; + +import com.intellij.lang.Language; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Pair; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiReference; +import com.intellij.psi.PsiReferenceProvider; +import com.intellij.psi.impl.source.tree.LeafPsiElement; +import com.intellij.psi.xml.XmlTag; +import com.intellij.psi.xml.XmlText; +import com.intellij.psi.xml.XmlTokenType; +import com.intellij.util.PairProcessor; +import com.intellij.util.ProcessingContext; +import com.intellij.util.xml.DomElement; +import com.intellij.util.xml.DomManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.maven.dom.model.*; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Sergey Evdokimov + */ +public class MavenPluginParamInfo { + + private static final Logger LOG = Logger.getInstance(MavenPluginParamInfo.class); + + /** + * This map contains descriptions of all plugins. + */ + private static volatile Map myMap; + + public static Map getMap() { + Map res = myMap; + + if (res == null) { + res = new HashMap(); + + for (MavenPluginDescriptor pluginDescriptor : MavenPluginDescriptor.EP_NAME.getExtensions()) { + if (pluginDescriptor.params == null) continue; + + Pair pluginId = MavenPluginDescriptor.parsePluginId(pluginDescriptor.mavenId); + + for (MavenPluginDescriptor.Param param : pluginDescriptor.params) { + String[] paramPath = param.name.split("/"); + + Map pluginsMap = res; + + for (int i = paramPath.length - 1; i >= 0; i--) { + pluginsMap = getOrCreate(pluginsMap, paramPath[i]); + } + + ParamInfo paramInfo = new ParamInfo(pluginDescriptor.getPluginDescriptor().getPluginClassLoader(), param); + + Map goalsMap = getOrCreate(pluginsMap, pluginId); + + ParamInfo oldValue = goalsMap.put(param.goal, paramInfo); + if (oldValue != null) { + LOG.error("Duplicated maven plugin parameter descriptor: " + + pluginId.first + ':' + pluginId.second + " -> " + + (param.goal != null ? "[" + param.goal + ']' : "") + param.name); + } + } + } + + myMap = res; + } + + return res; + } + + @NotNull + private static V getOrCreate(Map map, K key) { + Map res = (Map)map.get(key); + if (res == null) { + res = new HashMap(); + map.put(key, res); + } + + return (V)res; + } + + public static boolean isSimpleText(@NotNull XmlText paramValue) { + PsiElement prevSibling = paramValue.getPrevSibling(); + if (!(prevSibling instanceof LeafPsiElement) || ((LeafPsiElement)prevSibling).getElementType() != XmlTokenType.XML_TAG_END) { + return false; + } + + PsiElement nextSibling = paramValue.getNextSibling(); + if (!(nextSibling instanceof LeafPsiElement) || ((LeafPsiElement)nextSibling).getElementType() != XmlTokenType.XML_END_TAG_START) { + return false; + } + + return true; + } + + public static void processParamInfo(@NotNull XmlText paramValue, @NotNull PairProcessor processor) { + XmlTag paramTag = paramValue.getParentTag(); + if (paramTag == null) return; + + XmlTag configurationTag = paramTag; + DomElement domElement; + + Map m = getMap().get(paramTag.getName()); + + while (true) { + if (m == null) return; + + configurationTag = configurationTag.getParentTag(); + if (configurationTag == null) return; + + String tagName = configurationTag.getName(); + if ("configuration".equals(tagName)) { + domElement = DomManager.getDomManager(configurationTag.getProject()).getDomElement(configurationTag); + if (domElement instanceof MavenDomConfiguration) { + break; + } + + if (domElement != null) return; + } + + m = (Map)m.get(tagName); + } + + Map, Map> pluginsMap = m; + + MavenDomConfiguration domCfg = (MavenDomConfiguration)domElement; + + MavenDomPlugin domPlugin = domCfg.getParentOfType(MavenDomPlugin.class, true); + if (domPlugin == null) return; + + String pluginGroupId = domPlugin.getGroupId().getStringValue(); + String pluginArtifactId = domPlugin.getArtifactId().getStringValue(); + + Map goalsMap; + + if (pluginGroupId == null) { + goalsMap = pluginsMap.get(Pair.create("org.apache.maven.plugins", pluginArtifactId)); + if (goalsMap == null) { + goalsMap = pluginsMap.get(Pair.create("org.codehaus.mojo", pluginArtifactId)); + } + } + else { + goalsMap = pluginsMap.get(Pair.create(pluginGroupId, pluginArtifactId)); + } + + if (goalsMap == null) return; + + DomElement parent = domCfg.getParent(); + if (parent instanceof MavenDomPluginExecution) { + MavenDomGoals goals = ((MavenDomPluginExecution)parent).getGoals(); + for (MavenDomGoal goal : goals.getGoals()) { + ParamInfo info = goalsMap.get(goal.getStringValue()); + if (info != null) { + if (!processor.process(info, domCfg)) return; + } + } + } + + ParamInfo defaultInfo = goalsMap.get(null); + if (defaultInfo != null) { + if (!processor.process(defaultInfo, domCfg)) return; + } + } + + public static class ParamInfo { + private final ClassLoader myClassLoader; + + private final String myProviderClass; + + private volatile String myLanguageId; + private Language myLanguageInstance; + + private volatile MavenParamReferenceProvider myProviderInstance; + + private ParamInfo(ClassLoader classLoader, MavenPluginDescriptor.Param param) { + myClassLoader = classLoader; + myProviderClass = param.refProvider; + myLanguageId = param.language; + } + + public Language getLanguage() { + if (myLanguageInstance != null) { + return myLanguageInstance; + } + + String languageId = myLanguageId; + if (languageId == null) return null; + + Language l = Language.findLanguageByID(languageId); + if (l == null) { // May be plugin with language is disabled. + myLanguageId = null; + } + else { + myLanguageInstance = l; + } + + return l; + } + + public MavenParamReferenceProvider getProviderInstance() { + if (myProviderClass == null) { + return null; + } + + MavenParamReferenceProvider res = myProviderInstance; + + if (res == null) { + Object instance; + + try { + instance = myClassLoader.loadClass(myProviderClass).newInstance(); + } + catch (Exception e) { + throw new RuntimeException("Failed to create reference provider instance", e); + } + + if (instance instanceof MavenParamReferenceProvider) { + res = (MavenParamReferenceProvider)instance; + } + else { + final PsiReferenceProvider psiReferenceProvider = (PsiReferenceProvider)instance; + + res = new MavenParamReferenceProvider() { + @Override + public PsiReference[] getReferencesByElement(@NotNull PsiElement element, + @NotNull MavenDomConfiguration domCfg, + @NotNull ProcessingContext context) { + return psiReferenceProvider.getReferencesByElement(element, context); + } + }; + } + + myProviderInstance = res; + } + + return res; + } + } + +} diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamReferenceContributor.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamReferenceContributor.java index 0623885dc9ee..f60effd48406 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamReferenceContributor.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/api/MavenPluginParamReferenceContributor.java @@ -15,31 +15,23 @@ */ package org.jetbrains.idea.maven.plugins.api; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.Pair; import com.intellij.patterns.PlatformPatterns; import com.intellij.patterns.XmlPatterns; import com.intellij.psi.*; -import com.intellij.psi.impl.source.tree.LeafPsiElement; -import com.intellij.psi.xml.XmlTag; import com.intellij.psi.xml.XmlText; import com.intellij.psi.xml.XmlTokenType; +import com.intellij.util.PairProcessor; import com.intellij.util.ProcessingContext; -import com.intellij.util.xml.DomElement; -import com.intellij.util.xml.DomManager; import org.jetbrains.annotations.NotNull; -import org.jetbrains.idea.maven.dom.model.*; +import org.jetbrains.idea.maven.dom.model.MavenDomConfiguration; -import java.util.HashMap; -import java.util.Map; +import static org.jetbrains.idea.maven.plugins.api.MavenPluginParamInfo.ParamInfo; /** * @author Sergey Evdokimov */ public class MavenPluginParamReferenceContributor extends PsiReferenceContributor { - private static final Logger LOG = Logger.getInstance(MavenPluginParamReferenceContributor.class); - @Override public void registerReferenceProviders(PsiReferenceRegistrar registrar) { registrar.registerReferenceProvider( @@ -51,198 +43,38 @@ public class MavenPluginParamReferenceContributor extends PsiReferenceContributo private static class MavenPluginParamRefProvider extends PsiReferenceProvider { - /** - * This map contains descriptions of all plugins. - */ - private volatile Map myMap; - - public Map getMap() { - Map res = myMap; - - if (res == null) { - res = new HashMap(); - - for (MavenPluginDescriptor pluginDescriptor : MavenPluginDescriptor.EP_NAME.getExtensions()) { - if (pluginDescriptor.params == null) continue; - - Pair pluginId = MavenPluginDescriptor.parsePluginId(pluginDescriptor.mavenId); - - for (MavenPluginDescriptor.Param param : pluginDescriptor.params) { - String[] paramPath = param.name.split("/"); - - Map pluginsMap = res; - - for (int i = paramPath.length - 1; i >= 0; i--) { - pluginsMap = getOrCreate(pluginsMap, paramPath[i]); - } - - ParamInfo paramInfo = new ParamInfo(pluginDescriptor.getPluginDescriptor().getPluginClassLoader(), param.refProvider); - - Map goalsMap = getOrCreate(pluginsMap, pluginId); - - ParamInfo oldValue = goalsMap.put(param.goal, paramInfo); - if (oldValue != null) { - LOG.error("Duplicated maven plugin parameter descriptor: " - + pluginId.first + ':' + pluginId.second + " -> " - + (param.goal != null ? "[" + param.goal + ']' : "") + param.name); - } - } - } - - myMap = res; - } - - return res; - } - - @NotNull - private static V getOrCreate(Map map, K key) { - Map res = (Map)map.get(key); - if (res == null) { - res = new HashMap(); - map.put(key, res); - } - - return (V)res; - } - @NotNull @Override - public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { + public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) { XmlText xmlText = (XmlText)element.getParent(); - PsiElement prevSibling = xmlText.getPrevSibling(); - if (!(prevSibling instanceof LeafPsiElement) || ((LeafPsiElement)prevSibling).getElementType() != XmlTokenType.XML_TAG_END) return PsiReference.EMPTY_ARRAY; + if (!MavenPluginParamInfo.isSimpleText(xmlText)) return PsiReference.EMPTY_ARRAY; - PsiElement nextSibling = xmlText.getNextSibling(); - if (!(nextSibling instanceof LeafPsiElement) || ((LeafPsiElement)nextSibling).getElementType() != XmlTokenType.XML_END_TAG_START) return PsiReference.EMPTY_ARRAY; + class MyProcessor implements PairProcessor { + PsiReference[] result; - XmlTag paramTag = xmlText.getParentTag(); - if (paramTag == null) return PsiReference.EMPTY_ARRAY; - - XmlTag configurationTag = paramTag; - DomElement domElement; - - Map m = getMap().get(paramTag.getName()); - - while (true) { - if (m == null) return PsiReference.EMPTY_ARRAY; - - configurationTag = configurationTag.getParentTag(); - if (configurationTag == null) return PsiReference.EMPTY_ARRAY; - - String tagName = configurationTag.getName(); - if ("configuration".equals(tagName)) { - domElement = DomManager.getDomManager(configurationTag.getProject()).getDomElement(configurationTag); - if (domElement instanceof MavenDomConfiguration) { - break; + @Override + public boolean process(ParamInfo info, MavenDomConfiguration domCfg) { + MavenParamReferenceProvider providerInstance = info.getProviderInstance(); + if (providerInstance != null) { + result = providerInstance.getReferencesByElement(element, domCfg, context); + return false; } - if (domElement != null) return PsiReference.EMPTY_ARRAY; - } - - m = (Map)m.get(tagName); - } - - Map, Map> pluginsMap = m; - - MavenDomConfiguration domCfg = (MavenDomConfiguration)domElement; - - MavenDomPlugin domPlugin = domCfg.getParentOfType(MavenDomPlugin.class, true); - if (domPlugin == null) return PsiReference.EMPTY_ARRAY; - - String pluginGroupId = domPlugin.getGroupId().getStringValue(); - String pluginArtifactId = domPlugin.getArtifactId().getStringValue(); - - Map goalsMap; - - if (pluginGroupId == null) { - goalsMap = pluginsMap.get(Pair.create("org.apache.maven.plugins", pluginArtifactId)); - if (goalsMap == null) { - goalsMap = pluginsMap.get(Pair.create("org.codehaus.mojo", pluginArtifactId)); - } - } - else { - goalsMap = pluginsMap.get(Pair.create(pluginGroupId, pluginArtifactId)); - } - - if (goalsMap == null) return PsiReference.EMPTY_ARRAY; - - DomElement parent = domCfg.getParent(); - if (parent instanceof MavenDomPluginExecution) { - MavenDomGoals goals = ((MavenDomPluginExecution)parent).getGoals(); - for (MavenDomGoal goal : goals.getGoals()) { - ParamInfo info = goalsMap.get(goal.getStringValue()); - if (info != null) { - MavenParamReferenceProvider providerInstance = info.getProviderInstance(); - if (providerInstance != null) { - return providerInstance.getReferencesByElement(element, domCfg, context); - } - } + return true; } } - ParamInfo defaultInfo = goalsMap.get(null); - if (defaultInfo != null) { - MavenParamReferenceProvider providerInstance = defaultInfo.getProviderInstance(); - if (providerInstance != null) { - return providerInstance.getReferencesByElement(element, domCfg, context); - } + MyProcessor processor = new MyProcessor(); + + MavenPluginParamInfo.processParamInfo(xmlText, processor); + + if (processor.result != null) { + return processor.result; } return PsiReference.EMPTY_ARRAY; } } - private static class ParamInfo { - private final ClassLoader myClassLoader; - - private final String myProviderClass; - - private volatile MavenParamReferenceProvider myProviderInstance; - - public ParamInfo(ClassLoader classLoader, String providerClass) { - myClassLoader = classLoader; - myProviderClass = providerClass; - } - - public MavenParamReferenceProvider getProviderInstance() { - if (myProviderClass == null) { - return null; - } - - MavenParamReferenceProvider res = myProviderInstance; - - if (res == null) { - Object instance; - - try { - instance = myClassLoader.loadClass(myProviderClass).newInstance(); - } - catch (Exception e) { - throw new RuntimeException("Failed to create reference provider instance", e); - } - - if (instance instanceof MavenParamReferenceProvider) { - res = (MavenParamReferenceProvider)instance; - } - else { - final PsiReferenceProvider psiReferenceProvider = (PsiReferenceProvider)instance; - - res = new MavenParamReferenceProvider() { - @Override - public PsiReference[] getReferencesByElement(@NotNull PsiElement element, - @NotNull MavenDomConfiguration domCfg, - @NotNull ProcessingContext context) { - return psiReferenceProvider.getReferencesByElement(element, context); - } - }; - } - - myProviderInstance = res; - } - - return res; - } - } } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/groovy/MavenGroovyInjector.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/groovy/MavenGroovyInjector.java deleted file mode 100644 index 30648f82c6bf..000000000000 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/groovy/MavenGroovyInjector.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.plugins.groovy; - -import org.jetbrains.idea.maven.utils.MavenPluginConfigurationLanguageInjector; -import org.jetbrains.plugins.groovy.GroovyFileType; - -import java.util.Arrays; - -/** - * @author Sergey Evdokimov - */ -public class MavenGroovyInjector extends MavenPluginConfigurationLanguageInjector { - public MavenGroovyInjector() { - super("source", Arrays.asList("org.codehaus.groovy.maven", "org.codehaus.gmaven"), "gmaven-plugin", GroovyFileType.GROOVY_LANGUAGE); - } - -} diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/sql/MavenSqlInjector.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/sql/MavenSqlInjector.java deleted file mode 100644 index f212f34e59d4..000000000000 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/plugins/sql/MavenSqlInjector.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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.plugins.sql; - -import com.intellij.lang.Language; -import org.jetbrains.idea.maven.utils.MavenPluginConfigurationLanguageInjector; - -/** - * @author Sergey Evdokimov - */ -public class MavenSqlInjector extends MavenPluginConfigurationLanguageInjector { - public MavenSqlInjector() { - super("sqlCommand", "org.codehaus.mojo", "sql-maven-plugin", Language.findLanguageByID("SQL")); - } - -} diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenPluginConfigurationLanguageInjector.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenPluginConfigurationLanguageInjector.java deleted file mode 100644 index 6fb4ca793fc2..000000000000 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenPluginConfigurationLanguageInjector.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.utils; - -import com.intellij.lang.Language; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.InjectedLanguagePlaces; -import com.intellij.psi.LanguageInjector; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiLanguageInjectionHost; -import com.intellij.psi.xml.XmlTag; -import com.intellij.psi.xml.XmlText; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Collection; -import java.util.Collections; - -/** - * @author Sergey Evdokimov - */ -public class MavenPluginConfigurationLanguageInjector implements LanguageInjector { - - private final String myParameterName; - private final Collection myPluginGroupIds; - private final String myPluginArtifactId; - private final Language myLanguage; - - protected MavenPluginConfigurationLanguageInjector(@NotNull String parameterName, - @NotNull String pluginGroupId, - @NotNull String pluginArtifactId, - @Nullable Language language) { - this(parameterName, Collections.singleton(pluginGroupId), pluginArtifactId, language); - } - - protected MavenPluginConfigurationLanguageInjector(@NotNull String parameterName, - @NotNull Collection pluginGroupIds, - @NotNull String pluginArtifactId, - @Nullable Language language) { - myParameterName = parameterName; - myPluginGroupIds = pluginGroupIds; - myPluginArtifactId = pluginArtifactId; - myLanguage = language; - } - - @Override - public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) { - if (!(host instanceof XmlText)) return; - - PsiElement sourceTag = host.getParent(); - if (!isTagOfName(sourceTag, myParameterName)) return; - - PsiElement configurationTag = sourceTag.getParent(); - if (!isTagOfName(configurationTag, "configuration")) return; - - PsiElement configurationParent = configurationTag.getParent(); - - PsiElement pluginTag; - - if (isTagOfName(configurationParent, "execution")) { - PsiElement executionsTag = configurationParent.getParent(); - if (!isTagOfName(executionsTag, "executions")) return; - - pluginTag = executionsTag.getParent(); - } - else { - pluginTag = configurationParent; - } - - if (!isTagOfName(pluginTag, "plugin")) return; - - XmlTag plugin = (XmlTag)pluginTag; - - XmlTag groupId = plugin.findFirstSubTag("groupId"); - if (groupId == null || !myPluginGroupIds.contains(groupId.getValue().getText().trim())) return; - - XmlTag artifactId = plugin.findFirstSubTag("artifactId"); - if (artifactId == null || !artifactId.getValue().getText().trim().equals(myPluginArtifactId)) return; - - if (!"pom.xml".equals(plugin.getContainingFile().getName())) return; - - if (myLanguage != null) { // Language can be null if specified language is in disabled plugin - injectionPlacesRegistrar.addPlace(myLanguage, TextRange.from(0, host.getTextLength()), null, null); - } - } - - protected static boolean isTagOfName(@Nullable PsiElement element, String name) { - return element instanceof XmlTag && name.equals(((XmlTag)element).getName()); - } - -} diff --git a/plugins/maven/src/main/resources/META-INF/groovy-support.xml b/plugins/maven/src/main/resources/META-INF/groovy-support.xml index 458ff05d910a..bbc0b59d4338 100644 --- a/plugins/maven/src/main/resources/META-INF/groovy-support.xml +++ b/plugins/maven/src/main/resources/META-INF/groovy-support.xml @@ -4,7 +4,14 @@ - - + + + + + + + + + \ No newline at end of file diff --git a/plugins/maven/src/main/resources/META-INF/plugin.xml b/plugins/maven/src/main/resources/META-INF/plugin.xml index 1130f5ac4f6a..a8a76d9f9019 100644 --- a/plugins/maven/src/main/resources/META-INF/plugin.xml +++ b/plugins/maven/src/main/resources/META-INF/plugin.xml @@ -52,7 +52,7 @@ - + @@ -155,6 +155,10 @@ + + + +