public class FormatExtension extends Object
Adds a spotless{Name}Check
and spotless{Name}Apply
task.
Modifier and Type | Class and Description |
---|---|
class |
FormatExtension.ClangFormatConfig |
class |
FormatExtension.EclipseWtpConfig |
class |
FormatExtension.LicenseHeaderConfig
Created by
licenseHeader(String, String) or licenseHeaderFile(Object, String) . |
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 spotless) |
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
methods. |
FormatExtension.ClangFormatConfig |
clangFormat()
Uses the default version of clang-format.
|
FormatExtension.ClangFormatConfig |
clangFormat(String version)
Uses the specified version of clang-format.
|
void |
clearSteps()
Clears all of the existing steps.
|
SpotlessApply |
createIndependentApplyTask(String taskName)
Creates an independent
SpotlessApply for (very) unusual circumstances. |
void |
custom(String name,
groovy.lang.Closure<String> formatter)
Adds a custom step.
|
void |
custom(String name,
FormatterFunc formatter)
Adds a custom step.
|
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 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.
|
String |
getRatchetFrom() |
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.
|
FormatExtension.LicenseHeaderConfig |
licenseHeader(String licenseHeader,
String delimiter) |
FormatExtension.LicenseHeaderConfig |
licenseHeaderFile(Object licenseHeaderFile,
String delimiter) |
protected GradleException |
noDefaultTargetException() |
protected FileCollection |
parseTarget(Object target)
FileCollections pass through raw.
|
FormatExtension.PrettierConfig |
prettier()
Uses the default version of prettier.
|
FormatExtension.PrettierConfig |
prettier(Map<String,String> devDependencies)
Uses exactly the npm packages specified in the map.
|
FormatExtension.PrettierConfig |
prettier(String version)
Uses the specified version of prettier.
|
protected Provisioner |
provisioner() |
void |
ratchetFrom(String ratchetFrom) |
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() . |
void |
setRatchetFrom(String ratchetFrom)
Allows you to override the value from the parent
SpotlessExtension.setRatchetFrom(String)
for this specific format. |
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 |
toggleOffOn()
Disables formatting between
spotless:off and spotless:on . |
void |
toggleOffOn(String off,
String on)
Disables formatting between the given tags.
|
void |
toggleOffOnDisable()
Undoes all previous calls to
toggleOffOn() and toggleOffOn(String, String) . |
void |
toggleOffOnRegex(String regex)
Given a regex with exactly one capturing group, disables formatting
inside that captured group.
|
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 spotless)
protected final Provisioner provisioner()
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 String getRatchetFrom()
setRatchetFrom(String)
public void setRatchetFrom(String ratchetFrom)
Allows you to override the value from the parent SpotlessExtension.setRatchetFrom(String)
for this specific format.
public void ratchetFrom(String ratchetFrom)
setRatchetFrom(String)
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
If you pass any strings that start with “**/*”, this method will automatically filter out “build”, “.gradle”, and “.git” folders.
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 final 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.
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
methods. If you aren’t explicitly calling custom
, 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 custom
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 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 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 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()
Uses the default version of prettier.
public FormatExtension.PrettierConfig prettier(String version)
Uses the specified version of prettier.
public FormatExtension.PrettierConfig prettier(Map<String,String> devDependencies)
Uses exactly the npm packages specified in the map.
public FormatExtension.ClangFormatConfig clangFormat()
Uses the default version of clang-format.
public FormatExtension.ClangFormatConfig clangFormat(String version)
Uses the specified version of clang-format.
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type)
public FormatExtension.EclipseWtpConfig eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep type, String version)
public void toggleOffOnRegex(String regex)
Given a regex with exactly one capturing group, disables formatting inside that captured group.
public void toggleOffOn(String off, String on)
Disables formatting between the given tags.
public void toggleOffOn()
Disables formatting between spotless:off
and spotless:on
.
public void toggleOffOnDisable()
Undoes all previous calls to toggleOffOn()
and toggleOffOn(String, String)
.
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.
public SpotlessApply createIndependentApplyTask(String taskName)
Creates an independent SpotlessApply
for (very) unusual circumstances.
Most users will not want this method. In the rare case that you want to create
a SpotlessApply
which 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 corresponding check
task.
NOTE: does not respect the rarely-used spotlessFiles
property.
protected GradleException noDefaultTargetException()