notation/fprint.go

380 lines
6.8 KiB
Go
Raw Normal View History

2020-10-22 20:55:32 +02:00
package notation
2020-11-26 14:27:42 +01:00
import (
"fmt"
"strings"
)
func ifZero(a, b int) int {
if a == 0 {
return b
}
2020-11-26 14:27:42 +01:00
return a
2020-10-22 20:55:32 +02:00
}
2020-11-26 14:27:42 +01:00
func max(a, b int) int {
if a > b {
return a
}
2020-11-26 14:27:42 +01:00
return b
}
2020-11-26 14:27:42 +01:00
func strLen(s str) str {
l := strings.Split(s.raw, "\n")
for j, li := range l {
if j == 0 {
s.rawLen.first = len(li)
}
if len(li) > s.rawLen.max {
s.rawLen.max = len(li)
}
if j == len(l)-1 {
s.rawLen.last = len(li)
}
2020-10-22 20:55:32 +02:00
}
2020-11-26 14:27:42 +01:00
return s
2020-10-22 20:55:32 +02:00
}
func nodeLen(t int, n node) node {
2020-11-26 14:27:42 +01:00
// We assume here that an str is always contained
// by a node that has only a single str.
//
if s, ok := n.parts[0].(str); ok {
s = strLen(s)
n.parts[0] = s
n.len = len(s.val)
if s.raw == "" {
wl := wrapLen{
first: len(s.val),
max: len(s.val),
last: len(s.val),
}
2020-11-26 14:27:42 +01:00
n.wrapLen = wl
n.fullWrap = wl
return n
}
n.wrapLen = s.rawLen
n.fullWrap = s.rawLen
return n
}
2020-11-26 16:02:43 +01:00
// measure all parts:
2020-11-26 14:27:42 +01:00
for i := range n.parts {
switch pt := n.parts[i].(type) {
case node:
n.parts[i] = nodeLen(t, pt)
case wrapper:
for j := range pt.items {
pt.items[j] = nodeLen(t, pt.items[j])
}
2020-11-26 14:27:42 +01:00
}
}
2020-11-26 16:02:43 +01:00
// measure the unwrapped length:
2020-11-26 14:27:42 +01:00
for _, p := range n.parts {
switch pt := p.(type) {
2020-10-22 20:55:32 +02:00
case node:
2020-11-26 14:27:42 +01:00
n.len += pt.len
case wrapper:
if len(pt.items) == 0 {
continue
}
2020-10-22 20:55:32 +02:00
2020-11-26 14:27:42 +01:00
n.len += (len(pt.items) - 1) * len(pt.sep)
for _, pti := range pt.items {
n.len += pti.len
}
default:
n.len += len(fmt.Sprint(p))
}
}
2020-11-26 16:02:43 +01:00
// measure the wrapped and the fully wrapped length:
2020-11-26 14:27:42 +01:00
var w, f int
for _, p := range n.parts {
switch pt := p.(type) {
case node:
w += pt.wrapLen.first
if pt.len != pt.wrapLen.first {
n.wrapLen.first = ifZero(n.wrapLen.first, w)
n.wrapLen.max = max(n.wrapLen.max, w)
n.wrapLen.max = max(n.wrapLen.max, pt.wrapLen.max)
w = pt.wrapLen.last
2020-10-22 20:55:32 +02:00
}
2020-11-26 14:27:42 +01:00
f += pt.fullWrap.first
if pt.len != pt.fullWrap.first {
n.fullWrap.first = ifZero(n.fullWrap.first, f)
n.fullWrap.max = max(n.fullWrap.max, f)
n.fullWrap.max = max(n.fullWrap.max, pt.fullWrap.max)
f = pt.fullWrap.last
}
2020-10-22 20:55:32 +02:00
case wrapper:
2020-11-26 14:27:42 +01:00
if len(pt.items) == 0 {
2020-10-22 20:55:32 +02:00
continue
}
2020-11-26 14:27:42 +01:00
n.wrapLen.first = ifZero(n.wrapLen.first, w)
n.wrapLen.max = max(n.wrapLen.max, w)
n.fullWrap.first = ifZero(n.fullWrap.first, f)
n.fullWrap.max = max(n.fullWrap.max, f)
w = 0
f = 0
switch pt.mode {
case line:
// line wrapping is flexible, here
// we measure the longest case
//
w = (len(pt.items) - 1) * len(pt.sep)
for _, pti := range pt.items {
w += pti.len
}
// here me measure the shortest
// possible case
//
for _, pti := range pt.items {
f = max(f, pti.fullWrap.max)
}
default:
// for non-full wrap, we measure the full
// length of the items
//
for _, pti := range pt.items {
w = max(w, t+pti.len+len(pt.suffix))
}
2020-10-22 20:55:32 +02:00
2020-11-26 14:27:42 +01:00
// for full wrap, we measure the fully
// wrapped length of the items
//
for _, pti := range pt.items {
f = max(f, t+pti.fullWrap.max)
f = max(f, t+pti.fullWrap.last+len(pt.suffix))
2020-10-22 20:55:32 +02:00
}
}
2020-11-26 14:27:42 +01:00
n.wrapLen.max = max(n.wrapLen.max, w)
n.fullWrap.max = max(n.fullWrap.max, f)
w = 0
f = 0
default:
w += len(fmt.Sprint(p))
f += len(fmt.Sprint(p))
2020-10-22 20:55:32 +02:00
}
}
2020-11-26 14:27:42 +01:00
n.wrapLen.first = ifZero(n.wrapLen.first, w)
n.wrapLen.max = max(n.wrapLen.max, w)
n.wrapLen.last = w
2020-11-26 14:27:42 +01:00
n.fullWrap.first = ifZero(n.fullWrap.first, f)
n.fullWrap.max = max(n.fullWrap.max, f)
n.fullWrap.last = f
2020-10-22 20:55:32 +02:00
return n
}
func wrapNode(t, cf0, c0, c1 int, n node) node {
2020-11-26 14:27:42 +01:00
// fits:
if n.len <= c0 {
2020-10-22 20:55:32 +02:00
return n
}
2020-11-26 14:27:42 +01:00
// we don't want to make it longer:
if n.wrapLen.max >= n.len && n.fullWrap.max >= n.len {
2020-10-22 20:55:32 +02:00
return n
}
2020-11-26 14:27:42 +01:00
// tolerate below c1 when it's not worth wrapping:
if n.len <= c1 && n.len-c0 <= c0-n.wrapLen.max {
2020-10-22 20:55:32 +02:00
return n
}
2020-11-26 16:02:43 +01:00
// otherwise, we need to wrap the node:
n.wrap = true
2020-11-26 14:27:42 +01:00
// We assume here that an str is always contained
// by a node that has only a single str.
//
if s, ok := n.parts[0].(str); ok {
s.useRaw = s.raw != ""
n.parts[0] = s
return n
}
2020-11-26 16:02:43 +01:00
// before iterating over the parts, take a copy of
// the available column width and modify only the
// copy, to support trackback.
//
cc0, cc1 := c0, c1
lastWrapperIndex := -1
var trackBack bool
for i := 0; i < len(n.parts); i++ {
p := n.parts[i]
2020-10-22 20:55:32 +02:00
switch part := p.(type) {
case node:
part = wrapNode(t, cf0, cc0, cc1, part)
n.parts[i] = part
if part.wrap {
2020-11-26 14:27:42 +01:00
// This is an approximation: sometimes
// part.fullWrap.last should be applied
// here, but usually those are the same.
2020-11-26 14:27:42 +01:00
//
cc0 -= part.wrapLen.first
cc1 -= part.wrapLen.first
} else {
cc0 -= part.len
cc1 -= part.len
}
2020-11-26 14:27:42 +01:00
if cc1 >= 0 {
if part.wrap {
cc0 = c0 - part.wrapLen.last
cc1 = c1 - part.wrapLen.last
}
continue
}
2020-11-26 14:27:42 +01:00
if trackBack {
continue
}
// trackback from after the last wrapper:
i = lastWrapperIndex
trackBack = true
// force wrapping during trackback:
cc0 = 0
cc1 = 0
2020-10-22 20:55:32 +02:00
case wrapper:
if len(part.items) == 0 {
continue
}
cc0, cc1 = c0, c1
trackBack = false
lastWrapperIndex = i
switch part.mode {
case line:
2020-11-26 14:27:42 +01:00
// we only set the line endings. We use
// the full column width:
//
cl := cf0 - t
var w int
2020-11-26 14:27:42 +01:00
for j, nj := range part.items {
if w > 0 && w+len(part.sep)+nj.len > cl {
part.lineEnds = append(part.lineEnds, j)
w = 0
}
if w > 0 {
w += len(part.sep)
}
2020-11-26 14:27:42 +01:00
w += nj.len
}
part.lineEnds = append(part.lineEnds, len(part.items))
n.parts[i] = part
default:
for j := range part.items {
2020-11-26 14:27:42 +01:00
part.items[j] = wrapNode(t, cf0, c0-t, c1-t, part.items[j])
}
2020-10-22 20:55:32 +02:00
}
2020-11-26 14:27:42 +01:00
default:
s := fmt.Sprint(part)
cc0 -= len(s)
cc1 -= len(s)
if cc1 >= 0 {
continue
}
if trackBack {
continue
}
// trackback from after the last wrapper:
i = lastWrapperIndex
trackBack = true
// force wrapping during trackback:
cc0 = 0
cc1 = 0
2020-10-22 20:55:32 +02:00
}
}
return n
}
func fprint(w *writer, t int, n node) {
if w.err != nil {
return
}
for _, p := range n.parts {
switch part := p.(type) {
case node:
fprint(w, t, part)
2020-10-22 20:55:32 +02:00
case wrapper:
if len(part.items) == 0 {
continue
}
if !n.wrap {
for i, ni := range part.items {
if i > 0 {
w.write(part.sep)
}
fprint(w, t, ni)
}
continue
2020-10-22 20:55:32 +02:00
}
switch part.mode {
case line:
var (
lines [][]node
last int
)
for _, i := range part.lineEnds {
lines = append(lines, part.items[last:i])
last = i
}
for _, line := range lines {
2020-11-26 14:27:42 +01:00
w.line(1)
for i, ni := range line {
if i > 0 {
w.write(part.sep)
}
fprint(w, 0, ni)
}
}
default:
t++
for _, ni := range part.items {
w.line(t)
fprint(w, t, ni)
w.write(part.suffix)
2020-10-22 20:55:32 +02:00
}
t--
2020-10-22 20:55:32 +02:00
}
w.line(t)
2020-10-22 20:55:32 +02:00
default:
w.write(part)
}
}
}