public class FormatExtension extends Object
Adds a spotless{Name}Check
and spotless{Name}Apply
task.
Modifier and Type | Class and Description |
---|---|
class |
FormatExtension.EclipseWtpConfig |
class |
FormatExtension.NpmStepConfig<T extends FormatExtension.NpmStepConfig<?>> |
class |
FormatExtension.PrettierConfig |
Modifier and Type | Field and Description |
---|---|
protected List<FormatterStep> |
steps
The steps that need to be added.
|
protected FileCollection |
target
The files to be formatted = (target - targetExclude).
|
protected FileCollection |
targetExclude
The files to be formatted = (target - targetExclude).
|
Constructor and Description |
---|
FormatExtension(SpotlessExtension root) |
Modifier and Type | Method and Description |
---|---|
void |
addStep(FormatterStep newStep)
Adds a new step.
|
void |
bumpThisNumberIfACustomStepChanges(int number)
An optional performance optimization if you are using any of the
custom or customLazy methods. |
void |
clearSteps()
Clears all of the existing steps.
|
void |
custom(String name,
groovy.lang.Closure<String> formatter)
Adds a custom step.
|
void |
custom(String name,
FormatterFunc formatter)
Adds a custom step.
|
void |
customLazy(String name,
ThrowingEx.Supplier<FormatterFunc> formatterSupplier)
Adds the given custom step, which is constructed lazily for performance reasons.
|
void |
customLazyGroovy(String name,
ThrowingEx.Supplier<groovy.lang.Closure<String>> formatterSupplier)
Same as
customLazy(String, ThrowingEx.Supplier) , but for Groovy closures. |
FormatExtension.EclipseWtpConfig |
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type) |
FormatExtension.EclipseWtpConfig |
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type,
String version) |
void |
encoding(String charset)
Sets encoding to use (defaults to
SpotlessExtension.getEncoding() ). |
void |
endWithNewline()
Ensures that files end with a single newline.
|
Charset |
getEncoding()
Returns the encoding to use (defaults to
SpotlessExtension.getEncoding() . |
protected FormatterStep |
getExistingStep(String stepName)
Deprecated.
|
protected int |
getExistingStepIdx(String stepName)
Returns the index of the existing step with the given name, or -1 if no such step exists.
|
LineEnding |
getLineEndings()
Returns the line endings to use (defaults to
SpotlessExtension.getLineEndings() . |
protected Project |
getProject()
Returns the project that this extension is attached to.
|
void |
ignoreErrorForPath(String relativePath)
Ignores errors for the given relative path.
|
void |
ignoreErrorForStep(String stepName)
Ignores errors in the given step.
|
void |
indentWithSpaces()
Ensures that the files are indented using spaces.
|
void |
indentWithSpaces(int numSpacesPerTab)
Ensures that the files are indented using spaces.
|
void |
indentWithTabs()
Ensures that the files are indented using tabs.
|
void |
indentWithTabs(int tabToSpaces)
Ensures that the files are indented using tabs.
|
com.diffplug.gradle.spotless.FormatExtension.LicenseHeaderConfig |
licenseHeader(String licenseHeader,
String delimiter) |
com.diffplug.gradle.spotless.FormatExtension.LicenseHeaderConfig |
licenseHeaderFile(Object licenseHeaderFile,
String delimiter) |
void |
paddedCell()
Enables paddedCell mode.
|
void |
paddedCell(boolean paddedCell)
Enables paddedCell mode.
|
protected FileCollection |
parseTarget(Object target)
FileCollections pass through raw.
|
FormatExtension.PrettierConfig |
prettier() |
void |
replace(String name,
CharSequence original,
CharSequence after)
Highly efficient find-replace char sequence.
|
void |
replaceRegex(String name,
String regex,
String replacement)
Highly efficient find-replace regex.
|
protected void |
replaceStep(FormatterStep replacementStep)
Replaces the given step.
|
void |
setEncoding(Charset charset)
Sets the encoding to use (defaults to
SpotlessExtension.getEncoding() . |
void |
setEncoding(String name)
Sets the encoding to use (defaults to
SpotlessExtension.getEncoding() . |
void |
setLineEndings(LineEnding lineEndings)
Sets the line endings to use (defaults to
SpotlessExtension.getLineEndings() . |
protected void |
setupTask(SpotlessTask task)
Sets up a format task according to the values in this extension.
|
void |
target(Object... targets)
Sets which files should be formatted.
|
void |
targetExclude(Object... targets)
Sets which files will be excluded from formatting.
|
void |
trimTrailingWhitespace()
Removes trailing whitespace.
|
protected FileCollection target
The files to be formatted = (target - targetExclude).
protected FileCollection targetExclude
The files to be formatted = (target - targetExclude).
protected final List<FormatterStep> steps
The steps that need to be added.
public FormatExtension(SpotlessExtension root)
public void paddedCell()
Enables paddedCell mode. @see Padded cell
public void paddedCell(boolean paddedCell)
Enables paddedCell mode. @see Padded cell
public LineEnding getLineEndings()
Returns the line endings to use (defaults to SpotlessExtension.getLineEndings()
.
public void setLineEndings(LineEnding lineEndings)
Sets the line endings to use (defaults to SpotlessExtension.getLineEndings()
.
public Charset getEncoding()
Returns the encoding to use (defaults to SpotlessExtension.getEncoding()
.
public void setEncoding(String name)
Sets the encoding to use (defaults to SpotlessExtension.getEncoding()
.
public void setEncoding(Charset charset)
Sets the encoding to use (defaults to SpotlessExtension.getEncoding()
.
public void ignoreErrorForStep(String stepName)
Ignores errors in the given step.
public void ignoreErrorForPath(String relativePath)
Ignores errors for the given relative path.
public void encoding(String charset)
Sets encoding to use (defaults to SpotlessExtension.getEncoding()
).
public void target(Object... targets)
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
public void targetExclude(Object... targets)
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
protected FileCollection parseTarget(Object target)
FileCollections pass through raw. Strings are treated as the ‘include’ arg to fileTree, with project.rootDir as the dir. List
public void addStep(FormatterStep newStep)
Adds a new step.
@Deprecated @Nullable protected FormatterStep getExistingStep(String stepName)
Returns the existing step with the given name, if any.
protected int getExistingStepIdx(String stepName)
Returns the index of the existing step with the given name, or -1 if no such step exists.
protected void replaceStep(FormatterStep replacementStep)
Replaces the given step.
public void clearSteps()
Clears all of the existing steps.
public void bumpThisNumberIfACustomStepChanges(int number)
An optional performance optimization if you are using any of the custom
or customLazy
methods. If you aren’t explicitly calling custom
or customLazy
, 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 either the custom
or customLazy
methods, then gradle can never mark your files as up-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.
public void customLazy(String name, ThrowingEx.Supplier<FormatterFunc> formatterSupplier)
Adds the given custom step, which is constructed lazily for performance reasons.
The resulting function will receive a string with unix-newlines, and it must return a string unix newlines.
If you’re getting errors about closure cannot be cast to com.diffplug.common.base.Throwing$Function
, then use customLazyGroovy(String, ThrowingEx.Supplier)
.
public void customLazyGroovy(String name, ThrowingEx.Supplier<groovy.lang.Closure<String>> formatterSupplier)
Same as customLazy(String, ThrowingEx.Supplier)
, but for Groovy closures.
public void custom(String name, groovy.lang.Closure<String> formatter)
Adds a custom step. Receives a string with unix-newlines, must return a string with unix newlines.
public void custom(String name, FormatterFunc formatter)
Adds a custom step. Receives a string with unix-newlines, must return a string with unix newlines.
public void replace(String name, CharSequence original, CharSequence after)
Highly efficient find-replace char sequence.
public void replaceRegex(String name, String regex, String replacement)
Highly efficient find-replace regex.
public void trimTrailingWhitespace()
Removes trailing whitespace.
public void endWithNewline()
Ensures that files end with a single newline.
public void indentWithSpaces(int numSpacesPerTab)
Ensures that the files are indented using spaces.
public void indentWithSpaces()
Ensures that the files are indented using spaces.
public void indentWithTabs(int tabToSpaces)
Ensures that the files are indented using tabs.
public void indentWithTabs()
Ensures that the files are indented using tabs.
public com.diffplug.gradle.spotless.FormatExtension.LicenseHeaderConfig licenseHeader(String licenseHeader, String delimiter)
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.public com.diffplug.gradle.spotless.FormatExtension.LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile, String delimiter)
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.public FormatExtension.PrettierConfig prettier()
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type)
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type, String version)
protected void setupTask(SpotlessTask task)
Sets up a format task according to the values in this extension.
protected Project getProject()
Returns the project that this extension is attached to.