Skip to content

Commit

Permalink
support vue.js compoenents in any components key
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Feb 25, 2024
1 parent 96aa0ae commit ab4fa36
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/java/de/espend/idea/vuejs/index/ComponentUsageIndex.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package de.espend.idea.vuejs.index;

import com.intellij.lang.ecmascript6.psi.ES6ImportedBinding;
import com.intellij.lang.ecmascript6.psi.ES6Property;
import com.intellij.lang.ecmascript6.resolve.JSFileReferencesUtil;
import com.intellij.lang.javascript.psi.JSExpression;
import com.intellij.lang.javascript.psi.JSObjectLiteralExpression;
import com.intellij.lang.javascript.psi.JSProperty;
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.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.PathUtil;
import com.intellij.util.indexing.*;
Expand Down Expand Up @@ -47,14 +55,36 @@ public class ComponentUsageIndex extends FileBasedIndexExtension<String, List<St
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof ES6ImportedBinding es6ImportedBinding) {
VueJsUtil.ES6Import singleImported = VueJsUtil.getSingleImported(es6ImportedBinding);
if (singleImported != null) {
String fileName = PathUtil.getFileName(singleImported.fileNameWithoutExtension());
imports.put(JSFileReferencesUtil.getFileNameWithoutExtension(fileName, new String[] {".vue"}), Arrays.asList(singleImported.referenceText(), singleImported.importName()));
visitImportedBinding(es6ImportedBinding);
} else if (element instanceof JSProperty jsProperty && "components".equals(jsProperty.getName())) {
// components: {Foobar}
JSExpression value = jsProperty.getValue();
if (value instanceof JSObjectLiteralExpression jsObjectLiteralExpression) {
for (ES6Property es6Property : PsiTreeUtil.collectElementsOfType(jsObjectLiteralExpression, ES6Property.class)) {
JSExpression value1 = es6Property.getValue();
if (value1 instanceof JSReferenceExpression jsReferenceExpression) {
String referenceName = JSReferenceExpressionImpl.getReferenceName(jsReferenceExpression.getNode());
if (referenceName != null) {
for (PsiElement resolveLocallyWithMergedResult : JSStubBasedPsiTreeUtil.resolveLocallyWithMergedResults(referenceName, jsReferenceExpression)) {
if (resolveLocallyWithMergedResult instanceof ES6ImportedBinding es6ImportedBinding) {
visitImportedBinding(es6ImportedBinding);
}
}
}
}
}
}
}
super.visitElement(element);
}

private void visitImportedBinding(@NotNull ES6ImportedBinding es6ImportedBinding) {
VueJsUtil.ES6Import singleImported = VueJsUtil.getSingleImported(es6ImportedBinding);
if (singleImported != null) {
String fileName = PathUtil.getFileName(singleImported.fileNameWithoutExtension());
imports.put(JSFileReferencesUtil.getFileNameWithoutExtension(fileName, new String[] {".vue"}), Arrays.asList(singleImported.referenceText(), singleImported.importName()));
}
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import ButtonCounter from './ButtonCounter.vue'
@Component({
components: {
ButtonCounter,
},
})
export default {}
<template>
<h1>Here is a child component!</h1>
<ButtonCounter />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script setup>
import ButtonCounter from './ButtonCounter.vue'
</script>

<template>
<h1>Here is a child component!</h1>
<ButtonCounter />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import ButtonCounter from './ButtonCounter.vue'
export default {
components: {
ButtonCounter
}
}
</script>

<template>
<h1>Here is a child component!</h1>
<ButtonCounter />
</template>

0 comments on commit ab4fa36

Please sign in to comment.