11package core
22
33import (
4+ "fmt"
5+ "sort"
6+
47 "alon.kr/x/list"
58 "github.com/fatih/color"
69)
@@ -27,23 +30,59 @@ type ResultDetails struct {
2730type ResultList = list.List [Result ]
2831
2932type ResultStringer struct {
30- titles map [ResultType ]string
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+ }
3145}
3246
33- func NewResultStringer () ResultStringer {
47+ func calculateLineStartsFromSource (ctx SourceContext ) []UsmUint {
48+ starts := []UsmUint {0 }
49+ for i , c := range ctx {
50+ if c == '\n' {
51+ starts = append (starts , UsmUint (i + 1 ))
52+ }
53+ }
54+ return starts
55+ }
56+
57+ func NewResultStringer (ctx SourceContext , Filepath string ) ResultStringer {
3458 return ResultStringer {
35- titles : map [ResultType ]string {
36- InternalErrorResult : color .New (color .Bold , color .BgRed , color .FgWhite ).Sprint ("panic:" ),
37- ErrorResult : color .New (color .Bold , color .FgRed ).Sprint ("error:" ),
38- WarningResult : color .New (color .Bold , color .FgYellow ).Sprint ("warning:" ),
39- HintResult : color .New (color .Bold , color .FgCyan ).Sprint ("note:" ),
40- },
59+ Titles : newTitleStrings (),
60+ LineStarts : calculateLineStartsFromSource (ctx ),
61+ Filepath : Filepath ,
4162 }
4263}
4364
65+ func (w * ResultStringer ) viewToLocation (
66+ view UnmanagedSourceView ,
67+ ) (line UsmUint , col UsmUint ) {
68+ start := view .Start
69+ row := sort .Search (len (w .LineStarts ), func (i int ) bool {
70+ return start < w .LineStarts [i ]
71+ }) - 1
72+ col = start - w .LineStarts [row ]
73+ return UsmUint (row ), UsmUint (col )
74+ }
75+
4476func (w * ResultStringer ) StringResultDetails (details ResultDetails ) string {
45- title := w .titles [details .Type ]
46- return title + " " + details .Message
77+ location := w .Filepath
78+ if details .Location != nil {
79+ row , col := w .viewToLocation (* details .Location )
80+ location += fmt .Sprintf (":%d:%d" , row + 1 , col + 1 )
81+ }
82+
83+ title := w .Titles [details .Type ]
84+ message := details .Message
85+ return location + ": " + title + " " + message
4786}
4887
4988func (w * ResultStringer ) StringResult (result Result ) string {
0 commit comments