chore: make diff config independent

Kujtim Hoxha created

Change summary

internal/diff/diff.go                           | 5 -----
internal/llm/tools/edit.go                      | 6 +++---
internal/llm/tools/write.go                     | 2 +-
internal/tui/components/chat/sidebar/sidebar.go | 6 +++++-
4 files changed, 9 insertions(+), 10 deletions(-)

Detailed changes

internal/diff/diff.go 🔗

@@ -4,15 +4,10 @@ import (
 	"strings"
 
 	"github.com/aymanbagabas/go-udiff"
-	"github.com/charmbracelet/crush/internal/config"
 )
 
 // GenerateDiff creates a unified diff from two file contents
 func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int) {
-	// remove the cwd prefix and ensure consistent path format
-	// this prevents issues with absolute paths in different environments
-	cwd := config.Get().WorkingDir()
-	fileName = strings.TrimPrefix(fileName, cwd)
 	fileName = strings.TrimPrefix(fileName, "/")
 
 	var (

internal/llm/tools/edit.go 🔗

@@ -206,7 +206,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
 	_, additions, removals := diff.GenerateDiff(
 		"",
 		content,
-		filePath,
+		strings.TrimPrefix(filePath, config.Get().WorkingDir()),
 	)
 	rootDir := config.Get().WorkingDir()
 	permissionPath := filepath.Dir(filePath)
@@ -318,7 +318,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
 	_, additions, removals := diff.GenerateDiff(
 		oldContent,
 		newContent,
-		filePath,
+		strings.TrimPrefix(filePath, config.Get().WorkingDir()),
 	)
 
 	rootDir := config.Get().WorkingDir()
@@ -441,7 +441,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
 	_, additions, removals := diff.GenerateDiff(
 		oldContent,
 		newContent,
-		filePath,
+		strings.TrimPrefix(filePath, config.Get().WorkingDir()),
 	)
 	rootDir := config.Get().WorkingDir()
 	permissionPath := filepath.Dir(filePath)

internal/llm/tools/write.go 🔗

@@ -168,7 +168,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
 	diff, additions, removals := diff.GenerateDiff(
 		oldContent,
 		params.Content,
-		filePath,
+		strings.TrimPrefix(filePath, config.Get().WorkingDir()),
 	)
 
 	rootDir := config.Get().WorkingDir()

internal/tui/components/chat/sidebar/sidebar.go 🔗

@@ -160,6 +160,8 @@ func (m *sidebarCmp) handleFileHistoryEvent(event pubsub.Event[history.File]) te
 				before := existing.History.initialVersion.Content
 				after := existing.History.latestVersion.Content
 				path := existing.History.initialVersion.Path
+				cwd := config.Get().WorkingDir()
+				path = strings.TrimPrefix(path, cwd)
 				_, additions, deletions := diff.GenerateDiff(before, after, path)
 				existing.Additions = additions
 				existing.Deletions = deletions
@@ -213,7 +215,9 @@ func (m *sidebarCmp) loadSessionFiles() tea.Msg {
 
 	sessionFiles := make([]SessionFile, 0, len(fileMap))
 	for path, fh := range fileMap {
-		_, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, fh.initialVersion.Path)
+		cwd := config.Get().WorkingDir()
+		path = strings.TrimPrefix(path, cwd)
+		_, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, path)
 		sessionFiles = append(sessionFiles, SessionFile{
 			History:   fh,
 			FilePath:  path,