refactor(lsp): use same handle file in client and manager (#2168)

Carlos Alexandro Becker created

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

internal/lsp/client.go  | 21 +--------------------
internal/lsp/manager.go | 28 ++++++++++++++++++----------
2 files changed, 19 insertions(+), 30 deletions(-)

Detailed changes

internal/lsp/client.go 🔗

@@ -329,26 +329,7 @@ func (c *Client) HandlesFile(path string) bool {
 		slog.Debug("File outside workspace", "name", c.name, "file", path, "workDir", c.workDir)
 		return false
 	}
-
-	// If no file types are specified, handle all files (backward compatibility).
-	if len(c.fileTypes) == 0 {
-		return true
-	}
-
-	kind := powernap.DetectLanguage(path)
-	name := strings.ToLower(filepath.Base(path))
-	for _, filetype := range c.fileTypes {
-		suffix := strings.ToLower(filetype)
-		if !strings.HasPrefix(suffix, ".") {
-			suffix = "." + suffix
-		}
-		if strings.HasSuffix(name, suffix) || filetype == string(kind) {
-			slog.Debug("Handles file", "name", c.name, "file", name, "filetype", filetype, "kind", kind)
-			return true
-		}
-	}
-	slog.Debug("Doesn't handle file", "name", c.name, "file", name)
-	return false
+	return handlesFiletype(c.name, c.fileTypes, path)
 }
 
 // OpenFile opens a file in the LSP server.

internal/lsp/manager.go 🔗

@@ -17,8 +17,7 @@ import (
 	"github.com/charmbracelet/crush/internal/csync"
 	"github.com/charmbracelet/crush/internal/fsext"
 	powernapconfig "github.com/charmbracelet/x/powernap/pkg/config"
-	"github.com/charmbracelet/x/powernap/pkg/lsp"
-	"github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
+	powernap "github.com/charmbracelet/x/powernap/pkg/lsp"
 	"github.com/sourcegraph/jsonrpc2"
 )
 
@@ -215,14 +214,25 @@ func resolveServerName(manager *powernapconfig.Manager, name string) string {
 	return name
 }
 
-func handlesFiletype(server *powernapconfig.ServerConfig, ext string, language protocol.LanguageKind) bool {
-	for _, ft := range server.FileTypes {
-		if protocol.LanguageKind(ft) == language ||
-			ft == strings.TrimPrefix(ext, ".") ||
-			"."+ft == ext {
+func handlesFiletype(sname string, fileTypes []string, filePath string) bool {
+	if len(fileTypes) == 0 {
+		return true
+	}
+
+	kind := powernap.DetectLanguage(filePath)
+	name := strings.ToLower(filepath.Base(filePath))
+	for _, filetype := range fileTypes {
+		suffix := strings.ToLower(filetype)
+		if !strings.HasPrefix(suffix, ".") {
+			suffix = "." + suffix
+		}
+		if strings.HasSuffix(name, suffix) || filetype == string(kind) {
+			slog.Debug("Handles file", "name", sname, "file", name, "filetype", filetype, "kind", kind)
 			return true
 		}
 	}
+
+	slog.Debug("Doesn't handle file", "name", sname, "file", name)
 	return false
 }
 
@@ -241,9 +251,7 @@ func hasRootMarkers(dir string, markers []string) bool {
 }
 
 func handles(server *powernapconfig.ServerConfig, filePath, workDir string) bool {
-	language := lsp.DetectLanguage(filePath)
-	ext := filepath.Ext(filePath)
-	return handlesFiletype(server, ext, language) &&
+	return handlesFiletype(server.Command, server.FileTypes, filePath) &&
 		hasRootMarkers(workDir, server.RootMarkers)
 }