aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gotest.tools/internal/format/diff.go
blob: c938c97bec147c1e6532117a8254cc51ab813944 (plain) (blame)
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
package format

import (
	"bytes"
	"fmt"
	"strings"
	"unicode"

	"gotest.tools/internal/difflib"
)

const (
	contextLines = 2
)

// DiffConfig for a unified diff
type DiffConfig struct {
	A    string
	B    string
	From string
	To   string
}

// UnifiedDiff is a modified version of difflib.WriteUnifiedDiff with better
// support for showing the whitespace differences.
func UnifiedDiff(conf DiffConfig) string {
	a := strings.SplitAfter(conf.A, "\n")
	b := strings.SplitAfter(conf.B, "\n")
	groups := difflib.NewMatcher(a, b).GetGroupedOpCodes(contextLines)
	if len(groups) == 0 {
		return ""
	}

	buf := new(bytes.Buffer)
	writeFormat := func(format string, args ...interface{}) {
		buf.WriteString(fmt.Sprintf(format, args...))
	}
	writeLine := func(prefix string, s string) {
		buf.WriteString(prefix + s)
	}
	if hasWhitespaceDiffLines(groups, a, b) {
		writeLine = visibleWhitespaceLine(writeLine)
	}
	formatHeader(writeFormat, conf)
	for _, group := range groups {
		formatRangeLine(writeFormat, group)
		for _, opCode := range group {
			in, out := a[opCode.I1:opCode.I2], b[opCode.J1:opCode.J2]
			switch opCode.Tag {
			case 'e':
				formatLines(writeLine, " ", in)
			case 'r':
				formatLines(writeLine, "-", in)
				formatLines(writeLine, "+", out)
			case 'd':
				formatLines(writeLine, "-", in)
			case 'i':
				formatLines(writeLine, "+", out)
			}
		}
	}
	return buf.String()
}

// hasWhitespaceDiffLines returns true if any diff groups is only different
// because of whitespace characters.
func hasWhitespaceDiffLines(groups [][]difflib.OpCode, a, b []string) bool {
	for _, group := range groups {
		in, out := new(bytes.Buffer), new(bytes.Buffer)
		for _, opCode := range group {
			if opCode.Tag == 'e' {
				continue
			}
			for _, line := range a[opCode.I1:opCode.I2] {
				in.WriteString(line)
			}
			for _, line := range b[opCode.J1:opCode.J2] {
				out.WriteString(line)
			}
		}
		if removeWhitespace(in.String()) == removeWhitespace(out.String()) {
			return true
		}
	}
	return false
}

func removeWhitespace(s string) string {
	var result []rune
	for _, r := range s {
		if !unicode.IsSpace(r) {
			result = append(result, r)
		}
	}
	return string(result)
}

func visibleWhitespaceLine(ws func(string, string)) func(string, string) {
	mapToVisibleSpace := func(r rune) rune {
		switch r {
		case '\n':
		case ' ':
			return '·'
		case '\t':
			return '▷'
		case '\v':
			return '▽'
		case '\r':
			return '↵'
		case '\f':
			return '↓'
		default:
			if unicode.IsSpace(r) {
				return '�'
			}
		}
		return r
	}
	return func(prefix, s string) {
		ws(prefix, strings.Map(mapToVisibleSpace, s))
	}
}

func formatHeader(wf func(string, ...interface{}), conf DiffConfig) {
	if conf.From != "" || conf.To != "" {
		wf("--- %s\n", conf.From)
		wf("+++ %s\n", conf.To)
	}
}

func formatRangeLine(wf func(string, ...interface{}), group []difflib.OpCode) {
	first, last := group[0], group[len(group)-1]
	range1 := formatRangeUnified(first.I1, last.I2)
	range2 := formatRangeUnified(first.J1, last.J2)
	wf("@@ -%s +%s @@\n", range1, range2)
}

// Convert range to the "ed" format
func formatRangeUnified(start, stop int) string {
	// Per the diff spec at http://www.unix.org/single_unix_specification/
	beginning := start + 1 // lines start numbering with one
	length := stop - start
	if length == 1 {
		return fmt.Sprintf("%d", beginning)
	}
	if length == 0 {
		beginning-- // empty ranges begin at line just before the range
	}
	return fmt.Sprintf("%d,%d", beginning, length)
}

func formatLines(writeLine func(string, string), prefix string, lines []string) {
	for _, line := range lines {
		writeLine(prefix, line)
	}
	// Add a newline if the last line is missing one so that the diff displays
	// properly.
	if !strings.HasSuffix(lines[len(lines)-1], "\n") {
		writeLine("", "\n")
	}
}