Skip to content

Commit 9484af8

Browse files
author
Vitaliy Boyko
committed
Implemented MFTF test extends reference and completion
1 parent 65be14c commit 9484af8

File tree

19 files changed

+278
-128
lines changed

19 files changed

+278
-128
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.mftf.ActionGroupIndex" />
106106
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.mftf.DataIndex" />
107107
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.mftf.PageIndex" />
108-
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.mftf.StepKeyIndex" />
108+
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.mftf.TestNameIndex" />
109109
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.js.RequireJsIndex" />
110110
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.js.MagentoLibJsIndex" />
111111

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.completion.provider.mftf;
6+
7+
import com.intellij.codeInsight.completion.CompletionParameters;
8+
import com.intellij.codeInsight.completion.CompletionProvider;
9+
import com.intellij.codeInsight.completion.CompletionResultSet;
10+
import com.intellij.codeInsight.lookup.LookupElementBuilder;
11+
import com.intellij.psi.PsiElement;
12+
import com.intellij.util.ProcessingContext;
13+
import com.intellij.util.indexing.FileBasedIndex;
14+
import com.magento.idea.magento2plugin.magento.files.MftfPage;
15+
import com.magento.idea.magento2plugin.stubs.indexes.mftf.PageIndex;
16+
import com.magento.idea.magento2plugin.stubs.indexes.mftf.TestNameIndex;
17+
import org.jetbrains.annotations.NotNull;
18+
import java.util.Collection;
19+
20+
public class TestNameCompletionProvider extends CompletionProvider<CompletionParameters> {
21+
22+
@Override
23+
protected void addCompletions(
24+
@NotNull CompletionParameters parameters,
25+
ProcessingContext context,
26+
@NotNull CompletionResultSet result) {
27+
PsiElement position = parameters.getPosition().getOriginalElement();
28+
29+
if (position == null) {
30+
return;
31+
}
32+
33+
Collection<String> allKeys
34+
= FileBasedIndex.getInstance().getAllKeys(TestNameIndex.KEY, position.getProject());
35+
36+
for (String testName: allKeys) {
37+
result.addElement(LookupElementBuilder.create(testName));
38+
}
39+
}
40+
}

src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ public XmlCompletionContributor() {
248248
new DataCompletionProvider()
249249
);
250250

251+
// MFTF Test extends completion contributor
252+
extend(
253+
CompletionType.BASIC,
254+
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
255+
.inside(
256+
XmlPatterns.xmlAttribute().withName(MftfTest.EXTENDS_ATTRIBUTE)
257+
.withParent(XmlPatterns.xmlTag().withName(MftfTest.TEST_TAG)
258+
.withParent(XmlPatterns.xmlTag().withName(MftfTest.ROOT_TAG)
259+
)
260+
)
261+
),
262+
new TestNameCompletionProvider()
263+
);
264+
251265
registerCompletionsForDifferentNesting();
252266
}
253267

@@ -264,7 +278,7 @@ private void registerCompletionsForDifferentNesting() {
264278
i,
265279
XmlPatterns.xmlTag().withParent(
266280
XmlPatterns.xmlTag().withName(
267-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
281+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.TEST_TAG)
268282
)))
269283
),
270284
new SelectorCompletionProvider()
@@ -278,7 +292,7 @@ private void registerCompletionsForDifferentNesting() {
278292
XmlPatterns.xmlAttribute().withName(MftfActionGroup.URL_ATTRIBUTE)
279293
.withSuperParent(i ,XmlPatterns.xmlTag().withParent(
280294
XmlPatterns.xmlTag().withName(
281-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
295+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.TEST_TAG)
282296
)))
283297
),
284298
new PageCompletionProvider()

src/com/magento/idea/magento2plugin/indexes/IndexManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void manualReindex() {
4040
DataIndex.KEY,
4141
PageIndex.KEY,
4242
SectionIndex.KEY,
43-
StepKeyIndex.KEY,
43+
TestNameIndex.KEY,
4444
//graphql
4545
GraphQlResolverIndex.KEY
4646
};

src/com/magento/idea/magento2plugin/magento/files/MftfTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66

77
public class MftfTest {
88

9-
public static String ROOT_TAG = "test";
9+
public static String ROOT_TAG = "tests";
10+
public static String TEST_TAG = "test";
11+
public static String NAME_ATTRIBUTE = "name";
12+
public static String EXTENDS_ATTRIBUTE = "extends";
13+
public static String FILE_DIR_PARENTS = "Test/Mftf";
1014
}

