python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

Add support for pre-move comments in GameNode and StringExporter

Open aslyamov opened this issue 1 month ago • 0 comments

This PR adds a pre_comment attribute to GameNode and updates the StringExporter to support writing comments immediately before a move token.

Use Case: In many educational chess materials (and complex PGNs), there is a semantic difference between a comment explaining the previous position and a comment giving instructions specifically for the upcoming move (e.g., "Find the best move").

Currently, users are forced to append such comments to node.parent.comment, which works for PGN validity but loses the logical association in the object model.

Changes:

  1. Added pre_comment attribute to GameNode.
  2. Added visit_pre_comment to BaseVisitor and StringExporterMixin.
  3. Updated ChildNode._accept_node to visit the pre-comment before the move.

This implementation ensures that force_movenumber is triggered after a pre-comment, producing valid PGN output like: 1. e4 {Post-comment} {Pre-comment for next move} 2. Nf3

aslyamov avatar Nov 23 '25 15:11 aslyamov