You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform/lang/langserver/format.go

22 lines
657 B

package langserver
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// formatSource will apply hclwrite formatting to the given source code
// if it has valid syntax, or just return it verbatim if not. The second
// argument is true if the returned buffer might be different than the given
// buffer, or false if it's guaranteed identical (allowing the caller to skip
// invalidating caches, etc.)
func formatSource(in []byte) ([]byte, bool) {
_, diags := hclsyntax.ParseConfig(in, "", hcl.Pos{})
if diags.HasErrors() {
return in, false
}
return hclwrite.Format(in), true
}