Skip to content

Commit 71a0f9c

Browse files
authored
Merge pull request #374 from drpayyne/issue-321
Added completion and reference for module names in config.php with tests
2 parents 1ea55bf + fb52a67 commit 71a0f9c

File tree

14 files changed

+351
-118
lines changed

14 files changed

+351
-118
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
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"/>
Lines changed: 33 additions & 0 deletions
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+
import com.intellij.codeInsight.completion.CompletionContributor;
9+
import com.intellij.codeInsight.completion.CompletionType;
10+
import com.magento.idea.magento2plugin.completion.provider.ModuleNameCompletionProvider;
11+
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
12+
13+
@SuppressWarnings({
14+
"PMD.CallSuperInConstructor",
15+
})
16+
public class PhpCompletionContributor extends CompletionContributor {
17+
18+
/**
19+
* Constructor.
20+
*/
21+
public PhpCompletionContributor() {
22+
/*
23+
'modules' => [
24+
'Vendor_Module' => 1
25+
]
26+
*/
27+
extend(
28+
CompletionType.BASIC,
29+
PhpPatternsHelper.CONFIGPHP_COMPLETION,
30+
new ModuleNameCompletionProvider()
31+
);
32+
}
33+
}

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

Lines changed: 14 additions & 2 deletions
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;
9-
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
1010
import com.magento.idea.magento2plugin.reference.provider.EventDispatchReferenceProvider;
11+
import com.magento.idea.magento2plugin.reference.provider.ModuleNameReferenceProvider;
12+
import com.magento.idea.magento2plugin.util.php.PhpPatternsHelper;
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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,51 @@
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;
10+
import com.intellij.patterns.StandardPatterns;
911
import com.intellij.psi.PsiElement;
1012
import com.jetbrains.php.lang.PhpLanguage;
13+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
1114
import com.jetbrains.php.lang.patterns.PhpPatterns;
15+
import com.jetbrains.php.lang.psi.elements.ArrayHashElement;
1216
import com.jetbrains.php.lang.psi.elements.ParameterList;
17+
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
18+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1319

1420
public class PhpPatternsHelper {
1521
public static final ElementPattern<? extends PsiElement> STRING_METHOD_ARGUMENT =
16-
PhpPatterns
17-
.phpLiteralExpression()
18-
.withParent(
19-
PlatformPatterns
20-
.psiElement(ParameterList.class)
21-
.withParent(
22-
PhpPatterns
23-
.phpFunctionReference()
22+
PhpPatterns
23+
.phpLiteralExpression()
24+
.withParent(
25+
PlatformPatterns
26+
.psiElement(ParameterList.class)
27+
.withParent(
28+
PhpPatterns
29+
.phpFunctionReference()
30+
)
31+
).withLanguage(PhpLanguage.INSTANCE);
32+
33+
public static final ElementPattern<? extends PsiElement> CONFIGPHP_MODULENAME =
34+
PlatformPatterns.psiElement(StringLiteralExpression.class)
35+
.withSuperParent(5,PlatformPatterns.psiElement(ArrayHashElement.class)
36+
.withChild(PlatformPatterns.psiElement(PhpPsiElement.class)
37+
.withChild(PlatformPatterns.psiElement(StringLiteralExpression.class)
38+
.withText(StandardPatterns.string().contains("modules").withLength(9))
39+
)
40+
)
41+
);
42+
43+
public static final ElementPattern<? extends PsiElement> CONFIGPHP_COMPLETION =
44+
PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE)
45+
.withSuperParent(6, PlatformPatterns.psiElement(ArrayHashElement.class)
46+
.withChild(PlatformPatterns.psiElement(PhpPsiElement.class)
47+
.withChild(PlatformPatterns.psiElement(StringLiteralExpression.class)
48+
.withText(StandardPatterns.string().contains("modules").withLength(9))
49+
)
2450
)
25-
).withLanguage(PhpLanguage.INSTANCE);
51+
);
2652
}
Lines changed: 11 additions & 0 deletions
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+
];
Lines changed: 11 additions & 0 deletions
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+
];
Lines changed: 11 additions & 0 deletions
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+
];
Lines changed: 11 additions & 0 deletions
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+
];
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
public abstract 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+
/**
30+
* Assert that completion suggestions contains the given lookup strings.
31+
*/
32+
public void assertCompletionContains(final String filePath, final String... lookupStrings) {
33+
configureFixture(filePath);
34+
35+
final String messageEmptyLookup = "Failed that completion contains `%s`";
36+
final String messageComplationContains = "Failed that completion contains `%s` in `%s`";
37+
38+
checkContainsCompletion(lookupStrings, messageEmptyLookup, messageComplationContains);
39+
}
40+
41+
protected void assertFileContainsCompletions(
42+
final String filePath,
43+
final String... lookupStrings
44+
) {
45+
configureFixture(filePath);
46+
47+
final String messageEmptyLookup
48+
= "Failed that completion contains `%s` for file " + filePath;
49+
final String messageCompletionContains
50+
= "Failed that completion contains `%s` in `%s` for file " + filePath;
51+
52+
checkContainsCompletion(lookupStrings, messageEmptyLookup, messageCompletionContains);
53+
}
54+
55+
protected void assertFileNotContainsCompletions(
56+
final String filePath,
57+
final String... lookupStrings
58+
) {
59+
configureFixture(filePath);
60+
61+
final String messageCompletionNotContains
62+
= "Failed that completion does not contain `%s` in `%s` for file " + filePath;
63+
64+
checkDoesNotContainCompletion(
65+
lookupStrings, messageCompletionNotContains
66+
);
67+
}
68+
69+
protected void assertCompletionNotShowing(final String filePath) {
70+
configureFixture(filePath);
71+
72+
final List<String> lookupElements = myFixture.getLookupElementStrings();
73+
74+
if (lookupElements != null && !lookupElements.isEmpty()) {
75+
final String messageCompletionDoesNotShow
76+
= "Failed asserting that completion does not show up";
77+
78+
fail(messageCompletionDoesNotShow);
79+
}
80+
}
81+
82+
protected void checkContainsCompletion(
83+
final String[] lookupStrings,
84+
final String emptyLookupError,
85+
final String completionContainsError
86+
) {
87+
if (lookupStrings.length == 0) {
88+
fail(MESSAGE_NO_LOOKUP);
89+
}
90+
91+
final List<String> lookupElements = myFixture.getLookupElementStrings();
92+
93+
if (lookupElements == null || lookupElements.isEmpty()) {
94+
fail(String.format(emptyLookupError, Arrays.toString(lookupStrings)));
95+
}
96+
97+
for (final String lookupString : lookupStrings) {
98+
if (!lookupElements.contains(lookupString)) {
99+
fail(String.format(
100+
completionContainsError, lookupString, lookupElements.toString())
101+
);
102+
}
103+
}
104+
}
105+
106+
protected void checkDoesNotContainCompletion(
107+
final String[] lookupStrings,
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) {
117+
for (final String lookupString : lookupStrings) {
118+
if (lookupElements.contains(lookupString)) {
119+
fail(String.format(
120+
completionDoesNotContainError, lookupString, lookupElements.toString())
121+
);
122+
}
123+
}
124+
}
125+
}
126+
127+
protected abstract String getFixturePath(String fileName);
128+
}
Lines changed: 18 additions & 0 deletions
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+
public abstract 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+
}

0 commit comments

Comments
 (0)