Refuse to write a document's includes into one file by accident

- Write and WriteTo return an error for a document with includes unless the
  includes are inlined, instead of dropping the files they hold. Only
  WriteFiles collects included files and checks them for conflicts.
- ExpandVariables leaves comments as they are.
- An inlined file no longer leaves a line of its own for the include's line
  break.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:30:19 +00:00
co-authored by Claude Sonnet 5.5
parent ec4b27c005
commit d15c8fd3e6
3 changed files with 131 additions and 29 deletions
+7 -2
View File
@@ -11,6 +11,7 @@ package sfz
import (
"errors"
"fmt"
"io"
"strings"
)
@@ -120,9 +121,13 @@ type Document struct {
problems []Problem
}
// WriteTo writes the main file's text to w. An included file is a separate
// [File] with its own text, which stays in the document as an #include.
// WriteTo writes the main file's text to w, or returns [ErrInvalid] if the
// document has includes that were read, as the text would not hold the files
// they read. Use [Document.Write] or [Document.WriteFiles] for those.
func (d *Document) WriteTo(w io.Writer) (int64, error) {
if d.hasIncludes() {
return 0, fmt.Errorf("%w: the document has includes, which need Write with InlineIncludes, or WriteFiles", ErrInvalid)
}
n, err := io.WriteString(w, d.Root.String())
return int64(n), err
}