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:
@@ -113,7 +113,7 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
|
||||
|
||||
b := &builder{indent: o.Indent}
|
||||
for _, d := range defines {
|
||||
b.line("#define " + d.name + " " + d.value)
|
||||
b.define(d.name, d.value)
|
||||
}
|
||||
if len(control) > 0 {
|
||||
b.header("control", control)
|
||||
@@ -126,10 +126,16 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
|
||||
for _, ops := range regions {
|
||||
b.header("region", ops)
|
||||
}
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
return &Document{Root: b.file("")}, nil
|
||||
}
|
||||
|
||||
// The main file includes a file for each run of regions.
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
main := b.file("")
|
||||
for i := 0; i*o.RegionsPerFile < len(regions); i++ {
|
||||
chunk := regions[i*o.RegionsPerFile : min(len(regions), (i+1)*o.RegionsPerFile)]
|
||||
@@ -137,13 +143,19 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
|
||||
for _, ops := range chunk {
|
||||
cb.header("region", ops)
|
||||
}
|
||||
if cb.err != nil {
|
||||
return nil, cb.err
|
||||
}
|
||||
name := path.Clean(o.FileNamer(i))
|
||||
if nodes := lex(`#include "` + name + `"`); len(nodes) != 1 || nodes[0].Kind != KindInclude || nodes[0].Value != name {
|
||||
return nil, fmt.Errorf("%w: %q can't be the path of an included file", ErrInvalid, name)
|
||||
}
|
||||
child := cb.file(name)
|
||||
main.Nodes = append(main.Nodes,
|
||||
&Node{Kind: KindInclude, Raw: `#include "` + name + `"`, Value: name, Resolved: name, File: child},
|
||||
&Node{Kind: KindSpace, Raw: "\n"})
|
||||
}
|
||||
main.reposition()
|
||||
main.dirty = true
|
||||
return &Document{Root: main}, nil
|
||||
}
|
||||
|
||||
@@ -151,6 +163,8 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
|
||||
type builder struct {
|
||||
indent string
|
||||
text strings.Builder
|
||||
// err is the first thing that couldn't be written.
|
||||
err error
|
||||
}
|
||||
|
||||
func (b *builder) line(s string) {
|
||||
@@ -161,10 +175,20 @@ func (b *builder) line(s string) {
|
||||
func (b *builder) header(name string, ops []opcodeLine) {
|
||||
b.line("<" + name + ">")
|
||||
for _, op := range ops {
|
||||
if err := checkOpcode(op.name, op.value); err != nil && b.err == nil {
|
||||
b.err = err
|
||||
}
|
||||
b.line(b.indent + op.name + "=" + op.value)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *builder) define(name, value string) {
|
||||
if err := checkDefine(name, value); err != nil && b.err == nil {
|
||||
b.err = err
|
||||
}
|
||||
b.line("#define " + name + " " + value)
|
||||
}
|
||||
|
||||
// file returns what the builder has written as a file with the path.
|
||||
func (b *builder) file(p string) *File {
|
||||
f := &File{Path: p, Nodes: lex(b.text.String())}
|
||||
|
||||
Reference in New Issue
Block a user