intellij-plugin-v4
intellij-plugin-v4 copied to clipboard
Use $PROJECT_DIR$ instead of absolute path in misc.xml ANTLR configuration
Summary
The output directory is configured as an absolute path, which leads to problems when the project is moved.
Version
plugin: 1.14 Windows 7 x64 IntelliJ IDEA Community 2020.2
Description
When the user creates a parser file, right-clicks on it and selects 'Configure ANTLR', he is presented with a configuration fill form with, among other settings, 'Output directory where all output is generated'.
The setting is stored in $PROJECT_DIR$/.idea/misc.xml, for ex:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ANTLRGenerationPreferences">
<option name="perGrammarGenerationSettings">
<list>
<PerGrammarGenerationSettings>
<option name="outputDir" value="D:\projects\test\gen\myParser" />
[...]
Note that 'outputDir' is an absolute path, with the OS convention for directory separators. When the project is used on another machine, or in another directory, this has the side-effect that the output directory is mislocated, at best. On a different OS it may be misinterpreted completely.
Instead, it should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ANTLRGenerationPreferences">
<option name="perGrammarGenerationSettings">
<list>
<PerGrammarGenerationSettings>
<option name="outputDir" value="$PROJECT_DIR$/gen/myParser" />
[...]
The solution is to:
- detect whether the output directory is under the project path, and if so, replace the project path with
$PROJECT_DIR$ - use the slash character as directory separator
It's confusing.
If I configure the parser file with the GUI and set the output directory to "gen", and the package to "myParser",
- the first time, it will generate the files as expected in the project directory, under "gen/myParser" and set "package myParser" in the source files
- if I quit and reload the project, it will have lost its configuration
- if I put the configuration again, it will output the files directly under "gen"
Sometimes it adds multiple identical configurations in misc.xml.
It seems very unstable. Let me know if that's a known issue, if not I can try and describe the steps. I'm using a pure IntelliJ project because I can't see how to setup a project with gradle, it's too fickle.
I confirm the problem, it's completely unstable for me, whether I put a relative (gen) or an absolute path (using the button to select the directory), or even if I put $PROJECT_DIR$/gen/myParser.
-
Create Kotlin project, build Intellij (not Gradle)
-
Add lexer and parser files, for example in
src/main/antlr/ -
Create
gen/myParserdirectory -
Add
antlr-runtime-4.8lib to the libraries of the project (manually looking where they are) -
Mark src/main/antlr as
Sources Root, gen asGenerated Sources Root -
Right-click on parser .g4 file,
Configure ANTLR..., set output directory 'gen' (or use the button to select the directory), set package tomyParser -
Right-click on parser and
Generate ANTLR Recognizer=> the generated Java source files are under gen/myParser, they begin withpackage myParser -
Quit IntelliJ, remove the
gen/myParsercontent -
Start IntelliJ, load the project if not done automatically
-
Right-click on parser .g4 file,
Configure ANTLR...=> the configuration is gone -
Right-click on parser and
Generate ANTLR Recognizer=> the generated sources are put undergen, no package -
Right-click on parser .g4 file,
Configure ANTLR..., set output directory 'gen' (or use the button to select the directory), set package tomyParser -
Right-click on parser and
Generate ANTLR Recognizer=> the generated Java source files are under gen/myParser, they begin withpackage myParser
We would believe it's fine again (until we quite IntelliJ), but looking at .idea/misc.xml, it doesn't look good, the configuration is stored twice.
I have another older project in which I set manually the outputDir to $PROJECT_DIR$/gen/myParser, and it seems stable, but I can't reproduce that in my new project anymore. I don't know what difference causes this.

I noticed that if I replaced the backslashes by slashes (see screenshot), the form was saving the outputDir "correctly" (it will still fail next time IntelliJ starts): <option name="outputDir" value="$PROJECT_DIR$/gen" />
I have made a simple test, to try and reproduce the problem, but
- if I uncompress it in
D:\tmp, I can't reproduce the problem, no matter what I try, it remains stable and the configuration is not lost. - if I uncompress it in
D:\projects\ANTLR, the first time I load the project, I can reproduce the problem, the configuration is lost (blank form when I open the configuration in the GUI). Then every time after.
I'm giving up, here is the project in attachment, perhaps someone can see something. In the mean time, I have taken my old project, removed the sources, added the sources and parser files of the new project I must work on, and it seems OK so far.
Note: The first time, one may have to adjust the libraries. Mine are stored in C:\dev\Java\lib, there's antlr-4.8-complete.jar antlr-runtime-4.8.jar junit-jupiter-5.5.1.jar. Running Main.kt should work, because I left the generated parser. I wish I could use Gradle to avoid those dependency problems, but I yet have to find a build.gradle.kts that works with ANTLR (and even more, which sets up the plugin correctly).
Maybe IntelliJ or the plugin is storing something related to the project path in a cache somewhere.
If I rename the project directory in D:\projects\ANTLR (and remove .idea/workspace.xml, as always), for example testANTLR2 instead of testANTLR, then just checking the configuration is fine. If I rename back to testANTLR (and remove .idea/workspace.xml), checking the configuration shows it's empty in the GUI.
For information, I had to uninstall IntelliJ IDEA, wipe out all settings in %USERPROFILE%/AppData/Local/JetBrains and ../Roaming/.., reinstall everything. Now it seems stable again.
There must be a configuration cached somewhere that sometimes interferes and reset the plugin configuration.
Yikes. I am not sure how all the project cache files work.