Skip to content

Simplify PartPosition - #162

Merged
davepeck merged 3 commits into
mainfrom
dave/explore-part-position
Aug 24, 2026
Merged

Simplify PartPosition#162
davepeck merged 3 commits into
mainfrom
dave/explore-part-position

Conversation

@davepeck

@davepeck davepeck commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Instead of PartPosition.index representing an index into interleaved strings and interpolations, we now have PartPosition.s_index which can only ever represent an index to a Template string. An offset of 0 means "just before this string" (and therefore "just after the preceding interpolation, if any"); an offset of len(the_string) means "just after this string" (and therefore "just before the following interpolation, if any").

This simplifies a bunch of downstream code (see extract() and abs_pos_to_part_pos() for example) and removes some of the awkwardness around offset if the position were previously an interpolation.

@davepeck

Copy link
Copy Markdown
Contributor Author

@ianjosephwilson for your consideration

@ianjosephwilson

ianjosephwilson commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@davepeck This does seem more elegant overall than what I'd call my brute force approach. There is one weird situation but the math works and the ambiguous case seems resolved. I'm not tied to it so you should just merge it. We can keep adjusting things.

The one thing I do prefer is iterating over entire template with the 2-based index whenever that is needed but it seems like more and more of those loops are being replaced with more specialized solutions. Maybe we'll see what is in there when the smoke clears from all this.

Comment thread tdom/parser_utils_test.py
Comment thread tdom/template_utils_test.py
@davepeck
davepeck merged commit 85feaf9 into main Aug 24, 2026
2 checks passed
ianjosephwilson pushed a commit to ianjosephwilson/tdom that referenced this pull request Aug 25, 2026
* Consider a different shape for PartPosition

* Follow-on: simplify `abs_pos_to_part_pos()`.

* I think I prefer this for readability
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.

2 participants