Skip to content

Phase D.10: Search and replace with formatting preservation #23

@loadfix

Description

@loadfix

Add document-wide search and replace that preserves run formatting. Upstream #30 (39 comments).

API Design

  • document.search(text) — returns list of SearchMatch objects
  • document.replace(old_text, new_text) — replaces all occurrences, preserving the formatting of the first character's run
  • SearchMatch: .paragraph, .paragraph_index, .run_indices, .start, .end
  • Options: case_sensitive (default True), whole_word (default False)

Challenges

  • Text may span multiple runs (e.g., "hel" in one run, "lo" in another)
  • Replacement must merge/split runs appropriately
  • Must preserve bold/italic/font of the original runs

Implementation

  • Walk all paragraphs, reconstruct full text from runs with character-to-run mapping
  • Find matches in the full text
  • Map match positions back to runs
  • Replace text in the affected runs, splitting if needed

Upstream issue: #30

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestphase-dQuality of Life

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions