Problem
The current implementation of attach_orphan_specifiers in src/fences.rs uses a complex reverse-search and removal pattern that makes the code harder to follow:
- Uses
.rev().find(…) to search backwards through the output buffer
- Performs
out.remove(idx) operations to splice out orphaned specifiers
- Complex index management logic
Suggested Solution
As suggested by sourcery-ai[bot], rewrite the function as a state machine using a Peekable iterator. This approach would:
- Eliminate the reverse-search +
out.remove(idx) back-splice
- Use one pass with
peekable() to match "lang" + "```" pairs
- Keep all existing behaviour and tests intact
- Be much easier to follow and maintain
The suggested implementation uses a forward-looking approach that detects orphaned language specifiers by peeking ahead to see if the next line is exactly "```".
Context
Acceptance Criteria
Problem
The current implementation of
attach_orphan_specifiersinsrc/fences.rsuses a complex reverse-search and removal pattern that makes the code harder to follow:.rev().find(…)to search backwards through the output bufferout.remove(idx)operations to splice out orphaned specifiersSuggested Solution
As suggested by sourcery-ai[bot], rewrite the function as a state machine using a
Peekableiterator. This approach would:out.remove(idx)back-splicepeekable()to match "lang" + "```" pairsThe suggested implementation uses a forward-looking approach that detects orphaned language specifiers by peeking ahead to see if the next line is exactly "```".
Context
src/fences.rsattach_orphan_specifiersAcceptance Criteria
attach_orphan_specifiersusing a Peekable iterator approach