printer: add spacewidth support

This commit is contained in:
Fatih Arslan 2015-10-25 18:02:40 +03:00
parent 0fc42b65df
commit e947512362
3 changed files with 11 additions and 46 deletions

View File

@ -70,7 +70,7 @@ func (p *printer) printObjectType(o *ast.ObjectType) []byte {
buf.WriteByte(newline) buf.WriteByte(newline)
for _, item := range o.List.Items { for _, item := range o.List.Items {
buf.Write(indent(p.printObjectItem(item))) buf.Write(p.indent(p.printObjectItem(item)))
buf.WriteByte(newline) buf.WriteByte(newline)
} }
@ -110,12 +110,19 @@ func writeBlank(buf io.ByteWriter, indent int) {
} }
} }
func indent(buf []byte) []byte { func (p *printer) indent(buf []byte) []byte {
prefix := []byte{tab}
if p.cfg.SpaceWidth != 0 {
for i := 0; i < p.cfg.SpaceWidth; i++ {
prefix = append(prefix, blank)
}
}
var res []byte var res []byte
bol := true bol := true
for _, c := range buf { for _, c := range buf {
if bol && c != '\n' { if bol && c != '\n' {
res = append(res, []byte{tab}...) res = append(res, prefix...)
} }
res = append(res, c) res = append(res, c)
bol = c == '\n' bol = c == '\n'

View File

@ -36,4 +36,4 @@ resource "aws_instance" "db" = {
output "web_ip" = { output "web_ip" = {
value = "${aws_instance.web.private_ip}" value = "${aws_instance.web.private_ip}"
} }

42
testdata/complex.hcl vendored
View File

@ -1,42 +0,0 @@
// This comes from Terraform, as a test
variable "foo" {
default = "bar"
description = "bar"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
provider "do" {
api_key = "${var.foo}"
}
resource "aws_security_group" "firewall" {
count = 5
}
resource aws_instance "web" {
ami = "${var.foo}"
security_groups = [
"foo",
"${aws_security_group.firewall.foo}"
]
network_interface {
device_index = 0
description = "Main network interface"
}
}
resource "aws_instance" "db" {
security_groups = "${aws_security_group.firewall.*.id}"
VPC = "foo"
depends_on = ["aws_instance.web"]
}
output "web_ip" {
value = "${aws_instance.web.private_ip}"
}