mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 11:25:37 +00:00
Handle file-level comments for files with only blocks
Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
This commit is contained in:
parent
a5cc35fec7
commit
de9e778eab
@ -140,11 +140,23 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) {
|
|||||||
body := dec.file.Body.(*hclsyntax.Body)
|
body := dec.file.Body.(*hclsyntax.Body)
|
||||||
sortedAttrs := sortedAttributes(body.Attributes)
|
sortedAttrs := sortedAttributes(body.Attributes)
|
||||||
|
|
||||||
// Extract file-level head comments from before the first attribute
|
// Extract file-level head comments from before the first attribute or block
|
||||||
var fileComment string
|
var fileComment string
|
||||||
|
var firstPos int = -1
|
||||||
|
|
||||||
if len(sortedAttrs) > 0 {
|
if len(sortedAttrs) > 0 {
|
||||||
firstAttrPos := sortedAttrs[0].Attr.Range().Start.Byte
|
firstPos = sortedAttrs[0].Attr.Range().Start.Byte
|
||||||
fileComment = extractHeadComment(dec.fileBytes, firstAttrPos)
|
}
|
||||||
|
|
||||||
|
if len(body.Blocks) > 0 {
|
||||||
|
blockPos := body.Blocks[0].Range().Start.Byte
|
||||||
|
if firstPos == -1 || blockPos < firstPos {
|
||||||
|
firstPos = blockPos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if firstPos != -1 {
|
||||||
|
fileComment = extractHeadComment(dec.fileBytes, firstPos)
|
||||||
if fileComment != "" {
|
if fileComment != "" {
|
||||||
root.HeadComment = fileComment
|
root.HeadComment = fileComment
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user