fix(add_schematic_component): support hierarchical sub-sheets (#169)

DynamicSymbolLoader.create_component_instance only handled root
schematics: it located its insertion point via `(sheet_instances`,
which exists only in the root .kicad_sch. Adding a component to any
hierarchical sub-sheet raised "Could not find insertion point in
schematic" and aborted the call.

Fall back to inserting just before the closing paren of the outer
(kicad_sch ...) form when the marker is absent. WireManager.add_label
already uses the same fallback for hierarchical sub-sheets.

Adds three regression tests in TestCreateComponentInstanceSubSheet
covering successful insertion, paren balance, and sexpdata round-trip
on a sub-sheet without (sheet_instances).
This commit is contained in:
Matthew Runo
2026-05-18 11:05:14 -07:00
committed by GitHub
parent 4740013d24
commit 9843d75c91
2 changed files with 107 additions and 3 deletions

View File

@@ -483,9 +483,16 @@ class DynamicSymbolLoader:
insert_marker = "(sheet_instances"
insert_at = content.rfind(insert_marker)
if insert_at == -1:
raise ValueError("Could not find insertion point in schematic")
content = content[:insert_at] + instance_block + "\n " + content[insert_at:]
# Hierarchical sub-sheets don't carry (sheet_instances ...) — only the
# root .kicad_sch does. Fall back to inserting just before the final
# closing paren of the outer (kicad_sch ...) form.
stripped = content.rstrip()
if not stripped.endswith(")"):
raise ValueError("Could not find insertion point in schematic")
insert_at = len(stripped) - 1
content = content[:insert_at] + instance_block + "\n" + content[insert_at:]
else:
content = content[:insert_at] + instance_block + "\n " + content[insert_at:]
with open(schematic_path, "w", encoding="utf-8") as f:
f.write(content)