s/Pprint/PPrint/ everywhere.

This commit is contained in:
Qi Xiao 2018-10-13 16:51:00 +01:00
parent ad1a28adf0
commit 24e0449318
15 changed files with 58 additions and 58 deletions

View File

@ -11,11 +11,11 @@ import (
// Can be changed for testing.
var stderr io.Writer = os.Stderr
// PprintError pretty-prints an error. It uses the Pprint method if the error
// implements Pprinter, and uses Complain to print the error message otherwise.
func PprintError(err error) {
if pprinter, ok := err.(util.Pprinter); ok {
fmt.Fprintln(stderr, pprinter.Pprint(""))
// PPrintError pretty-prints an error. It uses the PPrint method if the error
// implements PPrinter, and uses Complain to print the error message otherwise.
func PPrintError(err error) {
if pprinter, ok := err.(util.PPrinter); ok {
fmt.Fprintln(stderr, pprinter.PPrint(""))
} else {
Complain(err.Error())
}

View File

@ -22,23 +22,23 @@ type pprinterError struct{}
func (pprinterError) Error() string { return "error" }
func (pprinterError) Pprint(_ string) string { return "pprint" }
func (pprinterError) PPrint(_ string) string { return "pprint" }
var pprintErrorTests = []struct {
name string
err error
wantBuf string
}{
{"A Pprinter error", pprinterError{}, "pprint\n"},
{"A PPrinter error", pprinterError{}, "pprint\n"},
{"A errors.New error", errors.New("ERROR"), "\033[31;1mERROR\033[m\n"},
}
func TestPprintError(t *testing.T) {
func TestPPrintError(t *testing.T) {
for _, test := range pprintErrorTests {
t.Run(test.name, func(t *testing.T) {
setup()
defer teardown()
PprintError(test.err)
PPrintError(test.err)
if stderrBuf.String() != test.wantBuf {
t.Errorf("Wrote %q, want %q", stderrBuf.String(), test.wantBuf)
}

View File

@ -17,7 +17,7 @@ type SourceRange struct {
Begin int
End int
savedPprintInfo *rangePprintInfo
savedPPrintInfo *rangePPrintInfo
}
// NewSourceRange creates a new SourceRange.
@ -26,7 +26,7 @@ func NewSourceRange(name, source string, begin, end int) *SourceRange {
}
// Information about the source range that are needed for pretty-printing.
type rangePprintInfo struct {
type rangePPrintInfo struct {
// Head is the piece of text immediately before Culprit, extending to, but
// not including the closest line boundary. If Culprit already starts after
// a line boundary, Head is an empty string.
@ -50,9 +50,9 @@ var (
culpritPlaceHolder = "^"
)
func (sr *SourceRange) pprintInfo() *rangePprintInfo {
if sr.savedPprintInfo != nil {
return sr.savedPprintInfo
func (sr *SourceRange) pprintInfo() *rangePPrintInfo {
if sr.savedPPrintInfo != nil {
return sr.savedPPrintInfo
}
before := sr.Source[:sr.Begin]
@ -72,12 +72,12 @@ func (sr *SourceRange) pprintInfo() *rangePprintInfo {
endLine := beginLine + strings.Count(culprit, "\n")
sr.savedPprintInfo = &rangePprintInfo{head, culprit, tail, beginLine, endLine}
return sr.savedPprintInfo
sr.savedPPrintInfo = &rangePPrintInfo{head, culprit, tail, beginLine, endLine}
return sr.savedPPrintInfo
}
// Pprint pretty-prints a SourceContext.
func (sr *SourceRange) Pprint(sourceIndent string) string {
// PPrint pretty-prints a SourceContext.
func (sr *SourceRange) PPrint(sourceIndent string) string {
if err := sr.checkPosition(); err != nil {
return err.Error()
}
@ -85,9 +85,9 @@ func (sr *SourceRange) Pprint(sourceIndent string) string {
"\n" + sourceIndent + sr.relevantSource(sourceIndent))
}
// PprintCompact pretty-prints a SourceContext, with no line break between the
// PPrintCompact pretty-prints a SourceContext, with no line break between the
// source position range description and relevant source excerpt.
func (sr *SourceRange) PprintCompact(sourceIndent string) string {
func (sr *SourceRange) PPrintCompact(sourceIndent string) string {
if err := sr.checkPosition(); err != nil {
return err.Error()
}

View File

@ -8,8 +8,8 @@ import (
var sourceRangeTests = []struct {
*SourceRange
indent string
wantPprint string
wantPprintCompact string
wantPPrint string
wantPPrintCompact string
}{
// Single-line culprit
{parseSourceRange("echo (bad)", "(", ")", true), "_",
@ -42,15 +42,15 @@ func TestSourceRange(t *testing.T) {
culpritLineBegin = "<"
culpritLineEnd = ">"
for i, test := range sourceRangeTests {
gotPprint := test.SourceRange.Pprint(test.indent)
if gotPprint != test.wantPprint {
t.Errorf("test%d.Pprint(%q) = %q, want %q",
i, test.indent, gotPprint, test.wantPprint)
gotPPrint := test.SourceRange.PPrint(test.indent)
if gotPPrint != test.wantPPrint {
t.Errorf("test%d.PPrint(%q) = %q, want %q",
i, test.indent, gotPPrint, test.wantPPrint)
}
gotPprintCompact := test.SourceRange.PprintCompact(test.indent)
if gotPprintCompact != test.wantPprintCompact {
t.Errorf("test%d.PprintCompact(%q) = %q, want %q",
i, test.indent, gotPprintCompact, test.wantPprintCompact)
gotPPrintCompact := test.SourceRange.PPrintCompact(test.indent)
if gotPPrintCompact != test.wantPPrintCompact {
t.Errorf("test%d.PPrintCompact(%q) = %q, want %q",
i, test.indent, gotPPrintCompact, test.wantPPrintCompact)
}
}
}

View File

@ -223,9 +223,9 @@ func (c *completion) start(ed eddefs.Editor, acceptSingleton bool) {
ed.AddTip("%v", err)
// We don't show the full stack trace. To make debugging still possible,
// we log it.
if pprinter, ok := err.(util.Pprinter); ok {
if pprinter, ok := err.(util.PPrinter); ok {
logger.Println("matcher error:")
logger.Println(pprinter.Pprint(""))
logger.Println(pprinter.PPrint(""))
}
} else if completer == "" {
ed.AddTip("unsupported completion :(")

View File

@ -18,12 +18,12 @@ func (ce *CompilationError) Error() string {
ce.Context.Begin, ce.Context.End, ce.Context.Name, ce.Message)
}
// Pprint pretty-prints a compilation error.
func (ce *CompilationError) Pprint(indent string) string {
// PPrint pretty-prints a compilation error.
func (ce *CompilationError) PPrint(indent string) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Compilation error: \033[31;1m%s\033[m\n", ce.Message)
buf.WriteString(ce.Context.PprintCompact(indent + " "))
buf.WriteString(ce.Context.PPrintCompact(indent + " "))
return buf.String()
}

View File

@ -39,24 +39,24 @@ func (exc *Exception) Error() string {
return exc.Cause.Error()
}
func (exc *Exception) Pprint(indent string) string {
func (exc *Exception) PPrint(indent string) string {
buf := new(bytes.Buffer)
var causeDescription string
if pprinter, ok := exc.Cause.(util.Pprinter); ok {
causeDescription = pprinter.Pprint(indent)
if pprinter, ok := exc.Cause.(util.PPrinter); ok {
causeDescription = pprinter.PPrint(indent)
} else {
causeDescription = "\033[31;1m" + exc.Cause.Error() + "\033[m"
}
fmt.Fprintf(buf, "Exception: %s\n", causeDescription)
if exc.Traceback.next == nil {
buf.WriteString(exc.Traceback.entry.PprintCompact(indent))
buf.WriteString(exc.Traceback.entry.PPrintCompact(indent))
} else {
buf.WriteString(indent + "Traceback:")
for tb := exc.Traceback; tb != nil; tb = tb.next {
buf.WriteString("\n" + indent + " ")
buf.WriteString(tb.entry.Pprint(indent + " "))
buf.WriteString(tb.entry.PPrint(indent + " "))
}
}
@ -66,7 +66,7 @@ func (exc *Exception) Pprint(indent string) string {
if e == OK {
continue
}
buf.WriteString("\n" + indent + " " + e.Pprint(indent+" "))
buf.WriteString("\n" + indent + " " + e.PPrint(indent+" "))
}
}
@ -208,7 +208,7 @@ func (f Flow) Error() string {
return flowNames[f]
}
func (f Flow) Pprint(string) string {
func (f Flow) PPrint(string) string {
return "\033[33;1m" + f.Error() + "\033[m"
}

View File

@ -26,7 +26,7 @@ func (ae ActionError) Repr(int) string {
return "?(edit:" + ae.Error() + ")"
}
// Pprint pretty-prints ae to the terminal.
func (ae ActionError) Pprint(string) string {
// PPrint pretty-prints ae to the terminal.
func (ae ActionError) PPrint(string) string {
return "\033[33;1m" + ae.Error() + "\033[m"
}

View File

@ -23,8 +23,8 @@ func TestActionError_Repr(t *testing.T) {
})
}
func TestActionError_Pprint(t *testing.T) {
tt.Test(t, tt.Fn("ActionError.Pprint", ActionError.Pprint), tt.Table{
func TestActionError_PPrint(t *testing.T) {
tt.Test(t, tt.Fn("ActionError.PPrint", ActionError.PPrint), tt.Table{
Args(ActionError(types.CommitCode), "").
Rets("\033[33;1mcommit-code\033[m"),
})

View File

@ -44,7 +44,7 @@ func (pe *Error) Error() string {
}
}
func (pe *Error) Pprint(indent string) string {
func (pe *Error) PPrint(indent string) string {
buf := new(bytes.Buffer)
switch len(pe.Entries) {
@ -53,14 +53,14 @@ func (pe *Error) Pprint(indent string) string {
case 1:
e := pe.Entries[0]
fmt.Fprintf(buf, "Parse error: \033[31;1m%s\033[m\n", e.Message)
buf.WriteString(e.Context.PprintCompact(indent + " "))
buf.WriteString(e.Context.PPrintCompact(indent + " "))
default:
fmt.Fprint(buf, "Multiple parse errors:")
for _, e := range pe.Entries {
buf.WriteString("\n" + indent + " ")
fmt.Fprintf(buf, "\033[31;1m%s\033[m\n", e.Message)
buf.WriteString(indent + " ")
buf.WriteString(e.Context.Pprint(indent + " "))
buf.WriteString(e.Context.PPrint(indent + " "))
}
}

View File

@ -55,7 +55,7 @@ var pprintParseTreeTests = tt.Table{
`),
}
func TestPprintParseTree(t *testing.T) {
func TestPPrintParseTree(t *testing.T) {
tt.Test(t, tt.Fn("PPrintParseTree (to string)", func(n Node) string {
var b strings.Builder
PPrintParseTree(n, &b)

View File

@ -42,7 +42,7 @@ func interact(ev *eval.Evaler, dataDir string, norc, newEdit bool) {
if !norc && dataDir != "" {
err := sourceRC(ev, dataDir)
if err != nil {
diag.PprintError(err)
diag.PPrintError(err)
}
}
@ -84,7 +84,7 @@ func interact(ev *eval.Evaler, dataDir string, norc, newEdit bool) {
err = ev.EvalSourceInTTY(eval.NewInteractiveSource(line))
if err != nil {
diag.PprintError(err)
diag.PPrintError(err)
}
}
}

View File

@ -12,7 +12,7 @@ import (
)
// script evaluates a script. The returned error contains enough context and can
// be printed as-is (with diag.PprintError).
// be printed as-is (with diag.PPrintError).
func script(ev *eval.Evaler, args []string, cmd, compileOnly bool) error {
arg0 := args[0]
ev.SetArgs(args[1:])

View File

@ -43,7 +43,7 @@ func (sh *Shell) Main(args []string) int {
if len(args) > 0 {
err := script(ev, args, sh.Cmd, sh.CompileOnly)
if err != nil {
diag.PprintError(err)
diag.PPrintError(err)
return 2
}
} else {

View File

@ -1,7 +1,7 @@
package util
// Pprinter wraps the Pprint function.
type Pprinter interface {
// Pprint takes an indentation string and pretty-prints.
Pprint(indent string) string
// PPrinter wraps the PPrint function.
type PPrinter interface {
// PPrint takes an indentation string and pretty-prints.
PPrint(indent string) string
}