| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package app
- import (
- "context"
- "maps"
- "time"
- "github.com/charmbracelet/crush/internal/csync"
- "github.com/charmbracelet/crush/internal/lsp"
- "github.com/charmbracelet/crush/internal/pubsub"
- )
- // LSPEventType represents the type of LSP event
- type LSPEventType string
- const (
- LSPEventStateChanged LSPEventType = "state_changed"
- LSPEventDiagnosticsChanged LSPEventType = "diagnostics_changed"
- )
- // LSPEvent represents an event in the LSP system
- type LSPEvent struct {
- Type LSPEventType
- Name string
- State lsp.ServerState
- Error error
- DiagnosticCount int
- }
- // LSPClientInfo holds information about an LSP client's state
- type LSPClientInfo struct {
- Name string
- State lsp.ServerState
- Error error
- Client *lsp.Client
- DiagnosticCount int
- ConnectedAt time.Time
- }
- var (
- lspStates = csync.NewMap[string, LSPClientInfo]()
- lspBroker = pubsub.NewBroker[LSPEvent]()
- )
- // SubscribeLSPEvents returns a channel for LSP events
- func SubscribeLSPEvents(ctx context.Context) <-chan pubsub.Event[LSPEvent] {
- return lspBroker.Subscribe(ctx)
- }
- // GetLSPStates returns the current state of all LSP clients
- func GetLSPStates() map[string]LSPClientInfo {
- return maps.Collect(lspStates.Seq2())
- }
- // GetLSPState returns the state of a specific LSP client
- func GetLSPState(name string) (LSPClientInfo, bool) {
- return lspStates.Get(name)
- }
- // updateLSPState updates the state of an LSP client and publishes an event
- func updateLSPState(name string, state lsp.ServerState, err error, client *lsp.Client, diagnosticCount int) {
- info := LSPClientInfo{
- Name: name,
- State: state,
- Error: err,
- Client: client,
- DiagnosticCount: diagnosticCount,
- }
- if state == lsp.StateReady {
- info.ConnectedAt = time.Now()
- }
- lspStates.Set(name, info)
- // Publish state change event
- lspBroker.Publish(pubsub.UpdatedEvent, LSPEvent{
- Type: LSPEventStateChanged,
- Name: name,
- State: state,
- Error: err,
- DiagnosticCount: diagnosticCount,
- })
- }
- // updateLSPDiagnostics updates the diagnostic count for an LSP client and publishes an event
- func updateLSPDiagnostics(name string, diagnosticCount int) {
- if info, exists := lspStates.Get(name); exists {
- info.DiagnosticCount = diagnosticCount
- lspStates.Set(name, info)
- // Publish diagnostics change event
- lspBroker.Publish(pubsub.UpdatedEvent, LSPEvent{
- Type: LSPEventDiagnosticsChanged,
- Name: name,
- State: info.State,
- Error: info.Error,
- DiagnosticCount: diagnosticCount,
- })
- }
- }
|