-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/de/espend/idea/vuejs/index/visitor/AppEntryPsiRecursiveElementVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package de.espend.idea.vuejs.index.visitor; | ||
|
||
import com.intellij.lang.ecmascript6.psi.ES6FromClause; | ||
import com.intellij.lang.ecmascript6.psi.ES6ImportDeclaration; | ||
import com.intellij.lang.ecmascript6.psi.ES6ImportedBinding; | ||
import com.intellij.lang.javascript.psi.JSArgumentList; | ||
import com.intellij.lang.javascript.psi.JSCallExpression; | ||
import com.intellij.lang.javascript.psi.JSExpression; | ||
import com.intellij.lang.javascript.psi.JSReferenceExpression; | ||
import com.intellij.lang.javascript.psi.impl.JSReferenceExpressionImpl; | ||
import com.intellij.lang.javascript.psi.util.JSStubBasedPsiTreeUtil; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiRecursiveElementVisitor; | ||
import com.intellij.util.PathUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class AppEntryPsiRecursiveElementVisitor extends PsiRecursiveElementVisitor { | ||
private final Map<String, List<String>> map; | ||
|
||
public AppEntryPsiRecursiveElementVisitor(@NotNull Map<String, List<String>> map) { | ||
this.map = map; | ||
} | ||
|
||
@Override | ||
public void visitElement(@NotNull PsiElement element) { | ||
if (element instanceof JSCallExpression jsCallExpression) { | ||
JSExpression methodExpression = jsCallExpression.getMethodExpression(); | ||
if (methodExpression != null) { | ||
String referenceName = JSReferenceExpressionImpl.getReferenceName(methodExpression.getNode()); | ||
|
||
if ("createApp".equals(referenceName) || "createElement".equals(referenceName)) { | ||
JSArgumentList argumentList = jsCallExpression.getArgumentList(); | ||
if (argumentList != null) { | ||
JSExpression[] arguments = argumentList.getArguments(); | ||
|
||
if (arguments.length > 0 && arguments[0] instanceof JSReferenceExpression jsReferenceExpression) { | ||
String referenceName1 = JSReferenceExpressionImpl.getReferenceName(jsReferenceExpression.getNode()); | ||
if (referenceName1 != null) { | ||
List<PsiElement> createApp = JSStubBasedPsiTreeUtil.resolveLocallyWithMergedResults(referenceName1, jsReferenceExpression); | ||
|
||
for (PsiElement psiElement : createApp) { | ||
if (psiElement instanceof ES6ImportedBinding es6ImportedBinding) { | ||
ES6ImportDeclaration declaration = es6ImportedBinding.getDeclaration(); | ||
if (declaration != null) { | ||
ES6FromClause fromClause = declaration.getFromClause(); | ||
if (fromClause != null) { | ||
String referenceText1 = fromClause.getReferenceText(); | ||
if (referenceText1 != null) { | ||
String referenceText = StringUtil.unquoteString(referenceText1); | ||
if (referenceText.endsWith(".vue")) { | ||
String fileName = PathUtil.getFileName(referenceText.substring(0, referenceText.length() - 4)); | ||
map.put(fileName, Arrays.asList(referenceText, referenceName)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
super.visitElement(element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...java/tests/de/espend/idea/vuejs/index/visitor/AppEntryPsiRecursiveElementVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tests.de.espend.idea.vuejs.index.visitor; | ||
|
||
import com.intellij.psi.PsiFile; | ||
import de.espend.idea.vuejs.index.visitor.AppEntryPsiRecursiveElementVisitor; | ||
import tests.de.espend.idea.vuejs.VueJsLightJavaCodeInsightFixtureTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class AppEntryPsiRecursiveElementVisitorTest extends VueJsLightJavaCodeInsightFixtureTestCase { | ||
public String getTestDataPath() { | ||
return "src/test/java/tests/de/espend/idea/vuejs/index/fixtures"; | ||
} | ||
|
||
public void testCreateapp() { | ||
PsiFile psiFile = myFixture.configureByFile("appentry-createapp.js"); | ||
|
||
Map<String, List<String>> map = new HashMap<>(); | ||
psiFile.acceptChildren(new AppEntryPsiRecursiveElementVisitor(map)); | ||
|
||
List<String> app = map.get("App"); | ||
|
||
assertEquals("./App.vue", app.get(0)); | ||
assertEquals("createApp", app.get(1)); | ||
} | ||
|
||
public void testCreateappV2() { | ||
PsiFile psiFile = myFixture.configureByFile("appentry-createelement-v2.ts"); | ||
|
||
Map<String, List<String>> map = new HashMap<>(); | ||
psiFile.acceptChildren(new AppEntryPsiRecursiveElementVisitor(map)); | ||
|
||
List<String> app = map.get("App"); | ||
|
||
assertEquals("./App.vue", app.get(0)); | ||
assertEquals("createElement", app.get(1)); | ||
} | ||
} |