Skip to content

Commit

Permalink
langserver: Switch to upstream LSP framing & logging
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 10, 2020
1 parent 10caf60 commit ec1a374
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 129 deletions.
127 changes: 0 additions & 127 deletions langserver/jrpc_hacks.go

This file was deleted.

6 changes: 4 additions & 2 deletions langserver/langserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/channel"
rpcServer "github.com/creachadair/jrpc2/server"
)

Expand All @@ -27,6 +28,7 @@ func NewLangServer(srvCtx context.Context, logger *log.Logger) *langServer {
opts := &jrpc2.ServerOptions{
AllowPush: true,
Logger: logger,
RPCLog: &rpcLogger{logger},
}

srvCtx, stopFunc := context.WithCancel(srvCtx)
Expand All @@ -49,7 +51,7 @@ func (ls *langServer) Start(reader io.Reader, writer io.WriteCloser) {
ls.stopFunc()
}

ch := LspFraming(ls.logger)(reader, writer)
ch := channel.LSP(reader, writer)

srv := jrpc2.NewServer(ls.assigner, ls.srvOptions).Start(ch)

Expand Down Expand Up @@ -87,7 +89,7 @@ func (ls *langServer) StartTCP(address string) error {
go func() {
ls.logger.Println("Starting loop server ...")
err = rpcServer.Loop(lst, ls.assigner, &rpcServer.LoopOptions{
Framing: LspFraming(ls.logger),
Framing: channel.LSP,
ServerOptions: ls.srvOptions,
})
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions langserver/rpc_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package langserver

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/creachadair/jrpc2"
)

type rpcLogger struct {
logger *log.Logger
}

func (rl *rpcLogger) LogRequest(ctx context.Context, req *jrpc2.Request) {
idStr := ""
if req.ID() != "" {
idStr = fmt.Sprintf(" (ID %s)", req.ID())
}
reqType := "request"
if req.IsNotification() {
reqType = "notification"
}

var params json.RawMessage
req.UnmarshalParams(&params)

rl.logger.Printf("Incoming %s for %q%s: %s",
reqType, req.Method(), idStr, params)
}

func (rl *rpcLogger) LogResponse(ctx context.Context, rsp *jrpc2.Response) {
idStr := ""
if rsp.ID() != "" {
idStr = fmt.Sprintf(" (ID %s)", rsp.ID())
}

req := jrpc2.InboundRequest(ctx)
if req.IsNotification() {
idStr = " (notification)"
}

if rsp.Error() != nil {
rl.logger.Printf("Error for %q%s: %s", req.Method(), idStr, rsp.Error())
return
}
var body json.RawMessage
rsp.UnmarshalResult(&body)
rl.logger.Printf("Response to %q%s: %s", req.Method(), idStr, body)
}

0 comments on commit ec1a374

Please sign in to comment.