-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
indentWithTabs removes leading single space #81
Comments
Remainding spaces are discarded here: https://github.com/diffplug/spotless/blob/master/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java#L102 |
As a short term measure, how about a custom lazy Spotless task that applies |
Of course, this feature addition would be nice to have in the medium term. :) |
A short term fix is ...
indentWithTabs(2)
replaceRegex 'Javadoc space indent', '^\\*', ' *' // this regex might not be exactly right, but it's close I'm hesitant to change IndentStep. It is a possibly-breaking change in one of the oldest rules we've got. The current implementation can catch bugs of the following sort:
The suggested change would break this case. Given how easy the regex fix is, I'm inclined to not alter the behavior of IndentStep. |
The regex indentWithTabs(2)
replaceRegex 'Javadoc space indent', '\R\*', '\n *' |
Oh no never mind, looks like JUnit 5's build.gradle actually already has a replaceRegex 'class-level Javadoc indentation fix', /^\*/, ' *' |
I've realised that the regex I mentioned above doesn't actually work for method javadocs (or other javadocs that are intended as a whole). The following steps seem to solve all problems (on one of my personal projects at least): googleJavaFormat '1.3'
indentWithTabs 2
replaceRegex 'class-level javadoc indentation fix', /^\*/, ' *'
replaceRegex 'method-level javadoc indentation fix', /\t\*/, '\t *' |
will there be an official fix anytime? |
@nedtwigg This really feels like something that should work out of the box. Virtually everyone who wants to use |
Proposal 1 (bad idea, probably wouldn't merge a PR)We have a mechanism for implicitly modifying license steps based on context, e.g. this is how we keep the license step from messing up spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Lines 219 to 225 in 3a845e9
You could add something like: if (steps.contains(::isGoogleJavaFormatStep) && steps.contains(::isIdentWithTabsStep)) {
steps.add(replaceRegex 'class-level javadoc indentation fix', /^\*/, ' *')
steps.add(replaceRegex 'method-level javadoc indentation fix', /\t\*/, '\t *')
} I think that's okay, but not great because it's hard for users to opt out of. Proposal 2 (okay idea, would happily merge a PR)A better solution would be to support something like this: spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Lines 154 to 156 in 3a845e9
and it would look something like this public void indentWithTabs() {
// needs to be terminal (returns void) because unlike the other methods in GoogleJavaFormatConfig, this is hard to undo. */
indentWithTabs(2)
replaceRegex 'class-level javadoc indentation fix', /^\*/, ' *'
replaceRegex 'method-level javadoc indentation fix', /\t\*/, '\t *'
}
|
Thanks for following up on this. From the snippets you shared it appears to me that you're somehow conflating this issue with Here's a minimal reproducer that only contains a plugins {
id("com.diffplug.spotless").version("6.1.0")
}
/**
* A simple greeting task.
*/
abstract class GreetingTask extends DefaultTask {
@TaskAction
def greet() {
println("hello from GreetingTask")
}
}
tasks.register("hello", GreetingTask)
spotless {
groovyGradle {
indentWithTabs()
}
} $ ./gradlew spotlessCheck
> Task :spotlessGroovyGradleCheck FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spotlessGroovyGradleCheck'.
> The following files had format violations:
build.gradle
@@ -3,8 +3,8 @@
}
/**
-·*·A·simple·greeting·task.
-·*/
+*·A·simple·greeting·task.
+*/
abstract·class·GreetingTask·extends·DefaultTask·{
\t@TaskAction
\tdef·greet()·{
Run './gradlew :spotlessApply' to fix these violations.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 888ms
3 actionable tasks: 3 executed Also, could you reopen the issue and label it as |
I see, thanks. Hopefully that's the case - admittedly, I haven't been too much into the Spotless internals. |
@vpavic I hope so too. I don't imagine it would be that hard to make a new I'm also assuming that implementing an public void indentWithTabs() {
int numberOfSpacesUsedForIndentation = ...
indentWithTabs(numberOfSpacesUsedForIndentation)
replaceRegex 'class-level javadoc indentation fix', /^\*/, ' *'
replaceRegex 'method-level javadoc indentation fix', /\t\*/, '\t *'
} |
Most of the other formatters (or at least eclipse and greclipse) allow you to configure indentation to use tabs in the first place, so I did see this as a GJF-specific usecase. But you raise a good point, C-style multiline comments are not unusual, we should "just work" for that case. I'm going to open another issue for that. |
I don't plan to implement #1070, but happy to merge a PR that does. |
Having standard Javadoc comments, like:
After running...
All Javadoc stars are in the fly-on-the-left-wall position:
See https://github.com/junit-team/junit5/compare/google_java_format#diff-65f0a88c2a9307f19525f2d3c03dc8f3 for a complete example.
The text was updated successfully, but these errors were encountered: