Skip to content

Commit

Permalink
use rangefuncs in report
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 28, 2025
1 parent 7102ee9 commit 90f9204
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 61 deletions.
7 changes: 3 additions & 4 deletions experimental/report/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func unifiedDiff(span Span, edits []Edit) (Span, []hunk) {

var out []hunk
var prevHunk int
parts(func(edits []Edit) bool {
for edits := range parts {
if len(edits) == 0 {
return true
continue
}

// First, figure out the start and end of the modified region.
Expand Down Expand Up @@ -129,8 +129,7 @@ func unifiedDiff(span Span, edits []Edit) (Span, []hunk) {
)

prevHunk = end
return true
})
}
return span, append(out, hunk{hunkUnchanged, src[prevHunk:]})
}

Expand Down
26 changes: 10 additions & 16 deletions experimental/report/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (r *renderer) diagnostic(report *Report, d Diagnostic) {
return snip.Path()
})

parts(func(i int, snippets []snippet) bool {
for i, snippets := range parts {
if i == 0 || d.snippets[i-1].Path() != d.snippets[i].Path() {
r.WriteString("\n")
r.WriteString(r.ss.nAccent)
Expand All @@ -243,7 +243,7 @@ func (r *renderer) diagnostic(report *Report, d Diagnostic) {
r.WriteString("\n")
}
r.suggestion(snippets[0])
return true
continue
}

// Add a blank line after the file. This gives the diagnostic window some
Expand All @@ -253,8 +253,7 @@ func (r *renderer) diagnostic(report *Report, d Diagnostic) {

window := buildWindow(d.level, locations[i:i+len(snippets)], snippets)
r.window(window)
return true
})
}

// Render a remedial file name for spanless errors.
if len(d.snippets) == 0 && d.inFile != "" {
Expand All @@ -274,10 +273,10 @@ func (r *renderer) diagnostic(report *Report, d Diagnostic) {
slicesx.Map(d.debug, func(s string) footer { return footer{r.ss.bError, "debug", s} }),
)

footers(func(f footer) bool {
for f := range footers {
isDebug := f.label == "debug"
if isDebug && !r.ShowDebug {
return true
continue
}

r.WriteString("\n")
Expand All @@ -289,9 +288,7 @@ func (r *renderer) diagnostic(report *Report, d Diagnostic) {
} else {
r.WriteWrapped(f.text, MaxMessageWidth)
}

return true
})
}

r.WriteString(r.ss.reset)
r.WriteString("\n\n")
Expand Down Expand Up @@ -486,7 +483,7 @@ func (r *renderer) window(w *window) {
// Next, we can render the underline parts. This aggregates all underlines
// for the same line into rendered chunks
parts := slicesx.PartitionKey(w.underlines, func(u underline) int { return u.line })
parts(func(_ int, part []underline) bool {
for _, part := range parts {
cur := &info[part[0].line-w.start]
cur.shouldEmit = true

Expand Down Expand Up @@ -520,7 +517,7 @@ func (r *renderer) window(w *window) {

// Now, convert the buffer into a proper string.
var out strings.Builder
slicesx.Partition(buf)(func(_ int, line []byte) bool {
for _, line := range slicesx.Partition(buf) {
level := Level(line[0])
if line[0] == 0 {
out.WriteString(r.ss.reset)
Expand All @@ -537,8 +534,7 @@ func (r *renderer) window(w *window) {
out.WriteByte('^')
}
}
return true
})
}

// Next we need to find the message that goes inline with the underlines. This will be
// the message belonging to the rightmost underline.
Expand Down Expand Up @@ -637,9 +633,7 @@ func (r *renderer) window(w *window) {
}
cur.underlines = append(cur.underlines, strings.TrimRight(sidebar+string(buf), " "))
}

return true
})
}

//nolint:dupword
// Now that we've laid out the underlines, we can add the starts and ends of all
Expand Down
7 changes: 3 additions & 4 deletions experimental/report/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ func Join(spans ...Spanner) Span {
// JoinSeq is like [Join], but takes a sequence of any spannable type.
func JoinSeq[S Spanner](seq iter.Seq[S]) Span {
joined := Span{Start: math.MaxInt}
seq(func(spanner S) bool {
for spanner := range seq {
span := getSpan(spanner)
if span.IsZero() {
return true
continue
}

if joined.IsZero() {
Expand All @@ -173,8 +173,7 @@ func JoinSeq[S Spanner](seq iter.Seq[S]) Span {

joined.Start = min(joined.Start, span.Start)
joined.End = max(joined.End, span.End)
return true
})
}

if joined.File == nil {
return Span{}
Expand Down
43 changes: 18 additions & 25 deletions experimental/report/width.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ func wordWrap(text string, width int) iter.Seq[string] {
return func(yield func(string) bool) {
// Split along lines first, since those are hard breaks we don't plan
// to change.
stringsx.Lines(text)(func(line string) bool {
for line := range stringsx.Lines(text) {
var nextIsSpace bool
var column, cursor int

stringsx.PartitionKey(line, unicode.IsSpace)(func(start int, chunk string) bool {
for start, chunk := range stringsx.PartitionKey(line, unicode.IsSpace) {
isSpace := nextIsSpace
nextIsSpace = !nextIsSpace

if isSpace && column == 0 {
return true
continue
}

w := stringWidth(column, chunk, true, nil) - column
if column+w <= width {
column += w
return true
continue
}

if !yield(strings.TrimSpace(line[cursor:start])) {
return false
return
}

if isSpace {
Expand All @@ -77,12 +77,13 @@ func wordWrap(text string, width int) iter.Seq[string] {
cursor = start
column = w
}
return true
})
}

rest := line[cursor:]
return rest == "" || yield(rest)
})
if rest != "" && !yield(rest) {
return
}
}
}
}

Expand All @@ -91,14 +92,13 @@ func wordWrap(text string, width int) iter.Seq[string] {
func stringWidth(column int, text string, allowNonPrint bool, out *writer) int {
// We can't just use StringWidth, because that doesn't respect tabstops
// correctly.
for text != "" {
nextTab := strings.IndexByte(text, '\t')
haveTab := nextTab != -1
next := text
if haveTab {
next, text = text[:nextTab], text[nextTab+1:]
} else {
text = ""
for i, next := range iterx.Enumerate(stringsx.Split(text, '\t')) {
if i > 0 {
tab := TabstopWidth - (column % TabstopWidth)
column += tab
if out != nil {
out.WriteSpaces(tab)
}
}

if !allowNonPrint {
Expand Down Expand Up @@ -140,14 +140,7 @@ func stringWidth(column int, text string, allowNonPrint bool, out *writer) int {
out.WriteString(next)
}
}

if haveTab {
tab := TabstopWidth - (column % TabstopWidth)
column += tab
if out != nil {
out.WriteSpaces(tab)
}
}
}

return column
}
19 changes: 7 additions & 12 deletions experimental/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"slices"
"unicode"

"github.com/bufbuild/protocompile/internal/ext/iterx"
"github.com/bufbuild/protocompile/internal/ext/stringsx"
"github.com/bufbuild/protocompile/internal/ext/unsafex"
)
Expand Down Expand Up @@ -52,15 +53,12 @@ func (w *writer) WriteSpaces(n int) {
func (w *writer) WriteString(data string) {
// Break the input along newlines; each time we're about to append a
// newline, discard all trailing whitespace that isn't a newline.
first := true
stringsx.Lines(data)(func(line string) bool {
if !first {
for i, line := range iterx.Enumerate(stringsx.Lines(data)) {
if i > 0 {
w.flush(true)
}
first = false
w.buf = append(w.buf, line...)
return true
})
}
}

var ansiEscapePat = regexp.MustCompile("^\033\\[([\\d;]*)m")
Expand All @@ -81,16 +79,13 @@ func (w *writer) WriteWrapped(data string, width int) {
margin++
}

first := true
wordWrap(data, width-margin)(func(line string) bool {
if !first {
for i, line := range iterx.Enumerate(wordWrap(data, width-margin)) {
if i > 0 {
w.WriteString("\n")
w.WriteSpaces(margin)
}
first = false
w.WriteString(line)
return true
})
}
}

// Flush flushes the buffer to the writer's output.
Expand Down

0 comments on commit 90f9204

Please sign in to comment.