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
+4 -1
View File
@@ -51,7 +51,7 @@ type lexer struct {
// add appends a node for the text from the current position to end.
func (l *lexer) add(src string, end int, n *Node) {
n.Raw = src[l.pos:end]
n.Line, n.Col = l.line, l.pos-l.lineStart+1
n.line, n.col = l.line, l.pos-l.lineStart+1
if n.Kind == KindText && len(l.nodes) > 0 {
if last := l.nodes[len(l.nodes)-1]; last.Kind == KindText {
// Nodes tile the text, so the two are one run of it.
@@ -129,6 +129,9 @@ func lex(src string) []*Node {
l.add(src, i+size, &Node{Kind: KindText})
}
}
for i, n := range l.nodes {
n.idx = i
}
return l.nodes
}