1- /**
1+ /*
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
32+ @ SuppressWarnings ({
33+ "PMD.CognitiveComplexity" ,
34+ "PMD.CyclomaticComplexity" ,
35+ "PMD.NPathComplexity" ,
36+ "PMD.AvoidInstantiatingObjectsInLoops"
37+ })
2438 @ NotNull
2539 @ 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 ();
3145
32- String filePath = GetFilePathUtil .getInstance ().execute (origValue );
46+ final String filePath = GetFilePathUtil .getInstance ().execute (origValue );
3347 if (null == filePath ) {
3448 return PsiReference .EMPTY_ARRAY ;
3549 }
3650
3751 // 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 ()) {
4055 return PsiReference .EMPTY_ARRAY ;
4156 }
57+ final PsiManager psiManager = PsiManager .getInstance (element .getProject ());
4258
43- PsiManager psiManager = PsiManager . getInstance ( element . getProject () );
59+ final List < PsiReference > psiReferences = new ArrayList <>( );
4460
4561 String currentPath = "" ;
46- String [] pathParts = filePath .split ("/" );
62+ final String [] pathParts = filePath .split ("/" );
4763 for (int i = 0 ; i < pathParts .length ; i ++) {
48- String pathPart = pathParts [i ];
64+ final String pathPart = pathParts [i ];
4965 Boolean currentPathIsBuilt = false ;
5066
51- Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
67+ final Map <TextRange , List <PsiElement >> psiPathElements = new THashMap <>();
5268
53- for (VirtualFile file : files ) {
54- String fileUrl = file .getUrl ();
69+ for (final VirtualFile file : files ) {
70+ final String fileUrl = file .getUrl ();
5571 if (!fileUrl .contains (filePath )) {
5672 continue ;
5773 }
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 ("/" );
6077
6178 if (!currentPathIsBuilt ) {
6279 currentPath = currentPath .isEmpty ()
@@ -65,69 +82,73 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
6582 currentPathIsBuilt = true ;
6683 }
6784
68- VirtualFile currentVf = VirtualFileManager .getInstance ()
85+ final VirtualFile currentVf = VirtualFileManager .getInstance ()
6986 .findFileByUrl (rootPathUrl .concat (currentPath ));
7087
7188 if (null != currentVf ) {
72- PsiElement psiElement = currentVf .isDirectory ()
89+ final PsiElement psiElement = currentVf .isDirectory ()
7390 ? psiManager .findDirectory (currentVf )
7491 : psiManager .findFile (currentVf );
7592 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 ()
83102 );
84103
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 <>();
87108 list .add (psiElement );
88109 psiPathElements .put (pathRange , list );
89- } else {
90- psiPathElements .get (pathRange ).add (psiElement );
91110 }
92111 }
93112 }
94113 }
95114
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+ );
100121 }
101122 }
102123
103- return psiReferences .toArray (new PsiReference [psiReferences . size () ]);
124+ return psiReferences .toArray (new PsiReference [0 ]);
104125 }
105126
106- private Collection < VirtualFile > getFiles ( @ NotNull PsiElement element )
107- {
127+ @ SuppressWarnings ( "PMD.CognitiveComplexity" )
128+ private Collection < VirtualFile > getFiles ( final @ NotNull PsiElement element ) {
108129 Collection <VirtualFile > files = new ArrayList <>();
109130
110- String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
131+ final String filePath = GetFilePathUtil .getInstance ().execute (element .getText ());
111132 if (null == filePath ) {
112133 return files ;
113134 }
114135
115- String fileName = filePath .substring (filePath .lastIndexOf ("/" ) + 1 );
136+ final String fileName = filePath .substring (filePath .lastIndexOf ('/' ) + 1 );
116137
117138 if (fileName .matches (".*\\ .\\ w+$" )) {
118139 // extension presents
119140 files = FilenameIndex .getVirtualFilesByName (
120- element .getProject (),
121141 fileName ,
122142 GlobalSearchScope .allScope (element .getProject ())
123143 );
124144 files .removeIf (f -> !f .getPath ().endsWith (filePath ));
125145
126146 // 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 ());
128149 if (null != vfs ) {
129150 files .removeIf (f -> {
130- for (VirtualFile vf : vfs ) {
151+ for (final VirtualFile vf : vfs ) {
131152 if (f .getPath ().startsWith (vf .getPath ().concat ("/" ))) {
132153 return false ;
133154 }
@@ -137,15 +158,16 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
137158 }
138159 } else if (isModuleNamePresent (element )) {
139160 // 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 ());
141163 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 );
145167 if (null != vfChildren ) {
146168 vfChildren .removeIf (f -> {
147- if (!f .isDirectory ()) {
148- String ext = f .getExtension ();
169+ if (!f .isDirectory ()) { //NOPMD
170+ final String ext = f .getExtension ();
149171 if (null != ext ) {
150172 return !f .getPath ().endsWith (filePath .concat ("." ).concat (ext ));
151173 }
@@ -161,8 +183,7 @@ private Collection<VirtualFile> getFiles(@NotNull PsiElement element)
161183 return files ;
162184 }
163185
164- private boolean isModuleNamePresent (@ NotNull PsiElement element )
165- {
186+ private boolean isModuleNamePresent (final @ NotNull PsiElement element ) {
166187 return GetModuleNameUtil .getInstance ().execute (element .getText ()) != null ;
167188 }
168189}
0 commit comments