Files
feature-next-arch/fix_dup.py
2026-08-10 10:00:52 +08:00

62 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
filepath = r'C:\Users\jsmbz\Documents\flutter-app-02\lib\features\home\presentation\widgets\map\testmap_pages.dart'
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern: find the second duplicate method (with debugPrint in catch)
# It starts with "/// 从 state.pathData 实时解析" and ends with "}\n\n Widget _buildWorkPanel"
pattern1 = r'\n /// 从 state\.pathData 实时解析 startWorkList\n /// 解决 BlocBuilder 因 state\.pathData 变化触发重建时,onTap 的异步赋值还未完成的时序问题\n List<dynamic> _parseStartWorkListFromPathData\(List<Map<String, dynamic>>\? pathData\) \{\n if \(pathData == null \|\| pathData\.isEmpty\) return \[\];\n final firstRecord = pathData\.first;\n final nestedJsonRaw = firstRecord\['jsonData'\];\n if \(nestedJsonRaw is! String\) return \[\];\n try \{\n final parsedJson = jsonDecode\(nestedJsonRaw\);\n if \(parsedJson is! Map<String, dynamic>\) return \[\];\n final planModel = parsedJson\['planModel'\];\n final isBow = planModel == WorkMode\.bow\.value;\n\n List<dynamic> pathList = \[\];\n final rawPath = parsedJson\['path'\];\n if \(rawPath is String\) \{\n try \{ pathList = jsonDecode\(rawPath\) as List; \} catch \(_\) \{\}\n \} else if \(rawPath is List\) \{\n pathList = rawPath;\n \}\n\n List<dynamic> outerList = \[\];\n final rawOuter = parsedJson\['outer'\];\n if \(rawOuter is String\) \{\n try \{ outerList = jsonDecode\(rawOuter\) as List; \} catch \(_\) \{\}\n \} else if \(rawOuter is List\) \{\n outerList = rawOuter;\n \}\n\n if \(isBow\) \{\n return pathList\.isNotEmpty \? pathList : outerList;\n \} else \{\n return outerList\.isNotEmpty \? outerList : pathList;\n \}\n \} catch \(e\) \{\n debugPrint\('❌ \[_parseStartWorkListFromPathData\] 解析失败: \$e'\);\n return \[\];\n \}\n \}\n\n Widget _buildWorkPanel'
# Try to find and replace the second duplicate method
match = re.search(pattern1, content)
if match:
print(f"Found duplicate method at position {match.start()}-{match.end()}")
content = content[:match.start()] + '\n Widget _buildWorkPanel' + content[match.end():]
else:
print("Pattern 1 not found, trying simpler approach...")
# Just find the second occurrence of _parseStartWorkListFromPathData
first_idx = content.find('_parseStartWorkListFromPathData')
second_idx = content.find('_parseStartWorkListFromPathData', first_idx + 1)
if second_idx != -1:
# Find the start of this method (go back to find " ///" or " List<dynamic>")
start = content.rfind(' /// 从 state.pathData', 0, second_idx)
if start == -1:
start = content.rfind(' List<dynamic> _parseStartWorkListFromPathData', 0, second_idx)
# Find the end of this method
end = content.find(' Widget _buildWorkPanel', second_idx)
if end == -1:
end = content.find('Widget _buildWorkPanel', second_idx)
if start != -1 and end != -1:
print(f"Found duplicate at {start}-{end}")
content = content[:start] + content[end:]
else:
print(f"Could not find boundaries: start={start}, end={end}")
else:
print("No duplicate method found")
# Pattern 2: remove duplicate call in _buildWorkPanel
# Find the second "// 核心修复:" inside _buildWorkPanel builder
first_core = content.find('// 🔥 核心修复:从 state.pathData 实时计算 startWorkList')
second_core = content.find('// 核心修复:从 state.pathData 实时计算 startWorkList', first_core + 1)
if second_core != -1:
# Go back to find the blank line before this duplicate block
start_call = content.rfind('\n', 0, second_core)
start_call = content.rfind('\n', 0, start_call) # go back one more line
# Find the end after the duplicate block
# The block is: "// 核心修复...\n final parsedList...\n if...\n startWorkList...\n }\n"
end_call = content.find('\n return Positioned(', second_core)
if end_call != -1:
print(f"Found duplicate call at {start_call}-{end_call}")
content = content[:start_call] + content[end_call:]
else:
print(f"Could not find end of duplicate call")
else:
print("No duplicate call found")
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
print("Done!")