~bigbes/sr-ht-spec

ref: b6931bdad118348d1e174299d960b1d0a6ee2189 sr-ht-spec/prosediff/render.go -rw-r--r-- 5.3 KiB
b6931bda — Eugene Blikh build: package spec.sr.ht as an apk and wire push->build->mirror 26 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package prosediff

import (
	"fmt"
	"strings"
	"unicode/utf8"
)

// RenderOptions controls the plain-text renderer. It exists for tests and for
// reading a diff in a terminal; the web layer walks Diff.Changes itself.
type RenderOptions struct {
	// Width wraps rendered prose. 0 means do not wrap.
	Width int
	// ShowEqual prints unchanged blocks too, instead of collapsing them.
	ShowEqual bool
	// Context is how many unchanged blocks to keep around a change when
	// ShowEqual is false. 0 keeps none.
	Context int
	// Markers are the inline delete/insert brackets. Empty uses the
	// defaults "[-", "-]", "{+", "+}".
	DelOpen, DelClose, InsOpen, InsClose string
}

// DefaultRenderOptions wraps at 80 columns and hides unchanged blocks.
func DefaultRenderOptions() RenderOptions {
	return RenderOptions{Width: 80, Context: 0}
}

func (o RenderOptions) markers() (string, string, string, string) {
	d0, d1, i0, i1 := o.DelOpen, o.DelClose, o.InsOpen, o.InsClose
	if d0 == "" && d1 == "" && i0 == "" && i1 == "" {
		return "[-", "-]", "{+", "+}"
	}
	return d0, d1, i0, i1
}

// RenderText renders a diff as plain text.
func RenderText(d *Diff, opts RenderOptions) string {
	var sb strings.Builder
	keep := visible(d.Changes, opts)

	lastPath := "\x00"
	skipped := 0
	flushSkipped := func() {
		if skipped > 0 {
			fmt.Fprintf(&sb, "  … %d unchanged block(s)\n", skipped)
			skipped = 0
		}
	}

	for i, c := range d.Changes {
		if !keep[i] {
			if c.Kind == ChangeEqual {
				skipped++
			}
			continue
		}
		flushSkipped()
		b := c.New
		if b == nil {
			b = c.Old
		}
		if p := strings.Join(b.HeadingPath, " › "); p != lastPath {
			if p == "" {
				p = "(document preamble)"
			}
			fmt.Fprintf(&sb, "\n@@ %s @@\n", p)
			lastPath = p
		}
		sb.WriteString(renderChange(c, opts))
	}
	flushSkipped()
	return sb.String()
}

func visible(changes []BlockChange, opts RenderOptions) []bool {
	keep := make([]bool, len(changes))
	for i, c := range changes {
		if c.Kind != ChangeEqual || opts.ShowEqual {
			keep[i] = true
		}
	}
	if opts.Context > 0 && !opts.ShowEqual {
		orig := append([]bool(nil), keep...)
		for i := range changes {
			if !orig[i] {
				continue
			}
			for j := i - opts.Context; j <= i+opts.Context; j++ {
				if j >= 0 && j < len(keep) {
					keep[j] = true
				}
			}
		}
	}
	return keep
}

func renderChange(c BlockChange, opts RenderOptions) string {
	var sb strings.Builder
	d0, d1, i0, i1 := opts.markers()

	switch c.Kind {
	case ChangeEqual:
		fmt.Fprintf(&sb, "  %s %s\n", loc(c.Old, c.New), c.New.Label())
		writeBody(&sb, "  ", c.New.Text, opts.Width, c.New.Kind.Prose())

	case ChangeInsert:
		fmt.Fprintf(&sb, "+ %s %s\n", loc(nil, c.New), c.New.Label())
		writeBody(&sb, "+ ", c.New.Text, opts.Width, c.New.Kind.Prose())

	case ChangeDelete:
		fmt.Fprintf(&sb, "- %s %s\n", loc(c.Old, nil), c.Old.Label())
		writeBody(&sb, "- ", c.Old.Text, opts.Width, c.Old.Kind.Prose())

	case ChangeMoveOut:
		fmt.Fprintf(&sb, "< %s %s moved away (now line %d)\n",
			loc(c.Old, nil), c.Old.Label(), c.New.StartLine)

	case ChangeMoveIn:
		fmt.Fprintf(&sb, "> %s %s moved here (was line %d)\n",
			loc(nil, c.New), c.New.Label(), c.Old.StartLine)

	case ChangeModify:
		note := ""
		if c.StructureOnly {
			note = fmt.Sprintf(" (structure only: %s → %s)", c.Old.Label(), c.New.Label())
		}
		if c.Moved {
			note += fmt.Sprintf(" (moved from line %d)", c.Old.StartLine)
		}
		fmt.Fprintf(&sb, "~ %s %s modified%s\n", loc(c.Old, c.New), c.New.Label(), note)
		if len(c.Lines) > 0 {
			for _, s := range c.Lines {
				switch s.Op {
				case OpEqual:
					fmt.Fprintf(&sb, "    %s\n", s.Text)
				case OpDelete:
					fmt.Fprintf(&sb, "  - %s\n", s.Text)
				case OpInsert:
					fmt.Fprintf(&sb, "  + %s\n", s.Text)
				}
			}
			break
		}
		var body strings.Builder
		for _, s := range c.Words {
			if s.Space && body.Len() > 0 {
				body.WriteByte(' ')
			}
			switch s.Op {
			case OpEqual:
				body.WriteString(s.Text)
			case OpDelete:
				body.WriteString(d0 + s.Text + d1)
			case OpInsert:
				body.WriteString(i0 + s.Text + i1)
			}
		}
		writeBody(&sb, "  ", body.String(), opts.Width, true)
	}
	return sb.String()
}

func loc(old, nw *Block) string {
	switch {
	case old != nil && nw != nil:
		if old.StartLine == nw.StartLine {
			return fmt.Sprintf("L%d", old.StartLine)
		}
		return fmt.Sprintf("L%d→%d", old.StartLine, nw.StartLine)
	case old != nil:
		return fmt.Sprintf("L%d", old.StartLine)
	case nw != nil:
		return fmt.Sprintf("L%d", nw.StartLine)
	}
	return "L?"
}

func writeBody(sb *strings.Builder, prefix, body string, width int, reflow bool) {
	var lines []string
	if reflow {
		lines = wrap(body, width-len(prefix))
	} else {
		lines = strings.Split(body, "\n")
	}
	for _, line := range lines {
		sb.WriteString(prefix)
		sb.WriteString(line)
		sb.WriteByte('\n')
	}
}

// wrap reflows prose to width columns. Prose is always rewrapped: a diff that
// preserved the source wrapping would put the reviewer back where a line
// differ left them.
func wrap(s string, width int) []string {
	fields := strings.Fields(s)
	if len(fields) == 0 {
		return []string{""}
	}
	if width <= 0 {
		return []string{strings.Join(fields, " ")}
	}
	var out []string
	line := fields[0]
	for _, f := range fields[1:] {
		if utf8.RuneCountInString(line)+1+utf8.RuneCountInString(f) > width {
			out = append(out, line)
			line = f
			continue
		}
		line += " " + f
	}
	return append(out, line)
}