-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathediting.py
More file actions
52 lines (35 loc) · 1.7 KB
/
Copy pathediting.py
File metadata and controls
52 lines (35 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Edit operations as reversible commands.
Each factory captures everything its undo needs *at execution time* —
``delete_span`` must remember the text it removed, which is exactly the
state a bare callback cannot carry and the reason Command earns its keep.
"""
from __future__ import annotations
from patterns.behavioral.command.examples.editor_undo.models import Document
from patterns.behavioral.command.pattern import Undoable
def insert_text(doc: Document, position: int, chunk: str) -> Undoable:
"""Insert ``chunk`` at ``position``; undo removes exactly that span."""
def undo() -> None:
doc.delete(position, len(chunk))
return Undoable(
do=lambda: doc.insert(position, chunk),
undo=undo,
label=f"insert {chunk!r}@{position}",
)
def delete_span(doc: Document, position: int, length: int) -> Undoable:
"""Delete ``length`` chars at ``position``; undo restores what was removed."""
removed: list[str] = [] # captured by do, needed by undo
def do() -> None:
removed.append(doc.delete(position, length))
def undo() -> None:
doc.insert(position, removed.pop())
return Undoable(do=do, undo=undo, label=f"delete {length}@{position}")
def replace_span(doc: Document, position: int, length: int, chunk: str) -> Undoable:
"""Replace ``length`` chars at ``position`` with ``chunk``, reversibly."""
removed: list[str] = []
def do() -> None:
removed.append(doc.delete(position, length))
doc.insert(position, chunk)
def undo() -> None:
doc.delete(position, len(chunk))
doc.insert(position, removed.pop())
return Undoable(do=do, undo=undo, label=f"replace {length}@{position} with {chunk!r}")