From 92cb42800fc2a09908298dcc443b6665dcfdefd7 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 18 May 2021 08:52:41 +0100 Subject: [PATCH] removed duplicated LSP commands, and suppressed edit warnings --- .gitignore | 1 + cmd/example/index.templ | 2 +- cmd/lspcmd/handler.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 813ba23fa..67697fbdd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ cmd/example/*.go dist cmd/templ cmd/lspcmd/*log.txt +.DS_Store diff --git a/cmd/example/index.templ b/cmd/example/index.templ index 101fa7ea0..f72b0f790 100644 --- a/cmd/example/index.templ +++ b/cmd/example/index.templ @@ -17,7 +17,7 @@ {% templ PersonTemplate(p Person) %}
-
{%= p.Name() %}
+
{%= p.Name() %}
{%= strings.ToUpper(p.Name()) %}
{% if p.Type == "test" %} diff --git a/cmd/lspcmd/handler.go b/cmd/lspcmd/handler.go index c63d7a150..f83aeccc5 100644 --- a/cmd/lspcmd/handler.go +++ b/cmd/lspcmd/handler.go @@ -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)) @@ -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, ¶ms); 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) { @@ -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 @@ -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, ¶ms) + 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, ¶ms, &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