summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/files.go
blob: 1a211e3ea5950a9da670cd954bd6d510e2f0d0f9 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main

import (
	"io/ioutil"
	"os"
	"strings"
)

func LoadNonemptyLines(fname string, joinContinuationLines bool) []*Line {
	lines, err := readLines(fname, joinContinuationLines)
	if err != nil {
		errorf(fname, NO_LINES, "Cannot be read.")
		return nil
	}
	if len(lines) == 0 {
		errorf(fname, NO_LINES, "Must not be empty.")
		return nil
	}
	return lines
}

func LoadExistingLines(fname string, foldBackslashLines bool) []*Line {
	lines, err := readLines(fname, foldBackslashLines)
	if lines == nil || err != nil {
		fatalf(fname, NO_LINES, "Cannot be read.")
	}
	return lines
}

func getLogicalLine(fname string, rawLines []*RawLine, pindex *int) *Line {
	text := ""
	index := *pindex
	firstlineno := rawLines[index].lineno
	var lineRawLines []*RawLine
	interestingRawLines := rawLines[index:]

	for i, rawLine := range interestingRawLines {
		indent, rawText, outdent, cont := splitRawLine(rawLine.textnl)

		if text == "" {
			text += indent
		}
		text += rawText
		lineRawLines = append(lineRawLines, rawLine)

		if cont == "\\" && i != len(interestingRawLines)-1 {
			text += " "
			index++
		} else {
			text += outdent + cont
			break
		}
	}

	lastlineno := rawLines[index].lineno
	*pindex = index + 1

	if firstlineno == lastlineno {
		return NewLine(fname, sprintf("%d", firstlineno), text, lineRawLines)
	} else {
		return NewLine(fname, sprintf("%d--%d", firstlineno, lastlineno), text, lineRawLines)
	}
}

func splitRawLine(textnl string) (leadingWhitespace, text, trailingWhitespace, cont string) {
	m1234 := strings.TrimSuffix(textnl, "\n")
	m234 := strings.TrimLeft(m1234, " \t")
	m23 := strings.TrimSuffix(m234, "\\")
	m2 := strings.TrimRight(m23, " \t")
	return m1234[:len(m1234)-len(m234)], m2, m23[len(m2):], m234[len(m23):]
}

func readLines(fname string, joinContinuationLines bool) ([]*Line, error) {
	rawText, err := ioutil.ReadFile(fname)
	if err != nil {
		return nil, err
	}

	return convertToLogicalLines(fname, string(rawText), joinContinuationLines), nil
}

func convertToLogicalLines(fname string, rawText string, joinContinuationLines bool) []*Line {
	var rawLines []*RawLine
	for lineno, rawLine := range strings.SplitAfter(rawText, "\n") {
		if rawLine != "" {
			rawLines = append(rawLines, &RawLine{1 + lineno, rawLine})
		}
	}

	var loglines []*Line
	if joinContinuationLines {
		for lineno := 0; lineno < len(rawLines); {
			loglines = append(loglines, getLogicalLine(fname, rawLines, &lineno))
		}
	} else {
		for _, rawLine := range rawLines {
			loglines = append(loglines, NewLine(fname, sprintf("%d", rawLine.lineno), strings.TrimSuffix(rawLine.textnl, "\n"), []*RawLine{rawLine}))
		}
	}

	if 0 < len(rawLines) && !hasSuffix(rawLines[len(rawLines)-1].textnl, "\n") {
		errorf(fname, sprintf("%d", rawLines[len(rawLines)-1].lineno), "File must end with a newline.")
	}

	return loglines
}

func saveAutofixChanges(lines []*Line) {
	if !G.opts.Autofix {
		return
	}

	changes := make(map[string][]*RawLine)
	changed := make(map[string]bool)
	for _, line := range lines {
		if line.changed {
			changed[line.fname] = true
		}
		changes[line.fname] = append(changes[line.fname], line.rawLines()...)
	}

	for fname := range changed {
		rawLines := changes[fname]
		tmpname := fname + ".pkglint.tmp"
		text := ""
		for _, rawLine := range rawLines {
			text += rawLine.textnl
		}
		err := ioutil.WriteFile(tmpname, []byte(text), 0777)
		if err != nil {
			errorf(tmpname, NO_LINES, "Cannot write.")
			continue
		}
		err = os.Rename(tmpname, fname)
		if err != nil {
			errorf(fname, NO_LINES, "Cannot overwrite with auto-fixed content.")
			continue
		}
		notef(fname, NO_LINES, "Has been auto-fixed. Please re-run pkglint.")
	}
}