mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(tools) diff preview for files without trailing newline
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package toolshared
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -8,11 +9,15 @@ import (
|
||||
"github.com/pmezard/go-difflib/difflib"
|
||||
)
|
||||
|
||||
const noContentChangeDiffMessage = "(no content change)"
|
||||
const (
|
||||
noContentChangeDiffMessage = "(no content change)"
|
||||
noNewlineAtEOFMarker = `\ No newline at end of file`
|
||||
)
|
||||
|
||||
// DiffResult creates a user-visible tool result containing a unified diff for
|
||||
// a successful file edit. The diff is included for both the LLM and the user so
|
||||
// the follow-up assistant response can reason about the exact change set.
|
||||
// the follow-up assistant response can reason about the resulting change set,
|
||||
// including EOF newline transitions.
|
||||
func DiffResult(path string, before, after []byte) *ToolResult {
|
||||
diff, err := buildUnifiedDiff(path, before, after)
|
||||
if err != nil {
|
||||
@@ -25,8 +30,8 @@ func DiffResult(path string, before, after []byte) *ToolResult {
|
||||
|
||||
func buildUnifiedDiff(path string, before, after []byte) (string, error) {
|
||||
diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
|
||||
A: difflib.SplitLines(string(before)),
|
||||
B: difflib.SplitLines(string(after)),
|
||||
A: splitDiffLinesPreservingEOF(before),
|
||||
B: splitDiffLinesPreservingEOF(after),
|
||||
FromFile: "a/" + diffDisplayPath(path),
|
||||
ToFile: "b/" + diffDisplayPath(path),
|
||||
Context: 3,
|
||||
@@ -43,6 +48,36 @@ func buildUnifiedDiff(path string, before, after []byte) (string, error) {
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
func splitDiffLinesPreservingEOF(content []byte) []string {
|
||||
if len(content) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
lines := make([]string, 0, bytes.Count(content, []byte{'\n'})+1)
|
||||
lineStart := 0
|
||||
for i, b := range content {
|
||||
if b != '\n' {
|
||||
continue
|
||||
}
|
||||
lines = append(lines, string(content[lineStart:i+1]))
|
||||
lineStart = i + 1
|
||||
}
|
||||
if lineStart < len(content) {
|
||||
lines = append(lines, string(content[lineStart:]))
|
||||
}
|
||||
|
||||
if lacksTrailingNewline(content) {
|
||||
lines[len(lines)-1] += "\n"
|
||||
lines = append(lines, noNewlineAtEOFMarker+"\n")
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func lacksTrailingNewline(content []byte) bool {
|
||||
return len(content) > 0 && !bytes.HasSuffix(content, []byte("\n"))
|
||||
}
|
||||
|
||||
func diffDisplayPath(path string) string {
|
||||
displayPath := strings.TrimLeft(filepath.ToSlash(path), "/")
|
||||
if displayPath == "" {
|
||||
|
||||
Reference in New Issue
Block a user