Skip to content
This repository was archived by the owner on Nov 7, 2026. It is now read-only.

Commit 7771e9c

Browse files
Merge pull request #13 from code4teaching/editor-store
Move undo/redo and tab editing out of ContentView
2 parents ea3c70f + 6c6a3be commit 7771e9c

17 files changed

Lines changed: 1901 additions & 1638 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Foundation
2+
3+
extension VisuStructDocumentEditing {
4+
/// Innerste **Fallauswahl** (`multiway`), die `path` enthält (oder selbst ist) — für „Fall hinzufügen/entfernen“.
5+
public static func multiwayAncestorPath(for path: [Int]?, in doc: VisuStructDocument) -> [Int]? {
6+
guard let path, !path.isEmpty else { return nil }
7+
for len in stride(from: path.count, through: 1, by: -1) {
8+
let prefix = Array(path.prefix(len))
9+
if let el = try? element(at: prefix, in: doc),
10+
el.type == .fallauswahl {
11+
return prefix
12+
}
13+
}
14+
return nil
15+
}
16+
17+
/// Spaltenindex der Fallauswahl, wenn die Auswahl **in** einer Spalte liegt (`[…, fall, spalte, …]`).
18+
public static func caseIndex(of selectedPath: [Int]?, inMultiwayAt multiwayPath: [Int]) -> Int? {
19+
guard let sp = selectedPath, sp.count > multiwayPath.count else { return nil }
20+
return sp[multiwayPath.count]
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
extension VisuStructDocumentEditing {
4+
/// Auswahl nach Undo/Redo oder Löschen: leeres Dokument → keine Auswahl; fehlende oder ungültige Auswahl → erstes Element.
5+
public static func sanitizedSelection(_ path: [Int]?, in doc: VisuStructDocument) -> [Int]? {
6+
if doc.elements.isEmpty { return nil }
7+
guard let path else { return [0] }
8+
if (try? element(at: path, in: doc)) == nil { return [0] }
9+
return path
10+
}
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Foundation
2+
3+
/// Undo-/Redo-Verlauf eines Diagramms: Schnappschüsse des Dokuments vor jeder Änderung.
4+
public struct VisuStructEditHistory: Equatable, Sendable {
5+
public static let defaultMaxSteps = 80
6+
7+
public private(set) var undoStack: [VisuStructDocument] = []
8+
public private(set) var redoStack: [VisuStructDocument] = []
9+
public let maxSteps: Int
10+
11+
public init(maxSteps: Int = VisuStructEditHistory.defaultMaxSteps) {
12+
self.maxSteps = maxSteps
13+
}
14+
15+
public var canUndo: Bool { !undoStack.isEmpty }
16+
public var canRedo: Bool { !redoStack.isEmpty }
17+
18+
/// Vor einer Änderung: bisherigen Zustand ablegen, Redo verwerfen.
19+
public mutating func record(_ current: VisuStructDocument) {
20+
pushUndo(current)
21+
redoStack.removeAll()
22+
}
23+
24+
/// Liefert den vorherigen Zustand und legt `current` für Redo ab; `nil`, wenn nichts rückgängig zu machen ist.
25+
public mutating func undo(from current: VisuStructDocument) -> VisuStructDocument? {
26+
guard let previous = undoStack.popLast() else { return nil }
27+
redoStack.append(current)
28+
return previous
29+
}
30+
31+
/// Liefert den wiederhergestellten Zustand und legt `current` für Undo ab; `nil`, wenn nichts wiederherzustellen ist.
32+
public mutating func redo(from current: VisuStructDocument) -> VisuStructDocument? {
33+
guard let next = redoStack.popLast() else { return nil }
34+
pushUndo(current)
35+
return next
36+
}
37+
38+
public mutating func reset() {
39+
undoStack.removeAll()
40+
redoStack.removeAll()
41+
}
42+
43+
private mutating func pushUndo(_ document: VisuStructDocument) {
44+
undoStack.append(document)
45+
while undoStack.count > maxSteps {
46+
undoStack.removeFirst()
47+
}
48+
}
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import XCTest
2+
import VisuStructCore
3+
4+
final class EditHistoryTests: XCTestCase {
5+
private func document(_ texts: String...) -> VisuStructDocument {
6+
var doc = VisuStructDocumentEditing.newEmpty()
7+
doc.elements = texts.map { VisuStructElement(type: .anweisung, textLines: [$0]) }
8+
return doc
9+
}
10+
11+
func testUndoAndRedoRestoreSnapshots() {
12+
let a = document("a"), b = document("b"), c = document("c")
13+
var history = VisuStructEditHistory()
14+
XCTAssertFalse(history.canUndo)
15+
history.record(a)
16+
history.record(b)
17+
XCTAssertEqual(history.undo(from: c), b)
18+
XCTAssertEqual(history.undo(from: b), a)
19+
XCTAssertNil(history.undo(from: a))
20+
XCTAssertEqual(history.redo(from: a), b)
21+
XCTAssertEqual(history.redo(from: b), c)
22+
XCTAssertNil(history.redo(from: c))
23+
}
24+
25+
func testRecordClearsRedo() {
26+
var history = VisuStructEditHistory()
27+
history.record(document("a"))
28+
_ = history.undo(from: document("b"))
29+
XCTAssertTrue(history.canRedo)
30+
history.record(document("a"))
31+
XCTAssertFalse(history.canRedo)
32+
}
33+
34+
func testUndoStackIsLimited() {
35+
var history = VisuStructEditHistory(maxSteps: 3)
36+
for i in 0..<5 { history.record(document("\(i)")) }
37+
XCTAssertEqual(history.undoStack, [document("2"), document("3"), document("4")])
38+
XCTAssertEqual(VisuStructEditHistory().maxSteps, 80)
39+
}
40+
41+
func testRedoAlsoRespectsLimit() {
42+
var history = VisuStructEditHistory(maxSteps: 2)
43+
history.record(document("0"))
44+
history.record(document("1"))
45+
_ = history.undo(from: document("2"))
46+
history.record(document("x"))
47+
history.record(document("y"))
48+
XCTAssertEqual(history.undoStack.count, 2)
49+
}
50+
51+
func testReset() {
52+
var history = VisuStructEditHistory()
53+
history.record(document("a"))
54+
_ = history.undo(from: document("b"))
55+
history.record(document("c"))
56+
history.reset()
57+
XCTAssertFalse(history.canUndo)
58+
XCTAssertFalse(history.canRedo)
59+
}
60+
61+
func testSanitizedSelection() {
62+
let doc = document("a", "b")
63+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([1], in: doc), [1])
64+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection([5], in: doc), [0])
65+
XCTAssertEqual(VisuStructDocumentEditing.sanitizedSelection(nil, in: doc), [0])
66+
var empty = doc
67+
empty.elements = []
68+
XCTAssertNil(VisuStructDocumentEditing.sanitizedSelection([0], in: empty))
69+
}
70+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import XCTest
2+
import VisuStructCore
3+
4+
final class MultiwayPathTests: XCTestCase {
5+
/// `[0]` Anweisung, `[1]` Fallauswahl mit zwei Fällen; im zweiten Fall eine innere Fallauswahl `[1, 1, 0]`.
6+
private func document() -> VisuStructDocument {
7+
let statement = VisuStructElement(type: .anweisung, textLines: ["x = 1"])
8+
let inner = VisuStructElement(
9+
type: .fallauswahl,
10+
textLines: ["y"],
11+
cases: [
12+
VisuStructCase(name: "1", elements: [statement]),
13+
VisuStructCase(name: "default", elements: [statement]),
14+
]
15+
)
16+
let outer = VisuStructElement(
17+
type: .fallauswahl,
18+
textLines: ["x"],
19+
cases: [
20+
VisuStructCase(name: "1", elements: [statement]),
21+
VisuStructCase(name: "default", elements: [inner]),
22+
]
23+
)
24+
var doc = VisuStructDocumentEditing.newEmpty()
25+
doc.elements = [statement, outer]
26+
return doc
27+
}
28+
29+
func testAncestorIsInnermostFallauswahl() {
30+
let doc = document()
31+
XCTAssertNil(VisuStructDocumentEditing.multiwayAncestorPath(for: nil, in: doc))
32+
XCTAssertNil(VisuStructDocumentEditing.multiwayAncestorPath(for: [0], in: doc))
33+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1], in: doc), [1])
34+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 0, 0], in: doc), [1])
35+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 1, 0], in: doc), [1, 1, 0])
36+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 1, 0, 1, 0], in: doc), [1, 1, 0])
37+
}
38+
39+
func testCaseIndex() {
40+
XCTAssertNil(VisuStructDocumentEditing.caseIndex(of: [1], inMultiwayAt: [1]))
41+
XCTAssertNil(VisuStructDocumentEditing.caseIndex(of: nil, inMultiwayAt: [1]))
42+
XCTAssertEqual(VisuStructDocumentEditing.caseIndex(of: [1, 1, 0], inMultiwayAt: [1]), 1)
43+
}
44+
}

0 commit comments

Comments
 (0)