Skip to content

Commit f9f9f3a

Browse files
authored
fix: handle live import declaration changes in lazy loader (#1192)
1 parent 838a60a commit f9f9f3a

18 files changed

+2132
-776
lines changed

cmd/templ/lspcmd/proxy/server.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/a-h/parse"
13+
"github.com/a-h/templ/internal/lazyloader"
1314
lsp "github.com/a-h/templ/lsp/protocol"
1415
"github.com/a-h/templ/lsp/uri"
1516

@@ -42,7 +43,7 @@ type Server struct {
4243
GoSource map[string]string
4344
NoPreload bool
4445
preLoadURIs []*lsp.DidOpenTextDocumentParams
45-
templDocLazyLoader templDocLazyLoader
46+
templDocLazyLoader lazyloader.TemplDocLazyLoader
4647
}
4748

4849
func NewServer(log *slog.Logger, target lsp.Server, cache *SourceMapCache, diagnosticCache *DiagnosticCache, noPreload bool) (s *Server) {
@@ -245,12 +246,10 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
245246
}
246247

247248
if p.NoPreload {
248-
p.templDocLazyLoader = newTemplDocLazyLoader(
249-
templDocHooks{
250-
didOpen: p.didOpen,
251-
didClose: p.didClose,
252-
},
253-
)
249+
p.templDocLazyLoader = lazyloader.New(lazyloader.NewParams{
250+
TemplDocHandler: p,
251+
OpenDocSources: p.GoSource,
252+
})
254253
} else {
255254
p.preload(ctx, params.WorkspaceFolders)
256255
}
@@ -707,6 +706,13 @@ func (p *Server) DidChange(ctx context.Context, params *lsp.DidChangeTextDocumen
707706
p.Log.Info("setting cache", slog.String("uri", string(params.TextDocument.URI)))
708707
p.SourceMapCache.Set(string(params.TextDocument.URI), generatorOutput.SourceMap)
709708
p.GoSource[string(params.TextDocument.URI)] = w.String()
709+
710+
if p.NoPreload {
711+
if err := p.templDocLazyLoader.Sync(ctx, params); err != nil {
712+
p.Log.Error("lazy loader sync", slog.Any("error", err))
713+
}
714+
}
715+
710716
// Change the path.
711717
params.TextDocument.URI = goURI
712718
params.TextDocument.URI = goURI
@@ -740,13 +746,13 @@ func (p *Server) DidClose(ctx context.Context, params *lsp.DidCloseTextDocumentP
740746
defer p.Log.Info("client -> server: DidClose end")
741747

742748
if p.NoPreload {
743-
return p.templDocLazyLoader.unload(ctx, params)
749+
return p.templDocLazyLoader.Unload(ctx, params)
744750
}
745751

746-
return p.didClose(ctx, params)
752+
return p.HandleDidClose(ctx, params)
747753
}
748754

749-
func (p *Server) didClose(ctx context.Context, params *lsp.DidCloseTextDocumentParams) (err error) {
755+
func (p *Server) HandleDidClose(ctx context.Context, params *lsp.DidCloseTextDocumentParams) (err error) {
750756
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
751757
if !isTemplFile {
752758
return p.Target.DidClose(ctx, params)
@@ -764,13 +770,13 @@ func (p *Server) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentPar
764770
defer p.Log.Info("client -> server: DidOpen end")
765771

766772
if p.NoPreload {
767-
return p.templDocLazyLoader.load(ctx, params)
773+
return p.templDocLazyLoader.Load(ctx, params)
768774
}
769775

770-
return p.didOpen(ctx, params)
776+
return p.HandleDidOpen(ctx, params)
771777
}
772778

773-
func (p *Server) didOpen(ctx context.Context, params *lsp.DidOpenTextDocumentParams) (err error) {
779+
func (p *Server) HandleDidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentParams) (err error) {
774780
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
775781
if !isTemplFile {
776782
return p.Target.DidOpen(ctx, params)

cmd/templ/lspcmd/proxy/templdoclazyloader.go

Lines changed: 0 additions & 175 deletions
This file was deleted.

0 commit comments

Comments
 (0)