/vscode-spotless-gradle

A VS Code extension to lint & format your source files using Spotless & Gradle.

Primary LanguageTypeScriptMIT LicenseMIT

Spotless Gradle

Marketplace Version Visual Studio Marketplace Installs Build & Publish CodeQL GitHub bug issues

A VS Code extension to lint & format your code using Spotless (via Gradle).

Spotless Gradle Screencast

Features

  • Provides diagnostics to show invalid formatting (with quick fixes)
  • Provides a Spotless fixAll code action (Format on Save)
  • Provides a Spotless formatter (Format Document)
  • Supports all languages that Spotless supports

Requirements

Usage

Before using this extension, ensure you've configured Spotless correctly in your Gradle build file. (Run ./gradlew spotlessDiagnose to prepare & validate Spotless.)

Enabling Spotless

Spotless formatting & diagnostics are disabled by default. Change the settings to adjust this behavior:

{
  "[java]": {
    "spotlessGradle.format.enable": true,
    "spotlessGradle.diagnostics.enable": true
  }
}

Formatting

Format on save:

{
  "[java]": {
    "spotlessGradle.format.enable": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.spotlessGradle": true
    }
  }
}

If there are multiple formatters for a language type, set Spotless to be the default:

{
  "[java]": {
    "spotlessGradle.format.enable": true,
    "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle"
  }
}

Disable other formatters to improve the performance, for example:

{
  "[java]": {
    "spotlessGradle.format.enable": true,
    "files.trimTrailingWhitespace": false
  }
}

Typical Usage

As Spotless formatting is not a global feature, you should enable Spotless on a per-project basis, which is achieved by adding a .vscode/settings.json file to the root of the project

Enable for specific languages:

{
  "java.format.enabled": false,
  "[java]": {
    "files.trimTrailingWhitespace": false,
    "spotlessGradle.diagnostics.enable": true,
    "spotlessGradle.format.enable": true,
    "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle",
    "editor.codeActionsOnSave": {
      "source.fixAll.spotlessGradle": true
    }
  },
  "[gradle]": {
    "files.trimTrailingWhitespace": false,
    "spotlessGradle.diagnostics.enable": true,
    "spotlessGradle.format.enable": true,
    "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle",
    "editor.codeActionsOnSave": {
      "source.fixAll.spotlessGradle": true
    }
  }
}

Enable for all languages:

{
  "java.format.enabled": false,
  "files.trimTrailingWhitespace": false,
  "spotlessGradle.diagnostics.enable": true,
  "spotlessGradle.format.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.spotlessGradle": true
  },
  "[java]": {
    "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle"
  },
  "[gradle]": {
    "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle"
  }
}

How it Works

This extension runs the spotlessApply Gradle task on the focused file using the Spotless IDE hook feature. Untitled/Unsaved files are ignored.

The vscode => Spotless interface is provided by the Gradle for Java extension.

👉 Architecture Overview

Troubleshooting

View logs by selecting Spotless Gradle and/or Gradle for Java in the output panel.

Support

Credits

Release Notes

See CHANGELOG.md.

License

See LICENSE.md.