Scan YAML framing preambles once

This commit is contained in:
2026-10-09 01:30:17 +00:00
parent ae656d8fa9
commit 3f8841f298
4 changed files with 145 additions and 64 deletions

122
show.go
View File

@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"unicode"
)
@@ -99,14 +98,14 @@ func runConfigShow[C any](invocation Invocation, suite ConfigSuite[C], input con
if err := invocation.Context.Err(); err != nil {
return showFailure(err, diagnostics, written)
}
if index > 0 && hasYAMLDirective(native.Bytes()) {
framed, hasDirective := frameEffectiveYAML(document.name, native.Bytes())
if index > 0 && hasDirective {
if err := writeReport(invocation.Streams.Out, []byte("...\n")); err != nil {
outcome := showFailure(err, diagnostics, true)
outcome.DeliveryError = err
return outcome
}
}
framed := frameEffectiveYAML(document.name, native.Bytes())
if index < len(documents)-1 && len(framed) > 0 && framed[len(framed)-1] != '\n' {
framed = append(framed, '\n')
}
@@ -122,24 +121,50 @@ func runConfigShow[C any](invocation Invocation, suite ConfigSuite[C], input con
return outcome
}
func hasYAMLDirective(native []byte) bool {
lines := bytes.Split(native, []byte("\n"))
for _, line := range lines {
trimmed := bytes.TrimSpace(line)
if isYAMLDocumentStart(string(trimmed)) {
return false
}
if bytes.HasPrefix(trimmed, []byte("%YAML ")) || bytes.HasPrefix(trimmed, []byte("%TAG ")) {
return true
}
if len(trimmed) > 0 && trimmed[0] != '#' {
return false
}
}
return false
type yamlPreamble struct {
markerStart int
markerEnd int
markerFound bool
inlineMarker bool
preambleEnd int
hasDirective bool
}
func frameEffectiveYAML(scope string, native []byte) []byte {
func analyzeYAMLPreamble(native []byte) yamlPreamble {
var analysis yamlPreamble
for start := 0; start < len(native); {
end := len(native)
if newline := bytes.IndexByte(native[start:], '\n'); newline >= 0 {
end = start + newline + 1
}
lineEnd := end
if lineEnd > start && native[lineEnd-1] == '\n' {
lineEnd--
}
trimmed := bytes.TrimSpace(native[start:lineEnd])
if isYAMLDocumentStart(trimmed) {
content := bytes.TrimSpace(trimmed[3:])
analysis.markerStart = start
analysis.markerEnd = end
analysis.markerFound = true
analysis.inlineMarker = len(content) > 0 && content[0] != '#'
return analysis
}
if bytes.HasPrefix(trimmed, []byte("%YAML ")) || bytes.HasPrefix(trimmed, []byte("%TAG ")) {
analysis.hasDirective = true
}
if len(trimmed) == 0 || trimmed[0] == '#' || bytes.HasPrefix(trimmed, []byte("%YAML ")) || bytes.HasPrefix(trimmed, []byte("%TAG ")) {
analysis.preambleEnd = end
} else {
return analysis
}
start = end
}
return analysis
}
func frameEffectiveYAML(scope string, native []byte) ([]byte, bool) {
analysis := analyzeYAMLPreamble(native)
label := scope
for _, r := range scope {
if unicode.IsControl(r) {
@@ -148,53 +173,36 @@ func frameEffectiveYAML(scope string, native []byte) []byte {
}
}
comment := []byte("# scope: " + label + "\n")
lines := bytes.SplitAfter(native, []byte("\n"))
position := 0
preambleEnd := 0
hasDirective := false
for _, line := range lines {
trimmed := strings.TrimSpace(string(line))
if isYAMLDocumentStart(trimmed) {
out := make([]byte, 0, len(native)+len(comment))
// Inline content may be a block scalar; place the label before
// its marker rather than interrupting the native value.
content := strings.TrimSpace(strings.TrimPrefix(trimmed, "---"))
if content != "" && !strings.HasPrefix(content, "#") {
out = append(out, native[:position]...)
out = append(out, comment...)
out = append(out, native[position:]...)
return out
}
out = append(out, native[:position+len(line)]...)
if len(line) > 0 && line[len(line)-1] != '\n' {
out = append(out, '\n')
}
if analysis.markerFound {
out := make([]byte, 0, len(native)+len(comment))
// Inline content may be a block scalar; place the label before
// its marker rather than interrupting the native value.
if analysis.inlineMarker {
out = append(out, native[:analysis.markerStart]...)
out = append(out, comment...)
out = append(out, native[position+len(line):]...)
return out
out = append(out, native[analysis.markerStart:]...)
return out, analysis.hasDirective
}
if strings.HasPrefix(trimmed, "%YAML ") || strings.HasPrefix(trimmed, "%TAG ") {
hasDirective = true
out = append(out, native[:analysis.markerEnd]...)
if analysis.markerEnd > analysis.markerStart && native[analysis.markerEnd-1] != '\n' {
out = append(out, '\n')
}
if trimmed == "" || strings.HasPrefix(trimmed, "#") || strings.HasPrefix(trimmed, "%YAML ") || strings.HasPrefix(trimmed, "%TAG ") {
preambleEnd = position + len(line)
} else {
break
}
position += len(line)
out = append(out, comment...)
out = append(out, native[analysis.markerEnd:]...)
return out, analysis.hasDirective
}
result := make([]byte, 0, len(native)+len(comment)+5)
if hasDirective {
result = append(result, native[:preambleEnd]...)
if analysis.hasDirective {
result = append(result, native[:analysis.preambleEnd]...)
}
result = append(result, []byte("---\n")...)
result = append(result, comment...)
if hasDirective {
result = append(result, native[preambleEnd:]...)
if analysis.hasDirective {
result = append(result, native[analysis.preambleEnd:]...)
} else {
result = append(result, native...)
}
return result
return result, analysis.hasDirective
}
func showFailure(cause error, diagnostics []Diagnostic, reportStarted bool) Outcome {
@@ -204,6 +212,6 @@ func showFailure(cause error, diagnostics []Diagnostic, reportStarted bool) Outc
return outcome
}
func isYAMLDocumentStart(line string) bool {
return strings.HasPrefix(line, "---") && (len(line) == 3 || line[3] == ' ' || line[3] == '\t')
func isYAMLDocumentStart(line []byte) bool {
return bytes.HasPrefix(line, []byte("---")) && (len(line) == 3 || line[3] == ' ' || line[3] == '\t')
}