refactor: simplify LSP diagnostic counts retrieval

Ayman Bagabas created

Change summary

internal/ui/model/lsp.go               | 12 +++++-------
internal/workspace/client_workspace.go | 24 ++++++++++++++++++++++--
internal/workspace/workspace.go        |  2 +-
3 files changed, 28 insertions(+), 10 deletions(-)

Detailed changes

internal/ui/model/lsp.go 🔗

@@ -32,13 +32,11 @@ func (m *UI) lspInfo(width, maxItems int, isSection bool) string {
 	var lsps []LSPInfo
 	for _, state := range states {
 		lspErrs := map[protocol.DiagnosticSeverity]int{}
-		if client, ok := m.com.Workspace.LSPGetClient(state.Name); ok {
-			counts := client.GetDiagnosticCounts()
-			lspErrs[protocol.SeverityError] = counts.Error
-			lspErrs[protocol.SeverityWarning] = counts.Warning
-			lspErrs[protocol.SeverityHint] = counts.Hint
-			lspErrs[protocol.SeverityInformation] = counts.Information
-		}
+		counts := m.com.Workspace.LSPGetDiagnosticCounts(state.Name)
+		lspErrs[protocol.SeverityError] = counts.Error
+		lspErrs[protocol.SeverityWarning] = counts.Warning
+		lspErrs[protocol.SeverityHint] = counts.Hint
+		lspErrs[protocol.SeverityInformation] = counts.Information
 
 		lsps = append(lsps, LSPInfo{LSPClientInfo: state, Diagnostics: lspErrs})
 	}

internal/workspace/client_workspace.go 🔗

@@ -22,6 +22,7 @@ import (
 	"github.com/charmbracelet/crush/internal/proto"
 	"github.com/charmbracelet/crush/internal/pubsub"
 	"github.com/charmbracelet/crush/internal/session"
+	"github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
 )
 
 // ClientWorkspace implements the Workspace interface by delegating all
@@ -359,8 +360,27 @@ func (w *ClientWorkspace) LSPGetStates() map[string]LSPClientInfo {
 	return result
 }
 
-func (w *ClientWorkspace) LSPGetClient(_ string) (*lsp.Client, bool) {
-	return nil, false
+func (w *ClientWorkspace) LSPGetDiagnosticCounts(name string) lsp.DiagnosticCounts {
+	diags, err := w.client.GetLSPDiagnostics(context.Background(), w.workspaceID(), name)
+	if err != nil {
+		return lsp.DiagnosticCounts{}
+	}
+	var counts lsp.DiagnosticCounts
+	for _, fileDiags := range diags {
+		for _, d := range fileDiags {
+			switch d.Severity {
+			case protocol.SeverityError:
+				counts.Error++
+			case protocol.SeverityWarning:
+				counts.Warning++
+			case protocol.SeverityInformation:
+				counts.Information++
+			case protocol.SeverityHint:
+				counts.Hint++
+			}
+		}
+	}
+	return counts
 }
 
 // -- Config (read-only) --

internal/workspace/workspace.go 🔗

@@ -107,7 +107,7 @@ type Workspace interface {
 	LSPStart(ctx context.Context, path string)
 	LSPStopAll(ctx context.Context)
 	LSPGetStates() map[string]LSPClientInfo
-	LSPGetClient(name string) (*lsp.Client, bool)
+	LSPGetDiagnosticCounts(name string) lsp.DiagnosticCounts
 
 	// Config (read-only data)
 	Config() *config.Config