Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy intelliJ "angle brackets (<>)" modify codestylesettings

i am currently having a problem with the codestylesettings i.e. the "Reformat Code" function in IntelliJ.

NECESARRY INFORMATION:

I am writing groovy scripts, which use some Java functionality (for example generics)

It appears that my version of groovy (which cannot be changed for various reasons) runs into compile problems when having a variable defined like this:

NON-WORKING:

final List<Map<String, Object>> listOfMaps = a["b"] as List<Map<String, Object>>

Problem is that the compiler can only interpret the last 2 closing angle brackets correctly if there is a space between those.

WORKING:

final List<Map<String, Object>> listOfMaps = a["b"] as List<Map<String, Object> >

This is a known bug in the version of groovy which i am using.

PROBLEM:

The "Reformat Code" functionality always removes the space, which i added between the closing angle brackets in order to make the script compilable.

QUESTION:

How can i teach IntelliJ to not remove the space?

ATTEMPTS:

This setting does exist for java files (Settings-> Editor -> Code Style -> JAVA -> Spaces -> Within -> Angle Brackets).

Exported XML Settings:

<JavaCodeStyleSettings>
<option name="SPACES_WITHIN_ANGLE_BRACKETS" value="true" />

But not for groovy, so i tried adding it for groovy like so:

<codeStyleSettings language="Groovy">
<option name="SPACES_WITHIN_ANGLE_BRACKETS" value="true" />

When reformatting it only freezes for like a minute or so and then removing the spaces again.

Probably cuz groovy is not aware of those angle brackets hence it is Java functionality used here.

like image 647
CodeFanatic Avatar asked Apr 18 '18 14:04

CodeFanatic


1 Answers

I have checked the decompiled source code and it doesn't seem SPACES_WITHIN_ANGLE_BRACKETS is supported for Groovy. Each formatter has a separate formatting rules and configs. None of the existing configs will help you in your quest

Only for Java

No Option in Groovy

As @ybedrov mentioned, you will need to open a enhancement request with them

No existing option will help you

As @daniel pointed out, there does exist one way, which is create your own custom language

http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support_tutorial.html

But that option is exists in the fairytale world, if you are just concerned about a single space. I would just a sed command to fix the issue by running a bash script

$ find . -name "*.groovy" | xargs sed 's|>>|> >|g' -i
like image 199
Tarun Lalwani Avatar answered Oct 12 '22 00:10

Tarun Lalwani