groovyfmt for VS Code
Formats and lints Groovy, Gradle build scripts and Jenkinsfiles with groovyfmt, a fast formatter and linter written in Rust. The extension starts groovyfmt --lsp, the language server built into the groovyfmt binary, so the editor gives the same results as the command line.
- Format Document and Format Selection, and format on save through VS Code's own settings
- parse errors and lint violations as you type
- a quick fix for each fixable violation, and "Fix all auto-fixable problems" (
source.fixAll.groovyfmt)
It covers *.groovy, *.gvy, *.gy, *.gsh, *.gradle, *.jenkinsfile, Jenkinsfile and Jenkinsfile.*. It has no syntax highlighting, completion or navigation of its own. VS Code's built-in Groovy support or another Groovy, Gradle or Jenkins extension provides those, and this extension works alongside them, whatever language id they give these files.
Install
Install the extension, then open a Groovy file. If groovyfmt isn't installed, the extension offers to download the release that matches its own version from GitHub and keeps it in the extension's storage. Each release of the extension carries the SHA-256 of that release's archives, taken when they were built, and a download that doesn't match is never written or run. A build of the extension made outside a release (from a pull request, or packaged by hand) carries no checksums and doesn't download; install groovyfmt yourself to use it.
To use a groovyfmt you installed yourself, see Finding groovyfmt. The groovyfmt README lists the ways to install it (Homebrew, npm, release archives, cargo).
Format on save is VS Code's editor.formatOnSave. If another extension also formats Groovy, make groovyfmt the default formatter. To apply the lint fixes on save too, add source.fixAll.groovyfmt to editor.codeActionsOnSave:
"[groovy]": {
"editor.defaultFormatter": "groovyfmt.groovyfmt",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": { "source.fixAll.groovyfmt": "explicit" }
}
A Gradle or Jenkins extension may give *.gradle files or Jenkinsfiles a language id of its own (gradle, jenkins or jenkinsfile); add the same block for that id to format them on save.
Settings
| Setting |
Default |
|
groovyfmt.enable |
true |
Run the language server. |
groovyfmt.path |
"" |
Path to the groovyfmt binary. In user settings it must be absolute. In a trusted workspace's settings it may be relative to the first workspace folder (in a multi-root workspace, the first folder's own setting wins over the workspace file's). |
Everything about formatting and linting (indentation, line length, which rules run, excluded files, the lint baseline) lives in groovyfmt's config file, .groovyfmt.toml, the same one the command line reads. The server finds the config for each file by walking up from its directory and reads it again on every request, so an edit to .groovyfmt.toml applies right away. To turn lint rules off, turn them off in the config; see Configuration and Lint.
Finding groovyfmt
The extension runs the first of these that exists:
groovyfmt.path.
- The workspace's npm package:
node_modules/@groovyfmt/<platform>/bin/groovyfmt, then node_modules/.bin/groovyfmt, in each workspace folder.
groovyfmt on PATH.
- The release binary it downloaded before, for this extension's version.
- Otherwise, in a release build, it asks whether to download that binary. "Not now" is remembered; run groovyfmt: Restart server to be asked again. Closing the prompt without choosing asks again next time.
The language server arrived in groovyfmt 0.9.0. An older binary is reported instead of started.
Workspace Trust
In a restricted (untrusted) workspace, the extension never runs a binary that the workspace chose: it ignores groovyfmt.path from the workspace's settings and the workspace's node_modules, and a relative groovyfmt.path from user settings is refused everywhere, since it would resolve inside whatever workspace is open. groovyfmt.path from your user settings, PATH and the downloaded binary still apply. When you trust the workspace, the server restarts with the workspace's choice.
Commands
- groovyfmt: Restart server stops the server, finds the binary again and starts it.
- groovyfmt: Show output opens the extension's log, which names the binary in use and where it came from.
When something is wrong (no binary, a binary that is too old, a server that stopped), a groovyfmt item appears in the status bar. Click it for the log.
Privacy
No telemetry. The only network request is the release download, made after you agree to it.
License
MIT