forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIComponentIndex.java
136 lines (119 loc) · 4.02 KB
/
UIComponentIndex.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
134
135
136
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.indexes;
import com.intellij.ide.highlighter.XmlFileType;
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.FilenameIndex;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.indexing.FileBasedIndex;
import com.intellij.util.indexing.ID;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("PMD")
public final class UIComponentIndex {
private UIComponentIndex() {
throw new AssertionError("Instantiating utility class...");
}
/**
* Available ui component file.
*
* @param virtualFile VirtualFile
* @return boolean
*/
public static boolean isUiComponentFile(final VirtualFile virtualFile) {
final VirtualFile parent = virtualFile.getParent();
return virtualFile.getFileType() == XmlFileType.INSTANCE && parent.isDirectory()
&& parent.getName().endsWith("ui_component");
}
/**
* Get ui component files.
*
* @param project Project
* @param fileName String
* @return List
*/
public static List<XmlFile> getUiComponentFiles(
final Project project,
final @Nullable String fileName
) {
final List<XmlFile> results = new ArrayList<XmlFile>();//NOPMD
final Collection<VirtualFile> xmlFiles = FilenameIndex.getAllFilesByExt(project, "xml");
final PsiManager psiManager = PsiManager.getInstance(project);
for (final VirtualFile xmlFile: xmlFiles) {
if (isUiComponentFile(xmlFile)) {
if (fileName != null && !xmlFile.getNameWithoutExtension().equals(fileName)) {
continue;
}
final PsiFile file = psiManager.findFile(xmlFile);
if (file != null) {
results.add((XmlFile)file);
}
}
}
return results;
}
/**
* Get ui component files.
*
* @param project Project
* @return List
*/
public static List<XmlFile> getUiComponentFiles(final Project project) {
return getUiComponentFiles(project, null);
}
/**
* Get All Keys.
*
* @param identifier ID
* @param project Project
* @return Collection
*/
public static Collection<String> getAllKeys(
final ID<String, Void> identifier,
final Project project
) {
return FileBasedIndex.getInstance().getAllKeys(identifier, project);
}
/**
* Get ui component files.
*
* @param project Project
* @param fileName String
* @return List
*/
public static List<XmlFile> getUIComponentFiles(Project project, @Nullable String fileName) {
List<XmlFile> results = new ArrayList<XmlFile>();
Collection<VirtualFile> xmlFiles = FilenameIndex.getAllFilesByExt(project, "xml");
PsiManager psiManager = PsiManager.getInstance(project);
for (VirtualFile xmlFile: xmlFiles) {
if (isUIComponentFile(xmlFile)) {
if (fileName != null && !xmlFile.getNameWithoutExtension().equals(fileName)) {
continue;
}
PsiFile file = psiManager.findFile(xmlFile);
if (file != null) {
results.add((XmlFile)file);
}
}
}
return results;
}
/**
* Available ui component file.
*
* @param virtualFile VirtualFile
* @return boolean
*/
public static boolean isUIComponentFile(VirtualFile virtualFile) {
VirtualFile parent = virtualFile.getParent();
return virtualFile.getFileType() == XmlFileType.INSTANCE && parent.isDirectory()
&& parent.getName().endsWith("ui_component");
}
}