From 20747fcf104aa05bdc4d7d2b12a2488940bcae2f Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Wed, 10 May 2023 23:56:24 +0200 Subject: commands: expand and complete template code Expand and complete template code. The exline does understand template code and can evaluate it. With this patch, the completion systems supports writing the templates by showing the available template data fields and template functions. Pressing after }} will show the evaluated template in the completion menu. Complex template code, such as if-else statements work as well. Examples: :filter -f {{ will show the possible template data fields and functions :filter -f {{index (.To|email) 0}} will show the value of the template expression in the completion list Pressing twice after a completed template expression will substitute the expression with its value. :{{if match .Folder "INBOX"}}check-mail{{else}}cf INBOX{{end}} Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/templates/template.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'lib') diff --git a/lib/templates/template.go b/lib/templates/template.go index bf257669..6eae591a 100644 --- a/lib/templates/template.go +++ b/lib/templates/template.go @@ -6,6 +6,7 @@ import ( "io" "os" "path" + "reflect" "text/template" "git.sr.ht/~rjarry/aerc/models" @@ -56,3 +57,54 @@ func ParseTemplate(name, content string) (*template.Template, error) { func Render(t *template.Template, w io.Writer, data models.TemplateData) error { return t.Execute(w, data) } + +// builtins is a slice of keywords and functions built into the Go standard +// library for templates. Since they are not exported, they are hardcoded here. +var builtins = []string{ + // from the Go standard library: src/text/template/parse/lex.go + "block", + "break", + "continue", + "define", + "else", + "end", + "if", + "range", + "nil", + "template", + "with", + + // from the Go standard library: src/text/template/funcs.go + "and", + "call", + "html", + "index", + "slice", + "js", + "len", + "not", + "or", + "print", + "printf", + "println", + "urlquery", + "eq", + "ge", + "gt", + "le", + "lt", + "ne", +} + +func Terms() []string { + var s []string + t := reflect.TypeOf((*models.TemplateData)(nil)).Elem() + for i := 0; i < t.NumMethod(); i++ { + s = append(s, "."+t.Method(i).Name) + } + for fnStr := range templateFuncs { + s = append(s, fnStr) + } + s = append(s, builtins...) + return s +} -- cgit