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:
+17
-16
@@ -25,7 +25,7 @@ func (d *Document) Validate() []Problem {
|
||||
type opcodeAt struct {
|
||||
value float64
|
||||
at *Node
|
||||
file string
|
||||
file *File
|
||||
}
|
||||
|
||||
// known is the result of a [Lookup].
|
||||
@@ -48,8 +48,9 @@ type validator struct {
|
||||
numbers map[string]opcodeAt
|
||||
}
|
||||
|
||||
func (v *validator) add(file string, n *Node, sev Severity, code, msg string) {
|
||||
v.problems = append(v.problems, Problem{File: file, Line: n.Line, Col: n.Col, Severity: sev, Code: code, Msg: msg})
|
||||
func (v *validator) add(f *File, n *Node, sev Severity, code, msg string) {
|
||||
line, col := f.Position(n)
|
||||
v.problems = append(v.problems, Problem{File: f.Path, Line: line, Col: col, Severity: sev, Code: code, Msg: msg})
|
||||
}
|
||||
|
||||
func (v *validator) define(f *File, n *Node) { v.undefined(f, n, n.Value) }
|
||||
@@ -57,7 +58,7 @@ func (v *validator) define(f *File, n *Node) { v.undefined(f, n, n.Value) }
|
||||
func (v *validator) include(f *File, n *Node) {
|
||||
v.undefined(f, n, n.Value)
|
||||
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
|
||||
v.add(f.Path, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
|
||||
v.add(f, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ func (v *validator) text(f *File, n *Node, raw string) {
|
||||
if strings.Contains(raw, "$") {
|
||||
return
|
||||
}
|
||||
v.add(f.Path, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(raw)))
|
||||
v.add(f, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(raw)))
|
||||
}
|
||||
|
||||
func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
||||
@@ -75,7 +76,7 @@ func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
||||
v.inHeader = true
|
||||
v.numbers = map[string]opcodeAt{}
|
||||
if _, ok := LookupHeader(name); !ok {
|
||||
v.add(f.Path, n, SeverityInfo, "header.unknown", fmt.Sprintf("<%s> isn't a header of the format", name))
|
||||
v.add(f, n, SeverityInfo, "header.unknown", fmt.Sprintf("<%s> isn't a header of the format", name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +103,7 @@ func (v *validator) endHeader() {
|
||||
|
||||
func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
||||
if !v.inHeader {
|
||||
v.add(f.Path, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
||||
v.add(f, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
||||
v.inHeader = true
|
||||
v.numbers = map[string]opcodeAt{}
|
||||
}
|
||||
@@ -116,17 +117,17 @@ func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
||||
}
|
||||
info, ok := k.info, k.ok
|
||||
if !ok {
|
||||
v.add(f.Path, n, SeverityInfo, "opcode.unknown", fmt.Sprintf("%s isn't an opcode of the format", name))
|
||||
v.add(f, n, SeverityInfo, "opcode.unknown", fmt.Sprintf("%s isn't an opcode of the format", name))
|
||||
return
|
||||
}
|
||||
if info.Deprecated {
|
||||
v.add(f.Path, n, SeverityInfo, "opcode.deprecated", fmt.Sprintf("%s is deprecated", name))
|
||||
v.add(f, n, SeverityInfo, "opcode.deprecated", fmt.Sprintf("%s is deprecated", name))
|
||||
}
|
||||
if strings.Contains(value, "$") {
|
||||
return
|
||||
}
|
||||
if (name == "sample" || name == "default_path") && outsideFolder(value) {
|
||||
v.add(f.Path, n, SeverityInfo, "sample.outside", fmt.Sprintf("%s=%s is outside the folder of the main file", name, value))
|
||||
v.add(f, n, SeverityInfo, "sample.outside", fmt.Sprintf("%s=%s is outside the folder of the main file", name, value))
|
||||
}
|
||||
v.value(f, n, name, value, info)
|
||||
}
|
||||
@@ -148,16 +149,16 @@ func (v *validator) value(f *File, n *Node, name, value string, info OpcodeInfo)
|
||||
x, ok = parseNumber(value)
|
||||
}
|
||||
if !ok {
|
||||
v.add(f.Path, n, SeverityWarning, "value.type", fmt.Sprintf("%s=%s isn't a number", name, value))
|
||||
v.add(f, n, SeverityWarning, "value.type", fmt.Sprintf("%s=%s isn't a number", name, value))
|
||||
return
|
||||
}
|
||||
if info.Type == TypeInteger && !info.Note && x != float64(int64(x)) {
|
||||
v.add(f.Path, n, SeverityInfo, "value.type", fmt.Sprintf("%s=%s isn't a whole number", name, value))
|
||||
v.add(f, n, SeverityInfo, "value.type", fmt.Sprintf("%s=%s isn't a whole number", name, value))
|
||||
}
|
||||
if info.Min != nil && x < *info.Min || info.Max != nil && x > *info.Max {
|
||||
v.add(f.Path, n, SeverityWarning, "value.range", fmt.Sprintf("%s=%s is outside %s", name, value, bounds(info)))
|
||||
v.add(f, n, SeverityWarning, "value.range", fmt.Sprintf("%s=%s is outside %s", name, value, bounds(info)))
|
||||
}
|
||||
v.numbers[name] = opcodeAt{value: x, at: n, file: f.Path}
|
||||
v.numbers[name] = opcodeAt{value: x, at: n, file: f}
|
||||
case TypeString:
|
||||
if len(info.Options) == 0 {
|
||||
return
|
||||
@@ -167,7 +168,7 @@ func (v *validator) value(f *File, n *Node, name, value string, info OpcodeInfo)
|
||||
return
|
||||
}
|
||||
}
|
||||
v.add(f.Path, n, SeverityWarning, "value.option", fmt.Sprintf("%s=%s isn't one of %s", name, value, strings.Join(info.Options, ", ")))
|
||||
v.add(f, n, SeverityWarning, "value.option", fmt.Sprintf("%s=%s isn't one of %s", name, value, strings.Join(info.Options, ", ")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +187,7 @@ func bounds(info OpcodeInfo) string {
|
||||
// undefined reports the variables in text that haven't been defined.
|
||||
func (v *validator) undefined(f *File, n *Node, text string) {
|
||||
for _, run := range v.defs.undefined(text) {
|
||||
v.add(f.Path, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||||
v.add(f, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user