v0.1.0 28f9d6e

context.go RawHistoryFolder

1// SPDX-License-Identifier: MIT
2
3package template
4
5import (
6	"fmt"
7	"strings"
8)
9
10// MaxErrors is the number of errors a [Context] keeps; later ones are
11// dropped. The reference keeps all of them, which lets a hostile file make the
12// list as long as the file.
13const MaxErrors = 1000
14
15// Error is a problem found while reading or evaluating a template.
16type Error struct {
17	// File is the name of the file the problem is in, or "".
18	File string
19	// Pos is where the problem is; the zero value means no position.
20	Pos Pos
21	// Raw is the message without the "file (Line: l, Col: c): " prefix.
22	Raw string
23
24	// fileOnly makes the prefix the file name alone, for errors of the YAML
25	// parser, which the reference reports without a position.
26	fileOnly bool
27
28	cause error
29}
30
31// Unwrap returns the error of package expr behind an evaluation error, if any.
32func (e *Error) Unwrap() error { return e.cause }
33
34// Error returns the message with its prefix, as the reference does:
35// "file.yml (Line: 3, Col: 5): message".
36func (e *Error) Error() string {
37	pos := e.Pos
38	if e.fileOnly {
39		pos = Pos{}
40	}
41	if p := errorPrefix(e.File, pos); p != "" {
42		return p + ": " + e.Raw
43	}
44	return e.Raw
45}
46
47func errorPrefix(file string, p Pos) string {
48	switch {
49	case file != "" && p.Line > 0:
50		return fmt.Sprintf("%s (Line: %d, Col: %d)", file, p.Line, p.Col)
51	case file != "":
52		return file
53	case p.Line > 0:
54		return fmt.Sprintf("(Line: %d, Col: %d)", p.Line, p.Col)
55	}
56	return ""
57}
58
59// message is an error whose text is one of the reference's messages, which
60// start with a capital letter.
61type message string
62
63func (m message) Error() string { return string(m) }
64
65// Context collects the errors of a read and carries its schema.
66type Context struct {
67	schema *Schema
68	errors []*Error
69	// Dropped counts the errors beyond [MaxErrors].
70	dropped int
71}
72
73// NewContext returns a context that validates against schema.
74func NewContext(schema *Schema) *Context { return &Context{schema: schema} }
75
76// Schema returns the schema the context validates against.
77func (c *Context) Schema() *Schema { return c.schema }
78
79// Errors returns the errors recorded so far.
80func (c *Context) Errors() []*Error { return append([]*Error(nil), c.errors...) }
81
82// HasErrors reports whether an error was recorded.
83func (c *Context) HasErrors() bool { return len(c.errors) > 0 }
84
85// Errorf records an error located at tok, which may be nil.
86func (c *Context) Errorf(tok Token, format string, a ...any) {
87	c.ErrorAt(tok, fmt.Sprintf(format, a...))
88}
89
90// ErrorAt records an error with message msg located at tok, which may be nil.
91func (c *Context) ErrorAt(tok Token, msg string) {
92	e := &Error{Raw: msg}
93	if tok != nil {
94		e.File, e.Pos = tok.File(), tok.Pos()
95	}
96	c.add(e)
97}
98
99// ErrorInFile records an error that is about a whole file.
100func (c *Context) ErrorInFile(file, msg string) { c.add(&Error{File: file, Raw: msg}) }
101
102// ErrorAtPos records an error at an explicit position of file.
103func (c *Context) ErrorAtPos(file string, p Pos, msg string) {
104	c.add(&Error{File: file, Pos: p, Raw: msg})
105}
106
107func (c *Context) add(e *Error) {
108	if len(c.errors) >= MaxErrors {
109		c.dropped++
110		return
111	}
112	c.errors = append(c.errors, e)
113}
114
115// Messages returns the full text of the errors.
116func (c *Context) Messages() []string {
117	out := make([]string, len(c.errors))
118	for i, e := range c.errors {
119		out[i] = e.Error()
120	}
121	return out
122}
123
124// Join returns the errors as one text, one per line.
125func (c *Context) Join() string { return strings.Join(c.Messages(), "\n") }

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