Class FormatExtension
- Direct Known Subclasses:
Antlr4Extension,BaseGroovyExtension,BaseKotlinExtension,CppExtension,CssExtension,FlexmarkExtension,FreshMarkExtension,GherkinExtension,GoExtension,JavaExtension,JavascriptExtension,JsonExtension,PomExtension,ProtobufExtension,PythonExtension,ScalaExtension,ShellExtension,SqlExtension,TypescriptExtension,YamlExtension
spotless{Name}Check and spotless{Name}Apply task.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassGeneric Biome formatter step that detects the language of the input file from the file name.classclassclassclassCreated bylicenseHeader(String, String)orlicenseHeaderFile(Object, String).static classclass -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<FormatterStep> The steps that need to be added.protected FileCollectionThe files to be formatted = (target - targetExclude).protected FileCollectionThe files to be formatted = (target - targetExclude).protected StringThe value from which files will be excluded if their content contain it. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStep(FormatterStep newStep) Adds a new step.voidaddStep(Function<Provisioner, FormatterStep> createStepFn) Adds a new step that requires a Provisioner.biome()Defaults to downloading the default Biome version from the network.Downloads the given Biome version from the network.voidbumpThisNumberIfACustomStepChanges(int number) An optional performance optimization if you are using any of thecustommethods.Uses the default version of clang-format.clangFormat(String version) Uses the specified version of clang-format.voidClears all of the existing steps.createIndependentApplyTask(String taskName) Eager version ofcreateIndependentApplyTaskLazy(String)createIndependentApplyTaskLazy(String taskName) Creates an independentSpotlessApplyfor (very) unusual circumstances.voidcustom(String name, FormatterFunc formatter) Adds a custom step.voidAdds a custom step.eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type) eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type, String version) voidSets encoding to use (defaults toSpotlessExtension.getEncoding()).voidEnsures that files end with a single newline.voidforbidRegex(String name, String regex, String lintDetail) A regex which generates a lint.Returns the encoding to use (defaults toSpotlessExtension.getEncoding().protected intgetExistingStepIdx(String stepName) Returns the index of the existing step with the given name, or -1 if no such step exists.Returns the line endings to use (defaults toSpotlessExtension.getLineEndings().protected ProjectReturns the project that this extension is attached to.idea()voidignoreErrorForPath(String relativePath) Deprecated.voidignoreErrorForStep(String stepName) Deprecated.UsesuppressLintsFor(Action)instead.voidDeprecated.voidindentWithSpaces(int numSpacesPerTab) Deprecated.voidDeprecated.voidindentWithTabs(int tabToSpaces) Deprecated.protected booleanisLicenseHeaderStep(FormatterStep formatterStep) voidEnsures that the files are indented using tabs.voidleadingSpacesToTabs(int spacesPerTab) Ensures that the files are indented using tabs.voidEnsures that the files are indented using spaces.voidleadingTabsToSpaces(int spacesPerTab) Ensures that the files are indented using spaces.licenseHeader(String licenseHeader, String delimiter) licenseHeaderFile(Object licenseHeaderFile, String delimiter) voidEnsures formatting of files via native binary.protected GradleExceptionprotected final FileCollectionparseTarget(Object target) FileCollections pass through raw.prettier()Uses the default version of prettier.Uses the specified version of prettier.Uses exactly the npm packages specified in the map.protected final ProvisionervoidratchetFrom(String ratchetFrom) voidreplace(String name, CharSequence original, CharSequence after) Highly efficient find-replace char sequence.voidreplaceRegex(String name, String regex, String replacement) Highly efficient find-replace regex.protected voidreplaceStep(FormatterStep replacementStep) Replaces the given step.voidsetEncoding(String name) Sets the encoding to use (defaults toSpotlessExtension.getEncoding().voidsetEncoding(Charset charset) Sets the encoding to use (defaults toSpotlessExtension.getEncoding().voidsetLineEndings(LineEnding lineEndings) Sets the line endings to use (defaults toSpotlessExtension.getLineEndings().voidsetRatchetFrom(String ratchetFrom) Allows you to override the value from the parentSpotlessExtension.setRatchetFrom(String)for this specific format.protected voidsetupTask(SpotlessTask task) Sets up a format task according to the values in this extension.voidsuppressLintsFor(Action<LintSuppression> lintSuppression) Suppresses any lints which meet the supplied criteria.voidSets which files should be formatted.voidtargetExclude(Object... targets) Sets which files will be excluded from formatting.voidtargetExcludeIfContentContains(String string) Excludes all files whose content containsstring.voidExcludes all files whose content contains the given regex.voidDisables formatting betweenspotless:offandspotless:on.voidtoggleOffOn(String off, String on) Disables formatting between the given tags.voidUndoes all previous calls totoggleOffOn()andtoggleOffOn(String, String).voidtoggleOffOnRegex(String regex) Given a regex with *exactly one capturing group*, disables formatting inside that captured group.voidRemoves trailing whitespace.<T extends FormatExtension>
voidSame aswithinBlocks(String, String, String, Action), except you can specify any language-specific subclass ofFormatExtensionto get language-specific steps.voidwithinBlocks(String name, String open, String close, Action<FormatExtension> configure) spotless { format 'examples', { target '*.md' withinBlocks 'javascript examples', '\n```javascript\n', '\n```\n', { prettier().config(['parser': 'javascript']) } ...<T extends FormatExtension>
voidwithinBlocksRegex(String name, String regex, Class<T> clazz, Action<T> configure) Same aswithinBlocksRegex(String, String, Action), except you can specify any language-specific subclass ofFormatExtensionto get language-specific steps.voidwithinBlocksRegex(String name, String regex, Action<FormatExtension> configure) Same aswithinBlocks(String, String, String, Action), except instead of an open/close pair, you specify a regex with exactly one capturing group.
-
Field Details
-
target
The files to be formatted = (target - targetExclude). -
targetExclude
The files to be formatted = (target - targetExclude). -
targetExcludeContentPattern
The value from which files will be excluded if their content contain it. -
steps
The steps that need to be added.
-
-
Constructor Details
-
FormatExtension
-
-
Method Details
-
provisioner
-
getLineEndings
Returns the line endings to use (defaults toSpotlessExtension.getLineEndings(). -
setLineEndings
Sets the line endings to use (defaults toSpotlessExtension.getLineEndings(). -
getEncoding
Returns the encoding to use (defaults toSpotlessExtension.getEncoding(). -
setEncoding
Sets the encoding to use (defaults toSpotlessExtension.getEncoding(). -
getRatchetFrom
- See Also:
-
setRatchetFrom
Allows you to override the value from the parentSpotlessExtension.setRatchetFrom(String)for this specific format. -
ratchetFrom
- See Also:
-
setEncoding
Sets the encoding to use (defaults toSpotlessExtension.getEncoding(). -
suppressLintsFor
Suppresses any lints which meet the supplied criteria. -
ignoreErrorForStep
Deprecated.UsesuppressLintsFor(Action)instead.Ignores errors in the given step. -
ignoreErrorForPath
Deprecated.UsesuppressLintsFor(Action)instead.Ignores errors for the given relative path. -
encoding
Sets encoding to use (defaults toSpotlessExtension.getEncoding()). -
isLicenseHeaderStep
-
target
Sets which files should be formatted. Files to be formatted = (target - targetExclude).When this method is called multiple times, only the last call has any effect.
FileCollections pass through raw. Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir. List
are treated as the 'includes' arg to fileTree, with project.rootDir as the dir. Anything else gets passed to getProject().files(). If you pass any strings that start with "**\/*", this method will automatically filter out "build", ".gradle", and ".git" folders.
-
targetExclude
Sets which files will be excluded from formatting. Files to be formatted = (target - targetExclude).When this method is called multiple times, only the last call has any effect.
FileCollections pass through raw. Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir. List
are treated as the 'includes' arg to fileTree, with project.rootDir as the dir. Anything else gets passed to getProject().files(). -
targetExcludeIfContentContains
Excludes all files whose content containsstring.When this method is called multiple times, only the last call has any effect.
-
targetExcludeIfContentContainsRegex
Excludes all files whose content contains the given regex.When this method is called multiple times, only the last call has any effect.
-
parseTarget
FileCollections pass through raw. Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir. Listare treated as the 'includes' arg to fileTree, with project.rootDir as the dir. Anything else gets passed to getProject().files(). -
addStep
Adds a new step. -
addStep
Adds a new step that requires a Provisioner. -
getExistingStepIdx
Returns the index of the existing step with the given name, or -1 if no such step exists. -
replaceStep
Replaces the given step. -
clearSteps
public void clearSteps()Clears all of the existing steps. -
bumpThisNumberIfACustomStepChanges
public void bumpThisNumberIfACustomStepChanges(int number) An optional performance optimization if you are using any of thecustommethods. If you aren't explicitly callingcustom, then this method has no effect.Spotless tracks what files have changed from run to run, so that it can run faster by only checking files which have changed, or whose formatting steps have changed. If you use the
custommethods, then Gradle can never mark your files asup-to-date, because it can't know if perhaps the behavior of your custom function has changed.If you set
bumpThisNumberIfACustomStepChanges( <some number> ), then spotless will assume that the custom rules have not changed if the number has not changed. If a custom rule does change, then you must bump the number so that spotless will know that it must recheck the files it has already checked. -
custom
Adds a custom step. Receives a string with unix-newlines, must return a string with unix newlines. -
custom
Adds a custom step. Receives a string with unix-newlines, must return a string with unix newlines. -
replace
Highly efficient find-replace char sequence. -
replaceRegex
Highly efficient find-replace regex. -
forbidRegex
A regex which generates a lint. -
trimTrailingWhitespace
public void trimTrailingWhitespace()Removes trailing whitespace. -
endWithNewline
public void endWithNewline()Ensures that files end with a single newline. -
leadingTabsToSpaces
public void leadingTabsToSpaces(int spacesPerTab) Ensures that the files are indented using spaces. -
indentWithSpaces
Deprecated. -
leadingTabsToSpaces
public void leadingTabsToSpaces()Ensures that the files are indented using spaces. -
indentWithSpaces
Deprecated. -
leadingSpacesToTabs
public void leadingSpacesToTabs(int spacesPerTab) Ensures that the files are indented using tabs. -
indentWithTabs
Deprecated. -
leadingSpacesToTabs
public void leadingSpacesToTabs()Ensures that the files are indented using tabs. -
indentWithTabs
Deprecated. -
nativeCmd
Ensures formatting of files via native binary. -
licenseHeader
- Parameters:
licenseHeader- Content that should be at the top of every file.delimiter- Spotless will look for a line that starts with this regular expression pattern to know what the "top" is.
-
licenseHeaderFile
public FormatExtension.LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile, String delimiter) - Parameters:
licenseHeaderFile- Content that should be at the top of every file.delimiter- Spotless will look for a line that starts with this regular expression pattern to know what the "top" is.
-
prettier
Uses the default version of prettier. -
prettier
Uses the specified version of prettier. -
prettier
Uses exactly the npm packages specified in the map. -
biome
Defaults to downloading the default Biome version from the network. To work offline, you can specify the path to the Biome executable viabiome().pathToExe(...). -
biome
Downloads the given Biome version from the network. -
clangFormat
Uses the default version of clang-format. -
clangFormat
Uses the specified version of clang-format. -
eclipseWtp
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type) -
eclipseWtp
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type, String version) -
idea
-
withinBlocks
spotless { format 'examples', { target '*.md' withinBlocks 'javascript examples', '\n```javascript\n', '\n```\n', { prettier().config(['parser': 'javascript']) } ... -
withinBlocks
public <T extends FormatExtension> void withinBlocks(String name, String open, String close, Class<T> clazz, Action<T> configure) Same aswithinBlocks(String, String, String, Action), except you can specify any language-specific subclass ofFormatExtensionto get language-specific steps.spotless { format 'examples', { target '*.md' withinBlocks 'java examples', '\n```java\n', '\n```\n', com.diffplug.gradle.spotless.JavaExtension, { googleJavaFormat() formatAnnotations() } ... -
withinBlocksRegex
Same aswithinBlocks(String, String, String, Action), except instead of an open/close pair, you specify a regex with exactly one capturing group. -
withinBlocksRegex
public <T extends FormatExtension> void withinBlocksRegex(String name, String regex, Class<T> clazz, Action<T> configure) Same aswithinBlocksRegex(String, String, Action), except you can specify any language-specific subclass ofFormatExtensionto get language-specific steps. -
toggleOffOnRegex
Given a regex with *exactly one capturing group*, disables formatting inside that captured group. -
toggleOffOn
Disables formatting between the given tags. -
toggleOffOn
public void toggleOffOn()Disables formatting betweenspotless:offandspotless:on. -
toggleOffOnDisable
public void toggleOffOnDisable()Undoes all previous calls totoggleOffOn()andtoggleOffOn(String, String). -
setupTask
Sets up a format task according to the values in this extension. -
getProject
Returns the project that this extension is attached to. -
createIndependentApplyTask
Eager version ofcreateIndependentApplyTaskLazy(String) -
createIndependentApplyTaskLazy
Creates an independentSpotlessApplyfor (very) unusual circumstances.Most users will not want this method. In the rare case that you want to create a
SpotlessApplywhich is independent of the normal Spotless machinery, this will let you do that.The returned task will not be hooked up to the global
spotlessApply, and there will be no correspondingchecktask.The task name must not end with `Apply`.
NOTE: does not respect the rarely-used
spotlessFilesproperty. -
noDefaultTargetException
-
suppressLintsFor(Action)instead.