Files
sfz/edit.go
T

231 lines
5.8 KiB
Go
Raw Normal View History

2026-12-01 17:41:34 +00:00
package sfz
import "strings"
// Header is a header of a file, with the opcodes that follow it up to the
// next header in the same file. Opcodes that an #include after it brings in
// belong to the included file's own headers.
type Header struct {
// File is the file the header is in.
File *File
// Node is the header's own node.
Node *Node
}
// Name returns the header's name without brackets, as written.
func (h *Header) Name() string { return h.Node.Name }
// index returns where the header's node is in its file.
func (h *Header) index() int {
for i, n := range h.File.Nodes {
if n == h.Node {
return i
}
}
return -1
}
// span returns the range of the file's nodes after the header's own, up to the
// next header.
func (h *Header) span() (start, end int) {
start = h.index() + 1
end = start
for end < len(h.File.Nodes) && h.File.Nodes[end].Kind != KindHeader {
end++
}
return start, end
}
// Opcodes returns the header's opcode nodes, in order.
func (h *Header) Opcodes() []*Node {
start, end := h.span()
var out []*Node
for _, n := range h.File.Nodes[start:end] {
if n.Kind == KindOpcode {
out = append(out, n)
}
}
return out
}
// Get returns the header's last node for an opcode, which is the one that
// takes effect.
func (h *Header) Get(name string) (*Node, bool) {
ops := h.Opcodes()
for i := len(ops) - 1; i >= 0; i-- {
if ops[i].Name == name {
return ops[i], true
}
}
return nil, false
}
// Set gives the opcode a value. An opcode the header already has keeps its
// place and the text around it, and takes the value as written even if it was a
// variable. A new one goes after the header's last opcode, laid out as that
// one is.
func (h *Header) Set(name, value string) {
if n, ok := h.Get(name); ok {
n.Raw = name + "=" + value
n.Value = strings.TrimSpace(value)
h.File.reposition()
return
}
start, end := h.span()
at := start
sep := ""
for i := start; i < end; i++ {
if h.File.Nodes[i].Kind == KindOpcode {
at = i + 1
}
}
if at == start {
// With no opcode to follow, the new one goes after the header, on its
// own line if the header is.
sep = " "
if start < len(h.File.Nodes) && h.File.Nodes[start].Kind == KindSpace && strings.Contains(h.File.Nodes[start].Raw, "\n") {
sep = h.File.Nodes[start].Raw
} else if start >= len(h.File.Nodes) {
sep = "\n"
}
} else {
sep = " "
if i := at - 2; i >= start && h.File.Nodes[i].Kind == KindSpace {
sep = h.File.Nodes[i].Raw
} else if strings.Contains(h.File.String(), "\n") {
sep = "\n"
}
}
nodes := []*Node{
{Kind: KindSpace, Raw: sep},
{Kind: KindOpcode, Raw: name + "=" + value, Name: name, Value: strings.TrimSpace(value)},
}
h.File.insert(at, nodes...)
}
// Delete removes the opcode, every time the header has it, with the space
// before it.
func (h *Header) Delete(name string) {
for {
n, ok := h.Get(name)
if !ok {
return
}
i := indexOf(h.File.Nodes, n)
from := i
if i > 0 && h.File.Nodes[i-1].Kind == KindSpace {
from = i - 1
}
h.File.Nodes = append(h.File.Nodes[:from], h.File.Nodes[i+1:]...)
h.File.reposition()
}
}
func indexOf(nodes []*Node, n *Node) int {
for i, m := range nodes {
if m == n {
return i
}
}
return -1
}
// insert adds nodes at an index.
func (f *File) insert(at int, nodes ...*Node) {
f.Nodes = append(f.Nodes[:at], append(nodes, f.Nodes[at:]...)...)
f.reposition()
}
// reposition sets the line and column of each node from the text before it.
func (f *File) reposition() {
line, col := 1, 1
for _, n := range f.Nodes {
n.Line, n.Col = line, col
for i := 0; i < len(n.Raw); i++ {
if n.Raw[i] == '\n' {
line++
col = 1
} else {
col++
}
}
}
}
// Headers returns the headers of every file of the document, in the order the
// files are read.
func (d *Document) Headers() []*Header {
var out []*Header
for _, f := range d.Files() {
for _, n := range f.Nodes {
if n.Kind == KindHeader {
out = append(out, &Header{File: f, Node: n})
}
}
}
return out
}
// AddHeader adds an empty header on a line of its own after another header and
// its opcodes, or at the end of the main file if after is nil, and returns it.
func (d *Document) AddHeader(name string, after *Header) *Header {
f := d.Root
at := len(f.Nodes)
if after != nil {
f = after.File
_, at = after.span()
}
var nodes []*Node
if at > 0 && !strings.HasSuffix(f.Nodes[at-1].Raw, "\n") {
nodes = append(nodes, &Node{Kind: KindSpace, Raw: "\n"})
}
h := &Node{Kind: KindHeader, Raw: "<" + name + ">", Name: name}
nodes = append(nodes, h, &Node{Kind: KindSpace, Raw: "\n"})
f.insert(at, nodes...)
return &Header{File: f, Node: h}
}
// Defines returns the #define nodes of every file, in order.
func (d *Document) Defines() []*Node { return d.nodes(KindDefine) }
// Includes returns the #include nodes of every file, in order.
func (d *Document) Includes() []*Node { return d.nodes(KindInclude) }
func (d *Document) nodes(kind NodeKind) []*Node {
var out []*Node
for _, f := range d.Files() {
for _, n := range f.Nodes {
if n.Kind == kind {
out = append(out, n)
}
}
}
return out
}
// SetDefine gives a variable a new value, by changing its first #define, so
// that each opcode that uses it changes with it. Setting an opcode that uses
// the variable with [Header.Set] replaces the variable there instead. A
// variable with no #define gets one at the start of the main file. The name
// includes its $.
func (d *Document) SetDefine(name, value string) {
for _, n := range d.Defines() {
if n.Name == name {
n.Raw = "#define " + name + " " + value
n.Value = value
d.reposition()
return
}
}
d.Root.insert(0,
&Node{Kind: KindDefine, Raw: "#define " + name + " " + value, Name: name, Value: value},
&Node{Kind: KindSpace, Raw: "\n"})
}
// reposition sets the positions of every file's nodes.
func (d *Document) reposition() {
for _, f := range d.Files() {
f.reposition()
}
}