(file_path, search_string)
| 8 | |
| 9 | |
| 10 | def search_in_file(file_path, search_string): |
| 11 | results = SearchResults() |
| 12 | |
| 13 | def perform_search(content, search_str): |
| 14 | name_pattern = re.compile(r'// @name:\s*' + re.escape(search_str) + r'\s*(.+)') |
| 15 | description_pattern = re.compile(r'// @description:\s*' + re.escape(search_str) + r'\s*(.+)') |
| 16 | results.name_matches.extend([match.strip() for match in name_pattern.findall(content)]) |
| 17 | results.description_matches.extend([match.strip() for match in description_pattern.findall(content)]) |
| 18 | |
| 19 | try: |
| 20 | with open(file_path, 'r', encoding='utf-8') as file: |
| 21 | content = file.read() |
| 22 | perform_search(content, search_string) |
| 23 | |
| 24 | if not results.name_matches and not results.description_matches: |
| 25 | perform_search(content, "zh-CN") |
| 26 | |
| 27 | except FileNotFoundError: |
| 28 | print("文件未找到,请检查文件路径。") |
| 29 | except Exception as e: |
| 30 | print(f"发生错误: {e}") |
| 31 | |
| 32 | return results |
no test coverage detected