diff options
Diffstat (limited to 'pkgtools/pkglint/files/line.go')
-rw-r--r-- | pkgtools/pkglint/files/line.go | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/pkgtools/pkglint/files/line.go b/pkgtools/pkglint/files/line.go index c1bc1d9eaaf..66515957241 100644 --- a/pkgtools/pkglint/files/line.go +++ b/pkgtools/pkglint/files/line.go @@ -46,6 +46,7 @@ func NewLine(fname, linenos, text string, rawLines []*RawLine) *Line { func (self *Line) rawLines() []*RawLine { return append(self.before, append(self.raw, self.after...)...) } + func (self *Line) printSource(out io.Writer) { if G.opts.PrintSource { io.WriteString(out, "\n") @@ -54,6 +55,7 @@ func (self *Line) printSource(out io.Writer) { } } } + func (self *Line) fatalf(format string, args ...interface{}) bool { self.printSource(G.logErr) return fatalf(self.fname, self.lines, format, args...) @@ -74,6 +76,7 @@ func (self *Line) debugf(format string, args ...interface{}) bool { self.printSource(G.logOut) return debugf(self.fname, self.lines, format, args...) } + func (self *Line) explain(explanation ...string) { if G.opts.Explain { complete := strings.Join(explanation, "\n") @@ -93,38 +96,32 @@ func (self *Line) explain(explanation ...string) { } G.explanationsAvailable = true } + func (self *Line) String() string { return self.fname + ":" + self.lines + ": " + self.text } func (self *Line) insertBefore(line string) { self.before = append(self.before, &RawLine{0, line + "\n"}) - self.changed = true - if G.opts.PrintAutofix { - self.notef("Autofix: inserting a line %q before this line.", line) - } + self.noteAutofix("Autofix: inserting a line %q before this line.", line) } + func (self *Line) insertAfter(line string) { self.after = append(self.after, &RawLine{0, line + "\n"}) - self.changed = true - if G.opts.PrintAutofix { - self.notef("Autofix: inserting a line %q after this line.", line) - } + self.noteAutofix("Autofix: inserting a line %q after this line.", line) } + func (self *Line) delete() { self.raw = nil self.changed = true } + func (self *Line) replace(from, to string) { for _, rawLine := range self.raw { if rawLine.lineno != 0 { if replaced := strings.Replace(rawLine.textnl, from, to, 1); replaced != rawLine.textnl { rawLine.textnl = replaced - self.changed = true - if G.opts.PrintAutofix { - self.notef("Autofix: replacing %q with %q.", from, to) - } - G.autofixAvailable = true + self.noteAutofix("Autofix: replacing %q with %q.", from, to) } } } @@ -134,12 +131,16 @@ func (self *Line) replaceRegex(from, to string) { if rawLine.lineno != 0 { if replaced := regcomp(from).ReplaceAllString(rawLine.textnl, to); replaced != rawLine.textnl { rawLine.textnl = replaced - self.changed = true - if G.opts.PrintAutofix { - self.notef("Autofix: replacing regular expression %q with %q.", from, to) - } - G.autofixAvailable = true + self.noteAutofix("Autofix: replacing regular expression %q with %q.", from, to) } } } } + +func (line *Line) noteAutofix(format string, args ...interface{}) { + line.changed = true + if G.opts.Autofix || G.opts.PrintAutofix { + line.notef(format, args...) + } + G.autofixAvailable = true +} |