How do I check if a Virtualfile or PsiFile is a scratch file

I have an RunConfigurationProducer wired which sets up a run configuration based on file type selected.

I am unable to find the module for the scratch file. It always returns null



protected boolean setupConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {

   Module
module = context.getModule();
// null module for all scratch files
}


This I assume is because a scratch file does not belong to any single module. Hence, I need to skip the configuration setup besed on whether the file selected is a scratch file (assuming a single file has been selected).

protected boolean setupConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {

    PsiFile file = context.getPsiLocation().getContainingFile();    
    VirtualFile virtualFile = file.getVirtualFile()
    
   // If the file selected is a scratch file need to return false
}


How do I find out if the selected file is a scratch file ?

0
3 comments

Why do you care about scratch files specifically? There can be many other similar plugins. Why don't you simply return false for every file where the module is null?

0
Avatar
Permanently deleted user

I was going to do that itself, however, I was wondering if there was any other way to handle it because I saw the issue on scratch files. Didn't realise other plugins could cause the same problems.

Thanks for the input, I will use the 'null check on module' approach.

0

Forgive the late reply, but I came from Google and didn't receive the answer from here as I would have expected. I since found the answer and want to leave this for future Googlers. I used this:

 

val currentDoc = FileEditorManager.getInstance(project).selectedTextEditor?.document!!
val currentFile = FileDocumentManager.getInstance().getFile(currentDoc)
val isScratch = ScratchUtil.isScratch(currentFile)
0

Please sign in to leave a comment.