import io, re

p = r'C:\Users\decid\Documents\projects\spt-claude-code\tools\claude-spt\src\tag_scan.rs'
s = io.open(p, encoding='utf-8').read()
NL = chr(10)
lines = s.split(NL)

DROP = [
    'sec', 'single_target_single_body', 'multi_target_comma_no_spaces', 'hyphenated_targets_parse',
    'body_runs_to_first_close_non_greedy', 'multiple_sections_in_one_output',
    'first_whitespace_ends_target_list_including_newline', 'no_whitespace_all_targets_empty_body',
    'unclosed_opener_is_not_a_section', 'empty_target_list_dropped', 'bare_at_at_id_is_not_a_trigger',
    'prose_without_the_block_is_inert', 'inline_code_quote_does_not_fire',
    'fenced_code_block_quote_does_not_fire', 'bare_tag_still_fires_alongside_a_quoted_one',
    'a_body_quoting_a_command_still_sends_verbatim', 'an_unbalanced_backtick_in_a_body_still_sends',
    'a_body_with_a_fenced_block_survives', 'tag_straddling_a_code_boundary_does_not_fire',
    'plan_dispatch_scans_new_texts_for_tags', 'plan_dispatch_commune_suppresses_tags',
]

ranges = []
for name in DROP:
    pat = re.compile(r'^    fn ' + re.escape(name) + r'\b')
    hit = None
    for i, l in enumerate(lines):
        if pat.match(l):
            hit = i
            break
    assert hit is not None, 'test not found: ' + name
    start = hit
    while start > 0 and (lines[start - 1].strip().startswith('///')
                         or lines[start - 1].strip().startswith('//')
                         or lines[start - 1].strip().startswith('#[')):
        start -= 1
    j = hit
    while j < len(lines) and lines[j] != '    }':
        j += 1
    ranges.append((start, j))

for a, b in sorted(ranges, reverse=True):
    del lines[a:b + 1]

s = NL.join(lines)

# The section header for the deleted grammar block.
s = s.replace('    // ---- grammar: parse_tag_sections ------------------------------------------------------------' + NL, '')
s = re.sub(NL + '{3,}', NL + NL, s)

# plan_dispatch_empty_when_no_markers still references .sends
old = '''        let d = plan_dispatch(&["nothing here".to_string()]);
        assert!(d.sends.is_empty());
        assert!(d.communes.is_empty());'''
if old in s:
    s = s.replace(old, '''        let d = plan_dispatch(&["nothing here".to_string()]);
        assert!(d.communes.is_empty());''')

io.open(p, 'w', encoding='utf-8', newline=NL).write(s)
print('tests stripped; parse_tag_sections refs now:', s.count('parse_tag_sections'), '| TagSection refs:', s.count('TagSection'))
