forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleIndex.java
133 lines (119 loc) · 4.4 KB
/
ModuleIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* 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.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.PhpFileType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.project.util.GetProjectBasePath;
import com.magento.idea.magento2plugin.stubs.indexes.ModuleNameIndex;
import com.magento.idea.magento2plugin.util.RegExUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class ModuleIndex {
private final Project project;
/**
* Constructor.
*
* @param project Project
*/
public ModuleIndex(final Project project) {
this.project = project;
}
public List<String> getEditableModuleNames() {
return getModuleNames(Package.vendor, true);
}
public List<String> getEditableThemeNames() {
return getThemeNames("/" + Package.vendor + "/magento/|/tests/|/test/", true);
}
public List<String> getModuleNames() {
return getModuleNames("/tests/|/test/", false);
}
/**
* Returns Module Names.
*
* @param filterPattern String
* @param withinProject boolean
* @return List
*/
public List<String> getModuleNames(final String filterPattern, final boolean withinProject) {
return getNames(filterPattern, withinProject, RegExUtil.Magento.MODULE_NAME);
}
/**
* Returns Theme Names.
*
* @param filterPattern String
* @param withinProject boolean
* @return List
*/
public List<String> getThemeNames(final String filterPattern, final boolean withinProject) {
return getNames(filterPattern, withinProject, RegExUtil.Magento.THEME_NAME);
}
private List<String> getNames(
final String filterPattern,
final boolean withinProject,
final String pattern
) {
final FileBasedIndex index = FileBasedIndex
.getInstance();
final List<String> allModulesList = new ArrayList<>();
final Collection<String> allModules = index.getAllKeys(ModuleNameIndex.KEY, project);
final Pattern compiled = Pattern.compile(filterPattern);
for (final String moduleName : allModules) {
if (!moduleName.matches(pattern)) {
continue;
}
final Collection<VirtualFile> files = index.getContainingFiles(
ModuleNameIndex.KEY, moduleName,
GlobalSearchScope.getScopeRestrictedByFileTypes(
GlobalSearchScope.allScope(project),
PhpFileType.INSTANCE
));
if (files.isEmpty()) {
continue;
}
final VirtualFile virtualFile = files.iterator().next();
if (withinProject && !VfsUtilCore
.isAncestor(GetProjectBasePath.execute(project), virtualFile, false)) {
continue;
}
final Matcher matcher = compiled.matcher(virtualFile.getPath());
if (matcher.find()) {
continue;
}
allModulesList.add(moduleName);
}
Collections.sort(allModulesList);
return allModulesList;
}
/**
* Returns PSI directory of the certain module.
*
* @param moduleName String
* @return PsiDirectory
*/
public PsiDirectory getModuleDirectoryByModuleName(final String moduleName) {
final FileBasedIndex index = FileBasedIndex
.getInstance();
final Collection<VirtualFile> files = index.getContainingFiles(
ModuleNameIndex.KEY,
moduleName,
GlobalSearchScope.getScopeRestrictedByFileTypes(
GlobalSearchScope.allScope(project),
PhpFileType.INSTANCE
));
final VirtualFile virtualFile = files.iterator().next();
return PsiManager.getInstance(project).findDirectory(virtualFile.getParent());
}
}