on main help sign in
refactor(lsp): use same handle file in client and manager (#2168)
074917bCarlos Alexandro Becker
internal/lsp/client.go+1−20
@@ -329,26 +329,7 @@ func (c *Client) HandlesFile(path string) bool {
329 slog.Debug("File outside workspace", "name", c.name, "file", path, "workDir", c.workDir)
329 slog.Debug("File outside workspace", "name", c.name, "file", path, "workDir", c.workDir)
330 return false
330 return false
331 }
331 }
332−
332+ return handlesFiletype(c.name, c.fileTypes, path)
333− // If no file types are specified, handle all files (backward compatibility).
334− if len(c.fileTypes) == 0 {
335− return true
336− }
337−
338− kind := powernap.DetectLanguage(path)
339− name := strings.ToLower(filepath.Base(path))
340− for _, filetype := range c.fileTypes {
341− suffix := strings.ToLower(filetype)
342− if !strings.HasPrefix(suffix, ".") {
343− suffix = "." + suffix
344− }
345− if strings.HasSuffix(name, suffix) || filetype == string(kind) {
346− slog.Debug("Handles file", "name", c.name, "file", name, "filetype", filetype, "kind", kind)
347− return true
348− }
349− }
350− slog.Debug("Doesn't handle file", "name", c.name, "file", name)
351− return false
352}
333}
353
334
354// OpenFile opens a file in the LSP server.
335// OpenFile opens a file in the LSP server.
internal/lsp/manager.go+18−10
@@ -17,8 +17,7 @@ import (
17 "github.com/charmbracelet/crush/internal/csync"
17 "github.com/charmbracelet/crush/internal/csync"
18 "github.com/charmbracelet/crush/internal/fsext"
18 "github.com/charmbracelet/crush/internal/fsext"
19 powernapconfig "github.com/charmbracelet/x/powernap/pkg/config"
19 powernapconfig "github.com/charmbracelet/x/powernap/pkg/config"
20− "github.com/charmbracelet/x/powernap/pkg/lsp"
21− "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
20+ powernap "github.com/charmbracelet/x/powernap/pkg/lsp"
22 "github.com/sourcegraph/jsonrpc2"
21 "github.com/sourcegraph/jsonrpc2"
23)
22)
24
23
@@ -215,14 +214,25 @@ func resolveServerName(manager *powernapconfig.Manager, name string) string {
215 return name
214 return name
216}
215}
217
216
218−func handlesFiletype(server *powernapconfig.ServerConfig, ext string, language protocol.LanguageKind) bool {
217+func handlesFiletype(sname string, fileTypes []string, filePath string) bool {
219− for _, ft := range server.FileTypes {
218+ if len(fileTypes) == 0 {
220− if protocol.LanguageKind(ft) == language ||
219+ return true
221− ft == strings.TrimPrefix(ext, ".") ||
220+ }
222− "."+ft == ext {
221+
222+ kind := powernap.DetectLanguage(filePath)
223+ name := strings.ToLower(filepath.Base(filePath))
224+ for _, filetype := range fileTypes {
225+ suffix := strings.ToLower(filetype)
226+ if !strings.HasPrefix(suffix, ".") {
227+ suffix = "." + suffix
228+ }
229+ if strings.HasSuffix(name, suffix) || filetype == string(kind) {
230+ slog.Debug("Handles file", "name", sname, "file", name, "filetype", filetype, "kind", kind)
223 return true
231 return true
224 }
232 }
225 }
233 }
234+
235+ slog.Debug("Doesn't handle file", "name", sname, "file", name)
226 return false
236 return false
227}
237}
228
238
@@ -241,9 +251,7 @@ func hasRootMarkers(dir string, markers []string) bool {
241}
251}
242
252
243func handles(server *powernapconfig.ServerConfig, filePath, workDir string) bool {
253func handles(server *powernapconfig.ServerConfig, filePath, workDir string) bool {
244− language := lsp.DetectLanguage(filePath)
245− ext := filepath.Ext(filePath)
246− return handlesFiletype(server, ext, language) &&
254+ return handlesFiletype(server.Command, server.FileTypes, filePath) &&
247 hasRootMarkers(workDir, server.RootMarkers)
255 hasRootMarkers(workDir, server.RootMarkers)
248}
256}
249
257