Skip to content

Commit 0dc3ed7

Browse files
authored
Merge pull request #1144 from Haehnchen/feature/multiple-bundle-names
refactoring bundle loading, replacing HashMaps with ArrayList for nonunique bundle project names
2 parents f5814de + 2bffcc9 commit 0dc3ed7

File tree

9 files changed

+62
-96
lines changed

9 files changed

+62
-96
lines changed

src/fr/adrienbrault/idea/symfony2plugin/asset/AssetDirectoryReader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
import com.intellij.openapi.vfs.VirtualFile;
66
import com.intellij.openapi.vfs.VirtualFileVisitor;
77
import com.intellij.psi.PsiDirectory;
8-
import com.jetbrains.php.PhpIndex;
98
import fr.adrienbrault.idea.symfony2plugin.Settings;
109
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
1110
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
1211
import org.apache.commons.lang.StringUtils;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
1514

16-
import java.util.*;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.Collection;
18+
import java.util.HashSet;
1719
import java.util.regex.Matcher;
1820
import java.util.regex.Pattern;
1921

@@ -68,7 +70,7 @@ public boolean visitFile(@NotNull VirtualFile virtualFile) {
6870
return files;
6971
}
7072

71-
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(project));
73+
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
7274
for(SymfonyBundle bundle : symfonyBundleUtil.getBundles()) {
7375
PsiDirectory bundleDirectory = bundle.getDirectory();
7476
if(null == bundleDirectory) {
@@ -112,8 +114,7 @@ public Collection<VirtualFile> resolveAssetFile(@NotNull Project project, @NotNu
112114
int i = filename.indexOf("/");
113115
if(i > 0) {
114116
String relativeFilename = filename.substring(1, i);
115-
SymfonyBundle bundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(relativeFilename);
116-
if(bundle != null) {
117+
for (SymfonyBundle bundle : new SymfonyBundleUtil(project).getBundle(relativeFilename)) {
117118
String assetPath = filename.substring(i + 1);
118119

119120
Matcher matcher = Pattern.compile("^(.*[/\\\\])\\*([.\\w+]*)$").matcher(assetPath);

src/fr/adrienbrault/idea/symfony2plugin/doctrine/EntityHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static PhpClass getEntityRepositoryClass(@NotNull Project project, @NotNu
112112
}
113113
}
114114

115-
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getContainingBundle(phpClass);
115+
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(project).getContainingBundle(phpClass);
116116
if(symfonyBundle != null) {
117117
PhpClass repositoryClass = getEntityRepositoryClass(project, symfonyBundle, presentableFQN);
118118
if(repositoryClass != null) {

src/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ public static ControllerClassOnShortcutReturn getControllerClassOnShortcut(@NotN
224224
String[] split = controllerName.split(":");
225225
if(split.length == 3) {
226226
// try to resolve on bundle path
227-
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(project).getBundle(split[0]);
228-
if(symfonyBundle != null) {
227+
for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(project).getBundle(split[0])) {
229228
PhpClass aClass = PhpElementsUtil.getClass(project, symfonyBundle.getNamespaceName() + "Controller\\" + split[1] + "Controller");
229+
// @TODO: support multiple bundle names
230230
if(aClass != null) {
231231
return new ControllerClassOnShortcutReturn(aClass);
232232
}

src/fr/adrienbrault/idea/symfony2plugin/templating/path/BundleTwigNamespaceExtension.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.adrienbrault.idea.symfony2plugin.templating.path;
22

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

27-
Collection<SymfonyBundle> symfonyBundles = new SymfonyBundleUtil(PhpIndex.getInstance(parameter.getProject())).getBundles();
26+
Collection<SymfonyBundle> symfonyBundles = new SymfonyBundleUtil(parameter.getProject()).getBundles();
2827
for (SymfonyBundle bundle : symfonyBundles) {
2928
PsiDirectory views = bundle.getSubDirectory("Resources", "views");
3029
if(views == null) {

src/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static String[] getControllerMethodShortcut(@NotNull Method method) {
125125
return new String[0];
126126
}
127127

128-
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(PhpIndex.getInstance(method.getProject()));
128+
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(method.getProject());
129129
SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(phpClass);
130130
if(symfonyBundle == null) {
131131
return new String[0];

src/fr/adrienbrault/idea/symfony2plugin/util/SymfonyBundleFileCompletionProvider.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.intellij.openapi.vfs.VirtualFile;
1010
import com.intellij.openapi.vfs.VirtualFileVisitor;
1111
import com.intellij.util.ProcessingContext;
12-
import com.jetbrains.php.PhpIndex;
1312
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
1413
import fr.adrienbrault.idea.symfony2plugin.util.dict.BundleFile;
1514
import fr.adrienbrault.idea.symfony2plugin.util.dict.ResourceFileInsertHandler;
@@ -38,9 +37,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
3837
return;
3938
}
4039

41-
PhpIndex phpIndex = PhpIndex.getInstance(completionParameters.getPosition().getProject());
42-
43-
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(phpIndex);
40+
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(completionParameters.getPosition().getProject());
4441
List<BundleFile> bundleFiles = new ArrayList<>();
4542

4643
for(SymfonyBundle symfonyBundle : symfonyBundleUtil.getBundles()) {

src/fr/adrienbrault/idea/symfony2plugin/util/SymfonyBundleUtil.java

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,69 @@
1010
import org.jetbrains.annotations.NotNull;
1111
import org.jetbrains.annotations.Nullable;
1212

13+
import java.util.ArrayList;
1314
import java.util.Collection;
1415
import java.util.HashMap;
1516
import java.util.Map;
17+
import java.util.stream.Collectors;
1618

1719
/**
1820
* @author Daniel Espendiller <daniel@espendiller.net>
1921
*/
2022
public class SymfonyBundleUtil {
23+
@NotNull
24+
private final Project project;
2125

22-
protected PhpIndex phpIndex;
23-
protected HashMap<String, SymfonyBundle> symfonyBundles;
26+
@Nullable
27+
private Collection<SymfonyBundle> symfonyBundles;
2428

25-
public SymfonyBundleUtil(PhpIndex phpIndex) {
26-
this.phpIndex = phpIndex;
27-
this.loadBundles();
29+
public SymfonyBundleUtil(@NotNull Project project) {
30+
this.project = project;
2831
}
2932

30-
public SymfonyBundleUtil(Project project) {
31-
this(PhpIndex.getInstance(project));
32-
}
33+
@NotNull
34+
public Collection<SymfonyBundle> getBundles() {
35+
if(symfonyBundles != null) {
36+
return symfonyBundles;
37+
}
3338

34-
protected void loadBundles() {
39+
symfonyBundles = new ArrayList<>();
3540

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

3943
for (PhpClass phpClass : phpClasses) {
40-
this.symfonyBundles.put(phpClass.getName(), new SymfonyBundle(phpClass));
44+
symfonyBundles.add(new SymfonyBundle(phpClass));
4145
}
4246

47+
return symfonyBundles;
4348
}
4449

45-
public Collection<SymfonyBundle> getBundles() {
46-
return this.symfonyBundles.values();
47-
}
48-
50+
@NotNull
4951
public Map<String, SymfonyBundle> getParentBundles() {
50-
5152
Map<String, SymfonyBundle> bundles = new HashMap<>();
5253

53-
for (Map.Entry<String, SymfonyBundle> entry : this.symfonyBundles.entrySet()) {
54-
if(entry.getValue().getParentBundleName() != null) {
55-
bundles.put(entry.getKey(), entry.getValue());
54+
for (SymfonyBundle bundle : getBundles()) {
55+
if(bundle.getParentBundleName() != null) {
56+
bundles.put(bundle.getName(), bundle);
5657
}
5758
}
5859

5960
return bundles;
6061
}
6162

62-
@Nullable
63-
public SymfonyBundle getBundle(String bundleName) {
64-
return this.symfonyBundles.get(bundleName);
65-
}
66-
67-
public boolean bundleExists(String bundleName) {
68-
return this.symfonyBundles.get(bundleName) != null;
69-
}
70-
71-
@Nullable
72-
public SymfonyBundle getContainingBundle(String bundleShortcutName) {
73-
74-
if(!bundleShortcutName.startsWith("@")) {
75-
return null;
76-
}
77-
78-
int stripedBundlePos = bundleShortcutName.indexOf("/");
79-
if(stripedBundlePos == -1) {
80-
return null;
81-
}
82-
83-
String bundleName = bundleShortcutName.substring(1, stripedBundlePos);
84-
for(SymfonyBundle bundle : this.getBundles()) {
85-
if(bundle.getName().equals(bundleName)) {
86-
return bundle;
87-
}
88-
}
89-
90-
return null;
63+
@NotNull
64+
public Collection<SymfonyBundle> getBundle(@NotNull String bundleName) {
65+
return getBundles()
66+
.stream()
67+
.filter(
68+
symfonyBundle -> bundleName.equals(symfonyBundle.getName())
69+
)
70+
.collect(Collectors.toSet());
9171
}
9272

93-
9473
@Nullable
95-
public SymfonyBundle getContainingBundle(PhpClass phpClass) {
96-
97-
for(SymfonyBundle bundle : this.getBundles()) {
74+
public SymfonyBundle getContainingBundle(@NotNull PhpClass phpClass) {
75+
for(SymfonyBundle bundle : getBundles()) {
9876
if(bundle.isInBundle(phpClass)) {
9977
return bundle;
10078
}
@@ -104,9 +82,8 @@ public SymfonyBundle getContainingBundle(PhpClass phpClass) {
10482
}
10583

10684
@Nullable
107-
public SymfonyBundle getContainingBundle(PsiFile psiFile) {
108-
109-
for(SymfonyBundle bundle : this.getBundles()) {
85+
public SymfonyBundle getContainingBundle(@NotNull PsiFile psiFile) {
86+
for(SymfonyBundle bundle : getBundles()) {
11087
if(bundle.isInBundle(psiFile)) {
11188
return bundle;
11289
}
@@ -117,8 +94,7 @@ public SymfonyBundle getContainingBundle(PsiFile psiFile) {
11794

11895
@Nullable
11996
public SymfonyBundle getContainingBundle(@NotNull VirtualFile virtualFile) {
120-
121-
for(SymfonyBundle bundle : this.getBundles()) {
97+
for(SymfonyBundle bundle : getBundles()) {
12298
if(bundle.isInBundle(virtualFile)) {
12399
return bundle;
124100
}
@@ -128,15 +104,13 @@ public SymfonyBundle getContainingBundle(@NotNull VirtualFile virtualFile) {
128104
}
129105

130106
@Nullable
131-
public SymfonyBundle getContainingBundle(PsiDirectory directory) {
132-
133-
for(SymfonyBundle bundle : this.getBundles()) {
107+
public SymfonyBundle getContainingBundle(@NotNull PsiDirectory directory) {
108+
for(SymfonyBundle bundle : getBundles()) {
134109
if(bundle.isInBundle(directory.getVirtualFile())) {
135110
return bundle;
136111
}
137112
}
138113

139114
return null;
140115
}
141-
142116
}

src/fr/adrienbrault/idea/symfony2plugin/util/controller/ControllerIndex.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.intellij.codeInsight.lookup.LookupElement;
44
import com.intellij.openapi.project.Project;
5-
import com.jetbrains.php.PhpIndex;
65
import com.jetbrains.php.lang.psi.elements.Method;
76
import com.jetbrains.php.lang.psi.elements.PhpClass;
87
import fr.adrienbrault.idea.symfony2plugin.routing.Route;
@@ -23,21 +22,19 @@
2322
* @author Daniel Espendiller <daniel@espendiller.net>
2423
*/
2524
public class ControllerIndex {
26-
27-
private Project project;
28-
private PhpIndex phpIndex;
25+
@NotNull
26+
final private Project project;
2927

3028
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;
3129

32-
public ControllerIndex(Project project) {
30+
public ControllerIndex(@NotNull Project project) {
3331
this.project = project;
34-
this.phpIndex = PhpIndex.getInstance(project);
3532
}
3633

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

40-
for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(phpIndex).getBundles()) {
37+
for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(project).getBundles()) {
4138
actions.addAll(this.getActionMethods(symfonyBundle));
4239
}
4340

src/fr/adrienbrault/idea/symfony2plugin/util/resource/FileResourceUtil.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.intellij.psi.search.GlobalSearchScope;
1414
import com.intellij.util.indexing.FileBasedIndex;
1515
import com.jetbrains.php.PhpIcons;
16-
import com.jetbrains.php.PhpIndex;
1716
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.FileResourcesIndex;
1817
import fr.adrienbrault.idea.symfony2plugin.util.FileResourceVisitorUtil;
1918
import fr.adrienbrault.idea.symfony2plugin.util.PhpIndexUtil;
@@ -200,18 +199,18 @@ public static Collection<PsiFile> getFileResourceTargetsInBundleScope(@NotNull P
200199

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

203-
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(bundleName);
204-
if(symfonyBundle == null) {
205-
return Collections.emptyList();
206-
}
202+
Collection<PsiFile> targets = new HashSet<>();
207203

208-
String path = content.substring(content.indexOf("/") + 1);
209-
PsiFile psiFile = PsiElementUtils.virtualFileToPsiFile(project, symfonyBundle.getRelative(path));
210-
if(psiFile == null) {
211-
return Collections.emptyList();
204+
for (SymfonyBundle bundle : new SymfonyBundleUtil(project).getBundle(bundleName)) {
205+
String path = content.substring(content.indexOf("/") + 1);
206+
PsiFile psiFile = PsiElementUtils.virtualFileToPsiFile(project, bundle.getRelative(path));
207+
208+
if(psiFile != null) {
209+
targets.add(psiFile);
210+
}
212211
}
213212

214-
return Collections.singletonList(psiFile);
213+
return targets;
215214
}
216215

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

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

234-
SymfonyBundle symfonyBundle = new SymfonyBundleUtil(PhpIndex.getInstance(project)).getBundle(bundleName);
235-
if(symfonyBundle == null) {
233+
if(new SymfonyBundleUtil(project).getBundle(bundleName).size() == 0) {
236234
return Collections.emptyList();
237235
}
238236

0 commit comments

Comments
 (0)