1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .reference .provider ;
6
7
7
8
import com .intellij .openapi .util .TextRange ;
8
- import com .intellij .openapi .vfs .*;
9
- import com .intellij .psi .*;
9
+ import com .intellij .openapi .vfs .VirtualFile ;
10
+ import com .intellij .openapi .vfs .VirtualFileManager ;
11
+ import com .intellij .psi .PsiElement ;
12
+ import com .intellij .psi .PsiManager ;
13
+ import com .intellij .psi .PsiReference ;
14
+ import com .intellij .psi .PsiReferenceProvider ;
10
15
import com .intellij .psi .search .FilenameIndex ;
11
16
import com .intellij .psi .search .GlobalSearchScope ;
12
17
import com .intellij .util .ProcessingContext ;
16
21
import com .magento .idea .magento2plugin .reference .provider .util .GetModuleSourceFilesUtil ;
17
22
import com .magento .idea .magento2plugin .reference .xml .PolyVariantReferenceBase ;
18
23
import gnu .trove .THashMap ;
24
+ import java .util .ArrayList ;
25
+ import java .util .Collection ;
26
+ import java .util .List ;
27
+ import java .util .Map ;
19
28
import org .jetbrains .annotations .NotNull ;
20
- import java .util .*;
21
29
22
30
public class FilePathReferenceProvider extends PsiReferenceProvider {
23
31
32
+ @ SuppressWarnings ({
33
+ "PMD.CognitiveComplexity" ,
34
+ "PMD.CyclomaticComplexity" ,
35
+ "PMD.NPathComplexity" ,
36
+ "PMD.AvoidInstantiatingObjectsInLoops"
37
+ })
24
38
@ NotNull
25
39
@ Override
26
- public PsiReference [] getReferencesByElement (@ NotNull PsiElement element , @ NotNull ProcessingContext context ) {
27
-
28
- List < PsiReference > psiReferences = new ArrayList <>();
29
-
30
- String origValue = element .getText ();
40
+ public PsiReference [] getReferencesByElement (
41
+ @ NotNull final PsiElement element ,
42
+ @ NotNull final ProcessingContext context
43
+ ) {
44
+ final String origValue = element .getText ();
31
45
32
- String filePath = GetFilePathUtil .getInstance ().execute (origValue );
46
+ final String filePath = GetFilePathUtil .getInstance ().execute (origValue );
33
47
if (null == filePath ) {
34
48
return PsiReference .EMPTY_ARRAY ;
35
49
}
36
50
37
51
// Find all files based on provided path
38
- Collection <VirtualFile > files = getFiles (element );
39
- if (!(files .size () > 0 )) {
52
+ final Collection <VirtualFile > files = getFiles (element );
53
+
54
+ if (files .isEmpty ()) {
40
55
return PsiReference .EMPTY_ARRAY ;
41
56
}
57
+ final PsiManager psiManager = PsiManager .getInstance (element .getProject ());
42
58
43
- PsiManager psiManager = PsiManager . getInstance ( element . getProject () );
59
+ final List < PsiReference > psiReferences = new ArrayList <>( );
44
60
45
61
String currentPath = "" ;
46
- String [] pathParts = filePath .split ("/" );
62
+ final String [] pathParts = filePath .split ("/" );
47
63
for (int i = 0 ; i < pathParts .length ; i ++) {
48
- String pathPart = pathParts [i ];
64
+ final String pathPart = pathParts [i ];
49
65
Boolean currentPathIsBuilt = false ;
50
66
51
- Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
67
+ final Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
52
68
53
- for (VirtualFile file : files ) {
54
- String fileUrl = file .getUrl ();
69
+ for (final VirtualFile file : files ) {
70
+ final String fileUrl = file .getUrl ();
55
71
if (!fileUrl .contains (filePath )) {
56
72
continue ;
57
73
}
58
- String rootPathUrl = fileUrl .substring (0 , fileUrl .indexOf (filePath ));
59
- String [] relativePathParts = fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
74
+ final String rootPathUrl = fileUrl .substring (0 , fileUrl .indexOf (filePath ));
75
+ final String [] relativePathParts
76
+ = fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
60
77
61
78
if (!currentPathIsBuilt ) {
62
79
currentPath = currentPath .isEmpty ()
@@ -65,69 +82,73 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
65
82
currentPathIsBuilt = true ;
66
83
}
67
84
68
- VirtualFile currentVf = VirtualFileManager .getInstance ()
85
+ final VirtualFile currentVf = VirtualFileManager .getInstance ()
69
86
.findFileByUrl (rootPathUrl .concat (currentPath ));
70
87
71
88
if (null != currentVf ) {
72
- PsiElement psiElement = currentVf .isDirectory ()
89
+ final PsiElement psiElement = currentVf .isDirectory ()
73
90
? psiManager .findDirectory (currentVf )
74
91
: psiManager .findFile (currentVf );
75
92
if (null != psiElement ) {
76
-
77
- TextRange pathRange = new TextRange (
78
- origValue .indexOf (filePath )
79
- + (currentPath .lastIndexOf ("/" ) == -1 ? 0 : currentPath .lastIndexOf ("/" ) + 1 ),
80
- origValue .indexOf (filePath )
81
- + (currentPath .lastIndexOf ("/" ) == -1 ? 0 : currentPath .lastIndexOf ("/" ) + 1 )
82
- + pathPart .length ()
93
+ final int currentPathIndex = currentPath .lastIndexOf ('/' ) == -1
94
+ ? 0 : currentPath .lastIndexOf ('/' ) + 1 ;
95
+
96
+ final TextRange pathRange = new TextRange (
97
+ origValue .indexOf (filePath )
98
+ + currentPathIndex ,
99
+ origValue .indexOf (filePath )
100
+ + currentPathIndex
101
+ + pathPart .length ()
83
102
);
84
103
85
- if (!psiPathElements .containsKey (pathRange )) {
86
- List <PsiElement > list = new ArrayList <>();
104
+ if (psiPathElements .containsKey (pathRange )) {
105
+ psiPathElements .get (pathRange ).add (psiElement );
106
+ } else {
107
+ final List <PsiElement > list = new ArrayList <>();
87
108
list .add (psiElement );
88
109
psiPathElements .put (pathRange , list );
89
- } else {
90
- psiPathElements .get (pathRange ).add (psiElement );
91
110
}
92
111
}
93
112
}
94
113
}
95
114
96
- if (psiPathElements .size () > 0 ) {
97
- psiPathElements .forEach (((textRange , psiElements ) ->
98
- psiReferences .add (new PolyVariantReferenceBase (element , textRange , psiElements ))
99
- ));
115
+ if (!psiPathElements .isEmpty ()) {
116
+ psiPathElements .forEach ((textRange , psiElements ) ->
117
+ psiReferences .add (
118
+ new PolyVariantReferenceBase (element , textRange , psiElements )
119
+ )
120
+ );
100
121
}
101
122
}
102
123
103
- return psiReferences .toArray (new PsiReference [psiReferences . size () ]);
124
+ return psiReferences .toArray (new PsiReference [0 ]);
104
125
}
105
126
106
- private Collection < VirtualFile > getFiles ( @ NotNull PsiElement element )
107
- {
127
+ @ SuppressWarnings ( "PMD.CognitiveComplexity" )
128
+ private Collection < VirtualFile > getFiles ( final @ NotNull PsiElement element ) {
108
129
Collection <VirtualFile > files = new ArrayList <>();
109
130
110
- String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
131
+ final String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
111
132
if (null == filePath ) {
112
133
return files ;
113
134
}
114
135
115
- String fileName = filePath .substring (filePath .lastIndexOf ("/" ) + 1 );
136
+ final String fileName = filePath .substring (filePath .lastIndexOf ('/' ) + 1 );
116
137
117
138
if (fileName .matches (".*\\ .\\ w+$" )) {
118
139
// extension presents
119
140
files = FilenameIndex .getVirtualFilesByName (
120
- element .getProject (),
121
141
fileName ,
122
142
GlobalSearchScope .allScope (element .getProject ())
123
143
);
124
144
files .removeIf (f -> !f .getPath ().endsWith (filePath ));
125
145
126
146
// filter by module
127
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ().execute (element .getText (), element .getProject ());
147
+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
148
+ .execute (element .getText (), element .getProject ());
128
149
if (null != vfs ) {
129
150
files .removeIf (f -> {
130
- for (VirtualFile vf : vfs ) {
151
+ for (final VirtualFile vf : vfs ) {
131
152
if (f .getPath ().startsWith (vf .getPath ().concat ("/" ))) {
132
153
return false ;
133
154
}
@@ -137,15 +158,16 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
137
158
}
138
159
} else if (isModuleNamePresent (element )) {
139
160
// extension absent
140
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ().execute (element .getText (), element .getProject ());
161
+ final Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
162
+ .execute (element .getText (), element .getProject ());
141
163
if (null != vfs ) {
142
- for (VirtualFile vf : vfs ) {
143
- Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil .
144
- getInstance ().execute (vf );
164
+ for (final VirtualFile vf : vfs ) {
165
+ final Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil
166
+ . getInstance ().execute (vf );
145
167
if (null != vfChildren ) {
146
168
vfChildren .removeIf (f -> {
147
- if (!f .isDirectory ()) {
148
- String ext = f .getExtension ();
169
+ if (!f .isDirectory ()) { //NOPMD
170
+ final String ext = f .getExtension ();
149
171
if (null != ext ) {
150
172
return !f .getPath ().endsWith (filePath .concat ("." ).concat (ext ));
151
173
}
@@ -161,8 +183,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
161
183
return files ;
162
184
}
163
185
164
- private boolean isModuleNamePresent (@ NotNull PsiElement element )
165
- {
186
+ private boolean isModuleNamePresent (final @ NotNull PsiElement element ) {
166
187
return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
167
188
}
168
189
}
0 commit comments