From d3033c5be8d8ec40c4509afbb96791007ae15ef4 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sun, 30 Jun 2024 17:08:06 +0100 Subject: [PATCH] fix: don't pass ranges to gopls if outside of a known Go code region, fixes #801 --- .version | 2 +- cmd/templ/lspcmd/proxy/server.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.version b/.version index d672fa852..332ca5d2c 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.732 \ No newline at end of file +0.2.734 \ No newline at end of file diff --git a/cmd/templ/lspcmd/proxy/server.go b/cmd/templ/lspcmd/proxy/server.go index ad367d2a2..4c34fed0e 100644 --- a/cmd/templ/lspcmd/proxy/server.go +++ b/cmd/templ/lspcmd/proxy/server.go @@ -80,9 +80,10 @@ func (p *Server) updatePosition(templURI lsp.DocumentURI, current lsp.Position) return true, goURI, updated } -func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.Range) (output lsp.Range) { +func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.Range) (output lsp.Range, ok bool) { output = input - sourceMap, ok := p.SourceMapCache.Get(string(templURI)) + var sourceMap *parser.SourceMap + sourceMap, ok = p.SourceMapCache.Get(string(templURI)) if !ok { return } @@ -276,14 +277,18 @@ func (p *Server) SetTrace(ctx context.Context, params *lsp.SetTraceParams) (err var supportedCodeActions = map[string]bool{} func (p *Server) CodeAction(ctx context.Context, params *lsp.CodeActionParams) (result []lsp.CodeAction, err error) { - p.Log.Info("client -> server: CodeAction") + p.Log.Info("client -> server: CodeAction", zap.Any("params", params)) defer p.Log.Info("client -> server: CodeAction end") isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI) if !isTemplFile { return p.Target.CodeAction(ctx, params) } templURI := params.TextDocument.URI - params.Range = p.convertTemplRangeToGoRange(templURI, params.Range) + var ok bool + if params.Range, ok = p.convertTemplRangeToGoRange(templURI, params.Range); !ok { + // Don't pass the request to gopls if the range is not within a Go code block. + return + } params.TextDocument.URI = goURI result, err = p.Target.CodeAction(ctx, params) if err != nil { @@ -712,7 +717,10 @@ func (p *Server) DocumentLinkResolve(ctx context.Context, params *lsp.DocumentLi } templURI := params.Target params.Target = goURI - params.Range = p.convertTemplRangeToGoRange(templURI, params.Range) + var ok bool + if params.Range, ok = p.convertTemplRangeToGoRange(templURI, params.Range); !ok { + return + } // Rewrite the result. result, err = p.Target.DocumentLinkResolve(ctx, params) if err != nil {