MCPcopy Create free account
hub / github.com/cogentcore/core / WriteContextDiff

Function WriteContextDiff

texteditor/difflib/bytes/bytes.go:1025–1111  ·  view source on GitHub ↗

WriteContextDiff compare two sequences of lines and generates the delta as a context diff. Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by diff.Context which defaults to three. By default, the diff control lines (those with

(writer io.Writer, diff ContextDiff)

Source from the content-addressed store, hash-verified

1023// The modification times are normally expressed in the ISO 8601 format.
1024// If not specified, the strings default to blanks.
1025func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
1026 buf := bufio.NewWriter(writer)
1027 defer buf.Flush()
1028 var diffErr error
1029 wf := func(format string, args ...interface{}) {
1030 _, err := buf.WriteString(fmt.Sprintf(format, args...))
1031 if diffErr == nil && err != nil {
1032 diffErr = err
1033 }
1034 }
1035 ws := func(s []byte) {
1036 _, err := buf.Write(s)
1037 if diffErr == nil && err != nil {
1038 diffErr = err
1039 }
1040 }
1041
1042 if len(diff.Eol) == 0 {
1043 diff.Eol = []byte("\n")
1044 }
1045
1046 prefix := map[byte][]byte{
1047 'i': []byte("+ "),
1048 'd': []byte("- "),
1049 'r': []byte("! "),
1050 'e': []byte(" "),
1051 }
1052
1053 started := false
1054 m := NewMatcher(diff.A, diff.B)
1055 for _, g := range m.GetGroupedOpCodes(diff.Context) {
1056 if !started {
1057 started = true
1058 fromDate := ""
1059 if len(diff.FromDate) > 0 {
1060 fromDate = "\t" + diff.FromDate
1061 }
1062 toDate := ""
1063 if len(diff.ToDate) > 0 {
1064 toDate = "\t" + diff.ToDate
1065 }
1066 if diff.FromFile != "" || diff.ToFile != "" {
1067 wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
1068 wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
1069 }
1070 }
1071
1072 first, last := g[0], g[len(g)-1]
1073 ws([]byte("***************"))
1074 ws(diff.Eol)
1075
1076 range1 := formatRangeContext(first.I1, last.I2)
1077 wf("*** %s ****%s", range1, diff.Eol)
1078 for _, c := range g {
1079 if c.Tag == 'r' || c.Tag == 'd' {
1080 for _, cc := range g {
1081 if cc.Tag == 'i' {
1082 continue

Callers 1

GetContextDiffStringFunction · 0.70

Calls 6

WriteStringMethod · 0.80
NewMatcherFunction · 0.70
formatRangeContextFunction · 0.70
WriteMethod · 0.65
FlushMethod · 0.45
GetGroupedOpCodesMethod · 0.45

Tested by

no test coverage detected