@@ -3,17 +3,20 @@ package proxy
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
7
+ "path/filepath"
6
8
"regexp"
7
9
"strings"
8
10
9
11
"github.com/a-h/parse"
10
12
lsp "github.com/a-h/protocol"
13
+ "go.lsp.dev/uri"
14
+ "go.uber.org/zap"
15
+
11
16
"github.com/a-h/templ"
12
17
"github.com/a-h/templ/cmd/templ/imports"
13
18
"github.com/a-h/templ/generator"
14
19
"github.com/a-h/templ/parser/v2"
15
- "go.lsp.dev/uri"
16
- "go.uber.org/zap"
17
20
)
18
21
19
22
// Server is responsible for rewriting messages that are
@@ -81,6 +84,7 @@ func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.
81
84
var sourceMap * parser.SourceMap
82
85
sourceMap , ok = p .SourceMapCache .Get (string (templURI ))
83
86
if ! ok {
87
+ p .Log .Warn ("templ->go: sourcemap not found in cache" )
84
88
return
85
89
}
86
90
// Map from the source position to target Go position.
@@ -101,6 +105,7 @@ func (p *Server) convertGoRangeToTemplRange(templURI lsp.DocumentURI, input lsp.
101
105
output = input
102
106
sourceMap , ok := p .SourceMapCache .Get (string (templURI ))
103
107
if ! ok {
108
+ p .Log .Warn ("go->templ: sourcemap not found in cache" )
104
109
return
105
110
}
106
111
// Map from the source position to target Go position.
@@ -228,6 +233,51 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
228
233
Save : & lsp.SaveOptions {IncludeText : true },
229
234
}
230
235
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
+
231
281
result .ServerInfo .Name = "templ-lsp"
232
282
result .ServerInfo .Version = templ .Version ()
233
283
0 commit comments