mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 18:07:51 +08:00
Remove ContextualError.
This commit is contained in:
parent
be3b938bcf
commit
7efeb9c56f
|
@ -138,8 +138,8 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error {
|
||||||
if tips && !atEnd(err, len(src)) {
|
if tips && !atEnd(err, len(src)) {
|
||||||
ed.addTip("compiler error: %s", err)
|
ed.addTip("compiler error: %s", err)
|
||||||
}
|
}
|
||||||
if err, ok := err.(*util.ContextualError); ok {
|
if err, ok := err.(*util.PosError); ok {
|
||||||
p := err.Pos()
|
p := err.Begin
|
||||||
for i, token := range ed.tokens {
|
for i, token := range ed.tokens {
|
||||||
if token.Node.Begin() <= p && p < token.Node.End() {
|
if token.Node.Begin() <= p && p < token.Node.End() {
|
||||||
ed.tokens[i].MoreStyle += styleForCompilerError
|
ed.tokens[i].MoreStyle += styleForCompilerError
|
||||||
|
@ -160,8 +160,6 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error {
|
||||||
|
|
||||||
func atEnd(e error, n int) bool {
|
func atEnd(e error, n int) bool {
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
case *util.ContextualError:
|
|
||||||
return e.Pos() == n
|
|
||||||
case *util.PosError:
|
case *util.PosError:
|
||||||
return e.Begin == n
|
return e.Begin == n
|
||||||
case *util.Errors:
|
case *util.Errors:
|
||||||
|
|
|
@ -185,9 +185,7 @@ func (ec *EvalCtx) PCall(f Caller, args []Value) (ex error) {
|
||||||
// errorf stops the ec.eval immediately by panicking with a diagnostic message.
|
// errorf stops the ec.eval immediately by panicking with a diagnostic message.
|
||||||
// The panic is supposed to be caught by ec.eval.
|
// The panic is supposed to be caught by ec.eval.
|
||||||
func (ec *EvalCtx) errorf(p int, format string, args ...interface{}) {
|
func (ec *EvalCtx) errorf(p int, format string, args ...interface{}) {
|
||||||
throw(util.NewContextualError(
|
throw(&util.PosError{p, p, fmt.Errorf(format, args...)})
|
||||||
fmt.Sprintf("%s (%s)", ec.name, ec.context), "error",
|
|
||||||
ec.text, p, format, args...))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SourceText evaluates a chunk of elvish source.
|
// SourceText evaluates a chunk of elvish source.
|
||||||
|
|
2
main.go
2
main.go
|
@ -225,8 +225,6 @@ func printError(err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch err := err.(type) {
|
switch err := err.(type) {
|
||||||
case *util.ContextualError:
|
|
||||||
fmt.Print(err.Pprint())
|
|
||||||
case *util.Errors:
|
case *util.Errors:
|
||||||
for _, e := range err.Errors {
|
for _, e := range err.Errors {
|
||||||
printError(e)
|
printError(e)
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ContextualError is an error associated with a particular point in a source
|
|
||||||
// text.
|
|
||||||
type ContextualError struct {
|
|
||||||
srcname string
|
|
||||||
title string
|
|
||||||
pos int
|
|
||||||
line string
|
|
||||||
lineno int
|
|
||||||
colno int
|
|
||||||
msg string
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewContextualError creates a new ContextualError.
|
|
||||||
func NewContextualError(srcname, title, text string, pos int, format string, args ...interface{}) *ContextualError {
|
|
||||||
lineno, colno, line := FindContext(text, pos)
|
|
||||||
return &ContextualError{srcname, title, pos, line, lineno, colno, fmt.Sprintf(format, args...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ContextualError) Pos() int {
|
|
||||||
return e.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error implements the error interface. It returns a compact representation of
|
|
||||||
// the error.
|
|
||||||
func (e *ContextualError) Error() string {
|
|
||||||
return fmt.Sprintf("%s:%d:%d %s:%s", e.srcname, e.lineno, e.colno, e.title, e.msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pprint pretty-prints a ContextualError with the error position, the affected
|
|
||||||
// line an arrow pointing to the point of error and the error message, with
|
|
||||||
// colors.
|
|
||||||
func (e *ContextualError) Pprint() string {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
// Position info
|
|
||||||
fmt.Fprintf(buf, "\033[1m%s:%d:%d: ", e.srcname, e.lineno+1, e.colno+1)
|
|
||||||
// "error:"
|
|
||||||
fmt.Fprintf(buf, "\033[31m%s: ", e.title)
|
|
||||||
// Message
|
|
||||||
fmt.Fprintf(buf, "\033[m\033[1m%s\033[m\n", e.msg)
|
|
||||||
// Context: line
|
|
||||||
// TODO Handle long lines
|
|
||||||
fmt.Fprintf(buf, "%s\n", e.line)
|
|
||||||
// Context: arrow
|
|
||||||
// TODO Handle multi-width characters
|
|
||||||
fmt.Fprintf(buf, "%s\033[32;1m^\033[m\n", strings.Repeat(" ", e.colno))
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
|
@ -1,6 +1,10 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// PosError is an error associated with a position range.
|
// PosError is an error associated with a position range.
|
||||||
type PosError struct {
|
type PosError struct {
|
||||||
|
@ -12,3 +16,23 @@ type PosError struct {
|
||||||
func (pe *PosError) Error() string {
|
func (pe *PosError) Error() string {
|
||||||
return fmt.Sprintf("%d-%d: %s", pe.Begin, pe.End, pe.Err.Error())
|
return fmt.Sprintf("%d-%d: %s", pe.Begin, pe.End, pe.Err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pprint pretty-prints a PosError with a header indicating the source and type
|
||||||
|
// of the error, the error text and the affected line with an additional line
|
||||||
|
// that points an arrow at the affected column.
|
||||||
|
func (pe *PosError) Pprint(srcname, errtype, src string) string {
|
||||||
|
lineno, colno, line := FindContext(src, pe.Begin)
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
// Source and type of the error
|
||||||
|
fmt.Fprintf(buf, "\033[1m%s:%d:%d: \033[31m%s:", srcname, lineno+1, colno+1, errtype)
|
||||||
|
// Message
|
||||||
|
fmt.Fprintf(buf, "\033[m\033[1m%s\033[m\n", pe.Err.Error())
|
||||||
|
// Affected line
|
||||||
|
// TODO Handle long lines
|
||||||
|
fmt.Fprintf(buf, "%s\n", line)
|
||||||
|
// Column indicator
|
||||||
|
// TODO Handle multi-width characters
|
||||||
|
fmt.Fprintf(buf, "%s\033[32;1m^\033[m\n", strings.Repeat(" ", colno))
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user