main 98594d0

definfo.go RawHistoryFolder

1// SPDX-License-Identifier: MIT
2
3package template
4
5import (
6	"strconv"
7	"strings"
8
9	"bnema.dev/ermite-actions/expr"
10)
11
12// DefinitionInfo is a definition together with the contexts and functions
13// expressions at that place may use: those of the definition and of every
14// definition it was reached through.
15type DefinitionInfo struct {
16	schema *Schema
17	// Definition is the schema definition.
18	Definition *Definition
19
20	allowed []string
21	named   []string
22	funcs   []expr.FuncInfo
23}
24
25func newDefinitionInfo(s *Schema, parent *DefinitionInfo, d *Definition) *DefinitionInfo {
26	di := &DefinitionInfo{schema: s, Definition: d}
27	if len(d.Context) > 0 {
28		seen := map[string]bool{}
29		if parent != nil {
30			for _, c := range parent.allowed {
31				di.allowed = append(di.allowed, c)
32				seen[strings.ToUpper(c)] = true
33			}
34		}
35		for _, c := range d.Context {
36			if u := strings.ToUpper(c); !seen[u] {
37				di.allowed = append(di.allowed, c)
38				seen[u] = true
39			}
40		}
41	} else if parent != nil {
42		di.allowed, di.named, di.funcs = parent.allowed, parent.named, parent.funcs
43		return di
44	}
45	di.named, di.funcs = splitAllowedContext(di.allowed)
46	return di
47}
48
49// child returns the info of the definition called name, reached through di.
50func (di *DefinitionInfo) child(name string) *DefinitionInfo {
51	return newDefinitionInfo(di.schema, di, di.schema.get(name))
52}
53
54// WithContexts returns a copy of di that also allows the named contexts and
55// functions ("vars", "hashFiles(1,255)"). The runner checks the "if" of a
56// composite step against a longer list than the manifest schema declares.
57func (di *DefinitionInfo) WithContexts(entries ...string) *DefinitionInfo {
58	cp := *di
59	cp.allowed = append(append([]string(nil), di.allowed...), entries...)
60	cp.named, cp.funcs = splitAllowedContext(cp.allowed)
61	return &cp
62}
63
64// AllowedContext returns the raw entries: context names and function
65// signatures such as "hashFiles(1,255)".
66func (di *DefinitionInfo) AllowedContext() []string { return di.allowed }
67
68// NamedContexts returns the names of the contexts expressions may use.
69func (di *DefinitionInfo) NamedContexts() []string { return di.named }
70
71// Functions returns the extra functions expressions may call, with their
72// arity.
73func (di *DefinitionInfo) Functions() []expr.FuncInfo { return di.funcs }
74
75// splitAllowedContext separates "name(min,max)" entries, with max a number or
76// MAX, from plain context names.
77func splitAllowedContext(allowed []string) ([]string, []expr.FuncInfo) {
78	var named []string
79	var funcs []expr.FuncInfo
80	for _, item := range allowed {
81		if f, ok := parseFuncEntry(item); ok {
82			funcs = append(funcs, f)
83		} else {
84			named = append(named, item)
85		}
86	}
87	return named, funcs
88}
89
90// parseFuncEntry matches ^([a-zA-Z0-9_]+)\(([0-9]+),([0-9]+|MAX)\)$.
91func parseFuncEntry(s string) (expr.FuncInfo, bool) {
92	open := strings.IndexByte(s, '(')
93	if open <= 0 || !strings.HasSuffix(s, ")") {
94		return expr.FuncInfo{}, false
95	}
96	name := s[:open]
97	for _, r := range name {
98		if r != '_' && (r < '0' || r > '9') && (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
99			return expr.FuncInfo{}, false
100		}
101	}
102	minS, maxS, ok := strings.Cut(s[open+1:len(s)-1], ",")
103	if !ok || !isDigits(minS) || (!isDigits(maxS) && maxS != "MAX") {
104		return expr.FuncInfo{}, false
105	}
106	lo, err := strconv.Atoi(minS)
107	if err != nil {
108		return expr.FuncInfo{}, false
109	}
110	hi := -1
111	if maxS != "MAX" {
112		if hi, err = strconv.Atoi(maxS); err != nil {
113			return expr.FuncInfo{}, false
114		}
115	}
116	return expr.FuncInfo{Name: name, MinArgs: lo, MaxArgs: hi}, true
117}
118
119func isDigits(s string) bool {
120	if s == "" {
121		return false
122	}
123	for i := 0; i < len(s); i++ {
124		if s[i] < '0' || s[i] > '9' {
125			return false
126		}
127	}
128	return true
129}
130
131// Info returns the info of the schema definition called name, with the
132// contexts and functions that definition allows, or nil when there is none.
133func (s *Schema) Info(name string) *DefinitionInfo {
134	d := s.definitions[name]
135	if d == nil {
136		return nil
137	}
138	return newDefinitionInfo(s, nil, d)
139}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 3

↑↓ moveenter openesc close