Skip to content

Commit d072b8d

Browse files
committed
feat(lsp): add workspace folder initialization
- Added logic to walk through workspace folders and initialize templ files. - Logs warnings when source maps are not found in cache. - Parses templates and sets source map cache contents. - Handles errors during file reading, template parsing, and generation.
1 parent 88eed0f commit d072b8d

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

cmd/templ/lspcmd/proxy/server.go

+52-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ package proxy
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"regexp"
79
"strings"
810

911
"github.com/a-h/parse"
1012
lsp "github.com/a-h/protocol"
13+
"go.lsp.dev/uri"
14+
"go.uber.org/zap"
15+
1116
"github.com/a-h/templ"
1217
"github.com/a-h/templ/cmd/templ/imports"
1318
"github.com/a-h/templ/generator"
1419
"github.com/a-h/templ/parser/v2"
15-
"go.lsp.dev/uri"
16-
"go.uber.org/zap"
1720
)
1821

1922
// Server is responsible for rewriting messages that are
@@ -81,6 +84,7 @@ func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.
8184
var sourceMap *parser.SourceMap
8285
sourceMap, ok = p.SourceMapCache.Get(string(templURI))
8386
if !ok {
87+
p.Log.Warn("templ->go: sourcemap not found in cache")
8488
return
8589
}
8690
// Map from the source position to target Go position.
@@ -101,6 +105,7 @@ func (p *Server) convertGoRangeToTemplRange(templURI lsp.DocumentURI, input lsp.
101105
output = input
102106
sourceMap, ok := p.SourceMapCache.Get(string(templURI))
103107
if !ok {
108+
p.Log.Warn("go->templ: sourcemap not found in cache")
104109
return
105110
}
106111
// Map from the source position to target Go position.
@@ -228,6 +233,51 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
228233
Save: &lsp.SaveOptions{IncludeText: true},
229234
}
230235

236+
for _, c := range params.WorkspaceFolders {
237+
path := strings.TrimPrefix(c.URI, "file://")
238+
werr := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
239+
if err != nil {
240+
return err
241+
}
242+
uri := uri.URI("file://" + path)
243+
isTemplFile, _ := convertTemplToGoURI(uri)
244+
if isTemplFile {
245+
b, err := os.ReadFile(path)
246+
if err != nil {
247+
return err
248+
}
249+
p.Log.Info("found file", zap.String("path", path))
250+
if !isTemplFile {
251+
return fmt.Errorf("not a templ file")
252+
}
253+
p.TemplSource.Set(string(uri), NewDocument(p.Log, string(b)))
254+
// Parse the template.
255+
template, ok, err := p.parseTemplate(ctx, uri, string(b))
256+
if err != nil {
257+
p.Log.Error("parseTemplate failure", zap.Error(err))
258+
}
259+
if !ok {
260+
p.Log.Info("parsing template did not succeed", zap.String("uri", string(uri)))
261+
return nil
262+
}
263+
w := new(strings.Builder)
264+
sm, _, err := generator.Generate(template, w)
265+
if err != nil {
266+
return fmt.Errorf("generate failure: %w", err)
267+
}
268+
p.Log.Info("setting source map cache contents", zap.String("uri", string(uri)))
269+
p.SourceMapCache.Set(string(uri), sm)
270+
// Set the Go contents.
271+
p.GoSource[string(uri)] = w.String()
272+
273+
}
274+
return nil
275+
})
276+
if werr != nil {
277+
p.Log.Error("walk error", zap.Error(werr))
278+
}
279+
}
280+
231281
result.ServerInfo.Name = "templ-lsp"
232282
result.ServerInfo.Version = templ.Version()
233283

0 commit comments

Comments
 (0)