Skip to content

Commit

Permalink
feat: connect to daemon after lsp is connected
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Dec 22, 2023
1 parent b92b05e commit 5d5edc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions server/daemon/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (c *Client) processIdField() jsonrpc2.CallOption {
return jsonrpc2.ExtraField("processId", c.processId)
}

func (c *Client) IsConnected() bool {
return c.rpcConn != nil && c.connState == ConnectedState
}

func (c *Client) EnsureConnection() error {
if c.rpcConn != nil || c.connState != NotConnectedState {
return nil
Expand Down
19 changes: 12 additions & 7 deletions server/lsp_server/lsp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func (s *LspServer) Handle(ctx context.Context, c *jsonrpc2.Conn, r *jsonrpc2.Re

switch r.Method {
case lsp.MethodInitialize:
if !s.daemonClient.IsConnected() {
if err := s.daemonClient.Connect(); err != nil {
if err := s.daemonClient.EnsureConnection(); err != nil {
c.ReplyWithError(ctx, r.ID, &jsonrpc2.Error{
Code: -32002,
Message: fmt.Sprintf("Unable to connect to daemon: %s", err.Error()),
})
return
}
}
}

c.Reply(ctx, r.ID, lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
TextDocumentSync: lsp.TextDocumentSyncKindFull,
Expand Down Expand Up @@ -199,13 +211,6 @@ func Start() error {
)

lspServer.daemonClient = daemonClient

if err := daemonClient.Connect(); err != nil {
if err := daemonClient.EnsureConnection(); err != nil {
return err
}
}

exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

Expand Down

0 comments on commit 5d5edc7

Please sign in to comment.