IDEA-53165 (Nice to have code highlight in groovy plugin source for maven)

This commit is contained in:
Sergey Evdokimov
2012-02-29 13:30:37 +04:00
parent aecc27a219
commit 509caedca8
4 changed files with 151 additions and 1 deletions
+1 -1
View File
@@ -52,7 +52,7 @@
<orderEntry type="library" name="gson" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="JUnit4" level="project" />
<orderEntry type="module" module-name="testFramework-java" exported="" scope="TEST" />
<orderEntry type="module" module-name="jetgroovy" scope="TEST" />
<orderEntry type="module" module-name="jetgroovy" />
<orderEntry type="module" module-name="maven2-server-impl" scope="RUNTIME" />
<orderEntry type="library" name="Guava" level="project" />
</component>
@@ -0,0 +1,68 @@
/*
* 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 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 org.jetbrains.plugins.groovy.GroovyFileType;
/**
* @author Sergey Evdokimov
*/
public class MavenGroovyInjector implements LanguageInjector {
@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
if (!(host instanceof XmlText)) return;
PsiElement sourceTag = host.getParent();
if (!isTagOfName(sourceTag, "source")) return;
PsiElement configurationTag = sourceTag.getParent();
if (!isTagOfName(configurationTag, "configuration")) return;
PsiElement executionTag = configurationTag.getParent();
if (!isTagOfName(executionTag, "execution")) return;
PsiElement executionsTag = executionTag.getParent();
if (!isTagOfName(executionsTag, "executions")) return;
PsiElement pluginTag = executionsTag.getParent();
if (!isTagOfName(pluginTag, "plugin")) return;
XmlTag plugin = (XmlTag)pluginTag;
XmlTag groupId = plugin.findFirstSubTag("groupId");
if (groupId == null || !groupId.getValue().getText().trim().equals("org.codehaus.groovy.maven")) return;
XmlTag artifactId = plugin.findFirstSubTag("artifactId");
if (artifactId == null || !artifactId.getValue().getText().trim().equals("gmaven-plugin")) return;
if (!"pom.xml".equals(plugin.getContainingFile().getName())) return;
injectionPlacesRegistrar.addPlace(GroovyFileType.GROOVY_LANGUAGE, TextRange.from(0, host.getTextLength()), null, null);
}
private static boolean isTagOfName(@Nullable PsiElement element, String name) {
return element instanceof XmlTag && name.equals(((XmlTag)element).getName());
}
}
@@ -4,4 +4,7 @@
<importer implementation="org.jetbrains.idea.maven.importing.Groovy_1_0_Importer"/>
<importer implementation="org.jetbrains.idea.maven.importing.Groovy_1_1_plus_Importer"/>
</extensions>
<extensions xmlns="com.intellij">
<languageInjector implementation="org.jetbrains.idea.maven.plugins.groovy.MavenGroovyInjector"/>
</extensions>
</idea-plugin>
@@ -0,0 +1,79 @@
/*
* 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 com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
/**
* @author Sergey Evdokimov
*/
class MavenGroovyInjectionTest extends LightCodeInsightFixtureTestCase {
public void testCompletion() {
myFixture.configureByText("pom.xml", """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>simpleMaven</groupId>
<artifactId>simpleMaven</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>groovy-magic</id>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
String<caret>
</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
""")
myFixture.completeBasic()
def lookups = myFixture.lookupElementStrings
assert lookups.containsAll(["String", "StringBuffer", "StringBuilder"])
}
}