Skip to content

Commit a746b28

Browse files
committed
Added completion and reference for module names in config.php with tests
1 parent b7d2dea commit a746b28

File tree

14 files changed

+338
-108
lines changed

14 files changed

+338
-108
lines changed

resources/META-INF/plugin.xml

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<projectService serviceImplementation="com.magento.idea.magento2plugin.project.Settings"/>
101101

102102
<completion.contributor language="XML" implementationClass="com.magento.idea.magento2plugin.completion.xml.XmlCompletionContributor" id="xml" />
103+
<completion.contributor language="PHP" implementationClass="com.magento.idea.magento2plugin.completion.php.PhpCompletionContributor" id="php" />
103104

104105
<psi.referenceContributor language="XML" implementation="com.magento.idea.magento2plugin.reference.xml.XmlReferenceContributor"/>
105106
<psi.referenceContributor language="PHP" implementation="com.magento.idea.magento2plugin.reference.php.PhpReferenceContributor"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.completion.php;
7+
8+
import com.intellij.codeInsight.completion.CompletionContributor;
9+
import com.intellij.codeInsight.completion.CompletionType;
10+
import com.magento.idea.magento2plugin.completion.provider.ModuleNameCompletionProvider;
11+
12+
import static com.magento.idea.magento2plugin.util.php.PhpPatternsHelper.CONFIGPHP_COMPLETION;
13+
14+
public class PhpCompletionContributor extends CompletionContributor {
15+
public PhpCompletionContributor() {
16+
/*
17+
'modules' => [
18+
'Vendor_Module' => 1
19+
]
20+
*/
21+
extend(
22+
CompletionType.BASIC,
23+
CONFIGPHP_COMPLETION,
24+
new ModuleNameCompletionProvider()
25+
);
26+
}
27+
}

src/com/magento/idea/magento2plugin/reference/php/PhpReferenceContributor.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.php;
67

78
import com.intellij.psi.PsiReferenceContributor;
89
import com.intellij.psi.PsiReferenceRegistrar;
10+
import com.magento.idea.magento2plugin.reference.provider.ModuleNameReferenceProvider;
911
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
1012
import com.magento.idea.magento2plugin.reference.provider.EventDispatchReferenceProvider;
1113
import org.jetbrains.annotations.NotNull;
1214

1315
public class PhpReferenceContributor extends PsiReferenceContributor {
1416
@Override
15-
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
17+
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
1618
// ->dispatch("event_name")
1719
registrar.registerReferenceProvider(
1820
PhpPatternsHelper.STRING_METHOD_ARGUMENT,
1921
new EventDispatchReferenceProvider()
2022
);
23+
24+
/*
25+
'modules' => [
26+
'Vendor_Module' => 1
27+
]
28+
*/
29+
registrar.registerReferenceProvider(
30+
PhpPatternsHelper.CONFIGPHP_MODULENAME,
31+
new ModuleNameReferenceProvider()
32+
);
2133
}
2234
}

src/com/magento/idea/magento2plugin/util/php/PhpPatternsHelper.java

+27
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.util.php;
67

78
import com.intellij.patterns.ElementPattern;
89
import com.intellij.patterns.PlatformPatterns;
910
import com.intellij.psi.PsiElement;
1011
import com.jetbrains.php.lang.PhpLanguage;
12+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
1113
import com.jetbrains.php.lang.patterns.PhpPatterns;
14+
import com.jetbrains.php.lang.psi.elements.ArrayHashElement;
1215
import com.jetbrains.php.lang.psi.elements.ParameterList;
16+
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
17+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
18+
19+
import static com.intellij.patterns.StandardPatterns.string;
1320

