@@ -62,6 +62,11 @@ type Client struct {
6262
6363 // Server state
6464 serverState atomic.Value
65+
66+ // Server capabilities as returned by initialize
67+ caps protocol.ServerCapabilities
68+ capsMu sync.RWMutex
69+ capsSet atomic.Bool
6570}
6671
6772// NewClient creates a new LSP client.
@@ -143,7 +148,7 @@ func (c *Client) RegisterServerRequestHandler(method string, handler ServerReque
143148}
144149
145150func (c *Client) InitializeLSPClient(ctx context.Context, workspaceDir string) (*protocol.InitializeResult, error) {
146− initParams := &protocol.InitializeParams{
151+ initParams := protocol.ParamInitialize{
147152 WorkspaceFoldersInitializeParams: protocol.WorkspaceFoldersInitializeParams{
148153 WorkspaceFolders: []protocol.WorkspaceFolder{
149154 {
@@ -220,12 +225,14 @@ func (c *Client) InitializeLSPClient(ctx context.Context, workspaceDir string) (
220225 },
221226 }
222227
223− var result protocol.InitializeResult
224− if err := c.Call(ctx, "initialize", initParams, &result); err != nil {
228+ result, err := c.Initialize(ctx, initParams)
229+ if err != nil {
225230 return nil, fmt.Errorf("initialize failed: %w", err)
226231 }
227232
228− if err := c.Notify(ctx, "initialized", struct{}{}); err != nil {
233+ c.setCapabilities(result.Capabilities)
234+
235+ if err := c.Initialized(ctx, protocol.InitializedParams{}); err != nil {
229236 return nil, fmt.Errorf("initialized notification failed: %w", err)
230237 }
231238
@@ -234,14 +241,9 @@ func (c *Client) InitializeLSPClient(ctx context.Context, workspaceDir string) (
234241 c.RegisterServerRequestHandler("workspace/configuration", HandleWorkspaceConfiguration)
235242 c.RegisterServerRequestHandler("client/registerCapability", HandleRegisterCapability)
236243 c.RegisterNotificationHandler("window/showMessage", HandleServerMessage)
237− c.RegisterNotificationHandler("textDocument/publishDiagnostics",
238− func(params json.RawMessage) { HandleDiagnostics(c, params) })
239−
240− // Notify the LSP server
241− err := c.Initialized(ctx, protocol.InitializedParams{})
242− if err != nil {
243− return nil, fmt.Errorf("initialization failed: %w", err)
244− }
244+ c.RegisterNotificationHandler("textDocument/publishDiagnostics", func(params json.RawMessage) {
245+ HandleDiagnostics(c, params)
246+ })
245247
246248 return &result, nil
247249}
@@ -329,16 +331,7 @@ func (c *Client) WaitForServerReady(ctx context.Context) error {
329331 slog.Debug("Waiting for LSP server to be ready...")
330332 }
331333
332− // Determine server type for specialized initialization
333− serverType := c.detectServerType()
334−
335− // For TypeScript-like servers, we need to open some key files first
336− if serverType == ServerTypeTypeScript {
337− if cfg.Options.DebugLSP {
338− slog.Debug("TypeScript-like server detected, opening key configuration files")
339− }
340− c.openKeyConfigFiles(ctx)
341− }
334+ c.openKeyConfigFiles(ctx)
342335
343336 for {
344337 select {
@@ -347,21 +340,19 @@ func (c *Client) WaitForServerReady(ctx context.Context) error {
347340 return fmt.Errorf("timeout waiting for LSP server to be ready")
348341 case <-ticker.C:
349342 // Try a ping method appropriate for this server type
350− err := c.pingServerByType(ctx, serverType)
351− if err == nil {
352− // Server responded successfully
353− c.SetServerState(StateReady)
343+ if err := c.ping(ctx); err != nil {
354344 if cfg.Options.DebugLSP {
355− slog.Debug("LSP server is ready")
345+ slog.Debug("LSP server not ready yet", "error", err, "server", c.name)
356346 }
357− return nil
358− } else {
359− slog.Debug("LSP server not ready yet", "error", err, "serverType", serverType)
347+ continue
360348 }
361349
350+ // Server responded successfully
351+ c.SetServerState(StateReady)
362352 if cfg.Options.DebugLSP {
363− slog.Debug("LSP server not ready yet", "error", err, "serverType", serverType)
353+ slog.Debug("LSP server is ready")
364354 }
355+ return nil
365356 }
366357 }
367358}
@@ -443,86 +434,13 @@ func (c *Client) openKeyConfigFiles(ctx context.Context) {
443434 }
444435}
445436
446−// pingServerByType sends a ping request appropriate for the server type
447−func (c *Client) pingServerByType(ctx context.Context, serverType ServerType) error {
448− switch serverType {
449− case ServerTypeTypeScript:
450− // For TypeScript, try a document symbol request on an open file
451− return c.pingTypeScriptServer(ctx)
452− case ServerTypeGo:
453− // For Go, workspace/symbol works well
454− return c.pingWithWorkspaceSymbol(ctx)
455− case ServerTypeRust:
456− // For Rust, workspace/symbol works well
457− return c.pingWithWorkspaceSymbol(ctx)
458− default:
459− // Default ping method
460− return c.pingWithWorkspaceSymbol(ctx)
461− }
462−}
463−
464−// pingTypeScriptServer tries to ping a TypeScript server with appropriate methods
465−func (c *Client) pingTypeScriptServer(ctx context.Context) error {
466− // First try workspace/symbol which works for many servers
467− if err := c.pingWithWorkspaceSymbol(ctx); err == nil {
468− return nil
469− }
470−
471− // If that fails, try to find an open file and request document symbols
472− c.openFilesMu.RLock()
473− defer c.openFilesMu.RUnlock()
474−
475− // If we have any open files, try to get document symbols for one
476− for uri := range c.openFiles {
477− filePath, err := protocol.DocumentURI(uri).Path()
478− if err != nil {
479− slog.Error("Failed to convert URI to path for TypeScript symbol collection", "uri", uri, "error", err)
480− continue
481− }
482−
483− if strings.HasSuffix(filePath, ".ts") || strings.HasSuffix(filePath, ".js") ||
484− strings.HasSuffix(filePath, ".tsx") || strings.HasSuffix(filePath, ".jsx") {
485− var symbols []protocol.DocumentSymbol
486− err := c.Call(ctx, "textDocument/documentSymbol", protocol.DocumentSymbolParams{
487− TextDocument: protocol.TextDocumentIdentifier{
488− URI: protocol.DocumentURI(uri),
489− },
490− }, &symbols)
491− if err == nil {
492− return nil
493− }
494− }
495− }
496−
497− // If we have no open TypeScript files, try to find and open one
498− workDir := config.Get().WorkingDir()
499− err := filepath.WalkDir(workDir, func(path string, d os.DirEntry, err error) error {
500− if err != nil {
501− return err
502− }
503−
504− // Skip directories and non-TypeScript files
505− if d.IsDir() {
506− return nil
507− }
508−
509− ext := filepath.Ext(path)
510− if ext == ".ts" || ext == ".js" || ext == ".tsx" || ext == ".jsx" {
511− // Found a TypeScript file, try to open it
512− if err := c.OpenFile(ctx, path); err == nil {
513− // Successfully opened, stop walking
514− return filepath.SkipAll
515− }
516− }
517−
437+// ping sends a ping request...
438+func (c *Client) ping(ctx context.Context) error {
439+ if _, err := c.Symbol(ctx, protocol.WorkspaceSymbolParams{}); err == nil {
518440 return nil
519− })
520− if err != nil {
521− slog.Debug("Error walking directory for TypeScript files", "error", err)
522441 }
523−
524− // Final fallback - just try a generic capability
525− return c.pingWithServerCapabilities(ctx)
442+ // This is a very lightweight request that should work for most servers
443+ return c.Notify(ctx, "$/cancelRequest", protocol.CancelParams{ID: "1"})
526444}
527445
528446// openTypeScriptFiles finds and opens TypeScript files to help initialize the server
@@ -597,20 +515,6 @@ func shouldSkipDir(path string) bool {
597515 return skipDirs[dirName]
598516}
599517
600−// pingWithWorkspaceSymbol tries a workspace/symbol request
601−func (c *Client) pingWithWorkspaceSymbol(ctx context.Context) error {
602− var result []protocol.SymbolInformation
603− return c.Call(ctx, "workspace/symbol", protocol.WorkspaceSymbolParams{
604− Query: "",
605− }, &result)
606−}
607−
608−// pingWithServerCapabilities tries to get server capabilities
609−func (c *Client) pingWithServerCapabilities(ctx context.Context) error {
610− // This is a very lightweight request that should work for most servers
611− return c.Notify(ctx, "$/cancelRequest", struct{ ID int }{ID: -1})
612−}
613−
614518type OpenFileInfo struct {
615519 Version int32
616520 URI protocol.DocumentURI
@@ -668,7 +572,7 @@ func (c *Client) OpenFile(ctx context.Context, filepath string) error {
668572 },
669573 }
670574
671− if err := c.Notify(ctx, "textDocument/didOpen", params); err != nil {
575+ if err := c.DidOpen(ctx, params); err != nil {
672576 return err
673577 }
674578
@@ -718,7 +622,7 @@ func (c *Client) NotifyChange(ctx context.Context, filepath string) error {
718622 },
719623 }
720624
721− return c.Notify(ctx, "textDocument/didChange", params)
625+ return c.DidChange(ctx, params)
722626}
723627
724628func (c *Client) CloseFile(ctx context.Context, filepath string) error {
@@ -741,7 +645,7 @@ func (c *Client) CloseFile(ctx context.Context, filepath string) error {
741645 if cfg.Options.DebugLSP {
742646 slog.Debug("Closing file", "file", filepath)
743647 }
744− if err := c.Notify(ctx, "textDocument/didClose", params); err != nil {
648+ if err := c.DidClose(ctx, params); err != nil {
745649 return err
746650 }
747651