Skip to content
This repository was archived by the owner on Jul 10, 2026. It is now read-only.

Commit d2b5cd0

Browse files
committed
discover custom template functions from Go source
Scans Go source files in the workspace for template.FuncMap definitions and extracts function names to prevent false 'function undefined' errors when analyzing templates that use custom functions. On LSP initialize, the server now walks the workspace looking for Go files with template.FuncMap composite literals, extracts the function names as keys, and registers them with the analyzer. This allows templates to use project-specific functions like 'lower', 'dict', 'localtime' etc without triggering spurious errors.
1 parent 0036eb9 commit d2b5cd0

5 files changed

Lines changed: 37 additions & 4 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
# gozer-3cy0
3+
title: Custom Template Function Discovery via Go AST Scanning
4+
status: completed
5+
type: feature
6+
priority: normal
7+
created_at: 2026-01-18T20:00:15Z
8+
updated_at: 2026-01-18T20:03:56Z
9+
---
10+
11+
Automatically discover custom template functions from Go source code to prevent false 'function undefined' errors.
12+
13+
## Checklist
14+
15+
- [x] Create `gota/analyzer/funcmap_scanner.go` with AST scanning logic
16+
- [x] Modify `gota/analyzer/analyzer.go` to merge custom functions with builtins
17+
- [x] Modify `gota/gota.go` to add custom function API
18+
- [x] Modify `gozer/cmd/go-template-lsp/main.go` to scan on initialize
19+
- [x] Add tests for funcmap scanner
20+
- [x] Run tests in both repos to verify

cmd/go-template-lsp/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ func processDiagnosticNotification(storage *workspaceStore, rootPathNotification
269269

270270
rootPath = uriToFilePath(rootPath)
271271

272+
// Scan for custom template functions defined in Go source files
273+
customFuncs, err := gota.ScanWorkspaceForFuncMap(rootPath)
274+
if err != nil {
275+
slog.Warn("failed to scan for custom template functions", slog.String("error", err.Error()))
276+
} else if len(customFuncs) > 0 {
277+
gota.SetWorkspaceCustomFunctions(customFuncs)
278+
funcNames := make([]string, 0, len(customFuncs))
279+
for name := range customFuncs {
280+
funcNames = append(funcNames, name)
281+
}
282+
slog.Info("discovered custom template functions", slog.Any("functions", funcNames))
283+
}
284+
272285
storage.RootPath = rootPath
273286
storage.RawFiles = gota.OpenProjectFiles(rootPath, TargetFileExtensions)
274287
storage.RawFiles = convertKeysFromFilePathToUri(storage.RawFiles)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ go 1.25.6
44

55
require github.com/yayolande/gota v0.8.4
66

7-
replace github.com/yayolande/gota => github.com/STR-Consulting/gota v0.8.4
7+
replace github.com/yayolande/gota => github.com/STR-Consulting/gota v0.8.5-0.20260118200612-37a5e41994a8

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/STR-Consulting/gota v0.8.4 h1:0eKo813kXDf87/d1kPsebNPd+K6fP4X9qB682XoFnlc=
2-
github.com/STR-Consulting/gota v0.8.4/go.mod h1:5kQtkYEDAKwdjlGKNuNGzs0mCJyUmkjCc2mj6Jeaq/I=
1+
github.com/STR-Consulting/gota v0.8.5-0.20260118200612-37a5e41994a8 h1:P//3nE1NoG+JoWf+JWI46P8p8ljHopDDcJ0OS4QzOBY=
2+
github.com/STR-Consulting/gota v0.8.5-0.20260118200612-37a5e41994a8/go.mod h1:5kQtkYEDAKwdjlGKNuNGzs0mCJyUmkjCc2mj6Jeaq/I=
33
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
44
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
55
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=

zed-ext/extension.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
id = "go-template-lsp"
22
name = "Go Template LSP"
3-
version = "0.2.2"
3+
version = "0.3.0"
44
schema_version = 1
55
authors = ["Jason Abbott", "Jason Staten", "yayolande", "Nikita Galaiko", "Mahmud Ridwan"]
66
description = "Go template support with LSP (hover, diagnostics, go-to-definition)"

0 commit comments

Comments
 (0)