How to override default java code folding with a plugin

Answered

Hi there,

I want to create a FoldingDescriptor for large swagger annotations, e.g. ApiOperation. Plugin source code at https://github.com/ideadapt/intellij-swagger-annotations.

My code is based on the idea presented here: https://jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/folding_builder.html, hence my class extends FoldingBuilderEx.

My problem is, that my FoldingDescriptor seems to interfere with the platform java code folding feature. See https://intellij-support.jetbrains.com/hc/en-us/community/posts/360004219940-Code-folding-getPlaceholderText-not-executing- which reports a similar problem.

During development I have found that the CompositeFoldingBuilder only registers one FoldingDescriptor per region, hence I set the order of my extension in plugin.xml to "first". This made my unit tests and "runIde" work. There is already another thread about a similar issue https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000001164-Overriding-FoldingDescriptors-SOLVED-

Unit tests are green, gradle task "runIde" also works. Fine. But when I install the plugin from disk and open a Java project in Intellij, it does not work.

I have read that it might be because com.intellij.codeInsight.folding.impl.JavaFoldingBuilderBase#addAnnotationsToFold interferes with my plugin code. According to the symptoms that is true (my plugin is loaded without errors, but annotation is still fold to @{...}, which is the platform java folding). But what can I do to prevent this interference?

Then I stumbled upon this thread https://intellij-support.jetbrains.com/hc/en-us/community/posts/206787745-How-to-add-custom-FoldingBuilder, stating java code folding can not simply be overwritten the same way code folding of a custom language plugin might be ovewritten. Is this true? If so, how exactly would I use the proposed TextEditorHighlightingPass to accomplish java code folding customization?

 

Thanks for any help, leading me into the right direction :)

0
4 comments
Avatar
Permanently deleted user

I have now added the official tutorial example to my repo. I it does not work either. Although when debugging "runIde" the breakpoint is hit, the code folding region is not shown in the editor. (whereas my other swagger annotation code folding regions did work at least in "runIde" mode ...).

The commit of interest: https://github.com/ideadapt/intellij-swagger-annotations/commit/b81f0f418234f9ef7fdcaa1e714aac1b879fed5e

Any ideas?

0

Sorry, this got lost.

It seems this is a limitation in the current folding API for which no clear workaround exists, unfortunately. It _might_ be possible to circumvent it by adding additional "post" custom folding pass (registered to run after com.intellij.codeInsight.daemon.impl.CodeFoldingPass).

0
Avatar
Permanently deleted user

Hi,

I have tried very similar thing with the custom pass in my attempt to implement a PsrLogger folding plugin, but eventually I have reached the same conclusion - there are limitations in the folding API for which I could not find a reasonable workaround. In particular, I found no way to remove conflicting foldings from previous passes, which was needed because otherwise sometimes the folding from previous (default) folding passes won over the folding added by my plugin. I spent about a week trying to get it working, without success. Any plans to do some refactoring in this area? Other than that my experience with writing the plugin was pretty good, the docs in combination with existing plugins as a studying material were sufficient to get me started. I just got unlucky with this particular roadblock..

PS: You make great products, keep up the good work!

0

Thanks for the update. I suggest to create a RFE in our tracker https://youtrack.jetbrains.com/issues/IDEA to discuss further possible improvements for this area.

0

Please sign in to leave a comment.