diff --git a/shared.py b/shared.py index f1ee0c4..dcaff08 100644 --- a/shared.py +++ b/shared.py @@ -75,27 +75,27 @@ def parse_cli_to_tree(cli_text: str) -> dict: stripped = line.strip() indent = len(line) - len(line.lstrip()) - # 🌟 嚴格判斷 1:如果遇到縮排退回 (包含縮排退回的 !),代表當前深度的區塊已結束 - if indent < current_indent: + # 🌟 嚴格判斷 1:遇到「真實指令」縮排退回,才代表區塊結束 + # (加上 stripped != '!',防止設備偶發的「0 縮排 !」導致區塊被誤殺) + if indent < current_indent and stripped != '!': return nodes, i - # 🌟 嚴格判斷 2:處理同層級的 ! + # 🌟 嚴格判斷 2:處理 ! (無論縮排多少,都當純分隔符消耗掉) if stripped == '!': - # 能走到這裡,代表 indent >= current_indent - # 這是對應當前層級的結束符號或分隔符,我們將其消耗掉並繼續,絕對不提早 return i += 1 continue - # 探測是否有子節點 + # 🌟 探測是否有子節點 (強化版:往下尋找第一個「非 !」的真實指令來判斷) has_children = False - if i + 1 < len(lines): - next_line = lines[i+1] - next_stripped = next_line.strip() - # 只有當下一行不是 ! 且縮排大於當前行時,才判定為有子節點 - if next_stripped != '!': - next_indent = len(next_line) - len(next_line.lstrip()) - if next_indent > indent: - has_children = True + next_i = i + 1 + while next_i < len(lines) and lines[next_i].strip() == '!': + next_i += 1 + + if next_i < len(lines): + next_line = lines[next_i] + next_indent = len(next_line) - len(next_line.lstrip()) + if next_indent > indent: + has_children = True if has_children: children, next_i = build_raw_tree(i + 1, next_indent)