Skip to content

Commit 83bd8a0

Browse files
committed
initial commit
This version adds a menu entry to go to the test class from the tested class
0 parents  commit 83bd8a0

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/

META-INF/plugin.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<idea-plugin version="2">
2+
<id>com.atoum.phpstormplugin</id>
3+
<name>atoum plugin</name>
4+
<version>1.0</version>
5+
<vendor email="[email protected]">Adrien Gallou</vendor>
6+
7+
<description><![CDATA[
8+
Integrate atoum in PHPStorm.
9+
]]></description>
10+
11+
<change-notes><![CDATA[
12+
CHANGELOG.
13+
]]>
14+
</change-notes>
15+
16+
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
17+
<idea-version since-build="131"/>
18+
19+
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products
20+
on how to target different products -->
21+
<!-- uncomment to enable plugin in all products
22+
<depends>com.intellij.modules.lang</depends>
23+
-->
24+
25+
<extensions defaultExtensionNs="com.intellij">*
26+
<!-- Add your extensions here -->
27+
</extensions>
28+
29+
<application-components>
30+
<!-- Add your application components here -->
31+
</application-components>
32+
33+
<project-components>
34+
<!-- Add your project components here -->
35+
</project-components>
36+
37+
<actions>
38+
<!-- Add your actions here -->
39+
<action id="OpenSpecForClass" class="pl.projectspace.idea.plugins.php.atoum.actions.AtoumSwitchContext"
40+
icon="/pl/projectspace/idea/plugins/php/atoum/icons/atoum_16_16.png"
41+
text="Go to atoum test">
42+
<add-to-group group-id="GoToCodeGroup" anchor="last"/>
43+
<add-to-group group-id="EditorPopupMenu.GoTo" anchor="last"/>
44+
</action>
45+
</actions>
46+
47+
48+
<depends>com.jetbrains.php</depends>
49+
<depends>com.intellij.modules.platform</depends>
50+
51+
</idea-plugin>

phpstorm-plugin.iml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="PLUGIN_MODULE" version="4">
3+
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" />
4+
<component name="NewModuleRootManager" inherit-compiler-output="true">
5+
<exclude-output />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
9+
</content>
10+
<orderEntry type="jdk" jdkName="IDEA PS-143.1184.87" jdkType="IDEA JDK" />
11+
<orderEntry type="sourceFolder" forTests="false" />
12+
<orderEntry type="library" scope="PROVIDED" name="php-openapi" level="project" />
13+
</component>
14+
</module>
238 Bytes
Loading
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package pl.projectspace.idea.plugins.php.atoum.actions;
2+
3+
import com.intellij.openapi.actionSystem.*;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.psi.util.PsiTreeUtil;
6+
import com.jetbrains.php.PhpIndex;
7+
import com.jetbrains.php.lang.psi.PhpFile;
8+
import com.jetbrains.php.lang.psi.elements.PhpClass;
9+
import org.jetbrains.annotations.Nullable;
10+
import com.intellij.ide.actions.OpenFileAction;
11+
import java.util.Collection;
12+
13+
public class AtoumSwitchContext extends AnAction {
14+
15+
@Override
16+
public void update(AnActionEvent e) {
17+
e.getPresentation().setEnabled(false);
18+
e.getPresentation().setVisible(true);
19+
e.getPresentation().setText("Go to atoum test");
20+
21+
PhpClass testedClass = getTestedClass(e);
22+
if (null == testedClass) {
23+
return;
24+
}
25+
26+
e.getPresentation().setText("Go to atoum test : " + testedClass.getFQN());
27+
e.getPresentation().setEnabled(true);
28+
}
29+
30+
public void actionPerformed(final AnActionEvent e) {
31+
PhpClass testedClass = getTestedClass(e);
32+
if (null == testedClass) {
33+
return;
34+
}
35+
OpenFileAction.openFile(testedClass.getContainingFile().getVirtualFile().getPath(), e.getProject());
36+
}
37+
38+
@Nullable
39+
protected PhpClass getTestedClass(final AnActionEvent e) {
40+
Object psiFile = e.getData(PlatformDataKeys.PSI_FILE);
41+
42+
if (null == psiFile) {
43+
return null;
44+
}
45+
46+
if (psiFile instanceof PhpFile) {
47+
PhpFile phpFile = ((PhpFile) psiFile);
48+
PhpClass testedClass = getTestedClass(e.getProject(), phpFile);
49+
if (null == testedClass) {
50+
return null;
51+
}
52+
53+
return testedClass;
54+
}
55+
56+
return null;
57+
}
58+
59+
@Nullable
60+
protected PhpClass getTestedClass(Project project, PhpFile phpFile) {
61+
PhpClass currentClass = getFirstClassFromFile(phpFile);
62+
if (null == currentClass) {
63+
return null;
64+
}
65+
66+
String testedClassname = currentClass.getNamespaceName() + "tests\\units\\" + currentClass.getName();
67+
Collection<PhpClass> phpClasses = PhpIndex.getInstance(project).getAnyByFQN(testedClassname);
68+
if (phpClasses.size() == 1) {
69+
PhpClass testedClass = (PhpClass)phpClasses.toArray()[0];
70+
return testedClass;
71+
}
72+
73+
return null;
74+
}
75+
76+
//https://github.com/Haehnchen/idea-php-symfony2-plugin/blob/cb422db9779025d65fdf0ba5d26a38d401eca939/src/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java#L784
77+
@Nullable
78+
private PhpClass getFirstClassFromFile(PhpFile phpFile) {
79+
Collection<PhpClass> phpClasses = PsiTreeUtil.collectElementsOfType(phpFile, PhpClass.class);
80+
return phpClasses.size() == 0 ? null : phpClasses.iterator().next();
81+
}
82+
83+
}

0 commit comments

Comments
 (0)