Skip to content

Commit e4bf8a1

Browse files
committed
Simplifying HCL decoder
1 parent fd40574 commit e4bf8a1

File tree

1 file changed

+6
-56
lines changed

1 file changed

+6
-56
lines changed

pkg/yqlib/decoder_hcl.go

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,44 +64,6 @@ func extractLineComment(src []byte, endPos int) string {
6464
return ""
6565
}
6666

67-
// extractLeadingComments extracts comments from the very beginning of the file.
68-
// It returns the comment text and the byte position of the last character in that leading block.
69-
func extractLeadingComments(src []byte) (string, int) {
70-
var comments []string
71-
i := 0
72-
73-
// Skip leading whitespace
74-
for i < len(src) && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
75-
i++
76-
}
77-
78-
lastPos := -1
79-
80-
// Extract comment lines from the start
81-
for i < len(src) && src[i] == '#' {
82-
lineStart := i
83-
// Find end of line
84-
for i < len(src) && src[i] != '\n' {
85-
i++
86-
}
87-
lastPos = i - 1
88-
comments = append(comments, strings.TrimSpace(string(src[lineStart:i])))
89-
// Skip newline
90-
if i < len(src) && src[i] == '\n' {
91-
i++
92-
}
93-
// Skip whitespace between comment lines
94-
for i < len(src) && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
95-
i++
96-
}
97-
}
98-
99-
if len(comments) > 0 {
100-
return strings.Join(comments, "\n"), lastPos
101-
}
102-
return "", -1
103-
}
104-
10567
// extractHeadComment extracts comments before a given start position
10668
func extractHeadComment(src []byte, startPos int) string {
10769
var comments []string
@@ -174,33 +136,21 @@ func (dec *hclDecoder) Decode() (*CandidateNode, error) {
174136

175137
root := &CandidateNode{Kind: MappingNode}
176138

177-
// Extract file-level head comments (comments at the very beginning of the file)
178-
leadingComment, _ := extractLeadingComments(dec.fileBytes)
179-
leadingUsed := false
180-
if leadingComment != "" {
181-
root.HeadComment = leadingComment
182-
}
183-
184139
// process attributes in declaration order
185140
body := dec.file.Body.(*hclsyntax.Body)
141+
firstAttr := true
186142
for _, attrWithName := range sortedAttributes(body.Attributes) {
187143
keyNode := createStringScalarNode(attrWithName.Name)
188144
valNode := convertHclExprToNode(attrWithName.Attr.Expr, dec.fileBytes)
189145

190146
// Attach comments if any
191147
attrRange := attrWithName.Attr.Range()
192148
headComment := extractHeadComment(dec.fileBytes, attrRange.Start.Byte)
193-
if !leadingUsed && leadingComment != "" {
194-
// Avoid double-applying the leading file comment to the first attribute
195-
switch headComment {
196-
case leadingComment:
197-
headComment = ""
198-
case "":
199-
headComment = leadingComment
200-
}
201-
leadingUsed = true
202-
}
203-
if headComment != "" {
149+
if firstAttr && headComment != "" {
150+
// For the first attribute, apply its head comment to the root
151+
root.HeadComment = headComment
152+
firstAttr = false
153+
} else if headComment != "" {
204154
keyNode.HeadComment = headComment
205155
}
206156
if lineComment := extractLineComment(dec.fileBytes, attrRange.End.Byte); lineComment != "" {

0 commit comments

Comments
 (0)