From 26c1d79761e7800837caffdd3c8c2abab96e0794 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Thu, 4 Jul 2024 15:34:11 +0100 Subject: [PATCH] feat: add support for gopls v0.16.x --- .version | 2 +- cmd/templ/lspcmd/main.go | 3 +-- cmd/templ/lspcmd/proxy/server.go | 24 ++++++++++++++---------- go.mod | 5 +++-- go.sum | 8 ++++---- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/.version b/.version index 332ca5d2c..4a932eea9 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.734 \ No newline at end of file +0.2.736 \ No newline at end of file diff --git a/cmd/templ/lspcmd/main.go b/cmd/templ/lspcmd/main.go index de244becc..a887dd208 100644 --- a/cmd/templ/lspcmd/main.go +++ b/cmd/templ/lspcmd/main.go @@ -100,7 +100,7 @@ func run(ctx context.Context, log *zap.Logger, templStream jsonrpc2.Stream, args log.Info("creating proxy") // Create the proxy to sit between. - serverProxy, serverInit := proxy.NewServer(log, goplsServer, cache, diagnosticCache) + serverProxy := proxy.NewServer(log, goplsServer, cache, diagnosticCache) // Create templ server. log.Info("creating templ server") @@ -109,7 +109,6 @@ func run(ctx context.Context, log *zap.Logger, templStream jsonrpc2.Stream, args // Allow both the server and the client to initiate outbound requests. clientInit(templClient) - serverInit(templClient) // Start the web server if required. if args.HTTPDebug != "" { diff --git a/cmd/templ/lspcmd/proxy/server.go b/cmd/templ/lspcmd/proxy/server.go index 4c34fed0e..c3070d48a 100644 --- a/cmd/templ/lspcmd/proxy/server.go +++ b/cmd/templ/lspcmd/proxy/server.go @@ -32,7 +32,6 @@ import ( // character positions. type Server struct { Log *zap.Logger - Client lsp.Client Target lsp.Server SourceMapCache *SourceMapCache DiagnosticCache *DiagnosticCache @@ -40,8 +39,8 @@ type Server struct { GoSource map[string]string } -func NewServer(log *zap.Logger, target lsp.Server, cache *SourceMapCache, diagnosticCache *DiagnosticCache) (s *Server, init func(lsp.Client)) { - s = &Server{ +func NewServer(log *zap.Logger, target lsp.Server, cache *SourceMapCache, diagnosticCache *DiagnosticCache) (s *Server) { + return &Server{ Log: log, Target: target, SourceMapCache: cache, @@ -49,9 +48,6 @@ func NewServer(log *zap.Logger, target lsp.Server, cache *SourceMapCache, diagno TemplSource: newDocumentContents(log), GoSource: make(map[string]string), } - return s, func(client lsp.Client) { - s.Client = client - } } // updatePosition maps positions and filenames from source templ files into the target *.go files. @@ -149,7 +145,7 @@ func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText st } } msg.Diagnostics = p.DiagnosticCache.AddGoDiagnostics(string(uri), msg.Diagnostics) - err = p.Client.PublishDiagnostics(ctx, msg) + err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, msg) if err != nil { p.Log.Error("failed to publish error diagnostics", zap.Error(err)) } @@ -184,7 +180,7 @@ func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText st }) } msg.Diagnostics = p.DiagnosticCache.AddGoDiagnostics(string(uri), msg.Diagnostics) - err = p.Client.PublishDiagnostics(ctx, msg) + err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, msg) if err != nil { p.Log.Error("failed to publish error diagnostics", zap.Error(err)) } @@ -192,7 +188,7 @@ func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText st } // Clear templ diagnostics. p.DiagnosticCache.ClearTemplDiagnostics(string(uri)) - err = p.Client.PublishDiagnostics(ctx, &lsp.PublishDiagnosticsParams{ + err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, &lsp.PublishDiagnosticsParams{ URI: uri, // Cannot be nil as per https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#publishDiagnosticsParams Diagnostics: []lsp.Diagnostic{}, @@ -408,10 +404,17 @@ func (p *Server) Completion(ctx context.Context, params *lsp.CompletionParams) ( } // Rewrite the result positions. p.Log.Info("completion: received items", zap.Int("count", len(result.Items))) + for i := 0; i < len(result.Items); i++ { item := result.Items[i] if item.TextEdit != nil { - item.TextEdit.Range = p.convertGoRangeToTemplRange(templURI, item.TextEdit.Range) + if item.TextEdit.TextEdit != nil { + item.TextEdit.TextEdit.Range = p.convertGoRangeToTemplRange(templURI, item.TextEdit.TextEdit.Range) + } + if item.TextEdit.InsertReplaceEdit != nil { + item.TextEdit.InsertReplaceEdit.Insert = p.convertGoRangeToTemplRange(templURI, item.TextEdit.InsertReplaceEdit.Insert) + item.TextEdit.InsertReplaceEdit.Replace = p.convertGoRangeToTemplRange(templURI, item.TextEdit.InsertReplaceEdit.Replace) + } } if len(item.AdditionalTextEdits) > 0 { doc, ok := p.TemplSource.Get(string(templURI)) @@ -435,6 +438,7 @@ func (p *Server) Completion(ctx context.Context, params *lsp.CompletionParams) ( // Add templ snippet. result.Items = append(result.Items, snippet...) + return } diff --git a/go.mod b/go.mod index a81d539f6..8708add70 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a github.com/a-h/pathvars v0.0.14 - github.com/a-h/protocol v0.0.0-20230224160810-b4eec67c1c22 + github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041 github.com/andybalholm/brotli v1.1.0 github.com/cenkalti/backoff/v4 v4.3.0 github.com/cli/browser v1.3.0 @@ -34,7 +34,8 @@ require ( go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.19.0 // indirect + golang.org/x/sys v0.21.0 // indirect ) // replace github.com/a-h/parse => /Users/adrian/github.com/a-h/parse +// replace github.com/a-h/protocol => /Users/adrian/github.com/a-h/protocol diff --git a/go.sum b/go.sum index 098f84ec2..cfc3b4216 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a h1:vlmAfVwFK9sRpDlJyuHY8 github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ= github.com/a-h/pathvars v0.0.14 h1:5/C0U5zuUtvAfVQCR5vow4E4MIRU7QIOLz8z26EeeQw= github.com/a-h/pathvars v0.0.14/go.mod h1:7rLTtvDVyKneR/N65hC0lh2sZ2KRyAmWFaOvv00uxb0= -github.com/a-h/protocol v0.0.0-20230224160810-b4eec67c1c22 h1:ehNdbGOAR8KTrLY/S90/9RJ4p/cgeNdt1sRt0DSiRWs= -github.com/a-h/protocol v0.0.0-20230224160810-b4eec67c1c22/go.mod h1:Gm0KywveHnkiIhqFSMZglXwWZRQICg3KDWLYdglv/d8= +github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041 h1:2enlC41iOwWklx9ZUqpQygsNAG6KIm3uMMUXzBJw5jA= +github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041/go.mod h1:Gm0KywveHnkiIhqFSMZglXwWZRQICg3KDWLYdglv/d8= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= @@ -80,8 +80,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=