Skip to content

Commit

Permalink
feat(lsp): add workspace folder initialization
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
tris203 committed Sep 9, 2024
1 parent 88eed0f commit d072b8d
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package proxy
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/a-h/parse"
lsp "github.com/a-h/protocol"
"go.lsp.dev/uri"
"go.uber.org/zap"

"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/imports"
"github.com/a-h/templ/generator"
"github.com/a-h/templ/parser/v2"
"go.lsp.dev/uri"
"go.uber.org/zap"
)

// Server is responsible for rewriting messages that are
Expand Down Expand Up @@ -81,6 +84,7 @@ func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.
var sourceMap *parser.SourceMap
sourceMap, ok = p.SourceMapCache.Get(string(templURI))
if !ok {
p.Log.Warn("templ->go: sourcemap not found in cache")
return
}
// Map from the source position to target Go position.
Expand All @@ -101,6 +105,7 @@ func (p *Server) convertGoRangeToTemplRange(templURI lsp.DocumentURI, input lsp.
output = input
sourceMap, ok := p.SourceMapCache.Get(string(templURI))
if !ok {
p.Log.Warn("go->templ: sourcemap not found in cache")
return
}
// Map from the source position to target Go position.
Expand Down Expand Up @@ -228,6 +233,51 @@ func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (
Save: &lsp.SaveOptions{IncludeText: true},
}

for _, c := range params.WorkspaceFolders {
path := strings.TrimPrefix(c.URI, "file://")
werr := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
uri := uri.URI("file://" + path)
isTemplFile, _ := convertTemplToGoURI(uri)
if isTemplFile {
b, err := os.ReadFile(path)
if err != nil {
return err
}
p.Log.Info("found file", zap.String("path", path))
if !isTemplFile {
return fmt.Errorf("not a templ file")
}
p.TemplSource.Set(string(uri), NewDocument(p.Log, string(b)))
// Parse the template.
template, ok, err := p.parseTemplate(ctx, uri, string(b))
if err != nil {
p.Log.Error("parseTemplate failure", zap.Error(err))
}
if !ok {
p.Log.Info("parsing template did not succeed", zap.String("uri", string(uri)))
return nil
}
w := new(strings.Builder)
sm, _, err := generator.Generate(template, w)
if err != nil {
return fmt.Errorf("generate failure: %w", err)
}
p.Log.Info("setting source map cache contents", zap.String("uri", string(uri)))
p.SourceMapCache.Set(string(uri), sm)
// Set the Go contents.
p.GoSource[string(uri)] = w.String()

}
return nil
})
if werr != nil {
p.Log.Error("walk error", zap.Error(werr))
}
}

result.ServerInfo.Name = "templ-lsp"
result.ServerInfo.Version = templ.Version()

Expand Down

0 comments on commit d072b8d

Please sign in to comment.