on main help sign in
refactor: simplify LSP diagnostic counts retrieval
7bb3a7bAyman Bagabas
internal/ui/model/lsp.go+5−7
@@ -32,13 +32,11 @@ func (m *UI) lspInfo(width, maxItems int, isSection bool) string {
32 var lsps []LSPInfo
32 var lsps []LSPInfo
33 for _, state := range states {
33 for _, state := range states {
34 lspErrs := map[protocol.DiagnosticSeverity]int{}
34 lspErrs := map[protocol.DiagnosticSeverity]int{}
35− if client, ok := m.com.Workspace.LSPGetClient(state.Name); ok {
35+ counts := m.com.Workspace.LSPGetDiagnosticCounts(state.Name)
36− counts := client.GetDiagnosticCounts()
37− lspErrs[protocol.SeverityError] = counts.Error
36+ lspErrs[protocol.SeverityError] = counts.Error
38− lspErrs[protocol.SeverityWarning] = counts.Warning
37+ lspErrs[protocol.SeverityWarning] = counts.Warning
39− lspErrs[protocol.SeverityHint] = counts.Hint
38+ lspErrs[protocol.SeverityHint] = counts.Hint
40− lspErrs[protocol.SeverityInformation] = counts.Information
39+ lspErrs[protocol.SeverityInformation] = counts.Information
41− }
42
40
43 lsps = append(lsps, LSPInfo{LSPClientInfo: state, Diagnostics: lspErrs})
41 lsps = append(lsps, LSPInfo{LSPClientInfo: state, Diagnostics: lspErrs})
44 }
42 }
internal/workspace/client_workspace.go+22−2
@@ -22,6 +22,7 @@ import (
22 "github.com/charmbracelet/crush/internal/proto"
22 "github.com/charmbracelet/crush/internal/proto"
23 "github.com/charmbracelet/crush/internal/pubsub"
23 "github.com/charmbracelet/crush/internal/pubsub"
24 "github.com/charmbracelet/crush/internal/session"
24 "github.com/charmbracelet/crush/internal/session"
25+ "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
25)
26)
26
27
27// ClientWorkspace implements the Workspace interface by delegating all
28// ClientWorkspace implements the Workspace interface by delegating all
@@ -359,8 +360,27 @@ func (w *ClientWorkspace) LSPGetStates() map[string]LSPClientInfo {
359 return result
360 return result
360}
361}
361
362
362−func (w *ClientWorkspace) LSPGetClient(_ string) (*lsp.Client, bool) {
363+func (w *ClientWorkspace) LSPGetDiagnosticCounts(name string) lsp.DiagnosticCounts {
363− return nil, false
364+ diags, err := w.client.GetLSPDiagnostics(context.Background(), w.workspaceID(), name)
365+ if err != nil {
366+ return lsp.DiagnosticCounts{}
367+ }
368+ var counts lsp.DiagnosticCounts
369+ for _, fileDiags := range diags {
370+ for _, d := range fileDiags {
371+ switch d.Severity {
372+ case protocol.SeverityError:
373+ counts.Error++
374+ case protocol.SeverityWarning:
375+ counts.Warning++
376+ case protocol.SeverityInformation:
377+ counts.Information++
378+ case protocol.SeverityHint:
379+ counts.Hint++
380+ }
381+ }
382+ }
383+ return counts
364}
384}
365
385
366// -- Config (read-only) --
386// -- Config (read-only) --
internal/workspace/workspace.go+1−1
@@ -107,7 +107,7 @@ type Workspace interface {
107 LSPStart(ctx context.Context, path string)
107 LSPStart(ctx context.Context, path string)
108 LSPStopAll(ctx context.Context)
108 LSPStopAll(ctx context.Context)
109 LSPGetStates() map[string]LSPClientInfo
109 LSPGetStates() map[string]LSPClientInfo
110− LSPGetClient(name string) (*lsp.Client, bool)
110+ LSPGetDiagnosticCounts(name string) lsp.DiagnosticCounts
111
111
112 // Config (read-only data)
112 // Config (read-only data)
113 Config() *config.Config
113 Config() *config.Config