Skip to content

Fixed casting exception, refactored code #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 46 additions & 41 deletions src/com/magento/idea/magento2plugin/indexes/XmlIndex.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,80 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.indexes;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.*;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlTagValue;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.stubs.indexes.xml.PhpClassNameIndex;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class XmlIndex {

private static XmlIndex INSTANCE;

private Project project;
public final class XmlIndex {

private XmlIndex() {
}

public static XmlIndex getInstance(final Project project) {
if (null == INSTANCE) {
INSTANCE = new XmlIndex();
}
INSTANCE.project = project;
return INSTANCE;
}

public static List<XmlTag> getPhpClassDeclarations(PhpClass phpClass) {

List<XmlTag> result = new ArrayList<>();

String fqn = phpClass.getPresentableFQN();

PsiManager psiManager = PsiManager.getInstance(phpClass.getProject());

Collection<VirtualFile> vfs = FileBasedIndex.getInstance()
.getContainingFiles(PhpClassNameIndex.KEY, fqn, GlobalSearchScope.allScope(phpClass.getProject()));

for (VirtualFile vf : vfs) {
XmlFile xmlFile = (XmlFile)psiManager.findFile(vf);
if (xmlFile == null) {
/**
* Get PHP class declarations in the *.xml files.
*
* @param phpClass PhpClass
*
* @return List[XmlTag]
*/
public static List<XmlTag> getPhpClassDeclarations(final PhpClass phpClass) {

final List<XmlTag> result = new ArrayList<>();
final String fqn = phpClass.getPresentableFQN();
final PsiManager psiManager = PsiManager.getInstance(phpClass.getProject());
final Collection<VirtualFile> vfs = FileBasedIndex.getInstance()
.getContainingFiles(
PhpClassNameIndex.KEY,
fqn,
GlobalSearchScope.allScope(phpClass.getProject())
);

for (final VirtualFile vf : vfs) {
final PsiFile psiFile = psiManager.findFile(vf);

if (!(psiFile instanceof XmlFile)) {
continue;
}
final XmlFile xmlFile = (XmlFile) psiFile;
final XmlTag[] xmlTags = PsiTreeUtil.getChildrenOfType(
xmlFile.getFirstChild(),
XmlTag.class
);

XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(xmlFile.getFirstChild(), XmlTag.class);
if (xmlTags == null) {
continue;
}

for (XmlTag xmlTag: xmlTags) {
for (final XmlTag xmlTag: xmlTags) {
fillList(xmlTag, fqn, result);
}
}

return result;

}

private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
for (XmlTag childTag: parentTag.getSubTags()) {
for (XmlAttribute xmlAttribute: childTag.getAttributes()) {
private static void fillList(
final XmlTag parentTag,
final String fqn,
final List<XmlTag> list
) {
for (final XmlTag childTag: parentTag.getSubTags()) {
for (final XmlAttribute xmlAttribute: childTag.getAttributes()) {
String xmlAttributeValue = xmlAttribute.getValue();
if (xmlAttributeValue != null) {
xmlAttributeValue = xmlAttributeValue.startsWith("\\")
Expand All @@ -78,14 +84,13 @@ private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
}
}
}
XmlTagValue childTagValue = childTag.getValue();
final XmlTagValue childTagValue = childTag.getValue();
String tagValue = childTagValue.getTrimmedText();
tagValue = tagValue.startsWith("\\") ? tagValue.substring(1) : tagValue;

if (!tagValue.isEmpty() && tagValue.startsWith(fqn)) {
list.add(childTag);
}


fillList(childTag, fqn, list);
}
}
Expand Down