22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .reference .provider ;
67
78import 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 ;
1015import com .intellij .psi .search .FilenameIndex ;
1116import com .intellij .psi .search .GlobalSearchScope ;
1217import com .intellij .util .ProcessingContext ;
1621import com .magento .idea .magento2plugin .reference .provider .util .GetModuleSourceFilesUtil ;
1722import com .magento .idea .magento2plugin .reference .xml .PolyVariantReferenceBase ;
1823import gnu .trove .THashMap ;
24+ import java .util .ArrayList ;
25+ import java .util .Collection ;
26+ import java .util .List ;
27+ import java .util .Map ;
1928import org .jetbrains .annotations .NotNull ;
20- import java .util .*;
2129
2230public class FilePathReferenceProvider extends PsiReferenceProvider {
2331
2432 @ NotNull
2533 @ Override
26- public PsiReference [] getReferencesByElement (@ NotNull PsiElement element , @ NotNull ProcessingContext context ) {
34+ public PsiReference [] getReferencesByElement (
35+ @ NotNull PsiElement element ,
36+ @ NotNull ProcessingContext context
37+ ) {
2738
2839 List <PsiReference > psiReferences = new ArrayList <>();
2940
@@ -56,7 +67,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
5667 continue ;
5768 }
5869 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 ("/" );
6072
6173 if (!currentPathIsBuilt ) {
6274 currentPath = currentPath .isEmpty ()
@@ -73,13 +85,15 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
7385 ? psiManager .findDirectory (currentVf )
7486 : psiManager .findFile (currentVf );
7587 if (null != psiElement ) {
88+ final int currentPathIndex = currentPath .lastIndexOf ("/" ) == -1
89+ ? 0 : currentPath .lastIndexOf ("/" ) + 1 ;
7690
7791 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 ()
8397 );
8498
8599 if (!psiPathElements .containsKey (pathRange )) {
@@ -94,17 +108,18 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
94108 }
95109
96110 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+ );
100116 }
101117 }
102118
103119 return psiReferences .toArray (new PsiReference [psiReferences .size ()]);
104120 }
105121
106- private Collection <VirtualFile > getFiles (@ NotNull PsiElement element )
107- {
122+ private Collection <VirtualFile > getFiles (@ NotNull PsiElement element ) {
108123 Collection <VirtualFile > files = new ArrayList <>();
109124
110125 String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
@@ -123,7 +138,8 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
123138 files .removeIf (f -> !f .getPath ().endsWith (filePath ));
124139
125140 // 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 ());
127143 if (null != vfs ) {
128144 files .removeIf (f -> {
129145 for (VirtualFile vf : vfs ) {
@@ -136,11 +152,12 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
136152 }
137153 } else if (isModuleNamePresent (element )) {
138154 // 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 ());
140157 if (null != vfs ) {
141158 for (VirtualFile vf : vfs ) {
142- Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil .
143- getInstance ().execute (vf );
159+ Collection <VirtualFile > vfChildren = GetAllSubFilesOfVirtualFileUtil
160+ . getInstance ().execute (vf );
144161 if (null != vfChildren ) {
145162 vfChildren .removeIf (f -> {
146163 if (!f .isDirectory ()) {
@@ -160,8 +177,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
160177 return files ;
161178 }
162179
163- private boolean isModuleNamePresent (@ NotNull PsiElement element )
164- {
180+ private boolean isModuleNamePresent (@ NotNull PsiElement element ) {
165181 return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
166182 }
167183}
0 commit comments