Add inspections/dependencies to file outside src
I'm trying to create a self-contained single file application to serve as a way of extending my plugin's functionality with custom code. The idea being my plugin will look for these files in a directory like .idea (but created specifically for this purpose), compile, and execute the code when appropriate. The issue that I'm seeing with this approach is that a lot of the analysis, syntax checking, and dependencies that are available for source files under the src are not available when I place these files under the aforementioned directory.
I've noticed that scratch files seem to have all the inspections I'd like to use for these custom source files. How would I enable those inspections and how would I add dependencies to these source files?
Please sign in to leave a comment.
if you want to edit the file under "myDirectoryCreatedSpecificly" with all code insight enabled then you should include this directory into source directories for this project, because a lot of smart code insight features depend on project configuration. For example, to be able to resolve to a symbol in a library, IDE must know where to look for this library.
Thanks for your response, but I'm not sure you've understood the goal behind my request. I'm not looking for this source code file to be a part of the main application as I don't want it to share the dependencies that the main application has. This source code file won't be a part of the main application but rather a part of the custom configuration of the plugin running inside the IDE. If it shared all the dependencies as the main application then I'd have to end up redeploying the main application to ensure it could run. Instead, I would like to strictly control the dependencies available to this source code file so I could ensure this file is self-contained and can run on its own.
Scratch files seem to have all the features I'm looking for to achieve this functionality. Perhaps you could point me in the direction of understanding how scratch files manage their dependencies and inspections?
So you need the plugin to be able to load this custom file and allow some code insight on this file?
Well, inspections do work for files outside source directories, If however you need indexes to work on your file too (e.g. to be able to find usages inside your file or Find In Path to find occurrences inside your file) then you need to implement your own
(please see com.intellij.ide.scratch.ScratchesAndConsolesIndexSetContributor). To resolve references inside this file there's com.intellij.ide.scratch.ScratchResolveScopeEnlarger and com.intellij.ide.scratch.ScratchFileServiceImpl.UseScopeExtension for finding refs in all scratch files. Please also see the package com.intellij.ide.scratch for other useful classes.
Essentially, yes. My plugin adds gutter and inlay marks to source code based on predefined criteria embedded in the plugin. My idea is to open this open so developers can define their own criteria via source code. My plugin would look for a particular annotation and use the methods it finds as a means of external criteria for adding these gutter/inlay marks.
I'm not worried about finding usages and references, more about syntax checking and dependencies. For example, even though this is a Java project, it allows me to write incorrect Java:
Since it's not in the src dir there seems to be a lot of analysis that just gets turned off. I would like to turn a few of them back on.
The dependency issue is because I would like to add the Java JDK and my library with the annotations. Controlling the dependencies allows me to keep the source code self-contained.
Thanks for the scratch files suggestions. I'll take a look through these. In the meantime, please let me know if you have any further suggestions.
What language this config file is supposed to be in?
Just regular Java. The picture above shows me creating a Java file outside the src directory and it allows me to input bad Java. Here is a similar file inside the src directory:
Now it correctly shows syntax errors.
Java could be difficult to inject to a file outside src, because there are many places hardcoded to the src/test/out directory structure. (For example, inspection "Wrong package statement" highlights java classes which do not abide to the directory structure above, etc). Please let us know when you managed the feat.