Skip to content

Commit de9e778

Browse files
Copilotmikefarah
andcommitted
Handle file-level comments for files with only blocks
Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
1 parent a5cc35f commit de9e778

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

pkg/yqlib/decoder_hcl.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,23 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) {
140140
body := dec.file.Body.(*hclsyntax.Body)
141141
sortedAttrs := sortedAttributes(body.Attributes)
142142

143-
// Extract file-level head comments from before the first attribute
143+
// Extract file-level head comments from before the first attribute or block
144144
var fileComment string
145+
var firstPos int = -1
146+
145147
if len(sortedAttrs) > 0 {
146-
firstAttrPos := sortedAttrs[0].Attr.Range().Start.Byte
147-
fileComment = extractHeadComment(dec.fileBytes, firstAttrPos)
148+
firstPos = sortedAttrs[0].Attr.Range().Start.Byte
149+
}
150+
151+
if len(body.Blocks) > 0 {
152+
blockPos := body.Blocks[0].Range().Start.Byte
153+
if firstPos == -1 || blockPos < firstPos {
154+
firstPos = blockPos
155+
}
156+
}
157+
158+
if firstPos != -1 {
159+
fileComment = extractHeadComment(dec.fileBytes, firstPos)
148160
if fileComment != "" {
149161
root.HeadComment = fileComment
150162
}

0 commit comments

Comments
 (0)