Skip to content

Commit

Permalink
Explain the rationale for formatting rule choices in comments (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Dec 29, 2024
1 parent d078c8c commit 43fff19
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,26 @@ final class ImmaculateConfiguration {

static void configure(FormattingWorkflow workflow, File configPath) {
workflow.java()
// Ending files by a new line is good practice, and avoids "No newline at end of file" comments by Git.
workflow.trailingNewline()
// Tabs can render differently, and can accidentally be inserted. Enforce spaces only.
workflow.noTabs()
// Reorder imports according to simple lexicographic ordering, and remove unused imports.
workflow.googleFixImports()
// Allow disabling the formatter for specific sections. Should be used sparingly.
workflow.toggleOff.set('spotless:off')
workflow.toggleOn.set('spotless:on')
// Most formatting rules are handled by the eclipse formatter config.
// They are generally chosen to match standard Java style, and eliminate discussions about style.
workflow.eclipse {
it.version '3.37.0'
it.config.set(configPath)
}

// Wildcard imports:
// - cannot be automatically removed if unused
// - make it harder to see which classes are being used
// - are often inserted automatically by IDEs, which leads to unnecessary diffs in imports
// courtesy of diffplug/spotless#240
// https://github.com/diffplug/spotless/issues/240#issuecomment-385206606
workflow.custom 'noWildcardImports', { String fileContents ->
Expand All @@ -57,12 +67,18 @@ final class ImmaculateConfiguration {
}
}

// Mixing non-nullable annotations with non-annotated types leads to confusion
// wrt. nullability of non-annotated types.
// Annotating all types would be too verbose, so we assume non-nullability by default,
// and disallow non-null annotations which are then unnecessary.
workflow.custom 'noNotNull', { String fileContents ->
if (fileContents.contains('@NotNull') || fileContents.contains('@Nonnull')) {
throw new InvalidUserDataException('@NotNull and @Nonnull are disallowed.')
}
}

// JetBrains nullability annotations can be used in more contexts,
// and we also use other JB annotations such as @ApiStatus.
workflow.custom 'jetbrainsNullable', { String fileContents ->
fileContents.replace('javax.annotation.Nullable', 'org.jetbrains.annotations.Nullable')
}
Expand Down

0 comments on commit 43fff19

Please sign in to comment.