From 89f970069c77101da76dfd25d6627cdcc8edb79e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 12 Apr 2026 19:17:12 +0000 Subject: [PATCH] Remove Parser.Range and subsliceOffset Range() existed to recover byte offsets from Highlight subslices. Now that ParserError carries an explicit Offset field, Range() is unnecessary. Remove it along with the private subsliceOffset helper in ast.go. Tests now use perr.Offset directly and construct Range literals for Shape() calls. Co-authored-by: Thomas Pelletier --- unstable/ast.go | 12 ------------ unstable/parser.go | 12 ------------ unstable/parser_test.go | 12 +++++------- 3 files changed, 5 insertions(+), 31 deletions(-) diff --git a/unstable/ast.go b/unstable/ast.go index a9de8c2..6b21592 100644 --- a/unstable/ast.go +++ b/unstable/ast.go @@ -90,18 +90,6 @@ type Range struct { Length uint32 } -func subsliceOffset(data []byte, subslice []byte) int { - if len(subslice) == 0 { - return len(data) - } - for i := range data { - if &data[i] == &subslice[0] { - return i - } - } - panic("subslice is not within data") -} - // Next returns a pointer to the next node, or nil if there is no next node. func (n *Node) Next() *Node { if n.next < 0 { diff --git a/unstable/parser.go b/unstable/parser.go index e915404..c03bd2c 100644 --- a/unstable/parser.go +++ b/unstable/parser.go @@ -70,18 +70,6 @@ func (p *Parser) offsetOf(b []byte) int { return len(p.data) - len(b) } -// Range returns a range description that corresponds to a given slice of the -// input. If the argument is not a subslice of the parser input, this function -// panics. -// -// Prefer using ParserError.Offset directly for error position information. -func (p *Parser) Range(b []byte) Range { - return Range{ - Offset: uint32(subsliceOffset(p.data, b)), //nolint:gosec // TOML documents are small - Length: uint32(len(b)), //nolint:gosec // TOML documents are small - } -} - // rangeOfToken computes the Range of a token given the remaining bytes after the token. // This is used when the token was extracted from the beginning of some position, // and 'rest' is what remains after the token. diff --git a/unstable/parser_test.go b/unstable/parser_test.go index 7ffab8f..8aef7b6 100644 --- a/unstable/parser_test.go +++ b/unstable/parser_test.go @@ -674,7 +674,7 @@ key3 = "value3" assert.Equal(t, []string{"key1", "key2", "key3"}, keys) } -func TestRangeOffsetAfterComment(t *testing.T) { +func TestErrorOffsetAfterComment(t *testing.T) { input := []byte("# comment\n= \"value\"") p := Parser{} @@ -689,12 +689,11 @@ func TestRangeOffsetAfterComment(t *testing.T) { if !errors.As(err, &perr) { t.Fatalf("expected ParserError, got %T", err) } - r := p.Range(perr.Highlight) - shape := p.Shape(r) - if r.Offset != 10 { - t.Errorf("Range offset: got %d, want 10", r.Offset) + if perr.Offset != 10 { + t.Errorf("offset: got %d, want 10", perr.Offset) } + shape := p.Shape(Range{Offset: uint32(perr.Offset), Length: uint32(len(perr.Highlight))}) if shape.Start.Line != 2 || shape.Start.Column != 1 { t.Errorf("position: got %d:%d, want 2:1", shape.Start.Line, shape.Start.Column) } @@ -753,8 +752,7 @@ func TestErrorHighlightPositions(t *testing.T) { if !errors.As(err, &perr) { t.Fatalf("expected ParserError, got %T", err) } - r := p.Range(perr.Highlight) - shape := p.Shape(r) + shape := p.Shape(Range{Offset: uint32(perr.Offset), Length: uint32(len(perr.Highlight))}) if shape.Start.Line != e.wantLine { t.Errorf("line: got %d, want %d", shape.Start.Line, e.wantLine)