Indicate users what specific rule gets triggered when running check or apply
- When we add
replaceRegexwithSpotless, it takes effect silently when runningmvn spotless:apply. If we usemvn spotless:check, it will point out the specific line which violates the rule but will not tell which specific rule the code violates. If we could have thenameofreplaceRegexprinted as well, it would help. - For example, if we use the following configuration, and there are some
wildcard importsin the code, it would be better if users could see some message liketriggering Remove wildcard imports rulewhen runningmvn spotless:applyormvn spotless:check.
<replaceRegex>
<name>Remove wildcard imports</name>
<searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex>
Thanks!
Firstly, it's important to note that Spotless is not a list of rules, it is a list of functions, String -> String, and that is all. There are lots of advantages to this model, like our idempotence guarantee. But one of the disadvantages is that it's hard to attribute a specific failure to a specific rule. But it is possible! Here is how we could do it.
- First, identify which lines are different between the input content and the canonical formatted content.
- Now, apply each step on the input content, one at a time.
- For each step, diff the before/after, and identify which lines were changed
- This is more complicated than it seems at first, because of the idempotence guarantee mentioned earlier, it might be the case that steps are applied more than once.
- Now, for each line in the canonical content, you have list of which steps altered that line, which you can use to annotate the changes.
That's a big project, but it would be possible. My guess is that it's not worth the effort, but who knows what PR's might show up someday :)
A workaround specifically for the wildcard import removal mentioned above could be to append a comment indicating that wildcard imports are disallowed and making the import invalid (see https://github.com/google/gson/pull/2550):
-import·java.util.*;
+import·java.util.DISALLOWED.*;·//·wildcard·imports·are·not·allowed