@@ -3,6 +3,7 @@ package core
33import (
44 "fmt"
55 "sort"
6+ "strings"
67
78 "alon.kr/x/list"
89 "github.com/fatih/color"
@@ -30,18 +31,11 @@ type ResultDetails struct {
3031type ResultList = list.List [Result ]
3132
3233type ResultStringer struct {
33- Titles map [ResultType ]string
34- Filepath string
35- LineStarts []UsmUint
36- }
37-
38- func newTitleStrings () map [ResultType ]string {
39- return map [ResultType ]string {
40- InternalErrorResult : color .New (color .Bold , color .BgRed , color .FgWhite ).Sprint ("panic:" ),
41- ErrorResult : color .New (color .Bold , color .FgRed ).Sprint ("error:" ),
42- WarningResult : color .New (color .Bold , color .FgYellow ).Sprint ("warning:" ),
43- HintResult : color .New (color .Bold , color .FgCyan ).Sprint ("note:" ),
44- }
34+ SourceContext
35+ Titles map [ResultType ]string
36+ SourceErrorPointer string
37+ Filepath string
38+ LineStarts []UsmUint
4539}
4640
4741func calculateLineStartsFromSource (ctx SourceContext ) []UsmUint {
@@ -56,9 +50,16 @@ func calculateLineStartsFromSource(ctx SourceContext) []UsmUint {
5650
5751func NewResultStringer (ctx SourceContext , Filepath string ) ResultStringer {
5852 return ResultStringer {
59- Titles : newTitleStrings (),
60- LineStarts : calculateLineStartsFromSource (ctx ),
61- Filepath : Filepath ,
53+ SourceContext : ctx ,
54+ Titles : map [ResultType ]string {
55+ InternalErrorResult : color .New (color .Bold , color .BgRed , color .FgWhite ).Sprint ("panic:" ),
56+ ErrorResult : color .New (color .Bold , color .FgRed ).Sprint ("error:" ),
57+ WarningResult : color .New (color .Bold , color .FgYellow ).Sprint ("warning:" ),
58+ HintResult : color .New (color .Bold , color .FgCyan ).Sprint ("note:" ),
59+ },
60+ SourceErrorPointer : color .New (color .Bold , color .FgMagenta ).Sprint ("^" ),
61+ LineStarts : calculateLineStartsFromSource (ctx ),
62+ Filepath : Filepath ,
6263 }
6364}
6465
@@ -73,22 +74,48 @@ func (w *ResultStringer) viewToLocation(
7374 return UsmUint (row ), UsmUint (col )
7475}
7576
77+ func (w * ResultStringer ) getLine (row UsmUint ) string {
78+ lineStart := w .LineStarts [row ]
79+ var lineEnd UsmUint
80+ if row >= UsmUint (len (w .LineStarts )- 1 ) {
81+ lineEnd = UsmUint (len (w .SourceContext ))
82+ } else {
83+ lineEnd = w .LineStarts [row + 1 ]
84+ }
85+ return string (w .SourceContext [lineStart :lineEnd ])
86+ }
87+
88+ func (w * ResultStringer ) stringLineContext (row , col UsmUint ) string {
89+ firstLinePad := fmt .Sprintf ("%*d" , 5 , row + 1 )
90+ border := " | "
91+ line := w .getLine (row )
92+
93+ secondLinePad := strings .Repeat (" " , len (firstLinePad ))
94+ pointerLine := strings .Repeat (" " , int (col )) + w .SourceErrorPointer
95+
96+ firstLine := firstLinePad + border + line
97+ secondLine := secondLinePad + border + pointerLine
98+ return firstLine + "\n " + secondLine + "\n "
99+ }
100+
76101func (w * ResultStringer ) StringResultDetails (details ResultDetails ) string {
77102 location := w .Filepath
103+ suffix := ""
78104 if details .Location != nil {
79105 row , col := w .viewToLocation (* details .Location )
80106 location += fmt .Sprintf (":%d:%d" , row + 1 , col + 1 )
107+ suffix = w .stringLineContext (row , col )
81108 }
82109
83110 title := w .Titles [details .Type ]
84111 message := details .Message
85- return location + ": " + title + " " + message
112+ return location + ": " + title + " " + message + " \n " + suffix
86113}
87114
88115func (w * ResultStringer ) StringResult (result Result ) string {
89116 s := ""
90117 for _ , details := range result {
91- s += w .StringResultDetails (details ) + " \n "
118+ s += w .StringResultDetails (details )
92119 }
93120 return s
94121}
0 commit comments