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
24
32
@ NotNull
25
33
@ Override
26
- public PsiReference [] getReferencesByElement (@ NotNull PsiElement element , @ NotNull ProcessingContext context ) {
34
+ public PsiReference [] getReferencesByElement (
35
+ @ NotNull PsiElement element ,
36
+ @ NotNull ProcessingContext context
37
+ ) {
27
38
28
39
List <PsiReference > psiReferences = new ArrayList <>();
29
40
@@ -56,7 +67,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
56
67
continue ;
57
68
}
58
69
String rootPathUrl = fileUrl .substring (0 , fileUrl .indexOf (filePath ));
59
- String [] relativePathParts = fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
70
+ String [] relativePathParts
71
+ = fileUrl .substring (fileUrl .indexOf (filePath )).split ("/" );
60
72
61
73
if (!currentPathIsBuilt ) {
62
74
currentPath = currentPath .isEmpty ()
@@ -73,13 +85,15 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
73
85
? psiManager .findDirectory (currentVf )
74
86
: psiManager .findFile (currentVf );
75
87
if (null != psiElement ) {
88
+ final int currentPathIndex = currentPath .lastIndexOf ("/" ) == -1
89
+ ? 0 : currentPath .lastIndexOf ("/" ) + 1 ;
76
90
77
91
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 ()
92
+ origValue .indexOf (filePath )
93
+ + currentPathIndex ,
94
+ origValue .indexOf (filePath )
95
+ + currentPathIndex
96
+ + pathPart .length ()
83
97
);
84
98
85
99
if (!psiPathElements .containsKey (pathRange )) {
@@ -94,17 +108,18 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
94
108
}
95
109
96
110
if (psiPathElements .size () > 0 ) {
97
- psiPathElements .forEach (((textRange , psiElements ) ->
98
- psiReferences .add (new PolyVariantReferenceBase (element , textRange , psiElements ))
99
- ));
111
+ psiPathElements .forEach ((textRange , psiElements ) ->
112
+ psiReferences .add (
113
+ new PolyVariantReferenceBase (element , textRange , psiElements )
114
+ )
115
+ );
100
116
}
101
117
}
102
118
103
119
return psiReferences .toArray (new PsiReference [psiReferences .size ()]);
104
120
}
105
121
106
- private Collection <VirtualFile > getFiles (@ NotNull PsiElement element )
107
- {
122
+ private Collection <VirtualFile > getFiles (@ NotNull PsiElement element ) {
108
123
Collection <VirtualFile > files = new ArrayList <>();
109
124
110
125
String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
@@ -123,7 +138,8 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
123
138
files .removeIf (f -> !f .getPath ().endsWith (filePath ));
124
139
125
140
// filter by module
126
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ().execute (element .getText (), element .getProject ());
141
+ Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
142
+ .execute (element .getText (), element .getProject ());
127
143
if (null != vfs ) {
128
144
files .removeIf (f -> {
129
145
for (VirtualFile vf : vfs ) {
@@ -136,11 +152,12 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
136
152
}
137
153
} else if (isModuleNamePresent (element )) {
138
154
// extension absent
139
- Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ().execute (element .getText (), element .getProject ());
155
+ Collection <VirtualFile > vfs = GetModuleSourceFilesUtil .getInstance ()
156
+ .execute (element .getText (), element .getProject ());
140
157
if (null != vfs ) {
141
158
for (VirtualFile vf : vfs ) {
142
- Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil .
143
- getInstance ().execute (vf );
159
+ Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil
160
+ . getInstance ().execute (vf );
144
161
if (null != vfChildren ) {
145
162
vfChildren .removeIf (f -> {
146
163
if (!f .isDirectory ()) {
@@ -160,8 +177,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
160
177
return files ;
161
178
}
162
179
163
- private boolean isModuleNamePresent (@ NotNull PsiElement element )
164
- {
180
+ private boolean isModuleNamePresent (@ NotNull PsiElement element ) {
165
181
return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
166
182
}
167
183
}
0 commit comments