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

Function WriteContextDiff

texteditor/difflib/difflib.go:944–1030  ·  view source on GitHub ↗

WriteContextDiff compares 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 LineDiffParams)

Source from the content-addressed store, hash-verified

942// The modification times are normally expressed in the ISO 8601 format.
943// If not specified, the strings default to blanks.
944func WriteContextDiff(writer io.Writer, diff LineDiffParams) error {
945 buf := bufio.NewWriter(writer)
946 defer buf.Flush()
947 var diffErr error
948 wf := func(format string, args ...any) {
949 _, err := buf.WriteString(fmt.Sprintf(format, args...))
950 if diffErr == nil && err != nil {
951 diffErr = err
952 }
953 }
954 ws := func(s string) {
955 _, err := buf.WriteString(s)
956 if diffErr == nil && err != nil {
957 diffErr = err
958 }
959 }
960
961 if len(diff.Eol) == 0 {
962 diff.Eol = "\n"
963 }
964
965 prefix := map[byte]string{
966 'i': "+ ",
967 'd': "- ",
968 'r': "! ",
969 'e': " ",
970 }
971
972 started := false
973 m := NewMatcher(diff.A, diff.B)
974 if diff.AutoJunk || diff.IsJunkLine != nil {
975 m = NewMatcherWithJunk(diff.A, diff.B, diff.AutoJunk, diff.IsJunkLine)
976 }
977 for _, g := range m.GetGroupedOpCodes(diff.Context) {
978 if !started {
979 started = true
980 fromDate := ""
981 if len(diff.FromDate) > 0 {
982 fromDate = "\t" + diff.FromDate
983 }
984 toDate := ""
985 if len(diff.ToDate) > 0 {
986 toDate = "\t" + diff.ToDate
987 }
988 if diff.FromFile != "" || diff.ToFile != "" {
989 wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
990 wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
991 }
992 }
993
994 first, last := g[0], g[len(g)-1]
995 ws("***************" + diff.Eol)
996
997 range1 := formatRangeContext(first.I1, last.I2)
998 wf("*** %s ****%s", range1, diff.Eol)
999 for _, c := range g {
1000 if c.Tag == 'r' || c.Tag == 'd' {
1001 for _, cc := range g {

Callers 1

GetContextDiffStringFunction · 0.70

Calls 6

WriteStringMethod · 0.80
NewMatcherFunction · 0.70
NewMatcherWithJunkFunction · 0.70
formatRangeContextFunction · 0.70
FlushMethod · 0.45
GetGroupedOpCodesMethod · 0.45

Tested by

no test coverage detected