Skip to content

refactoring bundle loading, replacing HashMaps with ArrayList for nonunique bundle project names #1144

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
merged 1 commit into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
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
Expand Up @@ -5,15 +5,17 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.psi.PsiDirectory;
import com.jetbrains.php.PhpIndex;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -68,7 +70,7 @@ public boolean visitFile(@NotNull VirtualFile virtualFile) {
return files;
}

SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(project));
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
for(SymfonyBundle bundle : symfonyBundleUtil.getBundles()) {
PsiDirectory bundleDirectory = bundle.getDirectory();
if(null == bundleDirectory) {
Expand Down Expand Up @@ -112,8 +114,7 @@ public Collection<VirtualFile> resolveAssetFile(@NotNull Project project, @NotNu
int i = filename.indexOf("/");
if(i > 0) {
String relativeFilename = filename.substring(1, i);
SymfonyBundle bundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(relativeFilename);
if(bundle != null) {
for (SymfonyBundle bundle : new SymfonyBundleUtil(project).getBundle(relativeFilename)) {
String assetPath = filename.substring(i + 1);

Matcher matcher = Pattern.compile("^(.*[/\\\\])\\*([.\\w+]*)$").matcher(assetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static PhpClass getEntityRepositoryClass(@NotNull Project project, @NotNu
}
}

SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getContainingBundle(phpClass);
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(project).getContainingBundle(phpClass);
if(symfonyBundle != null) {
PhpClass repositoryClass = getEntityRepositoryClass(project, symfonyBundle, presentableFQN);
if(repositoryClass != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public static ControllerClassOnShortcutReturn getControllerClassOnShortcut(@NotN
String[] split = controllerName.split(":");
if(split.length == 3) {
// try to resolve on bundle path
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(project).getBundle(split[0]);
if(symfonyBundle != null) {
for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(project).getBundle(split[0])) {
PhpClass aClass = PhpElementsUtil.getClass(project, symfonyBundle.getNamespaceName() + "Controller\\" + split[1] + "Controller");
// @TODO: support multiple bundle names
if(aClass != null) {
return new ControllerClassOnShortcutReturn(aClass);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.adrienbrault.idea.symfony2plugin.templating.path;

import com.intellij.psi.PsiDirectory;
import com.jetbrains.php.PhpIndex;
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtension;
import fr.adrienbrault.idea.symfony2plugin.extension.TwigNamespaceExtensionParameter;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
Expand All @@ -24,7 +23,7 @@ public class BundleTwigNamespaceExtension implements TwigNamespaceExtension {
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) {
Collection<TwigPath> twigPaths = new ArrayList<>();

Collection<SymfonyBundle> symfonyBundles = new SymfonyBundleUtil(PhpIndex.getInstance(parameter.getProject())).getBundles();
Collection<SymfonyBundle> symfonyBundles = new SymfonyBundleUtil(parameter.getProject()).getBundles();
for (SymfonyBundle bundle : symfonyBundles) {
PsiDirectory views = bundle.getSubDirectory("Resources", "views");
if(views == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static String[] getControllerMethodShortcut(@NotNull Method method) {
return new String[0];
}

SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(method.getProject()));
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(method.getProject());
SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(phpClass);
if(symfonyBundle == null) {
return new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIndex;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.dict.BundleFile;
import fr.adrienbrault.idea.symfony2plugin.util.dict.ResourceFileInsertHandler;
Expand Down Expand Up @@ -38,9 +37,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
return;
}

PhpIndex phpIndex = PhpIndex.getInstance(completionParameters.getPosition().getProject());

SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(phpIndex);
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(completionParameters.getPosition().getProject());
List<BundleFile> bundleFiles = new ArrayList<>();

for(SymfonyBundle symfonyBundle : symfonyBundleUtil.getBundles()) {
Expand Down
98 changes: 36 additions & 62 deletions src/fr/adrienbrault/idea/symfony2plugin/util/SymfonyBundleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,91 +10,69 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class SymfonyBundleUtil {
@NotNull
private final Project project;

protected PhpIndex phpIndex;
protected HashMap<String, SymfonyBundle> symfonyBundles;
@Nullable
private Collection<SymfonyBundle> symfonyBundles;

public SymfonyBundleUtil(PhpIndex phpIndex) {
this.phpIndex = phpIndex;
this.loadBundles();
public SymfonyBundleUtil(@NotNull Project project) {
this.project = project;
}

public SymfonyBundleUtil(Project project) {
this(PhpIndex.getInstance(project));
}
@NotNull
public Collection<SymfonyBundle> getBundles() {
if(symfonyBundles != null) {
return symfonyBundles;
}

protected void loadBundles() {
symfonyBundles = new ArrayList<>();

this.symfonyBundles = new HashMap<>();
Collection<PhpClass> phpClasses = this.phpIndex.getAllSubclasses("\\Symfony\\Component\\HttpKernel\\Bundle\\Bundle");
Collection<PhpClass> phpClasses = PhpIndex.getInstance(project).getAllSubclasses("\\Symfony\\Component\\HttpKernel\\Bundle\\Bundle");

for (PhpClass phpClass : phpClasses) {
this.symfonyBundles.put(phpClass.getName(), new SymfonyBundle(phpClass));
symfonyBundles.add(new SymfonyBundle(phpClass));
}

return symfonyBundles;
}

public Collection<SymfonyBundle> getBundles() {
return this.symfonyBundles.values();
}

@NotNull
public Map<String, SymfonyBundle> getParentBundles() {

Map<String, SymfonyBundle> bundles = new HashMap<>();

for (Map.Entry<String, SymfonyBundle> entry : this.symfonyBundles.entrySet()) {
if(entry.getValue().getParentBundleName() != null) {
bundles.put(entry.getKey(), entry.getValue());
for (SymfonyBundle bundle : getBundles()) {
if(bundle.getParentBundleName() != null) {
bundles.put(bundle.getName(), bundle);
}
}

return bundles;
}

@Nullable
public SymfonyBundle getBundle(String bundleName) {
return this.symfonyBundles.get(bundleName);
}

public boolean bundleExists(String bundleName) {
return this.symfonyBundles.get(bundleName) != null;
}

@Nullable
public SymfonyBundle getContainingBundle(String bundleShortcutName) {

if(!bundleShortcutName.startsWith("@")) {
return null;
}

int stripedBundlePos = bundleShortcutName.indexOf("/");
if(stripedBundlePos == -1) {
return null;
}

String bundleName = bundleShortcutName.substring(1, stripedBundlePos);
for(SymfonyBundle bundle : this.getBundles()) {
if(bundle.getName().equals(bundleName)) {
return bundle;
}
}

return null;
@NotNull
public Collection<SymfonyBundle> getBundle(@NotNull String bundleName) {
return getBundles()
.stream()
.filter(
symfonyBundle -> bundleName.equals(symfonyBundle.getName())
)
.collect(Collectors.toSet());
}


@Nullable
public SymfonyBundle getContainingBundle(PhpClass phpClass) {

for(SymfonyBundle bundle : this.getBundles()) {
public SymfonyBundle getContainingBundle(@NotNull PhpClass phpClass) {
for(SymfonyBundle bundle : getBundles()) {
if(bundle.isInBundle(phpClass)) {
return bundle;
}
Expand All @@ -104,9 +82,8 @@ public SymfonyBundle getContainingBundle(PhpClass phpClass) {
}

@Nullable
public SymfonyBundle getContainingBundle(PsiFile psiFile) {

for(SymfonyBundle bundle : this.getBundles()) {
public SymfonyBundle getContainingBundle(@NotNull PsiFile psiFile) {
for(SymfonyBundle bundle : getBundles()) {
if(bundle.isInBundle(psiFile)) {
return bundle;
}
Expand All @@ -117,8 +94,7 @@ public SymfonyBundle getContainingBundle(PsiFile psiFile) {

@Nullable
public SymfonyBundle getContainingBundle(@NotNull VirtualFile virtualFile) {

for(SymfonyBundle bundle : this.getBundles()) {
for(SymfonyBundle bundle : getBundles()) {
if(bundle.isInBundle(virtualFile)) {
return bundle;
}
Expand All @@ -128,15 +104,13 @@ public SymfonyBundle getContainingBundle(@NotNull VirtualFile virtualFile) {
}

@Nullable
public SymfonyBundle getContainingBundle(PsiDirectory directory) {

for(SymfonyBundle bundle : this.getBundles()) {
public SymfonyBundle getContainingBundle(@NotNull PsiDirectory directory) {
for(SymfonyBundle bundle : getBundles()) {
if(bundle.isInBundle(directory.getVirtualFile())) {
return bundle;
}
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.project.Project;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.routing.Route;
Expand All @@ -23,21 +22,19 @@
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class ControllerIndex {

private Project project;
private PhpIndex phpIndex;
@NotNull
final private Project project;

private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;

public ControllerIndex(Project project) {
public ControllerIndex(@NotNull Project project) {
this.project = project;
this.phpIndex = PhpIndex.getInstance(project);
}

public Collection<ControllerAction> getActions() {
Collection<ControllerAction> actions = new ArrayList<>();

for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(phpIndex).getBundles()) {
for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(project).getBundles()) {
actions.addAll(this.getActionMethods(symfonyBundle));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.PhpIndex;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.FileResourcesIndex;
import fr.adrienbrault.idea.symfony2plugin.util.FileResourceVisitorUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpIndexUtil;
Expand Down Expand Up @@ -200,18 +199,18 @@ public static Collection<PsiFile> getFileResourceTargetsInBundleScope(@NotNull P

String bundleName = content.substring(1, content.indexOf("/"));

SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(bundleName);
if(symfonyBundle == null) {
return Collections.emptyList();
}
Collection<PsiFile> targets = new HashSet<>();

String path = content.substring(content.indexOf("/") + 1);
PsiFile psiFile = PsiElementUtils.virtualFileToPsiFile(project, symfonyBundle.getRelative(path));
if(psiFile == null) {
return Collections.emptyList();
for (SymfonyBundle bundle : new SymfonyBundleUtil(project).getBundle(bundleName)) {
String path = content.substring(content.indexOf("/") + 1);
PsiFile psiFile = PsiElementUtils.virtualFileToPsiFile(project, bundle.getRelative(path));

if(psiFile != null) {
targets.add(psiFile);
}
}

return Collections.singletonList(psiFile);
return targets;
}

/**
Expand All @@ -231,8 +230,7 @@ public static Collection<PsiElement> getFileResourceTargetsInBundleDirectory(@No

String bundleName = content.substring(1, content.indexOf("\\"));

SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(bundleName);
if(symfonyBundle == null) {
if(new SymfonyBundleUtil(project).getBundle(bundleName).size() == 0) {
return Collections.emptyList();
}

Expand Down