forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langserver: Switch to upstream LSP framing & logging
- Loading branch information
1 parent
10caf60
commit ec1a374
Showing
3 changed files
with
55 additions
and
129 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶ms) | ||
|
||
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) | ||
} |