User comments: an editor in the notation, symbols, arrows on the board
Enter on a move (the move's menu, a double click on a comment, the ☰ menu) opens the editor where the comment stands: after any move, in a variation too (the flow is cut at the move), or at the start of the game; Enter — done, Esc — cancel, Shift+Enter — a paragraph; another move writes the draft; one edit — one undo step. Symbols of the move (! ? !! ?? !? ?!, Ctrl+1…6) and of the position in the editor and in the menu. The right mouse button draws arrows and circles on the board ([%cal]/[%csl] of the current move; colours by modifiers). Existing comments are edited and deleted; their commands and untouched bytes are kept. chess_core: with_text, with_shapes, shapes, Game::set_comments_before. Dev hooks CP_COMMENT_EDIT, CP_TYPE, CP_SHAPE. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
9967d6b759
commit
dc7d9f88b3
15 changed files with 1592 additions and 71 deletions
|
|
@ -15,6 +15,17 @@ pub enum ShapeColor {
|
|||
}
|
||||
|
||||
impl ShapeColor {
|
||||
/// The letter PGN writes: `R`, `G`, `B`, `Y`.
|
||||
pub fn letter(self) -> char {
|
||||
match self {
|
||||
ShapeColor::Red => 'R',
|
||||
ShapeColor::Green => 'G',
|
||||
ShapeColor::Blue => 'B',
|
||||
ShapeColor::Yellow => 'Y',
|
||||
ShapeColor::Other(c) => c,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_char(c: char) -> ShapeColor {
|
||||
match c {
|
||||
'R' => ShapeColor::Red,
|
||||
|
|
@ -127,6 +138,120 @@ fn command(name: &str, payload: &str) -> Annotation {
|
|||
}
|
||||
}
|
||||
|
||||
impl Annotation {
|
||||
/// The command as PGN writes it: `[%cal Rc7b6]`.
|
||||
pub fn to_pgn(&self) -> String {
|
||||
match self {
|
||||
Annotation::Squares(list) => format!(
|
||||
"[%csl {}]",
|
||||
list.iter()
|
||||
.map(|(c, sq)| format!("{}{sq}", c.letter()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
),
|
||||
Annotation::Arrows(list) => format!(
|
||||
"[%cal {}]",
|
||||
list.iter()
|
||||
.map(|(c, a, b)| format!("{}{a}{b}", c.letter()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
),
|
||||
Annotation::Other { name, payload } if payload.is_empty() => format!("[%{name}]"),
|
||||
Annotation::Other { name, payload } => format!("[%{name} {payload}]"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The commands of a node's comments, in order.
|
||||
fn commands(comments: &[String]) -> Vec<Annotation> {
|
||||
comments
|
||||
.iter()
|
||||
.flat_map(|c| parse_comment(c))
|
||||
.filter_map(|p| match p {
|
||||
CommentPart::Annotation(a) => Some(a),
|
||||
CommentPart::Text(_) => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One comment from `text` and `commands`: the text, then the commands; nothing when
|
||||
/// both are empty (an empty `Vec`, so that no `{}` is written).
|
||||
fn assemble(text: &str, commands: &[Annotation]) -> Vec<String> {
|
||||
let mut out = text.trim().to_owned();
|
||||
for c in commands {
|
||||
if !out.is_empty() {
|
||||
out.push(' ');
|
||||
}
|
||||
out.push_str(&c.to_pgn());
|
||||
}
|
||||
if out.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![out]
|
||||
}
|
||||
}
|
||||
|
||||
/// The comments of a node with their text replaced by what the user typed: the
|
||||
/// commands (`[%cal]`, `[%clk]`, …) stay, in their order, after the text. A `}` cannot
|
||||
/// be in a PGN comment and becomes `)`. Paragraphs (blank lines) are kept, a single
|
||||
/// line break is a space.
|
||||
pub fn with_text(comments: &[String], text: &str) -> Vec<String> {
|
||||
let mut clean = String::new();
|
||||
let mut blank = false;
|
||||
for line in text.replace('}', ")").lines() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
blank = !clean.is_empty();
|
||||
continue;
|
||||
}
|
||||
if blank {
|
||||
clean.push_str("\n\n");
|
||||
} else if !clean.is_empty() {
|
||||
clean.push(' ');
|
||||
}
|
||||
blank = false;
|
||||
clean.push_str(line);
|
||||
}
|
||||
assemble(&clean, &commands(comments))
|
||||
}
|
||||
|
||||
/// Arrows `(colour, from, to)` and squares `(colour, square)` of a node.
|
||||
pub type Shapes = (Vec<(ShapeColor, Square, Square)>, Vec<(ShapeColor, Square)>);
|
||||
|
||||
/// The arrows and the squares drawn on the board for a node (`[%cal]`, `[%csl]`).
|
||||
pub fn shapes(comments: &[String]) -> Shapes {
|
||||
let mut arrows = Vec::new();
|
||||
let mut squares = Vec::new();
|
||||
for a in commands(comments) {
|
||||
match a {
|
||||
Annotation::Arrows(list) => arrows.extend(list),
|
||||
Annotation::Squares(list) => squares.extend(list),
|
||||
Annotation::Other { .. } => {}
|
||||
}
|
||||
}
|
||||
(arrows, squares)
|
||||
}
|
||||
|
||||
/// The comments of a node with these arrows and squares instead of its own: the text
|
||||
/// and the other commands stay.
|
||||
pub fn with_shapes(
|
||||
comments: &[String],
|
||||
arrows: &[(ShapeColor, Square, Square)],
|
||||
squares: &[(ShapeColor, Square)],
|
||||
) -> Vec<String> {
|
||||
let mut cmds: Vec<Annotation> = commands(comments)
|
||||
.into_iter()
|
||||
.filter(|a| matches!(a, Annotation::Other { .. }))
|
||||
.collect();
|
||||
if !squares.is_empty() {
|
||||
cmds.push(Annotation::Squares(squares.to_vec()));
|
||||
}
|
||||
if !arrows.is_empty() {
|
||||
cmds.push(Annotation::Arrows(arrows.to_vec()));
|
||||
}
|
||||
assemble(&comment_display_text(comments), &cmds)
|
||||
}
|
||||
|
||||
/// The text of a node's comments as a reader sees it: board commands (`[%cal]`,
|
||||
/// `[%clk]`, …) left out, single line breaks reflowed (they are formatting), a blank
|
||||
/// line kept as a paragraph break (book chapters imported from ChessBase).
|
||||
|
|
@ -200,4 +325,36 @@ mod tests {
|
|||
]
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn with_text_keeps_commands_and_paragraphs() {
|
||||
let c = vec!["Старый текст [%cal Gf3d5][%clk 0:03:12]".to_owned()];
|
||||
assert_eq!(
|
||||
with_text(&c, " Новый }текст{\nещё\n\n\nабзац "),
|
||||
vec!["Новый )текст{ ещё\n\nабзац [%cal Gf3d5] [%clk 0:03:12]".to_owned()]
|
||||
);
|
||||
// No text: the commands alone; nothing at all: an empty Vec.
|
||||
assert_eq!(
|
||||
with_text(&c, " "),
|
||||
vec!["[%cal Gf3d5] [%clk 0:03:12]".to_owned()]
|
||||
);
|
||||
assert!(with_text(&["plain".to_owned()], "").is_empty());
|
||||
assert_eq!(with_text(&[], "a"), vec!["a".to_owned()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_shapes_replaces_board_commands_only() {
|
||||
let c = vec![
|
||||
"Текст [%cal Gf3d5] [%clk 0:03:12]".to_owned(),
|
||||
"[%csl Ra6]".to_owned(),
|
||||
];
|
||||
let (arrows, squares) = shapes(&c);
|
||||
assert_eq!(arrows.len(), 1);
|
||||
assert_eq!(squares.len(), 1);
|
||||
let out = with_shapes(&c, &[(ShapeColor::Red, Square::E2, Square::E4)], &[]);
|
||||
assert_eq!(out, vec!["Текст [%clk 0:03:12] [%cal Re2e4]".to_owned()]);
|
||||
assert_eq!(
|
||||
with_shapes(&c, &[], &[]),
|
||||
vec!["Текст [%clk 0:03:12]".to_owned()]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,10 +9,11 @@ use shakmaty::{CastlingMode, Move, Position, san::SanPlus, uci::UciMove};
|
|||
use crate::game::{Game, GameMove, MoveKind, NodeId};
|
||||
use crate::nag::Nag;
|
||||
|
||||
/// NAGs and trailing comments of a node (what annotation edits change).
|
||||
/// NAGs and comments of a node (what annotation edits change).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Annotations {
|
||||
nags: Vec<Nag>,
|
||||
comments_before: Vec<String>,
|
||||
comments_after: Vec<String>,
|
||||
}
|
||||
|
||||
|
|
@ -263,10 +264,12 @@ impl Game {
|
|||
let node = self.node(id).ok_or(EditError::UnknownNode)?;
|
||||
let old = Annotations {
|
||||
nags: node.nags.clone(),
|
||||
comments_before: node.comments_before.clone(),
|
||||
comments_after: node.comments_after.clone(),
|
||||
};
|
||||
let new = Annotations {
|
||||
nags,
|
||||
comments_before: old.comments_before.clone(),
|
||||
comments_after,
|
||||
};
|
||||
if old != new {
|
||||
|
|
@ -275,6 +278,31 @@ impl Game {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Replaces the comments before the move of `id` (an introduction to a line).
|
||||
/// Undoable; no-op if unchanged.
|
||||
pub fn set_comments_before(
|
||||
&mut self,
|
||||
id: NodeId,
|
||||
comments_before: Vec<String>,
|
||||
) -> Result<(), EditError> {
|
||||
self.check_attached(id)?;
|
||||
let node = self.node(id).ok_or(EditError::UnknownNode)?;
|
||||
let old = Annotations {
|
||||
nags: node.nags.clone(),
|
||||
comments_before: node.comments_before.clone(),
|
||||
comments_after: node.comments_after.clone(),
|
||||
};
|
||||
let new = Annotations {
|
||||
nags: old.nags.clone(),
|
||||
comments_before,
|
||||
comments_after: old.comments_after.clone(),
|
||||
};
|
||||
if old != new {
|
||||
self.record(Command::Annotate { node: id, old, new });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Runs `f` so that all its edits form a single undo step.
|
||||
pub fn edit_group<R>(&mut self, f: impl FnOnce(&mut Game) -> R) -> R {
|
||||
let outer = self.group.replace(Vec::new());
|
||||
|
|
@ -345,6 +373,7 @@ impl Game {
|
|||
Command::Annotate { node, new, .. } => {
|
||||
if let Some(n) = self.node_mut(*node) {
|
||||
n.nags.clone_from(&new.nags);
|
||||
n.comments_before.clone_from(&new.comments_before);
|
||||
n.comments_after.clone_from(&new.comments_after);
|
||||
}
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,10 @@ pub mod pgn;
|
|||
pub mod repair;
|
||||
pub mod start;
|
||||
|
||||
pub use comment::{Annotation, CommentPart, ShapeColor, comment_display_text, parse_comment};
|
||||
pub use comment::{
|
||||
Annotation, CommentPart, ShapeColor, comment_display_text, parse_comment, shapes, with_shapes,
|
||||
with_text,
|
||||
};
|
||||
pub use database::{OpenError, PgnDatabase, SourceFingerprint};
|
||||
pub use diagnostic::{Diagnostic, DiagnosticCode, Severity, SourceLocation};
|
||||
pub use edit::{EditError, EditOutcome};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue