1package model
2
3import (
4 "fmt"
5 "strings"
6
7 "charm.land/lipgloss/v2"
8 "github.com/charmbracelet/crush/internal/app"
9 "github.com/charmbracelet/crush/internal/lsp"
10 "github.com/charmbracelet/crush/internal/ui/common"
11 "github.com/charmbracelet/crush/internal/ui/styles"
12 "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
13)
14
15// LSPInfo wraps LSP client information with diagnostic counts by severity.
16type LSPInfo struct {
17 app.LSPClientInfo
18 Diagnostics map[protocol.DiagnosticSeverity]int
19}
20
21// lspInfo renders the LSP status section showing active LSP clients and their
22// diagnostic counts.
23func (m *UI) lspInfo(width, maxItems int, isSection bool) string {
24 var lsps []LSPInfo
25 t := m.com.Styles
26
27 for _, state := range m.lspStates {
28 client, ok := m.com.App.LSPClients.Get(state.Name)
29 if !ok {
30 continue
31 }
32 counts := client.GetDiagnosticCounts()
33 lspErrs := map[protocol.DiagnosticSeverity]int{
34 protocol.SeverityError: counts.Error,
35 protocol.SeverityWarning: counts.Warning,
36 protocol.SeverityHint: counts.Hint,
37 protocol.SeverityInformation: counts.Information,
38 }
39
40 lsps = append(lsps, LSPInfo{LSPClientInfo: state, Diagnostics: lspErrs})
41 }
42 title := t.Subtle.Render("LSPs")
43 if isSection {
44 title = common.Section(t, title, width)
45 }
46 list := t.Subtle.Render("None")
47 if len(lsps) > 0 {
48 list = lspList(t, lsps, width, maxItems)
49 }
50
51 return lipgloss.NewStyle().Width(width).Render(fmt.Sprintf("%s\n\n%s", title, list))
52}
53
54// lspDiagnostics formats diagnostic counts with appropriate icons and colors.
55func lspDiagnostics(t *styles.Styles, diagnostics map[protocol.DiagnosticSeverity]int) string {
56 errs := []string{}
57 if diagnostics[protocol.SeverityError] > 0 {
58 errs = append(errs, t.LSP.ErrorDiagnostic.Render(fmt.Sprintf("%s %d", styles.ErrorIcon, diagnostics[protocol.SeverityError])))
59 }
60 if diagnostics[protocol.SeverityWarning] > 0 {
61 errs = append(errs, t.LSP.WarningDiagnostic.Render(fmt.Sprintf("%s %d", styles.WarningIcon, diagnostics[protocol.SeverityWarning])))
62 }
63 if diagnostics[protocol.SeverityHint] > 0 {
64 errs = append(errs, t.LSP.HintDiagnostic.Render(fmt.Sprintf("%s %d", styles.HintIcon, diagnostics[protocol.SeverityHint])))
65 }
66 if diagnostics[protocol.SeverityInformation] > 0 {
67 errs = append(errs, t.LSP.InfoDiagnostic.Render(fmt.Sprintf("%s %d", styles.InfoIcon, diagnostics[protocol.SeverityInformation])))
68 }
69 return strings.Join(errs, " ")
70}
71
72// lspList renders a list of LSP clients with their status and diagnostics,
73// truncating to maxItems if needed.
74func lspList(t *styles.Styles, lsps []LSPInfo, width, maxItems int) string {
75 var renderedLsps []string
76 for _, l := range lsps {
77 var icon string
78 title := l.Name
79 var description string
80 var diagnostics string
81 switch l.State {
82 case lsp.StateStarting:
83 icon = t.ItemBusyIcon.String()
84 description = t.Subtle.Render("starting...")
85 case lsp.StateReady:
86 icon = t.ItemOnlineIcon.String()
87 diagnostics = lspDiagnostics(t, l.Diagnostics)
88 case lsp.StateError:
89 icon = t.ItemErrorIcon.String()
90 description = t.Subtle.Render("error")
91 if l.Error != nil {
92 description = t.Subtle.Render(fmt.Sprintf("error: %s", l.Error.Error()))
93 }
94 case lsp.StateDisabled:
95 icon = t.ItemOfflineIcon.Foreground(t.Muted.GetBackground()).String()
96 description = t.Subtle.Render("inactive")
97 default:
98 icon = t.ItemOfflineIcon.String()
99 }
100 renderedLsps = append(renderedLsps, common.Status(t, common.StatusOpts{
101 Icon: icon,
102 Title: title,
103 Description: description,
104 ExtraContent: diagnostics,
105 }, width))
106 }
107
108 if len(renderedLsps) > maxItems {
109 visibleItems := renderedLsps[:maxItems-1]
110 remaining := len(renderedLsps) - maxItems
111 visibleItems = append(visibleItems, t.Subtle.Render(fmt.Sprintf("β¦and %d more", remaining)))
112 return lipgloss.JoinVertical(lipgloss.Left, visibleItems...)
113 }
114 return lipgloss.JoinVertical(lipgloss.Left, renderedLsps...)
115}