Skip to content

Commit

Permalink
Standardise how blank lines are added to plan for unchanged blocks (#…
Browse files Browse the repository at this point in the history
…31330)

* Standardise blank lines added to plan for unchanged blocks

* Fix copy-paste error inside NestingList logic
  • Loading branch information
liamcervante committed Jun 30, 2022
1 parent d876e68 commit acb79a7
Show file tree
Hide file tree
Showing 2 changed files with 751 additions and 112 deletions.
56 changes: 28 additions & 28 deletions internal/command/format/diff.go
Expand Up @@ -310,7 +310,7 @@ func (p *blockBodyDiffPrinter) writeBlockBodyDiff(schema *configschema.Block, ol
if result.skippedBlocks == 1 {
noun = "block"
}
p.buf.WriteString("\n")
p.buf.WriteString("\n\n")
p.buf.WriteString(strings.Repeat(" ", indent+2))
p.buf.WriteString(fmt.Sprintf(p.color.Color("[dark_gray]# (%d unchanged %s hidden)[reset]"), result.skippedBlocks, noun))
}
Expand All @@ -326,8 +326,6 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
path cty.Path,
result *blockBodyDiffResult) bool {

blankBeforeBlocks := false

attrNames := make([]string, 0, len(attrsS))
displayAttrNames := make(map[string]string, len(attrsS))
attrNameLen := 0
Expand All @@ -349,8 +347,8 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
}
}
sort.Strings(attrNames)
if len(attrNames) > 0 {
blankBeforeBlocks = true
if len(attrNames) == 0 {
return false
}

for _, name := range attrNames {
Expand All @@ -365,7 +363,7 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
}
}

return blankBeforeBlocks
return true
}

// getPlanActionAndShow returns the action value
Expand Down Expand Up @@ -754,10 +752,7 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
action = plans.Update
}

if blankBefore {
p.buf.WriteRune('\n')
}
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, old, new, indent, path)
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, old, new, indent, blankBefore, path)
if skipped {
return 1
}
Expand Down Expand Up @@ -790,10 +785,7 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
commonLen = len(newItems)
}

if blankBefore && (len(oldItems) > 0 || len(newItems) > 0) {
p.buf.WriteRune('\n')
}

blankBeforeInner := blankBefore
for i := 0; i < commonLen; i++ {
path := append(path, cty.IndexStep{Key: cty.NumberIntVal(int64(i))})
oldItem := oldItems[i]
Expand All @@ -802,27 +794,33 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
if oldItem.RawEquals(newItem) {
action = plans.NoOp
}
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, oldItem, newItem, indent, path)
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, oldItem, newItem, indent, blankBeforeInner, path)
if skipped {
skippedBlocks++
} else {
blankBeforeInner = false
}
}
for i := commonLen; i < len(oldItems); i++ {
path := append(path, cty.IndexStep{Key: cty.NumberIntVal(int64(i))})
oldItem := oldItems[i]
newItem := cty.NullVal(oldItem.Type())
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, plans.Delete, oldItem, newItem, indent, path)
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, plans.Delete, oldItem, newItem, indent, blankBeforeInner, path)
if skipped {
skippedBlocks++
} else {
blankBeforeInner = false
}
}
for i := commonLen; i < len(newItems); i++ {
path := append(path, cty.IndexStep{Key: cty.NumberIntVal(int64(i))})
newItem := newItems[i]
oldItem := cty.NullVal(newItem.Type())
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, plans.Create, oldItem, newItem, indent, path)
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, plans.Create, oldItem, newItem, indent, blankBeforeInner, path)
if skipped {
skippedBlocks++
} else {
blankBeforeInner = false
}
}
case configschema.NestingSet:
Expand All @@ -845,10 +843,7 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
allItems = append(allItems, newItems...)
all := cty.SetVal(allItems)

if blankBefore {
p.buf.WriteRune('\n')
}

blankBeforeInner := blankBefore
for it := all.ElementIterator(); it.Next(); {
_, val := it.Element()
var action plans.Action
Expand All @@ -871,9 +866,11 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
newValue = val
}
path := append(path, cty.IndexStep{Key: val})
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, oldValue, newValue, indent, path)
skipped := p.writeNestedBlockDiff(name, nil, &blockS.Block, action, oldValue, newValue, indent, blankBeforeInner, path)
if skipped {
skippedBlocks++
} else {
blankBeforeInner = false
}
}

Expand Down Expand Up @@ -904,10 +901,7 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
}
sort.Strings(allKeysOrder)

if blankBefore {
p.buf.WriteRune('\n')
}

blankBeforeInner := blankBefore
for _, k := range allKeysOrder {
var action plans.Action
oldValue := oldItems[k]
Expand All @@ -926,9 +920,11 @@ func (p *blockBodyDiffPrinter) writeNestedBlockDiffs(name string, blockS *config
}

path := append(path, cty.IndexStep{Key: cty.StringVal(k)})
skipped := p.writeNestedBlockDiff(name, &k, &blockS.Block, action, oldValue, newValue, indent, path)
skipped := p.writeNestedBlockDiff(name, &k, &blockS.Block, action, oldValue, newValue, indent, blankBeforeInner, path)
if skipped {
skippedBlocks++
} else {
blankBeforeInner = false
}
}
}
Expand Down Expand Up @@ -974,11 +970,15 @@ func (p *blockBodyDiffPrinter) writeSensitiveNestedBlockDiff(name string, old, n
p.buf.WriteString("}")
}

func (p *blockBodyDiffPrinter) writeNestedBlockDiff(name string, label *string, blockS *configschema.Block, action plans.Action, old, new cty.Value, indent int, path cty.Path) bool {
func (p *blockBodyDiffPrinter) writeNestedBlockDiff(name string, label *string, blockS *configschema.Block, action plans.Action, old, new cty.Value, indent int, blankBefore bool, path cty.Path) bool {
if action == plans.NoOp && !p.verbose {
return true
}

if blankBefore {
p.buf.WriteRune('\n')
}

p.buf.WriteString("\n")
p.buf.WriteString(strings.Repeat(" ", indent))
p.writeActionSymbol(action)
Expand Down

0 comments on commit acb79a7

Please sign in to comment.