Skip to content

Commit

Permalink
removed duplicated LSP commands, and suppressed edit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 18, 2021
1 parent a460c07 commit 92cb428
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cmd/example/*.go
dist
cmd/templ
cmd/lspcmd/*log.txt
.DS_Store
2 changes: 1 addition & 1 deletion cmd/example/index.templ
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

{% templ PersonTemplate(p Person) %}
<div>
<div>{%= p.Name() %}</div>
<div id="id123">{%= p.Name() %}</div>
<a href={%= p.URL %}>{%= strings.ToUpper(p.Name()) %}</a>
<div>
{% if p.Type == "test" %}
Expand Down
36 changes: 36 additions & 0 deletions cmd/lspcmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (p *Proxy) proxyFromGoplsToClient(ctx context.Context, conn *jsonrpc2.Conn,
p.log.Info("gopls -> client", zap.String("method", r.Method), zap.Bool("notif", r.Notif))
if r.Notif {
p.log.Info("gopls -> client: notification", zap.String("method", r.Method), zap.Bool("notif", r.Notif))
if r.Method == "window/showMessage" && p.shouldSuppressWindowShowMessage(r) {
return
}
err := p.client.Notify(ctx, r.Method, r.Params)
if err != nil {
p.log.Error("gopls to client: notification: send error", zap.Error(err))
Expand All @@ -105,6 +108,14 @@ func (p *Proxy) proxyFromGoplsToClient(ctx context.Context, conn *jsonrpc2.Conn,
p.log.Info("gopls -> client: complete", zap.String("method", r.Method), zap.Bool("notif", r.Notif))
}

func (p *Proxy) shouldSuppressWindowShowMessage(r *jsonrpc2.Request) (shouldIgnore bool) {
var params lsp.ShowMessageRequestParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
return false
}
return strings.HasPrefix(params.Message, "Do not edit this file!")
}

// Handle implements jsonrpc2.Handler. This function receives from the text editor client, and calls the proxy function
// to determine how to play it back to the client.
func (p *Proxy) Handle(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.Request) {
Expand Down Expand Up @@ -133,6 +144,8 @@ func (p *Proxy) Handle(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.Req
p.log.Info("client -> gopls: notification complete", zap.String("method", r.Method))
} else {
switch r.Method {
case "initialize":
p.proxyInitialize(ctx, conn, r)
case "textDocument/completion":
p.proxyCompletion(ctx, conn, r)
return
Expand All @@ -154,6 +167,29 @@ func (p *Proxy) proxyCall(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.
p.log.Info("client -> gopls -> client: complete", zap.String("method", r.Method), zap.Bool("notif", r.Notif))
}

func (p *Proxy) proxyInitialize(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
// Unmarshal the params.
var params lsp.CompletionParams
err := json.Unmarshal(*req.Params, &params)
if err != nil {
p.log.Error("proxyInitialize: failed to unmarshal request params", zap.Error(err))
}
// Call gopls and get the response.
var resp lsp.InitializeResult
err = p.gopls.Call(ctx, req.Method, &params, &resp)
if err != nil {
p.log.Error("proxyInitialize: client -> gopls: error sending request", zap.Error(err))
}
// Remove all the gopls commands.
resp.Capabilities.ExecuteCommandProvider.Commands = []string{}
// Reply to the client.
err = conn.Reply(ctx, req.ID, &resp)
if err != nil {
p.log.Error("proxyInitialize: error sending response", zap.Error(err))
}
p.log.Info("proxyInitialize: client -> gopls -> client: complete", zap.Any("resp", resp))
}

func (p *Proxy) proxyCompletion(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
// Unmarshal the params.
var params lsp.CompletionParams
Expand Down

0 comments on commit 92cb428

Please sign in to comment.