summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/linechecker.go
blob: b384044f14197a26a85adcfaea8cb24b2ac363e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pkglint

import (
	"netbsd.org/pkglint/textproc"
	"strings"
)

type LineChecker struct {
	line *Line
}

func (ck LineChecker) CheckLength(maxLength int) {
	if len(ck.line.Text) <= maxLength {
		return
	}

	prefix := ck.line.Text[0:maxLength]
	if !strings.ContainsAny(prefix, " \t") {
		return
	}

	ck.line.Warnf("Line too long (should be no more than %d characters).",
		maxLength)
	ck.line.Explain(
		"Back in the old time, terminals with 80x25 characters were common.",
		"And this is still the default size of many terminal emulators.",
		"Moderately short lines also make reading easier.")
}

func (ck LineChecker) CheckValidCharacters() {
	invalid := invalidCharacters(ck.line.Text, textproc.XPrint)
	if invalid == "" {
		return
	}

	ck.line.Warnf("Line contains invalid characters (%s).", invalid)
}

func (ck LineChecker) CheckTrailingWhitespace() {

	// Markdown files may need trailing whitespace. If there should ever
	// be Markdown files in pkgsrc, this code has to be adjusted.

	rawIndex := len(ck.line.raw) - 1
	text := ck.line.raw[rawIndex].Text()
	trimmedLen := len(rtrimHspace(text))
	if trimmedLen == len(text) {
		return
	}

	fix := ck.line.Autofix()
	fix.Notef("Trailing whitespace.")
	fix.Explain(
		"This whitespace is irrelevant and can be removed.")
	fix.ReplaceAt(rawIndex, trimmedLen, text[trimmedLen:], "")
	fix.Apply()
}