Put draws and its methods in one file
This commit is contained in:
@@ -255,6 +255,12 @@ func checkNoRepeatedRead(format string, c formatOps, refs map[string]refBinding)
|
||||
})
|
||||
}
|
||||
|
||||
// tablePin is one table's pinned row.
|
||||
type tablePin struct {
|
||||
t *table
|
||||
row int
|
||||
}
|
||||
|
||||
// draws is what has already been drawn for held names: the variant each was drawn as,
|
||||
// so every path under it reads one row, and the draw each read made, by its one
|
||||
// spelling, so the same read written twice reads one draw. An expansion keeps one for
|
||||
@@ -274,6 +280,129 @@ type draw struct {
|
||||
null bool
|
||||
}
|
||||
|
||||
// pinned is the row the render pinned for t, if any.
|
||||
func (d *draws) pinned(t *table) (int, bool) {
|
||||
for _, p := range d.pins[:d.npins] {
|
||||
if p.t == t {
|
||||
return p.row, true
|
||||
}
|
||||
}
|
||||
r, ok := d.more[t]
|
||||
return r, ok
|
||||
}
|
||||
|
||||
// mustRow is the row pinned for t, which the walk reaching a column pinned.
|
||||
func (d *draws) mustRow(t *table) int {
|
||||
r, ok := d.pinned(t)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("fejkdata: a column of %s is rendered with no row pinned", t.category))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// pin pins row r of t, and the rows of t's ancestors it links to.
|
||||
func (d *draws) pin(t *table, r int) {
|
||||
for {
|
||||
if _, done := d.pinned(t); done {
|
||||
return
|
||||
}
|
||||
if d.npins < len(d.pins) {
|
||||
d.pins[d.npins] = tablePin{t, r}
|
||||
d.npins++
|
||||
} else {
|
||||
if d.more == nil {
|
||||
d.more = map[*table]int{}
|
||||
}
|
||||
d.more[t] = r
|
||||
}
|
||||
if t.parentT == nil {
|
||||
return
|
||||
}
|
||||
t, r = t.parentT, t.parentRow(r)
|
||||
}
|
||||
}
|
||||
|
||||
// each calls fn for every pinned row, in pin order, the spilled ones by table name.
|
||||
func (d *draws) each(fn func(t *table, r int)) {
|
||||
for _, p := range d.pins[:d.npins] {
|
||||
fn(p.t, p.row)
|
||||
}
|
||||
spilled := make([]*table, 0, len(d.more))
|
||||
for t := range d.more {
|
||||
spilled = append(spilled, t)
|
||||
}
|
||||
sort.Slice(spilled, func(i, j int) bool { return spilled[i].category < spilled[j].category })
|
||||
for _, t := range spilled {
|
||||
fn(t, d.more[t])
|
||||
}
|
||||
}
|
||||
|
||||
// rowOf is the render's row of t: the one pinned, else one drawn inside the
|
||||
// nearest pinned ancestor — its parent drawn inside that first where the ancestor
|
||||
// is further up — or over the whole table, and pinned with its ancestors.
|
||||
func (d *draws) rowOf(t *table) int {
|
||||
if r, ok := d.pinned(t); ok {
|
||||
return r
|
||||
}
|
||||
r := -1
|
||||
for a := t.parentT; a != nil && r < 0; a = a.parentT {
|
||||
if _, ok := d.pinned(a); !ok {
|
||||
continue
|
||||
}
|
||||
if t.parentT != a {
|
||||
d.rowOf(t.parentT)
|
||||
}
|
||||
pr, _ := d.pinned(t.parentT)
|
||||
r = t.drawUnder(d.s, pr)
|
||||
}
|
||||
if r < 0 {
|
||||
r = t.draw(d.s)
|
||||
}
|
||||
d.pin(t, r)
|
||||
return r
|
||||
}
|
||||
|
||||
// pinRow pins row r of t where it agrees with the rows pinned before it.
|
||||
func (d *draws) pinRow(t *table, r int) error {
|
||||
if pr, ok := d.pinned(t); ok && pr != r {
|
||||
return fmt.Errorf("%s and %s are two rows of %s", t.selectorSpelling(pr), t.selectorSpelling(r), t.path)
|
||||
}
|
||||
for a := t.parentT; a != nil; a = a.parentT {
|
||||
if pa, ok := d.pinned(a); ok && !t.under(r, a, pa) {
|
||||
return fmt.Errorf("%s is not inside %s", t.selectorSpelling(r), a.selectorSpelling(pa))
|
||||
}
|
||||
}
|
||||
d.pin(t, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
// selectRow pins the row a selector names.
|
||||
func (d *draws) selectRow(t *table, sel string) error {
|
||||
r, err := t.find(sel, d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.pinRow(t, r)
|
||||
}
|
||||
|
||||
// inside keeps the rows of t that sit inside every pinned ancestor.
|
||||
func (d *draws) inside(t *table, rows []int) []int {
|
||||
for a := t.parentT; a != nil; a = a.parentT {
|
||||
pa, ok := d.pinned(a)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var kept []int
|
||||
for _, r := range rows {
|
||||
if t.under(r, a, pa) {
|
||||
kept = append(kept, r)
|
||||
}
|
||||
}
|
||||
rows = kept
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// readField renders one arm of a token. An arm's key is a sibling field or a
|
||||
// reference linkRefs bound into fields. A name the expansion holds — a level some
|
||||
// token addresses by dotted path, or a field an operand reads — is drawn once and
|
||||
|
||||
Reference in New Issue
Block a user