Skip to content

Commit

Permalink
update the report package
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Dec 16, 2024
1 parent e42e956 commit 3ded693
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
23 changes: 7 additions & 16 deletions experimental/report/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package report

import (
"fmt"

"github.com/bufbuild/protocompile/experimental/internal"
)

// Level represents the severity of a diagnostic message.
Expand Down Expand Up @@ -128,8 +126,7 @@ func InFile(path string) DiagnosticOption {
// The first annotation added is the "primary" annotation, and will be rendered
// differently from the others.
//
// If at is nil (be it a nil interface, or a value that has a Nil() function
// that returns true), or returns a nil span, this function will return nil.
// If at is nil or returns the nil span, the returned DiagnosticOption is a no-op.
func Snippet(at Spanner) DiagnosticOption {
return Snippetf(at, "")
}
Expand All @@ -142,20 +139,10 @@ func Snippet(at Spanner) DiagnosticOption {
// The first annotation added is the "primary" annotation, and will be rendered
// differently from the others.
//
// If at is nil (be it a nil interface, or a value that has a Nil() function
// that returns true), or returns a nil span, this function will return nil.
// If at is nil or returns the nil span, the returned DiagnosticOption is a no-op.
func Snippetf(at Spanner, format string, args ...any) DiagnosticOption {
if internal.Nil(at) {
return nil
}

span := at.Span()
if span.Nil() {
return nil
}

return snippet{
Span: span,
Span: getSpan(at),
message: fmt.Sprintf(format, args...),
}
}
Expand Down Expand Up @@ -201,6 +188,10 @@ type snippet struct {
}

func (a snippet) apply(d *Diagnostic) {
if a.Span.Nil() {
return
}

a.primary = len(d.snippets) == 0
d.snippets = append(d.snippets, a)
}
Expand Down
25 changes: 15 additions & 10 deletions experimental/report/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@ import (
"strings"
"sync"
"unicode"

"github.com/bufbuild/protocompile/experimental/internal"
)

// TabstopWidth is the size we render all tabstops as.
const TabstopWidth int = 4

// Spanner is any type with a span.
// Spanner is any type with a [Span].
type Spanner interface {
// Should return the zero [Span] to indicate that it does not contribute
// span information.
Span() Span
}

// getSpan extracts a span from a Spanner, but returns the nil span when
// s is nil, which would otherwise panic.
func getSpan(s Spanner) Span {
if s == nil {
return Span{}
}
return s.Span()
}

// Span is a location within a [File].
type Span struct {
// The file this span refers to. The file must be indexed, since we plan to
Expand Down Expand Up @@ -85,16 +94,12 @@ func (s Span) String() string {
func Join(spans ...Spanner) Span {
joined := Span{Start: math.MaxInt}
for _, span := range spans {
if internal.Nil(span) {
continue
}

span := span.Span()
if span.File == nil {
span := getSpan(span)
if span.Nil() {
continue
}

if joined.File == nil {
if joined.Nil() {
joined.File = span.File
} else if joined.File != span.File {
panic(fmt.Sprintf(
Expand Down

0 comments on commit 3ded693

Please sign in to comment.