Skip to content

Commit

Permalink
Merge branch 'main' into string-errs
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h authored Dec 12, 2023
2 parents bc37302 + 4911b5d commit 11fda12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.481
0.2.486
22 changes: 21 additions & 1 deletion cmd/templ/lspcmd/proxy/diagnosticcache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package proxy

import (
lsp "github.com/a-h/protocol"
"sync"

lsp "github.com/a-h/protocol"
)

func NewDiagnosticCache() *DiagnosticCache {
Expand All @@ -22,20 +23,39 @@ type DiagnosticCache struct {
cache map[string]fileDiagnostic
}

func zeroLengthSliceIfNil(diags []lsp.Diagnostic) []lsp.Diagnostic {
if diags == nil {
return make([]lsp.Diagnostic, 0)
}
return diags
}

func (dc *DiagnosticCache) AddTemplDiagnostics(uri string, goDiagnostics []lsp.Diagnostic) []lsp.Diagnostic {
goDiagnostics = zeroLengthSliceIfNil(goDiagnostics)
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.goplsDiagnostics = goDiagnostics
diag.templDiagnostics = zeroLengthSliceIfNil(diag.templDiagnostics)
dc.cache[uri] = diag
return append(diag.templDiagnostics, goDiagnostics...)
}

func (dc *DiagnosticCache) ClearTemplDiagnostics(uri string) {
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.templDiagnostics = make([]lsp.Diagnostic, 0)
dc.cache[uri] = diag
}

func (dc *DiagnosticCache) AddGoDiagnostics(uri string, templDiagnostics []lsp.Diagnostic) []lsp.Diagnostic {
templDiagnostics = zeroLengthSliceIfNil(templDiagnostics)
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.templDiagnostics = templDiagnostics
diag.goplsDiagnostics = zeroLengthSliceIfNil(diag.goplsDiagnostics)
dc.cache[uri] = diag
return append(diag.goplsDiagnostics, templDiagnostics...)
}
6 changes: 4 additions & 2 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText st
return
}
// Clear templ diagnostics.
p.DiagnosticCache.ClearTemplDiagnostics(string(uri))
err = p.Client.PublishDiagnostics(ctx, &lsp.PublishDiagnosticsParams{
URI: uri,
Diagnostics: p.DiagnosticCache.AddGoDiagnostics(string(uri), nil),
URI: uri,
// Cannot be nil as per https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#publishDiagnosticsParams
Diagnostics: []lsp.Diagnostic{},
})
if err != nil {
p.Log.Error("failed to publish diagnostics", zap.Error(err))
Expand Down

0 comments on commit 11fda12

Please sign in to comment.