Skip to content

Commit 9897735

Browse files
committed
ctxt: create package
Part of #18.
1 parent f932f2c commit 9897735

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

cmd/pkg-diff-example/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212

1313
"github.com/pkg/diff"
14+
"github.com/pkg/diff/ctxt"
1415
"github.com/pkg/diff/myers"
1516
)
1617

@@ -75,7 +76,7 @@ func main() {
7576
defer cancel()
7677
}
7778
e := myers.Diff(ctx, ab)
78-
e = diff.EditScriptWithContextSize(e, *unified) // limit amount of output context
79+
e = ctxt.Size(e, *unified) // limit amount of output context
7980
opts := []diff.WriteOpt{
8081
diff.Names(aName, bName),
8182
}

context.go renamed to ctxt/size.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
package diff
1+
// Package ctxt provides routines to reduce the amount of context in an edit script.
2+
package ctxt
23

34
import (
4-
"fmt"
5-
65
"github.com/pkg/diff/edit"
76
)
87

9-
// WithContextSize returns an edit script preserving only n common elements of context for changes.
8+
// Size returns an edit script preserving only n common elements of context for changes.
109
// The returned edit script may alias the input.
11-
// If n is negative, WithContextSize panics.
12-
// To generate a "unified diff", use WithContextSize and then WriteUnified the resulting edit script.
13-
func EditScriptWithContextSize(e edit.Script, n int) edit.Script {
10+
// If n is negative, Size panics.
11+
func Size(e edit.Script, n int) edit.Script {
1412
if n < 0 {
15-
panic(fmt.Sprintf("EditScript.WithContextSize called with negative n: %d", n))
13+
panic("ctxt.Size called with negative n")
1614
}
1715

1816
// Handle small scripts.

example_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/pkg/diff"
8+
"github.com/pkg/diff/ctxt"
89
"github.com/pkg/diff/myers"
910
)
1011

@@ -18,7 +19,7 @@ func Example_testHelper() {
1819
if e.IsIdentity() {
1920
return
2021
}
21-
e = diff.EditScriptWithContextSize(e, 1)
22+
e = ctxt.Size(e, 1)
2223
diff.WriteUnified(e, os.Stdout, ab)
2324
// Output:
2425
// --- a

print.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const (
5353
// WriteUnified writes e to w using unified diff format.
5454
// ab writes the individual elements. Opts are optional write arguments.
5555
// WriteUnified returns the number of bytes written and the first error (if any) encountered.
56+
// Before writing, edit scripts usually have their context reduced,
57+
// such as by a call to ctxt.Size.
5658
func WriteUnified(e edit.Script, w io.Writer, ab WriterTo, opts ...WriteOpt) (int, error) {
5759
// read opts
5860
nameA := "a"

unified_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/pkg/diff"
10+
"github.com/pkg/diff/ctxt"
1011
"github.com/pkg/diff/myers"
1112
"github.com/sergi/go-diff/diffmatchpatch"
1213
)
@@ -82,7 +83,7 @@ func TestGolden(t *testing.T) {
8283
// Doing it as I have done, the lazy way, mixes concerns: diff algorithm vs unification algorithm
8384
// vs unified diff formatting.
8485
e := myers.Diff(context.Background(), ab)
85-
e = diff.EditScriptWithContextSize(e, 3)
86+
e = ctxt.Size(e, 3)
8687
buf := new(bytes.Buffer)
8788
diff.WriteUnified(e, buf, ab, test.opts...)
8889
got := buf.String()

0 commit comments

Comments
 (0)