Skip to content

fix: add document character offset to element locations#415

Draft
toddr-bot wants to merge 2 commits into
masterfrom
koan.toddr.bot/fix-issue-101
Draft

fix: add document character offset to element locations#415
toddr-bot wants to merge 2 commits into
masterfrom
koan.toddr.bot/fix-issue-101

Conversation

@toddr-bot

@toddr-bot toddr-bot commented Apr 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a 6th element to the location array: the absolute character offset from the beginning of the document (1-indexed). This allows tools to efficiently map between PPI elements and positions in the serialized document string, which is useful for editor integrations, source mapping, and precise text manipulation.

Fixes #101

Changes

  • Added LOCATION_OFFSET constant to PPI::Document (index 5)
  • Updated _default_location, _add_location, and index_locations to compute and propagate the character offset, including proper accounting for heredoc body content
  • Added character_offset() accessor to PPI::Element
  • Updated location() POD to document the 6-element array format
  • Updated all existing test assertions to expect 6-element location arrays

Test plan

  • Verified simple offsets: my $x = 1; → each token's character_offset() matches expected position
  • Verified heredoc offsets: tokens after heredoc body have correct offset accounting for body + terminator length
  • Verified serialization consistency: for all 169 tokens in the comprehensive test source, substr($serialized, $offset - 1, length($content)) equals the token's content
  • Full test suite passes: 69 files, 53,093 tests, 0 failures

Generated by Kōan /fix


Quality Report

Changes: 5 files changed, 326 insertions(+), 224 deletions(-)

Code scan: clean

Tests: skipped

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

toddr-bot and others added 2 commits April 27, 2026 04:31
Add failing tests (marked $TODO) for the upcoming character_offset feature:
- location() should return a 6-element array with offset as the 6th element
- character_offset() accessor should return correct absolute positions
- Offsets must account for heredoc body content
- All offsets must match positions in the serialized document

Addresses #101

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add a 6th element to the location array returned by location(): the
absolute character offset from the beginning of the document (1-indexed).

- Add LOCATION_OFFSET constant to PPI::Document
- Track cumulative offset in index_locations, including heredoc content
- Add character_offset() accessor to PPI::Element
- Update location() to return 6-element array
- Remove $TODO markers from tests and update all existing location
  assertions to include offset values

Fixes #101

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

consider implementing a document character offset value for locations

1 participant