Skip to content

Commit

Permalink
feat(proxy): preload URIs during initialization
Browse files Browse the repository at this point in the history
- Added a global variable `preLoadURIs` to store URIs to be preloaded.
- Modified `Initialize` method to append URIs to `preLoadURIs`.
- Updated `Initialized` method to preload URIs by calling `DidOpen`.
- Ensured `DidOpenTextDocumentParams` are created and appended correctly.
  • Loading branch information
tris203 committed Sep 10, 2024
1 parent d072b8d commit ab06581
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func NewServer(log *zap.Logger, target lsp.Server, cache *SourceMapCache, diagno
}
}

var preLoadURIs = []*lsp.DidOpenTextDocumentParams{}

// updatePosition maps positions and filenames from source templ files into the target *.go files.
func (p *Server) updatePosition(templURI lsp.DocumentURI, current lsp.Position) (ok bool, goURI lsp.DocumentURI, updated lsp.Position) {
log := p.Log.With(zap.String("uri", string(templURI)))
Expand Down Expand Up @@ -240,7 +242,7 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
return err
}
uri := uri.URI("file://" + path)
isTemplFile, _ := convertTemplToGoURI(uri)
isTemplFile, goURI := convertTemplToGoURI(uri)
if isTemplFile {
b, err := os.ReadFile(path)
if err != nil {
Expand Down Expand Up @@ -270,6 +272,16 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
// Set the Go contents.
p.GoSource[string(uri)] = w.String()

didOpenParams := &lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: goURI,
Text: w.String(),
Version: 1,
LanguageID: "go",
},
}

preLoadURIs = append(preLoadURIs, didOpenParams)
}
return nil
})
Expand All @@ -287,7 +299,16 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
func (p *Server) Initialized(ctx context.Context, params *lsp.InitializedParams) (err error) {
p.Log.Info("client -> server: Initialized")
defer p.Log.Info("client -> server: Initialized end")
return p.Target.Initialized(ctx, params)
goInitErr := p.Target.Initialized(ctx, params)

for _, doParams := range preLoadURIs {
doErr := p.Target.DidOpen(ctx, doParams)
if doErr != nil {
return doErr
}
}

return goInitErr
}

func (p *Server) Shutdown(ctx context.Context) (err error) {
Expand Down Expand Up @@ -514,8 +535,8 @@ func getPackageFromItemDetail(pkg string) string {
}

type importInsert struct {
LineIndex int
Text string
LineIndex int
}

var nonImportKeywordRegexp = regexp.MustCompile(`^(?:templ|func|css|script|var|const|type)\s`)
Expand Down

0 comments on commit ab06581

Please sign in to comment.