Skip to content

Commit eff7f6d

Browse files
committedAug 8, 2019
Add OptSuppressCommon
Add option that supresses common lines.
1 parent fe34798 commit eff7f6d

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed
 

‎diff.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,29 @@ import (
1010
"strings"
1111
)
1212

13+
type config struct {
14+
suppressCommon bool
15+
}
16+
17+
type option func(*config)
18+
19+
// OptSuppressCommon suppresses common lines.
20+
func OptSuppressCommon() option {
21+
return func(c *config) {
22+
c.suppressCommon = true
23+
}
24+
}
25+
1326
// Format returns a formatted diff of the two texts,
1427
// showing the entire text and the minimum line-level
1528
// additions and removals to turn text1 into text2.
1629
// (That is, lines only in text1 appear with a leading -,
1730
// and lines only in text2 appear with a leading +.)
18-
func Format(text1, text2 string) string {
31+
func Format(text1, text2 string, options ...option) string {
32+
var c config
33+
for _, option := range options {
34+
option(&c)
35+
}
1936
if text1 != "" && !strings.HasSuffix(text1, "\n") {
2037
text1 += "(missing final newline)"
2138
}
@@ -64,14 +81,25 @@ func Format(text1, text2 string) string {
6481
for i > 0 || j > 0 {
6582
cost := dist[i][j]
6683
if i > 0 && j > 0 && cost == dist[i-1][j-1] && lines1[len(lines1)-i] == lines2[len(lines2)-j] {
67-
fmt.Fprintf(&buf, " %s\n", lines1[len(lines1)-i])
84+
if !c.suppressCommon {
85+
k := len(lines1) - i
86+
fmt.Fprintf(&buf, " %s\n", lines1[k])
87+
}
6888
i--
6989
j--
7090
} else if i > 0 && cost == dist[i-1][j]+1 {
71-
fmt.Fprintf(&buf, "-%s\n", lines1[len(lines1)-i])
91+
k := len(lines1) - i
92+
if c.suppressCommon {
93+
fmt.Fprint(&buf, k+1)
94+
}
95+
fmt.Fprintf(&buf, "-%s\n", lines1[k])
7296
i--
7397
} else {
74-
fmt.Fprintf(&buf, "+%s\n", lines2[len(lines2)-j])
98+
k := len(lines2) - j
99+
if c.suppressCommon {
100+
fmt.Fprint(&buf, k+1)
101+
}
102+
fmt.Fprintf(&buf, "+%s\n", lines2[k])
75103
j--
76104
}
77105
}

‎diff_test.go

+30-15
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import (
1010
)
1111

1212
var formatTests = []struct {
13-
text1 string
14-
text2 string
15-
diff string
13+
text1 string
14+
text2 string
15+
diff string
16+
suppressCommon string
1617
}{
17-
{"a b c", "a b d e f", "a b -c +d +e +f"},
18-
{"", "a b c", "+a +b +c"},
19-
{"a b c", "", "-a -b -c"},
20-
{"a b c", "d e f", "-a -b -c +d +e +f"},
21-
{"a b c d e f", "a b d e f", "a b -c d e f"},
22-
{"a b c e f", "a b c d e f", "a b c +d e f"},
18+
{"a b c", "a b d e f", "a b -c +d +e +f", "3-c 3+d 4+e 5+f"},
19+
{"", "a b c", "+a +b +c", "1+a 2+b 3+c"},
20+
{"a b c", "", "-a -b -c", "1-a 2-b 3-c"},
21+
{"a b c", "d e f", "-a -b -c +d +e +f", "1-a 2-b 3-c 1+d 2+e 3+f"},
22+
{"a b c d e f", "a b d e f", "a b -c d e f", "3-c"},
23+
{"a b c e f", "a b c d e f", "a b c +d e f", "4+d"},
2324
}
2425

2526
func TestFormat(t *testing.T) {
@@ -33,11 +34,25 @@ func TestFormat(t *testing.T) {
3334
if text2 != "" {
3435
text2 += "\n"
3536
}
36-
out := Format(text1, text2)
37-
// Cut final \n, cut spaces, turn remaining \n into spaces.
38-
out = strings.ReplaceAll(strings.ReplaceAll(strings.TrimSuffix(out, "\n"), " ", ""), "\n", " ")
39-
if out != tt.diff {
40-
t.Errorf("diff(%q, %q) = %q, want %q", text1, text2, out, tt.diff)
41-
}
37+
compare(t, format, text1, text2, tt.diff)
38+
compare(t, suppressCommon, text1, text2, tt.suppressCommon)
39+
}
40+
}
41+
42+
func format(text1, text2 string) string {
43+
return Format(text1, text2)
44+
}
45+
46+
func suppressCommon(text1, text2 string) string {
47+
return Format(text1, text2, OptSuppressCommon())
48+
}
49+
50+
func compare(t *testing.T, testFn func(string, string) string, text1, text2 string, want string) {
51+
t.Helper()
52+
got := testFn(text1, text2)
53+
// Cut final \n, cut spaces, turn remaining \n into spaces.
54+
got = strings.ReplaceAll(strings.ReplaceAll(strings.TrimSuffix(got, "\n"), " ", ""), "\n", " ")
55+
if got != want {
56+
t.Errorf("diff(%q, %q) = %q, want %q", text1, text2, got, want)
4257
}
4358
}

0 commit comments

Comments
 (0)