Run starts the server and blocks until the connection closes.
(ctx context.Context)
| 55 | |
| 56 | // Run starts the server and blocks until the connection closes. |
| 57 | func (s *StdioServer) Run(ctx context.Context) error { |
| 58 | var transport Transport |
| 59 | if s.options.PipePath != "" { |
| 60 | t, err := NewPipeTransport(s.options.PipePath) |
| 61 | if err != nil { |
| 62 | return fmt.Errorf("failed to create pipe transport: %w", err) |
| 63 | } |
| 64 | defer t.Close() |
| 65 | transport = t |
| 66 | } else { |
| 67 | t := NewStdioTransport(s.options.In, s.options.Out) |
| 68 | defer t.Close() |
| 69 | transport = t |
| 70 | } |
| 71 | |
| 72 | fs := bundled.WrapFS(osvfs.FS()) |
| 73 | |
| 74 | // Wrap the base FS with callbackFS if callbacks are requested |
| 75 | var callbackFS *callbackFS |
| 76 | if len(s.options.Callbacks) > 0 { |
| 77 | callbackFS = newCallbackFS(fs, s.options.Callbacks) |
| 78 | fs = callbackFS |
| 79 | } |
| 80 | |
| 81 | projectSession := project.NewSession(&project.SessionInit{ |
| 82 | BackgroundCtx: ctx, |
| 83 | Logger: nil, // TODO: Add logging support |
| 84 | FS: fs, |
| 85 | Options: &project.SessionOptions{ |
| 86 | CurrentDirectory: s.options.Cwd, |
| 87 | DefaultLibraryPath: s.options.DefaultLibraryPath, |
| 88 | PositionEncoding: lsproto.PositionEncodingKindUTF8, |
| 89 | LoggingEnabled: false, |
| 90 | }, |
| 91 | }) |
| 92 | |
| 93 | session := NewSession(projectSession, &SessionOptions{ |
| 94 | UseBinaryResponses: !s.options.Async, // Only msgpack uses binary responses |
| 95 | }) |
| 96 | defer session.Close() |
| 97 | |
| 98 | // Accept connection from transport |
| 99 | rwc, err := transport.Accept() |
| 100 | if err != nil { |
| 101 | return fmt.Errorf("failed to accept connection: %w", err) |
| 102 | } |
| 103 | |
| 104 | // Create protocol and connection based on async mode |
| 105 | var conn Conn |
| 106 | if s.options.Async { |
| 107 | protocol := NewJSONRPCProtocol(rwc) |
| 108 | asyncConn := NewAsyncConnWithProtocol(rwc, protocol, session) |
| 109 | asyncConn.SetCollectTiming(s.options.CollectTiming) |
| 110 | conn = asyncConn |
| 111 | } else { |
| 112 | protocol := NewMessagePackProtocol(rwc) |
| 113 | syncConn := NewSyncConn(rwc, protocol, session) |
| 114 | syncConn.SetCollectTiming(s.options.CollectTiming) |
no test coverage detected