1421
public class PhpPatternsHelper {
1522
public static final ElementPattern<? extends PsiElement> STRING_METHOD_ARGUMENT =
@@ -23,4 +30,24 @@ public class PhpPatternsHelper {
2330
.phpFunctionReference()
2431
)
2532
).withLanguage(PhpLanguage.INSTANCE);
33+
34+
public static final ElementPattern<? extends PsiElement> CONFIGPHP_MODULENAME =
35+
PlatformPatterns.psiElement(StringLiteralExpression.class)
36+
.withSuperParent(5,PlatformPatterns.psiElement(ArrayHashElement.class)
37+
.withChild(PlatformPatterns.psiElement(PhpPsiElement.class)
38+
.withChild(PlatformPatterns.psiElement(StringLiteralExpression.class)
39+
.withText(string().contains("modules").withLength(9))
40+
)
41+
)
42+
);
43+
44+
public static final ElementPattern<? extends PsiElement> CONFIGPHP_COMPLETION =
45+
PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE)
46+
.withSuperParent(6, PlatformPatterns.psiElement(ArrayHashElement.class)
47+
.withChild(PlatformPatterns.psiElement(PhpPsiElement.class)
48+
.withChild(PlatformPatterns.psiElement(StringLiteralExpression.class)
49+
.withText(string().contains("modules").withLength(9))
50+
)
51+
)
52+
);
2653
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'modules' => [
9+
'Magento_C<caret>' => 1
10+
]
11+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'moduless' => [
9+
'Magento_C<caret>' => 1
10+
]
11+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'modules' => [
9+
'Magento_Catalog<caret>' => 1
10+
]
11+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'moduless' => [
9+
'Magento_Catalog<caret>' => 1
10+
]
11+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.completion;
7+
8+
import com.magento.idea.magento2plugin.BaseProjectTestCase;
9+
import com.magento.idea.magento2plugin.magento.packages.File;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
abstract public class BaseCompletionTestCase extends BaseProjectTestCase {
14+
private static final String MESSAGE_NO_LOOKUP = "No lookup element was provided";
15+
private final String testDataFolderPath
16+
= "testData" + File.separator + "completion" + File.separator;
17+
18+
@Override
19+
protected void setUp() throws Exception {
20+
super.setUp();
21+
myFixture.setTestDataPath(testDataFolderPath);
22+
}
23+
24+
private void configureFixture(final String filePath) {
25+
myFixture.configureByFile(filePath);
26+
myFixture.completeBasic();
27+
}
28+
29+
public void assertCompletionContains(final String filePath, final String... lookupStrings) {
30+
configureFixture(filePath);
31+
32+
final String messageEmptyLookup = "Failed that completion contains `%s`";
33+
final String messageComplationContains = "Failed that completion contains `%s` in `%s`";
34+
35+
checkContainsCompletion(lookupStrings, messageEmptyLookup, messageComplationContains);
36+
}
37+
38+
protected void assertFileContainsCompletions(
39+
final String filePath,
40+
final String... lookupStrings
41+
) {
42+
configureFixture(filePath);
43+
44+
final String messageEmptyLookup
45+
= "Failed that completion contains `%s` for file " + filePath;
46+
final String messageCompletionContains
47+
= "Failed that completion contains `%s` in `%s` for file " + filePath;
48+
49+
checkContainsCompletion(lookupStrings, messageEmptyLookup, messageCompletionContains);
50+
}
51+
52+
protected void assertFileNotContainsCompletions(
53+
final String filePath,
54+
final String... lookupStrings
55+
) {
56+
configureFixture(filePath);
57+
58+
final String messageEmptyLookup
59+
= "Failed that completion does not contain `%s` for file " + filePath;
60+
final String messageCompletionNotContains
61+
= "Failed that completion does not contain `%s` in `%s` for file " + filePath;
62+
63+
checkDoesNotContainCompletion(
64+
lookupStrings, messageEmptyLookup, messageCompletionNotContains
65+
);
66+
}
67+
68+
protected void assertCompletionNotShowing(final String filePath) {
69+
configureFixture(filePath);
70+
71+
final List<String> lookupElements = myFixture.getLookupElementStrings();
72+
73+
if (lookupElements != null && !lookupElements.isEmpty()) {
74+
final String messageCompletionDoesNotShow
75+
= "Failed asserting that completion does not show up";
76+
77+
fail(messageCompletionDoesNotShow);
78+
}
79+
}
80+
81+
protected void checkContainsCompletion(
82+
final String[] lookupStrings,
83+
final String emptyLookupError,
84+
final String completionContainsError
85+
) {
86+
if (lookupStrings.length == 0) {
87+
fail(MESSAGE_NO_LOOKUP);
88+
}
89+
90+
final List<String> lookupElements = myFixture.getLookupElementStrings();
91+
92+
if (lookupElements == null || lookupElements.isEmpty()) {
93+
fail(String.format(emptyLookupError, Arrays.toString(lookupStrings)));
94+
}
95+
96+
for (final String lookupString : lookupStrings) {
97+
if (!lookupElements.contains(lookupString)) {
98+
fail(String.format(
99+
completionContainsError, lookupString, lookupElements.toString())
100+
);
101+
}
102+
}
103+
}
104+
105+
protected void checkDoesNotContainCompletion(
106+
final String[] lookupStrings,
107+
final String emptyLookupError,
108+
final String completionDoesNotContainError
109+
) {
110+
if (lookupStrings.length == 0) {
111+
fail(MESSAGE_NO_LOOKUP);
112+
}
113+
114+
final List<String> lookupElements = myFixture.getLookupElementStrings();
115+
116+
if (lookupElements == null || lookupElements.isEmpty()) {
117+
fail(String.format(emptyLookupError, Arrays.toString(lookupStrings)));
118+
}
119+
120+
for (final String lookupString : lookupStrings) {
121+
if (lookupElements.contains(lookupString)) {
122+
fail(String.format(
123+
completionDoesNotContainError, lookupString, lookupElements.toString())
124+
);
125+
}
126+
}
127+
}
128+
129+
protected abstract String getFixturePath(String fileName);
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.completion.php;
7+
8+
import com.magento.idea.magento2plugin.completion.BaseCompletionTestCase;
9+
import com.magento.idea.magento2plugin.magento.packages.File;
10+
11+
abstract public class CompletionPhpFixtureTestCase extends BaseCompletionTestCase {
12+
private static final String FIXTURES_FOLDER_PATH = "php" + File.separator;
13+
14+
@Override
15+
protected String getFixturePath(final String fileName) {
16+
return prepareFixturePath(fileName, FIXTURES_FOLDER_PATH);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.completion.php;
7+
8+
public class ConfigPhpModuleCompletionRegistrarTest extends CompletionPhpFixtureTestCase {
9+
private static final String[] LOOKUP_MODULE_NAMES = {
10+
"Magento_Catalog",
11+
"Magento_Config"
12+
};
13+
14+
/**
15+
* Tests for module name completion under array key 'modules' in config.php
16+
*/
17+
public void testModuleNameMustHaveCompletion() {
18+
final String filePath = this.getFixturePath("config.php");
19+
myFixture.copyFileToProject(filePath);
20+
21+
assertFileContainsCompletions(filePath, LOOKUP_MODULE_NAMES);
22+
}
23+
24+
/**
25+
* Tests for no module name completion under a different array key in config.php
26+
*/
27+
public void testModuleNameMustNotHaveCompletion() {
28+
final String filePath = this.getFixturePath("config.php");
29+
myFixture.copyFileToProject(filePath);
30+
31+
assertCompletionNotShowing(filePath);
32+
}
33+
}

0 commit comments

Comments
 (0)