Add file (line/col) info to ref/relref errors

See #5371
This commit is contained in:
Bjørn Erik Pedersen
2018-11-01 11:28:30 +01:00
parent 33a7b36fd4
commit 1d18eb0574
22 changed files with 412 additions and 297 deletions

View File

@@ -15,72 +15,21 @@
package herrors
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/gohugoio/hugo/common/terminal"
"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/helpers"
"github.com/spf13/afero"
)
var fileErrorFormatFunc func(e ErrorContext) string
func createFileLogFormatter(formatStr string) func(e ErrorContext) string {
if formatStr == "" {
formatStr = "\":file::line::col\""
}
var identifiers = []string{":file", ":line", ":col"}
var identifiersFound []string
for i := range formatStr {
for _, id := range identifiers {
if strings.HasPrefix(formatStr[i:], id) {
identifiersFound = append(identifiersFound, id)
}
}
}
replacer := strings.NewReplacer(":file", "%s", ":line", "%d", ":col", "%d")
format := replacer.Replace(formatStr)
f := func(e ErrorContext) string {
args := make([]interface{}, len(identifiersFound))
for i, id := range identifiersFound {
switch id {
case ":file":
args[i] = e.Filename
case ":line":
args[i] = e.LineNumber
case ":col":
args[i] = e.ColumnNumber
}
}
msg := fmt.Sprintf(format, args...)
if terminal.IsTerminal(os.Stdout) {
return terminal.Notice(msg)
}
return msg
}
return f
}
func init() {
fileErrorFormatFunc = createFileLogFormatter(os.Getenv("HUGO_FILE_LOG_FORMAT"))
}
// LineMatcher contains the elements used to match an error to a line
type LineMatcher struct {
FileError FileError
Position text.Position
Error error
LineNumber int
Offset int
Line string
@@ -91,33 +40,34 @@ type LineMatcherFn func(m LineMatcher) bool
// SimpleLineMatcher simply matches by line number.
var SimpleLineMatcher = func(m LineMatcher) bool {
return m.FileError.LineNumber() == m.LineNumber
return m.Position.LineNumber == m.LineNumber
}
var _ text.Positioner = ErrorContext{}
// ErrorContext contains contextual information about an error. This will
// typically be the lines surrounding some problem in a file.
type ErrorContext struct {
// The source filename.
Filename string
// If a match will contain the matched line and up to 2 lines before and after.
// Will be empty if no match.
Lines []string
// The position of the error in the Lines above. 0 based.
Pos int
LinesPos int
// The linenumber in the source file from where the Lines start. Starting at 1.
LineNumber int
// The column number in the source file. Starting at 1.
ColumnNumber int
position text.Position
// The lexer to use for syntax highlighting.
// https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages
ChromaLexer string
}
// Position returns the text position of this error.
func (e ErrorContext) Position() text.Position {
return e.position
}
var _ causer = (*ErrorWithFileContext)(nil)
// ErrorWithFileContext is an error with some additional file context related
@@ -128,7 +78,11 @@ type ErrorWithFileContext struct {
}
func (e *ErrorWithFileContext) Error() string {
return fileErrorFormatFunc(e.ErrorContext) + ": " + e.cause.Error()
pos := e.Position()
if pos.IsValid() {
return pos.String() + ": " + e.cause.Error()
}
return e.cause.Error()
}
func (e *ErrorWithFileContext) Cause() error {
@@ -163,24 +117,27 @@ func WithFileContext(e error, realFilename string, r io.Reader, matcher LineMatc
var errCtx ErrorContext
if le.Offset() != -1 {
posle := le.Position()
if posle.Offset != -1 {
errCtx = locateError(r, le, func(m LineMatcher) bool {
if le.Offset() >= m.Offset && le.Offset() < m.Offset+len(m.Line) {
fe := m.FileError
m.FileError = ToFileErrorWithOffset(fe, -fe.LineNumber()+m.LineNumber)
if posle.Offset >= m.Offset && posle.Offset < m.Offset+len(m.Line) {
lno := posle.LineNumber - m.Position.LineNumber + m.LineNumber
m.Position = text.Position{LineNumber: lno}
}
return matcher(m)
})
} else {
errCtx = locateError(r, le, matcher)
}
if errCtx.LineNumber == -1 {
pos := &errCtx.position
if pos.LineNumber == -1 {
return e, false
}
errCtx.Filename = realFilename
pos.Filename = realFilename
if le.Type() != "" {
errCtx.ChromaLexer = chromaLexerFromType(le.Type())
@@ -233,17 +190,20 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) ErrorContext
panic("must provide an error")
}
errCtx := ErrorContext{LineNumber: -1, ColumnNumber: 1, Pos: -1}
errCtx := ErrorContext{position: text.Position{LineNumber: -1, ColumnNumber: 1, Offset: -1}, LinesPos: -1}
b, err := ioutil.ReadAll(r)
if err != nil {
return errCtx
}
pos := &errCtx.position
lepos := le.Position()
lines := strings.Split(string(b), "\n")
if le != nil && le.ColumnNumber() >= 0 {
errCtx.ColumnNumber = le.ColumnNumber()
if le != nil && lepos.ColumnNumber >= 0 {
pos.ColumnNumber = lepos.ColumnNumber
}
lineNo := 0
@@ -252,32 +212,33 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) ErrorContext
for li, line := range lines {
lineNo = li + 1
m := LineMatcher{
FileError: le,
Position: le.Position(),
Error: le,
LineNumber: lineNo,
Offset: posBytes,
Line: line,
}
if errCtx.Pos == -1 && matches(m) {
errCtx.LineNumber = lineNo
if errCtx.LinesPos == -1 && matches(m) {
pos.LineNumber = lineNo
break
}
posBytes += len(line)
}
if errCtx.LineNumber != -1 {
low := errCtx.LineNumber - 3
if pos.LineNumber != -1 {
low := pos.LineNumber - 3
if low < 0 {
low = 0
}
if errCtx.LineNumber > 2 {
errCtx.Pos = 2
if pos.LineNumber > 2 {
errCtx.LinesPos = 2
} else {
errCtx.Pos = errCtx.LineNumber - 1
errCtx.LinesPos = pos.LineNumber - 1
}
high := errCtx.LineNumber + 2
high := pos.LineNumber + 2
if high > len(lines) {
high = len(lines)
}