Skip to content

1031: Remove deprecated FilenameIndex#getVirtualFilesByName method usage #1037

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
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.provider;

import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.*;
import com.intellij.psi.*;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ProcessingContext;
Expand All @@ -16,47 +21,59 @@
import com.magento.idea.magento2plugin.reference.provider.util.GetModuleSourceFilesUtil;
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
import gnu.trove.THashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import java.util.*;

public class FilePathReferenceProvider extends PsiReferenceProvider {

@SuppressWarnings({
"PMD.CognitiveComplexity",
"PMD.CyclomaticComplexity",
"PMD.NPathComplexity",
"PMD.AvoidInstantiatingObjectsInLoops"
})
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {

List<PsiReference> psiReferences = new ArrayList<>();

String origValue = element.getText();
public PsiReference[] getReferencesByElement(
@NotNull final PsiElement element,
@NotNull final ProcessingContext context
) {
final String origValue = element.getText();

String filePath = GetFilePathUtil.getInstance().execute(origValue);
final String filePath = GetFilePathUtil.getInstance().execute(origValue);
if (null == filePath) {
return PsiReference.EMPTY_ARRAY;
}

// Find all files based on provided path
Collection<VirtualFile> files = getFiles(element);
if (!(files.size() > 0)) {
final Collection<VirtualFile> files = getFiles(element);

if (files.isEmpty()) {
return PsiReference.EMPTY_ARRAY;
}
final PsiManager psiManager = PsiManager.getInstance(element.getProject());

PsiManager psiManager = PsiManager.getInstance(element.getProject());
final List<PsiReference> psiReferences = new ArrayList<>();

String currentPath = "";
String[] pathParts = filePath.split("/");
final String[] pathParts = filePath.split("/");
for (int i = 0; i < pathParts.length; i++) {
String pathPart = pathParts[i];
final String pathPart = pathParts[i];
Boolean currentPathIsBuilt = false;

Map<TextRange, List<PsiElement>> psiPathElements = new THashMap<>();
final Map<TextRange, List<PsiElement>> psiPathElements = new THashMap<>();

for (VirtualFile file : files) {
String fileUrl = file.getUrl();
for (final VirtualFile file : files) {
final String fileUrl = file.getUrl();
if (!fileUrl.contains(filePath)) {
continue;
}
String rootPathUrl = fileUrl.substring(0, fileUrl.indexOf(filePath));
String[] relativePathParts = fileUrl.substring(fileUrl.indexOf(filePath)).split("/");
final String rootPathUrl = fileUrl.substring(0, fileUrl.indexOf(filePath));
final String[] relativePathParts
= fileUrl.substring(fileUrl.indexOf(filePath)).split("/");

if (!currentPathIsBuilt) {
currentPath = currentPath.isEmpty()
Expand All @@ -65,69 +82,73 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
currentPathIsBuilt = true;
}

VirtualFile currentVf = VirtualFileManager.getInstance()
final VirtualFile currentVf = VirtualFileManager.getInstance()
.findFileByUrl(rootPathUrl.concat(currentPath));

if (null != currentVf) {
PsiElement psiElement = currentVf.isDirectory()
final PsiElement psiElement = currentVf.isDirectory()
? psiManager.findDirectory(currentVf)
: psiManager.findFile(currentVf);
if (null != psiElement) {

TextRange pathRange = new TextRange(
origValue.indexOf(filePath)
+ (currentPath.lastIndexOf("/") == -1 ? 0 : currentPath.lastIndexOf("/") + 1),
origValue.indexOf(filePath)
+ (currentPath.lastIndexOf("/") == -1 ? 0 : currentPath.lastIndexOf("/") + 1)
+ pathPart.length()
final int currentPathIndex = currentPath.lastIndexOf('/') == -1
? 0 : currentPath.lastIndexOf('/') + 1;

final TextRange pathRange = new TextRange(
origValue.indexOf(filePath)
+ currentPathIndex,
origValue.indexOf(filePath)
+ currentPathIndex
+ pathPart.length()
);

if (!psiPathElements.containsKey(pathRange)) {
List<PsiElement> list = new ArrayList<>();
if (psiPathElements.containsKey(pathRange)) {
psiPathElements.get(pathRange).add(psiElement);
} else {
final List<PsiElement> list = new ArrayList<>();
list.add(psiElement);
psiPathElements.put(pathRange, list);
} else {
psiPathElements.get(pathRange).add(psiElement);
}
}
}
}

if (psiPathElements.size() > 0) {
psiPathElements.forEach(((textRange, psiElements) ->
psiReferences.add(new PolyVariantReferenceBase(element, textRange, psiElements))
));
if (!psiPathElements.isEmpty()) {
psiPathElements.forEach((textRange, psiElements) ->
psiReferences.add(
new PolyVariantReferenceBase(element, textRange, psiElements)
)
);
}
}

return psiReferences.toArray(new PsiReference[psiReferences.size()]);
return psiReferences.toArray(new PsiReference[0]);
}

private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
{
@SuppressWarnings("PMD.CognitiveComplexity")
private Collection<VirtualFile> getFiles(final @NotNull PsiElement element) {
Collection<VirtualFile> files = new ArrayList<>();

String filePath = GetFilePathUtil.getInstance().execute(element.getText());
final String filePath = GetFilePathUtil.getInstance().execute(element.getText());
if (null == filePath) {
return files;
}

String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
final String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);

if (fileName.matches(".*\\.\\w+$")) {
// extension presents
files = FilenameIndex.getVirtualFilesByName(
element.getProject(),
fileName,
GlobalSearchScope.allScope(element.getProject())
);
files.removeIf(f -> !f.getPath().endsWith(filePath));

// filter by module
Collection<VirtualFile> vfs = GetModuleSourceFilesUtil.getInstance().execute(element.getText(), element.getProject());
final Collection<VirtualFile> vfs = GetModuleSourceFilesUtil.getInstance()
.execute(element.getText(), element.getProject());
if (null != vfs) {
files.removeIf(f -> {
for (VirtualFile vf : vfs) {
for (final VirtualFile vf : vfs) {
if (f.getPath().startsWith(vf.getPath().concat("/"))) {
return false;
}
Expand All @@ -137,15 +158,16 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
}
} else if (isModuleNamePresent(element)) {
// extension absent
Collection<VirtualFile> vfs = GetModuleSourceFilesUtil.getInstance().execute(element.getText(), element.getProject());
final Collection<VirtualFile> vfs = GetModuleSourceFilesUtil.getInstance()
.execute(element.getText(), element.getProject());
if (null != vfs) {
for (VirtualFile vf : vfs) {
Collection<VirtualFile> vfChildren = GetAllSubFilesOfVirtualFileUtil.
getInstance().execute(vf);
for (final VirtualFile vf : vfs) {
final Collection<VirtualFile> vfChildren = GetAllSubFilesOfVirtualFileUtil
.getInstance().execute(vf);
if (null != vfChildren) {
vfChildren.removeIf(f -> {
if (!f.isDirectory()) {
String ext = f.getExtension();
if (!f.isDirectory()) { //NOPMD
final String ext = f.getExtension();
if (null != ext) {
return !f.getPath().endsWith(filePath.concat(".").concat(ext));
}
Expand All @@ -161,8 +183,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
return files;
}

private boolean isModuleNamePresent(@NotNull PsiElement element)
{
private boolean isModuleNamePresent(final @NotNull PsiElement element) {
return GetModuleNameUtil.getInstance().execute(element.getText()) != null;
}
}