1package lsp
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/charmbracelet/crush/internal/app"
  8	"github.com/charmbracelet/crush/internal/config"
  9	"github.com/charmbracelet/crush/internal/csync"
 10	"github.com/charmbracelet/crush/internal/lsp"
 11	"github.com/charmbracelet/crush/internal/tui/components/core"
 12	"github.com/charmbracelet/crush/internal/tui/styles"
 13	"github.com/charmbracelet/lipgloss/v2"
 14	"github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
 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 *csync.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			case lsp.StateDisabled:
 81				icon = t.ItemOfflineIcon.Foreground(t.FgMuted)
 82				description = t.S().Base.Foreground(t.FgMuted).Render("no root markers found")
 83			}
 84		}
 85
 86		// Calculate diagnostic counts if we have LSP clients
 87		var extraContent string
 88		if lspClients != nil {
 89			lspErrs := map[protocol.DiagnosticSeverity]int{
 90				protocol.SeverityError:       0,
 91				protocol.SeverityWarning:     0,
 92				protocol.SeverityHint:        0,
 93				protocol.SeverityInformation: 0,
 94			}
 95			if client, ok := lspClients.Get(l.Name); ok {
 96				for _, diagnostics := range client.GetDiagnostics() {
 97					for _, diagnostic := range diagnostics {
 98						if severity, ok := lspErrs[diagnostic.Severity]; ok {
 99							lspErrs[diagnostic.Severity] = severity + 1
100						}
101					}
102				}
103			}
104
105			errs := []string{}
106			if lspErrs[protocol.SeverityError] > 0 {
107				errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
108			}
109			if lspErrs[protocol.SeverityWarning] > 0 {
110				errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
111			}
112			if lspErrs[protocol.SeverityHint] > 0 {
113				errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
114			}
115			if lspErrs[protocol.SeverityInformation] > 0 {
116				errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
117			}
118			extraContent = strings.Join(errs, " ")
119		}
120
121		lspList = append(lspList,
122			core.Status(
123				core.StatusOpts{
124					Icon:         icon.String(),
125					Title:        l.Name,
126					Description:  description,
127					ExtraContent: extraContent,
128				},
129				opts.MaxWidth,
130			),
131		)
132	}
133
134	return lspList
135}
136
137// RenderLSPBlock renders a complete LSP block with optional truncation indicator.
138func RenderLSPBlock(lspClients *csync.Map[string, *lsp.Client], opts RenderOptions, showTruncationIndicator bool) string {
139	t := styles.CurrentTheme()
140	lspList := RenderLSPList(lspClients, opts)
141
142	// Add truncation indicator if needed
143	if showTruncationIndicator && opts.MaxItems > 0 {
144		lspConfigs := config.Get().LSP.Sorted()
145		if len(lspConfigs) > opts.MaxItems {
146			remaining := len(lspConfigs) - opts.MaxItems
147			if remaining == 1 {
148				lspList = append(lspList, t.S().Base.Foreground(t.FgMuted).Render("β¦"))
149			} else {
150				lspList = append(lspList,
151					t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("β¦and %d more", remaining)),
152				)
153			}
154		}
155	}
156
157	content := lipgloss.JoinVertical(lipgloss.Left, lspList...)
158	if opts.MaxWidth > 0 {
159		return lipgloss.NewStyle().Width(opts.MaxWidth).Render(content)
160	}
161	return content
162}