Skip to content

Commit 284b8f6

Browse files
author
Vitaliy Boyko
committed
Covered Require js completion and reference
1 parent 7b8eaf0 commit 284b8f6

File tree

23 files changed

+282
-75
lines changed

23 files changed

+282
-75
lines changed

src/com/magento/idea/magento2plugin/magento/packages/Package.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public class Package {
88
public static String PACKAGES_ROOT = "app/code";
9+
public static String LIB_WEB_ROOT = "lib/web";
910
public static String APP = "app";
1011
public static String VENDOR = "vendor";
1112
public static String MODULE_BASE_AREA_DIR = "etc";

src/com/magento/idea/magento2plugin/stubs/indexes/js/MagentoLibJsIndex.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import com.intellij.util.indexing.*;
1010
import com.intellij.util.io.EnumeratorStringDescriptor;
1111
import com.intellij.util.io.KeyDescriptor;
12+
import com.magento.idea.magento2plugin.magento.packages.File;
13+
import com.magento.idea.magento2plugin.magento.packages.Package;
14+
import com.magento.idea.magento2plugin.project.Settings;
1215
import org.jetbrains.annotations.NotNull;
1316

1417
import java.util.HashMap;
@@ -29,7 +32,8 @@ public ID<String, Void> getName() {
2932
public DataIndexer<String, Void, FileContent> getIndexer() {
3033
return inputData -> {
3134
Map<String, Void> map = new HashMap<>();
32-
String libPath = inputData.getProject().getBasePath() + "/lib/web/";
35+
String libPath = Settings.getMagentoPath(inputData.getProject()) +
36+
File.separator + Package.LIB_WEB_ROOT + File.separator;
3337
VirtualFile file = inputData.getFile();
3438

3539
if (!file.getPath().contains(libPath)){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="general" >
5+
<field name="test_field" component="testF<caret>">
6+
</field>
7+
</fieldset>
8+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var config = {
2+
map: {
3+
'*': {
4+
testFile: 'Foo_Bar/js/file',
5+
}
6+
},
7+
paths: {
8+
'testFile2': 'Foo_Bar/js/file2'
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'lib'
3+
], function(lib) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'lib'
3+
], function(lib) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var test = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'Foo_Bar/js/file<caret>'
3+
], function(test) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'testjs<caret>'
3+
], function(test) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'testFile<caret>'
3+
], function(test) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'testFile2<caret>'
3+
], function(test) {
4+
'use strict';
5+
6+
return {
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="general" >
5+
<field name="test_field" component="Foo_Bar/js/file<caret>">
6+
</field>
7+
</fieldset>
8+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="general" >
5+
<field name="test_field" component="testjs<caret>">
6+
</field>
7+
</fieldset>
8+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="general" >
5+
<field name="test_field" component="testFile<caret>">
6+
</field>
7+
</fieldset>
8+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="general" >
5+
<field name="test_field" component="testFile2<caret>">
6+
</field>
7+
</fieldset>
8+
</form>

tests/com/magento/idea/magento2plugin/BaseProjectTestCase.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected String getTestDataPath() {
4040

4141
protected void enablePluginAndReindex() {
4242
Settings settings = Settings.getInstance(myFixture.getProject());
43+
settings.magentoPath = "/src";
4344
settings.pluginEnabled = true;
4445
settings.mftfSupportEnabled = true;
4546
IndexManager.manualReindex();
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+
package com.magento.idea.magento2plugin.completion.xml;
6+
7+
public class RequireJsCompletionRegistrarTest extends CompletionXmlFixtureTestCase {
8+
9+
public void testMappedComponentMustHaveCompletion() {
10+
String filePath = this.getFixturePath("test_form.xml");
11+
myFixture.copyFileToProject(filePath);
12+
13+
assertCompletionContains(filePath, new String[] {
14+
"testFile",
15+
"testFile2"
16+
});
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.reference;
6+
7+
import com.intellij.psi.PsiElement;
8+
import com.intellij.psi.PsiFile;
9+
import com.intellij.psi.PsiReference;
10+
import com.intellij.psi.xml.XmlAttributeValue;
11+
import com.jetbrains.php.lang.psi.elements.PhpClass;
12+
import com.magento.idea.magento2plugin.inspections.BaseInspectionsTestCase;
13+
import com.magento.idea.magento2plugin.magento.packages.File;
14+
15+
abstract public class BaseReferenceTestCase extends BaseInspectionsTestCase {
16+
17+
private static final String testDataFolderPath = "testData" + File.separator + "reference" + File.separator;
18+
19+
@Override
20+
protected void setUp() throws Exception {
21+
super.setUp();
22+
myFixture.setTestDataPath(testDataFolderPath);
23+
}
24+
25+
protected void assertHasReferenceToXmlAttributeValue(String reference) {
26+
PsiElement element = getElementFromCaret();
27+
assertEquals(reference, ((XmlAttributeValue) element.getReferences()[0].resolve()).getValue());
28+
}
29+
30+
protected void assertHasReferenceToFile(String reference) {
31+
String referenceNotFound = "Failed that element contains reference to the file `%s`";
32+
33+
PsiElement element = getElementFromCaret();
34+
for (PsiReference psiReference: element.getReferences()) {
35+
PsiElement resolved = psiReference.resolve();
36+
if (!(resolved instanceof PsiFile)) {
37+
continue;
38+
}
39+
if (((PsiFile) resolved).getVirtualFile().getPath().endsWith(reference)) {
40+
return;
41+
}
42+
}
43+
44+
fail(String.format(referenceNotFound, reference));
45+
}
46+
47+
protected void assertHasReferencePhpClass(String phpClassFqn) {
48+
PsiElement element = getElementFromCaret();
49+
PsiReference[] references = element.getReferences();
50+
assertEquals(
51+
phpClassFqn,
52+
((PhpClass) references[references.length -1].resolve())
53+
.getPresentableFQN()
54+
);
55+
}
56+
57+
protected void assertEmptyReference() {
58+
PsiElement element = getElementFromCaret();
59+
assertEmpty(element.getReferences());
60+
}
61+
62+
private PsiElement getElementFromCaret() {
63+
return myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.reference.js;
6+
7+
import com.magento.idea.magento2plugin.magento.packages.File;
8+
import com.magento.idea.magento2plugin.reference.BaseReferenceTestCase;
9+
10+
abstract public class ReferenceJsFixtureTestCase extends BaseReferenceTestCase {
11+
12+
private static final String fixturesFolderPath = "js" + File.separator;
13+
14+
protected String getFixturePath(String fileName) {
15+
return prepareFixturePath(fileName, fixturesFolderPath);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.reference.js;
6+
7+
public class RequireJsReferenceRegistrarTest extends ReferenceJsFixtureTestCase {
8+
9+
public void testMappedInjectionParameterMustHaveReference() {
10+
String filePath = this.getFixturePath("test.js");
11+
myFixture.configureByFile(filePath);
12+
13+
assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file.js");
14+
}
15+
16+
public void testPathInjectionParameterMustHaveReference() {
17+
String filePath = this.getFixturePath("test.js");
18+
myFixture.configureByFile(filePath);
19+
20+
assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file2.js");
21+
}
22+
23+
public void testFileInjectionParameterMustHaveReference() {
24+
String filePath = this.getFixturePath("test.js");
25+
myFixture.configureByFile(filePath);
26+
27+
assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file.js");
28+
}
29+
30+
public void testLibInjectionParameterMustHaveReference() {
31+
String filePath = this.getFixturePath("test.js");
32+
myFixture.configureByFile(filePath);
33+
34+
assertHasReferenceToFile("/lib/web/testjs.js");
35+
}
36+
}

tests/com/magento/idea/magento2plugin/reference/php/ReferencePhpFixtureTestCase.java

+2-37
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,14 @@
44
*/
55
package com.magento.idea.magento2plugin.reference.php;
66

7-
import com.intellij.psi.PsiElement;
8-
import com.intellij.psi.PsiReference;
9-
import com.intellij.psi.impl.source.xml.XmlAttributeValueImpl;
10-
import com.magento.idea.magento2plugin.BaseProjectTestCase;
11-
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
7+
import com.magento.idea.magento2plugin.reference.BaseReferenceTestCase;
128
import com.magento.idea.magento2plugin.magento.packages.File;
139

14-
abstract public class ReferencePhpFixtureTestCase extends BaseProjectTestCase {
10+
abstract public class ReferencePhpFixtureTestCase extends BaseReferenceTestCase {
1511

16-
private static final String testDataFolderPath = "testData" + File.separator + "reference" + File.separator;
1712
private static final String fixturesFolderPath = "php" + File.separator;
1813

19-
@Override
20-
protected void setUp() throws Exception {
21-
super.setUp();
22-
myFixture.setTestDataPath(testDataFolderPath);
23-
}
24-
2514
protected String getFixturePath(String fileName) {
2615
return prepareFixturePath(fileName, fixturesFolderPath);
2716
}
28-
29-
protected void assertHasReferenceToXmlAttributeValue(String attributeValue) {
30-
String referenceNotFound = "Failed that documents contains reference to XML attribute with value `%s`";
31-
32-
PsiElement element = getElementFromCaret();
33-
PsiReference[] references = element.getReferences();
34-
for (PsiReference reference: references) {
35-
if (!(reference instanceof PolyVariantReferenceBase)) {
36-
continue;
37-
}
38-
assertEquals(
39-
attributeValue,
40-
((XmlAttributeValueImpl) reference.resolve())
41-
.getValue()
42-
);
43-
return;
44-
}
45-
46-
fail(String.format(referenceNotFound, attributeValue));
47-
}
48-
49-
private PsiElement getElementFromCaret() {
50-
return myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();
51-
}
5217
}

0 commit comments

Comments
 (0)