raydac / java-comment-preprocessor

preprocessor for computer languages with C-like comment format (C/C++/Java/Go)
Apache License 2.0
171 stars 27 forks source link

IntelliJ IDEA interoperability #42

Open Jolanrensen opened 2 years ago

Jolanrensen commented 2 years ago

What a cool concept this is! Not only does it allow me to change source code for Java files, but also for Kotlin and Scala files (which is what I use it for at the moment).

However, I do seem to have some issues to make IntelliJ understand what's happening in my multi-module maven project.

The current problem I'm having: when running mvn compile, all works well. The tests work, I can hit run in IntelliJ on a file (which is not delegated to maven because that fails in multi module projects because of the other module's package being "not available on maven central") and I get the properly modified sources. However, when I change the source code in a module that contains bits that need preprocessing and I hit run on a file in another module that uses it, the preprocessing won't take place and it won't work anymore (since IntelliJ then skips maven).

Is there any way to make IntelliJ somehow aware of this preprocessor, like it is for annotation processors, so that it always gets preprocessed before executing? Or would I for instance have to switch to Gradle for better multi-module support?

If a plugin would be needed for this, something similar already exists for a JCP-like preprocessor: https://github.com/FalseHonesty/intellij-preprocessor which also does nice highlighting and such. It's just a bit out of date, but autocomplete and highlighting might actually be a nice feature for JCP too :).

Jolanrensen commented 2 years ago

As an update: It works better in Gradle! IntelliJ correctly uses JCP in multi module projects. The only downside is that a run/compile is needed to view code changes from other modules. That doesn't happen live.

Jolanrensen commented 2 years ago

One other thing I noticed is that IntelliJ incorrectly marks the source directories, making the project harder to work with. If I configure the project like here, I need to unmark build/java-comment-preprocessor/preprocess and mark /src/main/kotlin as sources root (similar with test folders).

I did try to use the idea plugin and:

idea.module {
    sourceDirs = mutableSetOf(File("./src/main/kotlin"))
}

but to no avail...

Any tips in this regard? Can I somehow revert my code sourceSets when the building/deploying is finished?

Edit: I updated the same example.

Jolanrensen commented 2 years ago

With the updated example, the folders are marked correctly, but IntelliJ still skips the preprocessing defined in gradle, instead opting for an incremental build :(

Jolanrensen commented 2 years ago

@raydac My current state: Inside the preprocess task: outputs.upToDateWhen { target.get().exists() } to make sure it rebuilds after a clean. But the only way I can get the compilation to work reliably is by

tasks.compileKotlin {
    dependsOn(preprocessMain)
    outputs.upToDateWhen { false }
}

It seems that IntelliJ skips the compilation preprocessing step instead doing an incremental update, but it does check the outputs.upToDateWhen predicates. So, question: How can I get the outcoming files to properly implement upToDateWhen in either the preprocess task configuration or compileKotlin, to return false when there were any file changes?

Jolanrensen commented 2 years ago

outputs.upToDateWhen { preprocessMain.outcomingFiles.files.isEmpty() } is now my go to.

Incremental build support for Gradle might actually be nice to speed up JCP builds!