Skip to content

Refactor attach_orphan_specifiers to use state machine with Peekable iterator #103

@coderabbitai

Description

@coderabbitai

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

  • Rewrite attach_orphan_specifiers using a Peekable iterator approach
  • Ensure all existing tests continue to pass
  • Verify the same behaviour is maintained
  • Code should be easier to read and understand

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions