From b347a6a816ada3af6e374a480d13d469979d8c34 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 13 Dec 2023 12:42:58 -0500 Subject: [PATCH 2/2] fix: re-export symbols Signed-off-by: Ayman Bagabas --- unified.go | 122 ++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/unified.go b/unified.go index 85523a1b435a..268ad228825b 100644 --- a/unified.go +++ b/unified.go @@ -41,57 +41,57 @@ func ToUnified(oldLabel, newLabel, content string, edits []Edit, contextLines in // unified represents a set of edits as a unified diff. type unified struct { - // from is the name of the original file. - from string - // to is the name of the modified file. - to string - // hunks is the set of edit hunks needed to transform the file content. - hunks []*hunk + // From is the name of the original file. + From string + // To is the name of the modified file. + To string + // Hunks is the set of edit Hunks needed to transform the file content. + Hunks []*hunk } // Hunk represents a contiguous set of line edits to apply. type hunk struct { // The line in the original source where the hunk starts. - fromLine int + FromLine int // The line in the original source where the hunk finishes. - toLine int + ToLine int // The set of line based edits to apply. - lines []line + Lines []line } // Line represents a single line operation to apply as part of a Hunk. type line struct { - // kind is the type of line this represents, deletion, insertion or copy. - kind opKind - // content is the content of this line. + // Kind is the type of line this represents, deletion, insertion or copy. + Kind OpKind + // Content is the Content of this line. // For deletion it is the line being removed, for all others it is the line // to put in the output. - content string + Content string } -// opKind is used to denote the type of operation a line represents. -type opKind int +// OpKind is used to denote the type of operation a line represents. +type OpKind int const ( - // opDelete is the operation kind for a line that is present in the input + // Delete is the operation kind for a line that is present in the input // but not in the output. - opDelete opKind = iota - // opInsert is the operation kind for a line that is new in the output. - opInsert - // opEqual is the operation kind for a line that is the same in the input and + Delete OpKind = iota + // Insert is the operation kind for a line that is new in the output. + Insert + // Equal is the operation kind for a line that is the same in the input and // output, often used to provide context around edited lines. - opEqual + Equal ) // String returns a human readable representation of an OpKind. It is not // intended for machine processing. -func (k opKind) String() string { +func (k OpKind) String() string { switch k { - case opDelete: + case Delete: return "delete" - case opInsert: + case Insert: return "insert" - case opEqual: + case Equal: return "equal" default: panic("unknown operation kind") @@ -103,8 +103,8 @@ func (k opKind) String() string { func toUnified(fromName, toName string, content string, edits []Edit, contextLines int) (unified, error) { gap := contextLines * 2 u := unified{ - from: fromName, - to: toName, + From: fromName, + To: toName, } if len(edits) == 0 { return u, nil @@ -129,35 +129,35 @@ func toUnified(fromName, toName string, content string, edits []Edit, contextLin switch { case h != nil && start == last: - //direct extension + // direct extension case h != nil && start <= last+gap: - //within range of previous lines, add the joiners + // within range of previous lines, add the joiners addEqualLines(h, lines, last, start) default: - //need to start a new hunk + // need to start a new hunk if h != nil { // add the edge to the previous hunk addEqualLines(h, lines, last, last+contextLines) - u.hunks = append(u.hunks, h) + u.Hunks = append(u.Hunks, h) } toLine += start - last h = &hunk{ - fromLine: start + 1, - toLine: toLine + 1, + FromLine: start + 1, + ToLine: toLine + 1, } // add the edge to the new hunk delta := addEqualLines(h, lines, start-contextLines, start) - h.fromLine -= delta - h.toLine -= delta + h.FromLine -= delta + h.ToLine -= delta } last = start for i := start; i < end; i++ { - h.lines = append(h.lines, line{kind: opDelete, content: lines[i]}) + h.Lines = append(h.Lines, line{Kind: Delete, Content: lines[i]}) last++ } if edit.New != "" { for _, content := range splitLines(edit.New) { - h.lines = append(h.lines, line{kind: opInsert, content: content}) + h.Lines = append(h.Lines, line{Kind: Insert, Content: content}) toLine++ } } @@ -165,7 +165,7 @@ func toUnified(fromName, toName string, content string, edits []Edit, contextLin if h != nil { // add the edge to the final hunk addEqualLines(h, lines, last, last+contextLines) - u.hunks = append(u.hunks, h) + u.Hunks = append(u.Hunks, h) } return u, nil } @@ -187,7 +187,7 @@ func addEqualLines(h *hunk, lines []string, start, end int) int { if i >= len(lines) { return delta } - h.lines = append(h.lines, line{kind: opEqual, content: lines[i]}) + h.Lines = append(h.Lines, line{Kind: Equal, Content: lines[i]}) delta++ } return delta @@ -196,19 +196,19 @@ func addEqualLines(h *hunk, lines []string, start, end int) int { // String converts a unified diff to the standard textual form for that diff. // The output of this function can be passed to tools like patch. func (u unified) String() string { - if len(u.hunks) == 0 { + if len(u.Hunks) == 0 { return "" } b := new(strings.Builder) - fmt.Fprintf(b, "--- %s\n", u.from) - fmt.Fprintf(b, "+++ %s\n", u.to) - for _, hunk := range u.hunks { + fmt.Fprintf(b, "--- %s\n", u.From) + fmt.Fprintf(b, "+++ %s\n", u.To) + for _, hunk := range u.Hunks { fromCount, toCount := 0, 0 - for _, l := range hunk.lines { - switch l.kind { - case opDelete: + for _, l := range hunk.Lines { + switch l.Kind { + case Delete: fromCount++ - case opInsert: + case Insert: toCount++ default: fromCount++ @@ -217,32 +217,32 @@ func (u unified) String() string { } fmt.Fprint(b, "@@") if fromCount > 1 { - fmt.Fprintf(b, " -%d,%d", hunk.fromLine, fromCount) - } else if hunk.fromLine == 1 && fromCount == 0 { + fmt.Fprintf(b, " -%d,%d", hunk.FromLine, fromCount) + } else if hunk.FromLine == 1 && fromCount == 0 { // Match odd GNU diff -u behavior adding to empty file. fmt.Fprintf(b, " -0,0") } else { - fmt.Fprintf(b, " -%d", hunk.fromLine) + fmt.Fprintf(b, " -%d", hunk.FromLine) } if toCount > 1 { - fmt.Fprintf(b, " +%d,%d", hunk.toLine, toCount) - } else if hunk.toLine == 1 && toCount == 0 { + fmt.Fprintf(b, " +%d,%d", hunk.ToLine, toCount) + } else if hunk.ToLine == 1 && toCount == 0 { // Match odd GNU diff -u behavior adding to empty file. fmt.Fprintf(b, " +0,0") } else { - fmt.Fprintf(b, " +%d", hunk.toLine) + fmt.Fprintf(b, " +%d", hunk.ToLine) } fmt.Fprint(b, " @@\n") - for _, l := range hunk.lines { - switch l.kind { - case opDelete: - fmt.Fprintf(b, "-%s", l.content) - case opInsert: - fmt.Fprintf(b, "+%s", l.content) + for _, l := range hunk.Lines { + switch l.Kind { + case Delete: + fmt.Fprintf(b, "-%s", l.Content) + case Insert: + fmt.Fprintf(b, "+%s", l.Content) default: - fmt.Fprintf(b, " %s", l.content) + fmt.Fprintf(b, " %s", l.Content) } - if !strings.HasSuffix(l.content, "\n") { + if !strings.HasSuffix(l.Content, "\n") { fmt.Fprintf(b, "\n\\ No newline at end of file\n") } } -- 2.43.0