checkstyle-idea
checkstyle-idea copied to clipboard
customImportOrder does not work
Thank you @mqzry for the feature. There is a bug from the pull #363 which prevents customImportOrder to work properly.
private void parseImportOrderRules(@NotNull String value) {
String[] groups = value.split(value);
customImportOrder = Arrays.stream(groups).map(ImportGroup::valueOf).collect(Collectors.toList());
}
Using value.split("###") might fix the issue. [Spec].(http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.html)
This is already fixed by @yeoji in https://github.com/jshiell/checkstyle-idea/pull/372
Looks like #372 change is already released in 5.16.3. But, the below configuration is not working in Intellij IDEA 2017.3.3
<module name="CustomImportOrder">
<property name="standardPackageRegExp" value="^(java|javax)\."/>
<property name="specialImportsRegExp" value="^org\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="true"/>
<property name="customImportOrderRules" value="STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE###STATIC"/>
</module>
@appraveen Is there an error? What doesn't work?
I just tested using the configuration you have provided and it seems alright. Imported correctly and I had no errors. I am on IntelliJ 2017.2.5 though, not sure if that matters.
I am not getting any error, when I import, it says, "Checkstyle configuration settings were imported to Default copy". I don't see the order reflected in the dialog and reformatting the code does not pick up the order mentioned in checkstyle.xml
Checstyle -IDEA

Code Style Preference

@appraveen Hmm this is what I got after importing and it's correct.

Is your plugin updated to the latest version? Does the import layout table change at all after importing?
@yeoji Same issue for 2017.3.4. Doesn't import counts to use "*" as well. Maybe something had changed in the API since 2017.2?
@appraveen @e-tyryshkin You're right, I just upgraded to 2017.3.4 to test this out and it doesn't import properly! Hmm since the plugin is developed with 2016.1, I'm not too sure how to go about figuring out what has changed/fixing this. I'll try and see if I can find anything out though.
This is a standard problem, I fear - sometime new versions can do odd things. But people tend to get a bit miffed if we follow the release, as a lot of large orgs don't or can't upgrade immediately.
I'd try switching the version referenced in build.gradle to match the current release and see if anything shows up. Even if we can't release with it built against that SDK, perhaps the results will be illuminating.
This should not be a problem. Looking at the Intellij Version 2017.2.5 to 2017.3.4, API seem to be broken between their minor version upgrade. I don't know have any knowledge on their API. Perhaps, you can create an issue with Intellij to check why this is broken on 2017.3.4
If something works in 2017.2.5 i would expect it to work on all 2017.x.y versions where x>=2 (y>=5 when x==2, otherwise y can be '*')
I am using the plugin version: 5.20.0 with Checkstyle 8.11 in IDEA 2018.2:
IntelliJ IDEA 2018.2 EAP (Ultimate Edition)
Build #IU-182.3684.2, built on July 10, 2018
JRE: 1.8.0_152-release-1248-b8 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.5
Here is an a snippet that demonstrates a CustomImportOrder check:
<module name="CustomImportOrder">
<property name="severity" value="error"/>
<property name="customImportOrderRules"
value="SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
<property name="specialImportsRegExp" value="^javax\."/>
<property name="standardPackageRegExp" value="^java\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module>
Full example available here
Importing the configuration from Preferences / Code Style / Java presents:

Then, I changed the rule to the following, taking out severity:
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
<property name="specialImportsRegExp" value="^javax\."/>
<property name="standardPackageRegExp" value="^java\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module>
Second error presents itself complaining about missing enum constant for SAME_PACKAGE(3). So I switched the rule to:
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="SAME_PACKAGE###THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
<property name="specialImportsRegExp" value="^javax\."/>
<property name="standardPackageRegExp" value="^java\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module>
In summary and to confirm:
- Seems like
severityis not supported by the plugin? - Seems like
SAME_PACKAGE(n)is not supported by the plugin?
PS Happy to open up a separate issue, if it helps further.
@mmoayyed thanks for the report. What version of Checkstyle are you using with this config? At first glance that exception seems to be coming from Checkstyle, not the plugin. In general the plugin just concerns itself with munging the file paths - most of the work, including the majority of parsing, is still done by the underlying Checkstyle instance.
Apologies. I should have mentioned that I use Checkstyle 8.11. (Corrected the original post).
It's worth noting that with the original CustomImportOrder rule (with severity and such defined), the plugin is able to perfectly recognize the Checkstyle rules and examine the code, reporting back faulty import orders. That bit works just fine. The issue seems to only present itself when the rules are about to be imported into IDEA as a Code Style profile.
@mmoayyed thanks - and another thank you for your excellent bug report, it's much appreciated!
The good news: I can reproduce it. The file works fine for addition & scanning, but fails when imported as a code style. The bad news: the code style work was a contribution, so it's a bit of a mystery. But I'll see what I can dig up 😄
I've added some fixes - severity is now supported (and ignored), and SAME_PACKAGE(n) should now work (it wasn't checking for the (n) previously). Released as 5.21.0.
Outstanding. Thank you very much for your time. I'll give the new release a try and will report back.
I should report back the latest version of the plugin that is 5.21.0 resolves this issue. Once again, thank you very much for your help. I hope to get better at this gradually and start contributing more :)
I'm seeing a similar issue with 5.22.1:

<module name="CustomImportOrder">
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
<property name="customImportOrderRules" value="STATIC###THIRD_PARTY_PACKAGE"/>
</module>
So the checkstyle.xml is saying static first, but the Import Layout section of the Code Style after importing (with no errors reported) is still showing the default IntelliJ order (statics last and separate java & javax sections).
The Checkstyle plugin is configured with the right version (8.12).
Taking another look - i did find that you need to make sure the Scheme selected in the Code Style -> Java preferences is set to Project (and not anything related to the IDE) - in which case it half gets the Import Layout right (the java & javax sections are the issue now, they shouldn't be present):

This is still an issue in 5.54.0. Here's a snippet of the imports section I was using
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
<property name="specialImportsRegExp" value="^javax\."/>
<property name="standardPackageRegExp" value="^java\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module>
I found a workaround:
- Import the checkstyle settings into Code Style | Java | Scheme IDE | Gear | Import Scheme
- Manually set the
importstab | Import Layout until the CheckStyle passes - Gear | Export | Editorconfig file
This exports the resulting settings as.editorconfig(to help share with your team)
This is still an issue in 2021 (5.55.1).
This is still an issue in 5.54.0. Here's a snippet of the imports section I was using
<module name="CustomImportOrder"> <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/> <property name="specialImportsRegExp" value="^javax\."/> <property name="standardPackageRegExp" value="^java\."/> <property name="sortImportsInGroupAlphabetically" value="true"/> <property name="separateLineBetweenGroups" value="false"/> </module>I found a workaround:
- Import the checkstyle settings into Code Style | Java | Scheme IDE | Gear | Import Scheme
- Manually set the
importstab | Import Layout until the CheckStyle passes- Gear | Export | Editorconfig file This exports the resulting settings as
.editorconfig(to help share with your team)
The huge downside is that once you edit the checkstyle again you have to edit the order again.