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

Commit fc3ecc7

Browse files
committed
Move inspector hints and Fallauswahl path helpers out of ContentView
- VisuStructCore: VisuStructDocumentEditing.multiwayAncestorPath and caseIndex(of:inMultiwayAt:), with tests. - Inspector/InspectorElementHints.swift: line editor placeholder and condition hint line per element type. - Remove the unused statementIOModeIfApplicable(). No behavior change intended. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cp4EQgFQyGbM8MVLiWbJKQ
1 parent 8c92504 commit fc3ecc7

4 files changed

Lines changed: 130 additions & 82 deletions

File tree

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: 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+
}

‎VisuStruct-iOS/VisuStructXcodeOpen/VisuStruct/ContentView.swift‎

Lines changed: 16 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -929,11 +929,16 @@ struct ContentView: View {
929929

930930
// MARK: - Inspector (unter dem Canvas)
931931

932+
private var selectedElement: VisuStructElement? {
933+
guard let path = selectedPath else { return nil }
934+
return try? VisuStructDocumentEditing.element(at: path, in: document)
935+
}
936+
932937
private var inspectorFallauswahlToolbar: InspectorFallauswahlToolbarContext? {
933-
guard let faPath = multiwayAncestorPath(for: selectedPath) else { return nil }
938+
guard let faPath = VisuStructDocumentEditing.multiwayAncestorPath(for: selectedPath, in: document) else { return nil }
934939
let faCaseCount =
935940
(try? VisuStructDocumentEditing.element(at: faPath, in: document))?.cases?.count ?? 0
936-
let removeCaseIdx = selectedCaseIndex(multiwayPath: faPath, selectedPath: selectedPath)
941+
let removeCaseIdx = VisuStructDocumentEditing.caseIndex(of: selectedPath, inMultiwayAt: faPath)
937942
return InspectorFallauswahlToolbarContext(
938943
caseCount: faCaseCount,
939944
removeCaseIndex: removeCaseIdx,
@@ -981,8 +986,14 @@ struct ContentView: View {
981986
lineEditorVisibleTextBinding: lineEditorVisibleTextBinding,
982987
editorFocusToken: editorFocusToken,
983988
lineEditorFixedPrefixLabel: lineEditorFixedPrefixLabel,
984-
inspectorConditionEditorHintLine: inspectorConditionEditorHintLine(),
985-
inspectorLineEditorPlaceholder: inspectorLineEditorPlaceholder(),
989+
inspectorConditionEditorHintLine: selectedElement.flatMap { InspectorElementHints.conditionHintLine(for: $0.type) },
990+
inspectorLineEditorPlaceholder: selectedElement.flatMap {
991+
InspectorElementHints.lineEditorPlaceholder(
992+
for: $0.type,
993+
ioMode: VisuStructLineEditorText.ioMode(of: tabsModel.currentTab.lineEditorText),
994+
lang: lang
995+
)
996+
},
986997
fallauswahl: inspectorFallauswahlToolbar,
987998
onApplyLineEditor: applyLineEditor,
988999
onApplyRespectingFocus: applyLineEditorRespectingTextFocus,
@@ -1008,63 +1019,6 @@ struct ContentView: View {
10081019
)
10091020
}
10101021

1011-
private func statementIOModeIfApplicable() -> StatementIOMode? {
1012-
guard let path = selectedPath,
1013-
let el = try? VisuStructDocumentEditing.element(at: path, in: document),
1014-
el.type == .anweisung else { return nil }
1015-
1016-
return VisuStructLineEditorText.ioMode(of: tabsModel.currentTab.lineEditorText)
1017-
}
1018-
1019-
/// Placeholder for the line editor, shown only when empty (grau im Feld). Code-Beispiele als Hilfe: `inspectorConditionEditorHintLine()` darüber.
1020-
private func inspectorLineEditorPlaceholder() -> String? {
1021-
guard let path = selectedPath,
1022-
let el = try? VisuStructDocumentEditing.element(at: path, in: document) else { return nil }
1023-
1024-
switch el.type {
1025-
case .anweisung:
1026-
switch VisuStructLineEditorText.ioMode(of: tabsModel.currentTab.lineEditorText) {
1027-
case .statement:
1028-
return lang.tr("inspector_placeholder_statement")
1029-
case .input:
1030-
return "int age \"Enter age\""
1031-
case .output:
1032-
return "\"Your age: \" + age"
1033-
}
1034-
case .forSchleife:
1035-
return nil
1036-
case .verzweigung, .whileSchleife, .doUntilSchleife, .endlosschleife:
1037-
return lang.tr("inspector_placeholder_condition")
1038-
case .fallauswahl:
1039-
return lang.tr("inspector_placeholder_selector")
1040-
case .aufruf:
1041-
return lang.tr("inspector_placeholder_call")
1042-
case .aussprung:
1043-
return lang.tr("inspector_placeholder_break")
1044-
case .leerElement:
1045-
return nil
1046-
}
1047-
}
1048-
1049-
/// Sichtbare Hilfszeile über dem Bedingungs-Editor (wie Statement-Hinweis unter dem Modus-Picker) — Englisch, code-like.
1050-
private func inspectorConditionEditorHintLine() -> String? {
1051-
guard let path = selectedPath,
1052-
let el = try? VisuStructDocumentEditing.element(at: path, in: document) else { return nil }
1053-
switch el.type {
1054-
case .verzweigung:
1055-
return "age >= 18"
1056-
case .whileSchleife:
1057-
return "i < 10"
1058-
case .doUntilSchleife:
1059-
return "again == 1"
1060-
case .fallauswahl:
1061-
// Ausdruck in Klammern: switch (day) { … } — gleicher Stil wie die anderen Hinweise (englisch, code-nah).
1062-
return "day"
1063-
default:
1064-
return nil
1065-
}
1066-
}
1067-
10681022
// MARK: - Inspector (iPhone: ein-/ausblendbar)
10691023

10701024
private var inspectorPanelPhoneAware: some View {
@@ -1106,20 +1060,6 @@ struct ContentView: View {
11061060

11071061
// MARK: - Fallauswahl (Multiway)
11081062

1109-
/// Innerste **Fallauswahl** (`multiway`), die die aktuelle Auswahl enthält (für „Fall hinzufügen“).
1110-
private func multiwayAncestorPath(for path: [Int]?) -> [Int]? {
1111-
guard let path, !path.isEmpty else { return nil }
1112-
let doc = document
1113-
for len in stride(from: path.count, through: 1, by: -1) {
1114-
let prefix = Array(path.prefix(len))
1115-
if let el = try? VisuStructDocumentEditing.element(at: prefix, in: doc),
1116-
el.type == .fallauswahl {
1117-
return prefix
1118-
}
1119-
}
1120-
return nil
1121-
}
1122-
11231063
private func addCaseToFallauswahl(at fallPath: [Int]) {
11241064
do {
11251065
let (updated, newPath) = try VisuStructDocumentEditing.appendCaseToFallauswahl(document, fallauswahlPath: fallPath)
@@ -1131,12 +1071,6 @@ struct ContentView: View {
11311071
}
11321072
}
11331073

1134-
/// Spaltenindex der Fallauswahl, wenn die Auswahl **in** einer Spalte liegt (`[…, fall, spalte, …]`).
1135-
private func selectedCaseIndex(multiwayPath: [Int], selectedPath: [Int]?) -> Int? {
1136-
guard let sp = selectedPath, sp.count > multiwayPath.count else { return nil }
1137-
return sp[multiwayPath.count]
1138-
}
1139-
11401074
private func removeCaseFromFallauswahl(at fallPath: [Int], caseIndex: Int) {
11411075
do {
11421076
let (updated, newPath) = try VisuStructDocumentEditing.removeCaseFromFallauswahl(
@@ -1152,7 +1086,7 @@ struct ContentView: View {
11521086

11531087
/// Bindung für den **Fallnamen**, wenn die Auswahl in einer Spalte einer Fallauswahl liegt (nicht beim bloßen Kopf `[n]` ohne Folgeindex).
11541088
private func multiwayCaseLabelBinding() -> Binding<String>? {
1155-
guard let fa = multiwayAncestorPath(for: selectedPath),
1089+
guard let fa = VisuStructDocumentEditing.multiwayAncestorPath(for: selectedPath, in: document),
11561090
let sp = selectedPath,
11571091
sp.count > fa.count else { return nil }
11581092
let caseIdx = sp[fa.count]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import VisuStructCore
2+
3+
/// Hilfetexte im Inspector je nach Elementtyp.
4+
enum InspectorElementHints {
5+
/// Placeholder for the line editor, shown only when empty (grau im Feld). Code-Beispiele als Hilfe: `conditionHintLine(for:)` darüber.
6+
static func lineEditorPlaceholder(for type: VisuStructElementType, ioMode: StatementIOMode, lang: AppLanguage) -> String? {
7+
switch type {
8+
case .anweisung:
9+
switch ioMode {
10+
case .statement:
11+
return lang.tr("inspector_placeholder_statement")
12+
case .input:
13+
return "int age \"Enter age\""
14+
case .output:
15+
return "\"Your age: \" + age"
16+
}
17+
case .forSchleife:
18+
return nil
19+
case .verzweigung, .whileSchleife, .doUntilSchleife, .endlosschleife:
20+
return lang.tr("inspector_placeholder_condition")
21+
case .fallauswahl:
22+
return lang.tr("inspector_placeholder_selector")
23+
case .aufruf:
24+
return lang.tr("inspector_placeholder_call")
25+
case .aussprung:
26+
return lang.tr("inspector_placeholder_break")
27+
case .leerElement:
28+
return nil
29+
}
30+
}
31+
32+
/// Sichtbare Hilfszeile über dem Bedingungs-Editor (wie Statement-Hinweis unter dem Modus-Picker) — Englisch, code-like.
33+
static func conditionHintLine(for type: VisuStructElementType) -> String? {
34+
switch type {
35+
case .verzweigung:
36+
return "age >= 18"
37+
case .whileSchleife:
38+
return "i < 10"
39+
case .doUntilSchleife:
40+
return "again == 1"
41+
case .fallauswahl:
42+
// Ausdruck in Klammern: switch (day) { … } — gleicher Stil wie die anderen Hinweise (englisch, code-nah).
43+
return "day"
44+
default:
45+
return nil
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)