Why is psiFile.getVirtualFile().getFileType() sometimes RegExpFileType for the same java file?

Hello,

I develop a LocalInspectionTool and override the checkFile method like this:

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile, @NotNull final InspectionManager manager, final boolean isOnTheFly) { 
       ...
       psiFile.getVirtualFile().getFileType() instanceof RegExpFileType // is sometimes true, sometimes false, because it is JavaFileType
       ...
 }


I verified that this is exactly the same file which is a java file! The exactly the same file is checked by different LocalInspectionTool's, but the FileType may change.

The question is, why is the fileType sometimes wrong? Actually the main issue about this is that

ProjectFileIndex.SERVICE.getInstance(file.getProject()).getSourceRootForFile(virtualFile); 


returns the wrong value (null) for a RegExpFileType and a valid path if it is JavaFileType.
Without correct source root for a file my plugin cannot handle it correctly.

If you know how to get correct source for a root file with RegExFileType it would be helpful in my situation as well.

Intellij Version: Ultimate 12.1.6

Thx !

11 comments

Please post registration tag content of this inspection from your plugin.xml


0

The LocalInspectionTool's are created dynamicly during startup like this:

https://github.com/omayevskiy/sonar-intellij-plugin/blob/master/src/main/java/org/mayevskiy/intellij/sonar/SonarInspectionToolProvider.java

where the InspectionToolProvider is registered this way in plugins.xml:

https://github.com/omayevskiy/sonar-intellij-plugin/blob/master/META-INF/plugin.xml

what I do is just running all those inspections for the one same file.

0

I assume this is some injected RegExp fragment "file" located in Java File. Could you verify what text content that file has?

0
psiFile.getContext().toString()

returns

PsiLiteralExpression:"^(\\d+-)?(\\d+)$"


and

psiFile.getVirtualFile().toString()

returns

VirtualFileWindow in /my/path/MyClass.java // file name is changed


actually this reg expr is really part of the java file.
Why does it happen? How do I ensure that each inspection is run for the file? Not every inspection get's the correct java psiFile and this way it is not handled!
I don't expect in checkFile that I get sub parts of a file!

0

Hi,

VirtualFileWindow means that you are in injected fragment and there is no physical file on disc. You should rather use containing file to get information you need or check for a language (org.intellij.lang.regexp.RegExpLanguage), even register the inspection for the corresponding language in plugin.xml (language="RegExp")

0

first many thx for fast answers! :)

the java file I am analysing looks like:

Class {
   ....
   private String myReg = "^(\\d+-)?(\\d+)$";
   ....
}



What I do is :
run analyse
scope: current file
profile: all generated Inspections
of course this file exists on disc!
Actually the same happens if the scope is more then one file, but then it is just harder to understand what is going on ;)

the question is: why does intellij provides such regex fragments instead of the java file, and why it does not happen for some Inspections and why not for others?
The only difference between the Inspection classes is the description, rule key etc. Also all Inspections are java inspections (but well I did not defined it anywhere in intellij)

0

for the file MyClass.java exists 2 PsiFiles: one java file and one injected file with RegEx language. Both files should be passed to your inspection. Are you sure that only one of them is passed?

0

yes I am sure
actually this explains why I see the violations twice in phpstorm ;)
but not in intelliij :-(

0

The code is the same which run the inspections. You may see difference when IntelliLang plugin is not enabled in one of the cases or injection is disabled.

0

thank you very much, I'll check it on monday!

0

checked it today
unfortunatelly the plugin itself depends on intellij lang plugin, so it cannot run without

<depends>com.intellij.modules.lang</depends>


well the main difference between phpstorm and intellij is at least that phpstorm does not know anything about the JavaFileType ;-)
still cannot resolve this issue
how do I ensure that every physical file (does not matter which language!) is passed to each inspection?

0

Please sign in to leave a comment.