src/com/magento/idea/magento2plugin/reference/provider/mftf/ActionGroupReferenceProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
15
package com.magento.idea.magento2plugin.reference.provider.mftf;
26

37
import com.intellij.ide.highlighter.XmlFileType;
@@ -12,7 +16,6 @@
1216
import com.magento.idea.magento2plugin.stubs.indexes.mftf.ActionGroupIndex;
1317
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
1418
import org.jetbrains.annotations.NotNull;
15-
1619
import java.util.ArrayList;
1720
import java.util.Collection;
1821
import java.util.List;

src/com/magento/idea/magento2plugin/reference/provider/mftf/DataReferenceProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
15
package com.magento.idea.magento2plugin.reference.provider.mftf;
26

37
import com.intellij.ide.highlighter.XmlFileType;
@@ -18,12 +22,10 @@
1822
import com.magento.idea.magento2plugin.stubs.indexes.mftf.DataIndex;
1923
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
2024
import org.jetbrains.annotations.NotNull;
21-
2225
import java.util.ArrayList;
2326
import java.util.Collection;
2427
import java.util.List;
2528

26-
2729
public class DataReferenceProvider extends PsiReferenceProvider {
2830

2931
@NotNull

src/com/magento/idea/magento2plugin/reference/provider/mftf/PageReferenceProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
15
package com.magento.idea.magento2plugin.reference.provider.mftf;
26

37
import com.intellij.ide.highlighter.XmlFileType;
@@ -16,7 +20,6 @@
1620
import com.magento.idea.magento2plugin.stubs.indexes.mftf.PageIndex;
1721
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
1822
import org.jetbrains.annotations.NotNull;
19-
2023
import java.util.ArrayList;
2124
import java.util.Collection;
2225
import java.util.List;

src/com/magento/idea/magento2plugin/reference/provider/mftf/SectionReferenceProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
15
package com.magento.idea.magento2plugin.reference.provider.mftf;
26

37
import com.intellij.ide.highlighter.XmlFileType;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.reference.provider.mftf;
6+
7+
import com.intellij.ide.highlighter.XmlFileType;
8+
import com.intellij.openapi.util.text.StringUtil;
9+
import com.intellij.openapi.vfs.VirtualFile;
10+
import com.intellij.psi.PsiElement;
11+
import com.intellij.psi.PsiManager;
12+
import com.intellij.psi.PsiReference;
13+
import com.intellij.psi.PsiReferenceProvider;
14+
import com.intellij.psi.search.GlobalSearchScope;
15+
import com.intellij.psi.xml.XmlAttributeValue;
16+
import com.intellij.psi.xml.XmlFile;
17+
import com.intellij.util.ProcessingContext;
18+
import com.intellij.util.indexing.FileBasedIndex;
19+
import com.magento.idea.magento2plugin.magento.files.MftfTest;
20+
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
21+
import com.magento.idea.magento2plugin.stubs.indexes.mftf.TestNameIndex;
22+
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
23+
import org.jetbrains.annotations.NotNull;
24+
import java.util.ArrayList;
25+
import java.util.Collection;
26+
import java.util.List;
27+
28+
public class TestNameReferenceProvider extends PsiReferenceProvider {
29+
30+
@NotNull
31+
@Override
32+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
33+
List<PsiReference> psiReferences = new ArrayList<>();
34+
35+
String origValue = StringUtil.unquoteString(element.getText());
36+
37+
Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance()
38+
.getContainingFiles(
39+
TestNameIndex.KEY,
40+
origValue,
41+
GlobalSearchScope.getScopeRestrictedByFileTypes(
42+
GlobalSearchScope.allScope(element.getProject()),
43+
XmlFileType.INSTANCE
44+
)
45+
);
46+
47+
PsiManager psiManager = PsiManager.getInstance(element.getProject());
48+
List<PsiElement> psiElements = new ArrayList<>();
49+
50+
for (VirtualFile virtualFile: containingFiles) {
51+
XmlFile xmlFile = (XmlFile) psiManager.findFile(virtualFile);
52+
53+
if (xmlFile == null) {
54+
continue;
55+
}
56+
57+
Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil
58+
.findAttributeValueElements(xmlFile, MftfTest.TEST_TAG, MftfTest.NAME_ATTRIBUTE, origValue);
59+
60+
psiElements.addAll(valueElements);
61+
}
62+
63+
if (psiElements.size() > 0) {
64+
psiReferences.add(new PolyVariantReferenceBase(element, psiElements));
65+
}
66+
67+
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
68+
}
69+
}

0 commit comments

Comments
 (0)