Check edited values, keep comments in place and make edits cheap

- Header.Set, Document.SetDefine and Document.AddHeader return an error for
  a value or name that can't be written as one opcode, define or header, and
  Build returns the same error for what it can't write.
- Set puts a new opcode after the last one's trailing comment, and never
  where text on its line would become part of its value. Delete keeps the
  line breaks it doesn't own.
- Node.Line and Node.Col are replaced by File.Position, which works lines and
  columns out when asked, and a node remembers where it was in its file, so an
  edit no longer renumbers the file or searches it from the start.
- Add FuzzEdit checks that an edited file reads back as its nodes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:27:17 +00:00
co-authored by Claude Sonnet 5.5
parent 1bb842589e
commit d4b44b6ffb
11 changed files with 482 additions and 129 deletions
+13 -14
View File
@@ -65,9 +65,10 @@ var controlSettings = []string{"default_path", "note_offset", "octave_offset"}
// target is where the opcodes after a header go.
type target struct {
ops Opcodes
// region is the header of a <region>, which becomes a region when the next
// header starts or the text ends.
region *Node
// region is whether the header is a <region>, which becomes a region when
// the next header starts or the text ends, and line is where it is.
region bool
line int
file string
}
@@ -100,12 +101,10 @@ func (f *flattener) variables(*File, *Node) {}
func (f *flattener) text(*File, *Node, string) {}
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
if variable {
// The header has no node of its own, so it takes the line of the node that
// made it.
n = &Node{Name: name, Line: n.Line}
}
f.startHeader(n, file.Path)
// A header that a variable made has no node of its own, so it takes the line
// of the node that made it.
line, _ := file.Position(n)
f.startHeader(name, line, file.Path)
}
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
@@ -113,7 +112,7 @@ func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.set
// closeRegion adds the region being read, if any. A region takes the <control>
// settings made before it, which later <control> headers don't change.
func (f *flattener) closeRegion() {
if f.into == nil || f.into.region == nil {
if f.into == nil || !f.into.region {
return
}
control := Opcodes{}
@@ -123,12 +122,12 @@ func (f *flattener) closeRegion() {
}
}
ops := control.with(f.levels[levelGlobal]).with(f.levels[levelMaster]).with(f.levels[levelGroup]).with(f.into.ops)
f.flat.Regions = append(f.flat.Regions, FlatRegion{Opcodes: ops, File: f.into.file, Line: f.into.region.Line})
f.flat.Regions = append(f.flat.Regions, FlatRegion{Opcodes: ops, File: f.into.file, Line: f.into.line})
}
func (f *flattener) startHeader(n *Node, file string) {
func (f *flattener) startHeader(header string, line int, file string) {
f.closeRegion()
name := strings.ToLower(n.Name)
name := strings.ToLower(header)
switch name {
case "control":
// A new <control> resets default_path, as ARIA does, but keeps the
@@ -151,7 +150,7 @@ func (f *flattener) startHeader(n *Node, file string) {
}
f.into = &target{ops: f.levels[lvl], file: file}
if lvl == levelRegion {
f.into.region = n
f.into.region, f.into.line = true, line
}
}
}