Share the source index with validators
This commit is contained in:
@@ -16,10 +16,16 @@ type CitationResolver struct {
|
||||
|
||||
// NewCitationResolver prepares citation resolution for doc.
|
||||
func NewCitationResolver(doc *source.SourceDocument) (*CitationResolver, error) {
|
||||
return NewCitationResolverForSource(doc, nil)
|
||||
}
|
||||
|
||||
// NewCitationResolverForSource reuses an optional read-only index for doc's
|
||||
// unchanged source snapshot. Standalone callers may omit the index.
|
||||
func NewCitationResolverForSource(doc *source.SourceDocument, index *source.DocumentIndex) (*CitationResolver, error) {
|
||||
if doc == nil {
|
||||
return nil, fmt.Errorf("source document must not be nil")
|
||||
}
|
||||
return &CitationResolver{doc: doc, index: source.NewDocumentIndex(doc)}, nil
|
||||
return &CitationResolver{doc: doc, index: DocumentIndexForSource(doc, index)}, nil
|
||||
}
|
||||
|
||||
// CitedText resolves cited source ranges in document order, including each
|
||||
|
||||
@@ -75,6 +75,35 @@ func TestNewCitationResolverRejectsNilDocument(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCitationResolverForSourceUsesCurrentLayoutWithOptionalIndex(t *testing.T) {
|
||||
for _, supplied := range []bool{false, true} {
|
||||
for _, units := range [][]source.SourceUnit{
|
||||
{{ID: 40, Text: "first"}, {ID: 10, Text: "second"}},
|
||||
{{ID: 10, Text: "second"}, {ID: 40, Text: "first"}},
|
||||
} {
|
||||
doc := &source.SourceDocument{ID: "same-source", Units: units}
|
||||
var index *source.DocumentIndex
|
||||
if supplied {
|
||||
built := source.NewDocumentIndex(doc)
|
||||
index = &built
|
||||
}
|
||||
resolver, err := NewCitationResolverForSource(doc, index)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
refs := []source.SourceRef{{SourceID: doc.ID, StartUnitID: units[0].ID, EndUnitID: units[1].ID}}
|
||||
want := units[0].Text + "\n" + units[1].Text
|
||||
if got, err := resolver.CitedText(refs); err != nil || got != want {
|
||||
t.Fatalf("supplied=%t CitedText() = %q, %v; want %q", supplied, got, err, want)
|
||||
}
|
||||
refs[0].StartUnitID = 999
|
||||
if _, err := resolver.CitedText(refs); err == nil {
|
||||
t.Fatal("unknown endpoint was accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCitationResolverUsesSourceOrderAndPreservesEmptyText(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{
|
||||
{ID: 40, Text: "first"}, {ID: 10, Text: ""}, {ID: 70, Text: "third"}, {ID: 20, Text: "last"},
|
||||
|
||||
@@ -21,10 +21,16 @@ func NewSourceRefOrder(doc *source.SourceDocument) SourceRefOrder {
|
||||
// NewSourceRefOrderForSource uses a borrowed whole-source index when provided.
|
||||
// The caller must supply an index for doc; otherwise one is built locally.
|
||||
func NewSourceRefOrderForSource(doc *source.SourceDocument, index *source.DocumentIndex) SourceRefOrder {
|
||||
return NewSourceRefOrderFromIndex(DocumentIndexForSource(doc, index))
|
||||
}
|
||||
|
||||
// DocumentIndexForSource borrows the supplied whole-source snapshot, or builds
|
||||
// one locally when omitted. A supplied index must match doc's unchanged ID/order.
|
||||
func DocumentIndexForSource(doc *source.SourceDocument, index *source.DocumentIndex) source.DocumentIndex {
|
||||
if index != nil {
|
||||
return NewSourceRefOrderFromIndex(*index)
|
||||
return *index
|
||||
}
|
||||
return NewSourceRefOrder(doc)
|
||||
return source.NewDocumentIndex(doc)
|
||||
}
|
||||
|
||||
// NewSourceRefOrderFromIndex captures source identity and unit positions from
|
||||
|
||||
Reference in New Issue
Block a user