refactor: use existing `fsext.HasPrefix` helper

Andrey Nering created

Change summary

internal/agent/tools/diagnostics.go | 15 ++-------------
internal/lsp/client.go              | 17 +----------------
2 files changed, 3 insertions(+), 29 deletions(-)

Detailed changes

internal/agent/tools/diagnostics.go 🔗

@@ -5,13 +5,13 @@ import (
 	_ "embed"
 	"fmt"
 	"log/slog"
-	"path/filepath"
 	"sort"
 	"strings"
 	"time"
 
 	"charm.land/fantasy"
 	"github.com/charmbracelet/crush/internal/csync"
+	"github.com/charmbracelet/crush/internal/fsext"
 	"github.com/charmbracelet/crush/internal/lsp"
 	"github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
 )
@@ -57,12 +57,6 @@ func getDiagnostics(filePath string, lsps *csync.Map[string, *lsp.Client], worki
 	fileDiagnostics := []string{}
 	projectDiagnostics := []string{}
 
-	absWd, err := filepath.Abs(workingDir)
-	if err != nil {
-		slog.Error("Failed to resolve working directory", "error", err)
-		return "Error: Failed to resolve working directory"
-	}
-
 	for lspName, client := range lsps.Seq2() {
 		for location, diags := range client.GetDiagnostics() {
 			path, err := location.Path()
@@ -72,12 +66,7 @@ func getDiagnostics(filePath string, lsps *csync.Map[string, *lsp.Client], worki
 			}
 
 			// Skip diagnostics for files outside the working directory.
-			absPath, err := filepath.Abs(path)
-			if err != nil {
-				slog.Debug("Failed to resolve diagnostic path", "path", path, "error", err)
-				continue
-			}
-			if !strings.HasPrefix(absPath, absWd) {
+			if !fsext.HasPrefix(path, workingDir) {
 				continue
 			}
 

internal/lsp/client.go 🔗

@@ -270,7 +270,7 @@ func (c *Client) OpenFile(ctx context.Context, filepath string) error {
 	}
 
 	// Skip files outside the current working directory.
-	if !c.isFileInWorkingDir(filepath) {
+	if !fsext.HasPrefix(filepath, c.workingDir) {
 		return nil
 	}
 
@@ -479,18 +479,3 @@ func HasRootMarkers(dir string, rootMarkers []string) bool {
 	}
 	return false
 }
-
-// isFileInWorkingDir checks if the given file path is within the client's working directory.
-func (c *Client) isFileInWorkingDir(filePath string) bool {
-	absFilepath, err := filepath.Abs(filePath)
-	if err != nil {
-		return false
-	}
-
-	absWd, err := filepath.Abs(c.workingDir)
-	if err != nil {
-		return false
-	}
-
-	return strings.HasPrefix(absFilepath, absWd)
-}