Skip to content

Commit f545b99

Browse files
committed
Added location to results
1 parent 37da905 commit f545b99

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

core/result.go

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package core
22

33
import (
4+
"fmt"
5+
"sort"
6+
47
"alon.kr/x/list"
58
"github.com/fatih/color"
69
)
@@ -27,23 +30,59 @@ type ResultDetails struct {
2730
type ResultList = list.List[Result]
2831

2932
type 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+
4476
func (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

4988
func (w *ResultStringer) StringResult(result Result) string {

usm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
"alon.kr/x/usm/core"
89
"alon.kr/x/usm/lex"
910
"alon.kr/x/usm/parse"
1011
"github.com/spf13/cobra"
1112
)
1213

14+
var inputFilepath string = ""
15+
1316
func setInputSource(cmd *cobra.Command, args []string) error {
1417
if len(args) > 0 {
15-
file, err := os.Open(args[0])
18+
inputFilepath = filepath.Clean(args[0])
19+
file, err := os.Open(inputFilepath)
1620
if err != nil {
1721
return fmt.Errorf("error opening file: %v", err)
1822
}
@@ -61,7 +65,7 @@ func fmtCommand(cmd *cobra.Command, args []string) {
6165
strCtx := parse.StringContext{SourceContext: view.Ctx()}
6266
fmt.Print(file.String(&strCtx))
6367
} else {
64-
stringer := core.NewResultStringer()
68+
stringer := core.NewResultStringer(view.Ctx(), inputFilepath)
6569
fmt.Print(stringer.StringResult(result))
6670
}
6771
}

0 commit comments

Comments
 (0)