Skip to content

Commit

Permalink
feat: add support for gopls v0.16.x
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jul 4, 2024
1 parent 8b1f399 commit 26c1d79
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.734
0.2.736
3 changes: 1 addition & 2 deletions cmd/templ/lspcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 != "" {
Expand Down
24 changes: 14 additions & 10 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ import (
// character positions.
type Server struct {
Log *zap.Logger
Client lsp.Client
Target lsp.Server
SourceMapCache *SourceMapCache
DiagnosticCache *DiagnosticCache
TemplSource *DocumentContents
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,
DiagnosticCache: diagnosticCache,
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.
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -184,15 +180,15 @@ 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))
}
return
}
// 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{},
Expand Down Expand Up @@ -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))
Expand All @@ -435,6 +438,7 @@ func (p *Server) Completion(ctx context.Context, params *lsp.CompletionParams) (

// Add templ snippet.
result.Items = append(result.Items, snippet...)

return
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 26c1d79

Please sign in to comment.