1package lsp
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/charmbracelet/lipgloss/v2"
  8
  9	"github.com/charmbracelet/crush/internal/app"
 10	"github.com/charmbracelet/crush/internal/config"
 11	"github.com/charmbracelet/crush/internal/lsp"
 12	"github.com/charmbracelet/crush/internal/lsp/protocol"
 13	"github.com/charmbracelet/crush/internal/tui/components/core"
 14	"github.com/charmbracelet/crush/internal/tui/styles"
 15)
 16
 17// RenderOptions contains options for rendering LSP lists.
 18type RenderOptions struct {
 19	MaxWidth    int
 20	MaxItems    int
 21	ShowSection bool
 22	SectionName string
 23}
 24
 25// RenderLSPList renders a list of LSP status items with the given options.
 26func RenderLSPList(lspClients map[string]*lsp.Client, opts RenderOptions) []string {
 27	t := styles.CurrentTheme()
 28	lspList := []string{}
 29
 30	if opts.ShowSection {
 31		sectionName := opts.SectionName
 32		if sectionName == "" {
 33			sectionName = "LSPs"
 34		}
 35		section := t.S().Subtle.Render(sectionName)
 36		lspList = append(lspList, section, "")
 37	}
 38
 39	lspConfigs := config.Get().LSP.Sorted()
 40	if len(lspConfigs) == 0 {
 41		lspList = append(lspList, t.S().Base.Foreground(t.Border).Render("None"))
 42		return lspList
 43	}
 44
 45	// Get LSP states
 46	lspStates := app.GetLSPStates()
 47
 48	// Determine how many items to show
 49	maxItems := len(lspConfigs)
 50	if opts.MaxItems > 0 {
 51		maxItems = min(opts.MaxItems, len(lspConfigs))
 52	}
 53
 54	for i, l := range lspConfigs {
 55		if i >= maxItems {
 56			break
 57		}
 58
 59		// Determine icon color and description based on state
 60		icon := t.ItemOfflineIcon
 61		description := l.LSP.Command
 62
 63		if l.LSP.Disabled {
 64			description = t.S().Subtle.Render("disabled")
 65		} else if state, exists := lspStates[l.Name]; exists {
 66			switch state.State {
 67			case lsp.StateStarting:
 68				icon = t.ItemBusyIcon
 69				description = t.S().Subtle.Render("starting...")
 70			case lsp.StateReady:
 71				icon = t.ItemOnlineIcon
 72				description = l.LSP.Command
 73			case lsp.StateError:
 74				icon = t.ItemErrorIcon
 75				if state.Error != nil {
 76					description = t.S().Subtle.Render(fmt.Sprintf("error: %s", state.Error.Error()))
 77				} else {
 78					description = t.S().Subtle.Render("error")
 79				}
 80			}
 81		}
 82
 83		// Calculate diagnostic counts if we have LSP clients
 84		var extraContent string
 85		if lspClients != nil {
 86			lspErrs := map[protocol.DiagnosticSeverity]int{
 87				protocol.SeverityError:       0,
 88				protocol.SeverityWarning:     0,
 89				protocol.SeverityHint:        0,
 90				protocol.SeverityInformation: 0,
 91			}
 92			if client, ok := lspClients[l.Name]; ok {
 93				for _, diagnostics := range client.GetDiagnostics() {
 94					for _, diagnostic := range diagnostics {
 95						if severity, ok := lspErrs[diagnostic.Severity]; ok {
 96							lspErrs[diagnostic.Severity] = severity + 1
 97						}
 98					}
 99				}
100			}
101
102			errs := []string{}
103			if lspErrs[protocol.SeverityError] > 0 {
104				errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
105			}
106			if lspErrs[protocol.SeverityWarning] > 0 {
107				errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
108			}
109			if lspErrs[protocol.SeverityHint] > 0 {
110				errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
111			}
112			if lspErrs[protocol.SeverityInformation] > 0 {
113				errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
114			}
115			extraContent = strings.Join(errs, " ")
116		}
117
118		lspList = append(lspList,
119			core.Status(
120				core.StatusOpts{
121					Icon:         icon.String(),
122					Title:        l.Name,
123					Description:  description,
124					ExtraContent: extraContent,
125				},
126				opts.MaxWidth,
127			),
128		)
129	}
130
131	return lspList
132}
133
134// RenderLSPBlock renders a complete LSP block with optional truncation indicator.
135func RenderLSPBlock(lspClients map[string]*lsp.Client, opts RenderOptions, showTruncationIndicator bool) string {
136	t := styles.CurrentTheme()
137	lspList := RenderLSPList(lspClients, opts)
138
139	// Add truncation indicator if needed
140	if showTruncationIndicator && opts.MaxItems > 0 {
141		lspConfigs := config.Get().LSP.Sorted()
142		if len(lspConfigs) > opts.MaxItems {
143			remaining := len(lspConfigs) - opts.MaxItems
144			if remaining == 1 {
145				lspList = append(lspList, t.S().Base.Foreground(t.FgMuted).Render("β¦"))
146			} else {
147				lspList = append(lspList,
148					t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("β¦and %d more", remaining)),
149				)
150			}
151		}
152	}
153
154	content := lipgloss.JoinVertical(lipgloss.Left, lspList...)
155	if opts.MaxWidth > 0 {
156		return lipgloss.NewStyle().Width(opts.MaxWidth).Render(content)
157	}
158	return content
159}