Package com.diffplug.gradle.spotless
Class FormatExtension
- java.lang.Object
-
- com.diffplug.gradle.spotless.FormatExtension
-
- Direct Known Subclasses:
Antlr4Extension
,CppExtension
,FreshMarkExtension
,GroovyExtension
,GroovyGradleExtension
,JavaExtension
,JsonExtension
,KotlinExtension
,KotlinGradleExtension
,PythonExtension
,ScalaExtension
,SqlExtension
,TypescriptExtension
public class FormatExtension extends Object
Adds aspotless{Name}Check
andspotless{Name}Apply
task.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
FormatExtension.ClangFormatConfig
class
FormatExtension.EclipseWtpConfig
class
FormatExtension.LicenseHeaderConfig
Created bylicenseHeader(String, String)
orlicenseHeaderFile(Object, String)
.class
FormatExtension.NpmStepConfig<T extends FormatExtension.NpmStepConfig<?>>
class
FormatExtension.PrettierConfig
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description FormatExtension(SpotlessExtension spotless)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addStep(FormatterStep newStep)
Adds a new step.void
bumpThisNumberIfACustomStepChanges(int number)
An optional performance optimization if you are using any of thecustom
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 independentSpotlessApply
for (very) unusual circumstances.void
custom(String name, FormatterFunc formatter)
Adds a custom step.void
custom(String name, groovy.lang.Closure<String> 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 toSpotlessExtension.getEncoding()
).void
endWithNewline()
Ensures that files end with a single newline.Charset
getEncoding()
Returns the encoding to use (defaults toSpotlessExtension.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 toSpotlessExtension.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)
void
nativeCmd(String name, String pathToExe, List<String> arguments)
Ensures formatting of files via native binary.protected GradleException
noDefaultTargetException()
protected FileCollection
parseTarget(Object target)
FileCollections pass through raw.FormatExtension.PrettierConfig
prettier()
Uses the default version of prettier.FormatExtension.PrettierConfig
prettier(String version)
Uses the specified version of prettier.FormatExtension.PrettierConfig
prettier(Map<String,String> devDependencies)
Uses exactly the npm packages specified in the map.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(String name)
Sets the encoding to use (defaults toSpotlessExtension.getEncoding()
.void
setEncoding(Charset charset)
Sets the encoding to use (defaults toSpotlessExtension.getEncoding()
.void
setLineEndings(LineEnding lineEndings)
Sets the line endings to use (defaults toSpotlessExtension.getLineEndings()
.void
setRatchetFrom(String ratchetFrom)
Allows you to override the value from the parentSpotlessExtension.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 betweenspotless:off
andspotless:on
.void
toggleOffOn(String off, String on)
Disables formatting between the given tags.void
toggleOffOnDisable()
Undoes all previous calls totoggleOffOn()
andtoggleOffOn(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.<T extends FormatExtension>
voidwithinBlocks(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 ofFormatExtension
to get language-specific steps.void
withinBlocks(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 ofFormatExtension
to get language-specific steps.void
withinBlocksRegex(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 Detail
-
target
protected FileCollection target
The files to be formatted = (target - targetExclude).
-
targetExclude
protected FileCollection targetExclude
The files to be formatted = (target - targetExclude).
-
steps
protected final List<FormatterStep> steps
The steps that need to be added.
-
-
Constructor Detail
-
FormatExtension
@Inject public FormatExtension(SpotlessExtension spotless)
-
-
Method Detail
-
provisioner
protected final Provisioner provisioner()
-
getLineEndings
public LineEnding getLineEndings()
Returns the line endings to use (defaults toSpotlessExtension.getLineEndings()
.
-
setLineEndings
public void setLineEndings(LineEnding lineEndings)
Sets the line endings to use (defaults toSpotlessExtension.getLineEndings()
.
-
getEncoding
public Charset getEncoding()
Returns the encoding to use (defaults toSpotlessExtension.getEncoding()
.
-
setEncoding
public void setEncoding(String name)
Sets the encoding to use (defaults toSpotlessExtension.getEncoding()
.
-
getRatchetFrom
public String getRatchetFrom()
- See Also:
setRatchetFrom(String)
-
setRatchetFrom
public void setRatchetFrom(String ratchetFrom)
Allows you to override the value from the parentSpotlessExtension.setRatchetFrom(String)
for this specific format.
-
ratchetFrom
public void ratchetFrom(String ratchetFrom)
- See Also:
setRatchetFrom(String)
-
setEncoding
public void setEncoding(Charset charset)
Sets the encoding to use (defaults toSpotlessExtension.getEncoding()
.
-
ignoreErrorForStep
public void ignoreErrorForStep(String stepName)
Ignores errors in the given step.
-
ignoreErrorForPath
public void ignoreErrorForPath(String relativePath)
Ignores errors for the given relative path.
-
encoding
public void encoding(String charset)
Sets encoding to use (defaults toSpotlessExtension.getEncoding()
).
-
target
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. Listare 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
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. Listare treated as the 'includes' arg to fileTree, with project.rootDir as the dir. Anything else gets passed to getProject().files().
-
parseTarget
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. Listare treated as the 'includes' arg to fileTree, with project.rootDir as the dir. Anything else gets passed to getProject().files().
-
addStep
public void addStep(FormatterStep newStep)
Adds a new step.
-
getExistingStepIdx
protected int getExistingStepIdx(String stepName)
Returns the index of the existing step with the given name, or -1 if no such step exists.
-
replaceStep
protected void replaceStep(FormatterStep replacementStep)
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 thecustom
methods. 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 thecustom
methods, 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 setbumpThisNumberIfACustomStepChanges( <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
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.
-
custom
public void custom(String name, FormatterFunc formatter)
Adds a custom step. Receives a string with unix-newlines, must return a string with unix newlines.
-
replace
public void replace(String name, CharSequence original, CharSequence after)
Highly efficient find-replace char sequence.
-
replaceRegex
public void replaceRegex(String name, String regex, String replacement)
Highly efficient find-replace regex.
-
trimTrailingWhitespace
public void trimTrailingWhitespace()
Removes trailing whitespace.
-
endWithNewline
public void endWithNewline()
Ensures that files end with a single newline.
-
indentWithSpaces
public void indentWithSpaces(int numSpacesPerTab)
Ensures that the files are indented using spaces.
-
indentWithSpaces
public void indentWithSpaces()
Ensures that the files are indented using spaces.
-
indentWithTabs
public void indentWithTabs(int tabToSpaces)
Ensures that the files are indented using tabs.
-
indentWithTabs
public void indentWithTabs()
Ensures that the files are indented using tabs.
-
nativeCmd
public void nativeCmd(String name, String pathToExe, List<String> arguments)
Ensures formatting of files via native binary.
-
licenseHeader
public FormatExtension.LicenseHeaderConfig licenseHeader(String licenseHeader, String delimiter)
- 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
public FormatExtension.PrettierConfig prettier()
Uses the default version of prettier.
-
prettier
public FormatExtension.PrettierConfig prettier(String version)
Uses the specified version of prettier.
-
prettier
public FormatExtension.PrettierConfig prettier(Map<String,String> devDependencies)
Uses exactly the npm packages specified in the map.
-
clangFormat
public FormatExtension.ClangFormatConfig clangFormat()
Uses the default version of clang-format.
-
clangFormat
public FormatExtension.ClangFormatConfig clangFormat(String version)
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)
-
withinBlocks
public void withinBlocks(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']) } ...
-
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 ofFormatExtension
to get language-specific steps.spotless { format 'examples', { target '*.md' withinBlocks 'java examples', '\n```java\n', '\n```\n', com.diffplug.gradle.spotless.JavaExtension, { googleJavaFormat() } ...
-
withinBlocksRegex
public void withinBlocksRegex(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.
-
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 ofFormatExtension
to get language-specific steps.
-
toggleOffOnRegex
public void toggleOffOnRegex(String regex)
Given a regex with *exactly one capturing group*, disables formatting inside that captured group.
-
toggleOffOn
public void toggleOffOn(String off, String on)
Disables formatting between the given tags.
-
toggleOffOn
public void toggleOffOn()
Disables formatting betweenspotless:off
andspotless:on
.
-
toggleOffOnDisable
public void toggleOffOnDisable()
Undoes all previous calls totoggleOffOn()
andtoggleOffOn(String, String)
.
-
setupTask
protected void setupTask(SpotlessTask task)
Sets up a format task according to the values in this extension.
-
getProject
protected Project getProject()
Returns the project that this extension is attached to.
-
createIndependentApplyTask
public SpotlessApply createIndependentApplyTask(String taskName)
Creates an independentSpotlessApply
for (very) unusual circumstances. Most users will not want this method. In the rare case that you want to create aSpotlessApply
which is independent of the normal Spotless machinery, this will let you do that. The returned task will not be hooked up to the globalspotlessApply
, and there will be no correspondingcheck
task. NOTE: does not respect the rarely-usedspotlessFiles
property.
-
noDefaultTargetException
protected GradleException noDefaultTargetException()
-
-