The Terraform Language Server implements a set of Code Actions which perform different actions on the current document. These commands are typically code fixes to either refactor code, fix problems or to beautify/refactor code.
The server will format a given document according to Terraform formatting conventions.
To enable the format code action globally, set source.formatAll.terraform
to true for the editor.codeActionsOnSave
setting and set editor.formatOnSave
to false.
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.formatAll.terraform": true
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
}
Important: Disable
editor.formatOnSave
if you are usingsource.formatAll.terraform
ineditor.codeActionsOnSave
. Thesource.formatAll.terraform
code action is is meant to be used instead ofeditor.formatOnSave
, as it provides a guarantee of order of execution based on the list provided. If you have both settings enabled, then your document will be formatted twice.
If you would like editor.formatOnSave
to be true for other extensions but false for the Terraform extension, you can configure your settings as follows:
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.formatAll.terraform": true
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": false,
},
Alternatively, you can include all terraform related Code Actions inside the language specific setting if you prefer:
"editor.formatOnSave": true,
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.formatAll.terraform": true
},
},