From d06b9d58bc6bbde451f57621c33a30e451a665a1 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Fri, 19 Dec 2025 14:14:46 -0300 Subject: [PATCH] refactor: use existing `fsext.HasPrefix` helper --- internal/agent/tools/diagnostics.go | 15 ++------------- internal/lsp/client.go | 17 +---------------- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/internal/agent/tools/diagnostics.go b/internal/agent/tools/diagnostics.go index a4d3bc5a00d25b7cd73464cd92f845d4a3b45936..390cbdb971090f50cfd7fb52768918bded157824 100644 --- a/internal/agent/tools/diagnostics.go +++ b/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 } diff --git a/internal/lsp/client.go b/internal/lsp/client.go index 4556293e0e47334b798039d16726ac67abc13f4e..abf4255137f4344db43c44af65d73a05667b1f04 100644 --- a/internal/lsp/client.go +++ b/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) -}