22
33import com .intellij .notification .*;
44import com .intellij .openapi .project .Project ;
5+ import com .intellij .openapi .roots .ProjectRootManager ;
56import com .intellij .openapi .vfs .VirtualFile ;
67import com .intellij .psi .PsiElement ;
78import com .intellij .psi .PsiFile ;
@@ -51,14 +52,14 @@ public static PsiElement findRobot(@Nullable String resource, @NotNull Project p
5152 if (resource == null ) {
5253 return null ;
5354 }
54- PsiElement result = getFromCache (resource , project );
55- if (result != null ) {
56- return result ;
57- }
55+ // PsiElement result = getFromCache(resource, project);
56+ // if (result != null) {
57+ // return result;
58+ // }
5859 String [] file = getFilename (resource , "" , project );
5960 debug (resource , "Attempting global robot search" , project );
60- result = findGlobalFile (resource , file [0 ], file [1 ], project , originalElement );
61- addToCache (result , resource );
61+ PsiElement result = findGlobalFile (resource , file [0 ], file [1 ], project , originalElement );
62+ // addToCache(result, resource);
6263 return result ;
6364 }
6465
@@ -68,17 +69,22 @@ public static PsiElement findPython(@Nullable String library, @NotNull Project p
6869 if (library == null ) {
6970 return null ;
7071 }
71- PsiElement result = getFromCache (library , project );
72- if (result != null ) {
73- return result ;
74- }
72+ PsiElement result ;
73+ // PsiElement result = getFromCache(library, project);
74+ // if (result != null) {
75+ // return result;
76+ // }
7577 // robotframework
7678 // Using library name
7779 // The most common way to specify a test library to import is using its name,
7880 // like it has been done in all the examples in this section. In these cases
7981 // Robot Framework tries to find the class or module implementing the library
8082 // from the module search path.
81- if (! library .contains ("/" )) {
83+ if (! library .contains ("/" ) && ! library .endsWith (".py" )) {
84+ result = getFromCache (library , project );
85+ if (result != null ) {
86+ return result ;
87+ }
8288 debug (library , "Attempting class search" , project );
8389 result = PythonResolver .findClass (library , project , false );
8490 if (result != null ) {
@@ -126,29 +132,29 @@ public static PsiElement findPython(@Nullable String library, @NotNull Project p
126132 debug (library , "Attempting project directory search" , project );
127133 result = findProjectDirectory (library , file [0 ], file [1 ], project , originalElement );
128134 if (result != null ) {
129- addToCache (result , library );
135+ // addToCache(result, library);
130136 return result ;
131137 }
132138 // search global scope... this can get messy
133139 debug (library , "Attempting global directory search" , project );
134140 result = findGlobalDirectory (library , file [0 ], file [1 ], project , originalElement );
135141 if (result != null ) {
136- addToCache (result , library );
142+ // addToCache(result, library);
137143 return result ;
138144 }
139145 } else {
140146 // search project scope
141147 debug (library , "Attempting project file search" , project );
142148 result = findProjectFile (library , file [0 ], file [1 ], project , originalElement );
143149 if (result != null ) {
144- addToCache (result , library );
150+ // addToCache(result, library);
145151 return result ;
146152 }
147153 // search global scope... this can get messy
148154 debug (library , "Attempting global file search" , project );
149155 result = findGlobalFile (library , file [0 ], file [1 ], project , originalElement );
150156 if (result != null ) {
151- addToCache (result , library );
157+ // addToCache(result, library);
152158 return result ;
153159 }
154160 }
@@ -186,29 +192,77 @@ private static PsiFile findFile(@NotNull String original, @NotNull String path,
186192 debug (original , "path::" + path , project );
187193 debug (original , (directory ? "directory::" : "file::" ) + fileName , project );
188194
189- if (path . contains ( "./" )) {
195+ if (! isAbsolutePath ( path )) {
190196 // contains a relative path
191197 VirtualFile workingDir = originalElement .getContainingFile ().getVirtualFile ().getParent ();
192- VirtualFile relativePath = workingDir .findFileByRelativePath (path );
193- if (relativePath != null && relativePath .isDirectory () && relativePath .getCanonicalPath () != null ) {
194- debug (original , "changing relative path to: " + relativePath .getCanonicalPath (), project );
195- path = relativePath .getCanonicalPath ();
196- if (!path .endsWith ("/" )) {
197- path += "/" ;
198+ PsiFile psiFile = findFileInRelativePath (workingDir , original , path , fileName , project , search , originalElement , directory );
199+ if (psiFile != null ) {
200+ return psiFile ;
201+ }
202+ VirtualFile [] contentRoots = ProjectRootManager .getInstance (project ).getContentRoots ();
203+ for (VirtualFile contentRoot : contentRoots ) {
204+ psiFile = findFileInRelativePath (contentRoot , original , path , fileName , project , search , originalElement , directory );
205+ if (psiFile != null ) {
206+ return psiFile ;
198207 }
199208 }
209+ debug (original , "can't found" , project );
210+ return null ;
211+ }
212+
213+ String fullName = path + fileName ;
214+ PsiElement result = getFromCache (fullName , project );
215+ if (result instanceof PsiFile ) {
216+ return (PsiFile ) result ;
217+ }
218+ PsiFile psiFile = getMatchedFile (original , fullName , fileName , project , search , originalElement , directory );
219+ if (psiFile != null ) {
220+ addToCache (psiFile , fullName );
221+ return psiFile ;
222+ }
223+
224+ debug (original , "can't found" , project );
225+ return null ;
226+ }
227+
228+ @ Nullable
229+ private static PsiFile findFileInRelativePath (@ NotNull VirtualFile baseDir , @ NotNull String original , @ NotNull String path , @ NotNull String fileName ,
230+ @ NotNull Project project , @ NotNull GlobalSearchScope search ,
231+ @ NotNull PsiElement originalElement , boolean directory ) {
232+ VirtualFile relativePath = baseDir .findFileByRelativePath (path );
233+ if (relativePath != null && relativePath .isDirectory () && relativePath .getCanonicalPath () != null ) {
234+ String newpath = relativePath .getCanonicalPath ();
235+ debug (original , "changing relative path to: " + newpath , project );
236+ String fullName = newpath + "/" + fileName ;
237+ PsiElement result = getFromCache (fullName , project );
238+ if (result instanceof PsiFile ) {
239+ return (PsiFile ) result ;
240+ }
241+ PsiFile psiFile = getMatchedFile (original , fullName , fileName , project , search , originalElement , directory );
242+ if (psiFile != null ) {
243+ addToCache (psiFile , fullName );
244+ return psiFile ;
245+ }
200246 }
201- path = path + fileName ;
247+ return null ;
248+ }
202249
250+ @ Nullable
251+ private static PsiFile getMatchedFile (@ NotNull String original , @ NotNull String fullName , @ NotNull String fileName ,
252+ @ NotNull Project project , @ NotNull GlobalSearchScope search ,
253+ @ NotNull PsiElement originalElement , boolean directory ) {
203254 Collection <VirtualFile > files = FilenameIndex .getVirtualFilesByName (project , fileName , search );
204255 debug (original , "matching: " + collectionToString (files ), project );
256+ if (! isAbsolutePath (fullName )) {
257+ fullName = "/" + fullName ;
258+ }
205259 for (VirtualFile file : files ) {
206260 String tryPath = file .getCanonicalPath ();
207261 if (tryPath == null )
208262 continue ;
209263 debug (original , "trying: " + tryPath , project );
210264 // TODO: if path is abosute path startsWith "/", endsWith comparision may return true if tryPath have prefix
211- if (tryPath .endsWith (path ) &&
265+ if (tryPath .endsWith (fullName ) &&
212266 ((directory && file .isDirectory ()) || (!directory && !file .isDirectory ()))) {
213267 VirtualFile targetFound = file ;
214268 if (directory ) {
@@ -283,8 +337,8 @@ private static String[] getFilename(@NotNull String path, @NotNull String suffix
283337 pathElements [pathElements .length - 1 ] = "" ;
284338 results [0 ] = String .join ("/" , pathElements );
285339 // absolute or relative path
286- if (! results [0 ].matches ("([a-zA-Z]:)?/.*|.*([$%]\\ {.+}).*" ) && ! results [0 ].contains ("./" ))
287- results [0 ] = "./" + results [0 ];
340+ // if (! results[0].matches("([a-zA-Z]:)?/.*|.*([$%]\\{.+}).*") && ! results[0].contains("./"))
341+ // results[0] = "./" + results[0];
288342 if (RobotOptionsProvider .getInstance (project ).stripVariableInLibraryPath ()) {
289343 results [0 ] = results [0 ].replaceAll ("[$%]\\ {|}" , "" );
290344 }
@@ -295,6 +349,10 @@ private static String[] getFilename(@NotNull String path, @NotNull String suffix
295349 return results ;
296350 }
297351
352+ private static boolean isAbsolutePath (String path ) {
353+ return path .matches ("([a-zA-Z]:)?/.*" );
354+ }
355+
298356 private static void debug (@ NotNull String lookup , String data , @ NotNull Project project ) {
299357 if (RobotOptionsProvider .getInstance (project ).isDebug ()) {
300358 String message = String .format ("[RobotFileManager][%s] %s" , lookup , data );
0 commit comments