蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse File Search 全量文本检索、Java Type References 检索与结果树聚合离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-search-menu.py
explore-search-menu.py134 行
"""Offline Eclipse File Search & Java Search reference verifier for Ch26 (search-menu)."""
import json
import re

VERSION = "CPython 3.12.14 offline Eclipse Workspace File/Java Search verifier (desktop SWT IDE absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    workspace_files = {
        "demo/src/com/example/Person.java": [
            "package com.example;",
            "public class Person {",
            "    private String name;",
            "}",
        ],
        "demo/src/com/example/PersonService.java": [
            "package com.example;",
            "public class PersonService {",
            "    public Person create(String name) {",
            "        return new Person();",
            "    }",
            "}",
        ],
        "demo/resources/app.properties": [
            "entity.default=Person",
        ],
        "docs/readme.txt": [
            "Domain object: Person",
        ],
    }

    # Case 1: File Search (Ctrl+H > File Search) matches across all file types and supports folder scoping
    all_file_hits = sorted(
        path for path, lines in workspace_files.items() if any("Person" in ln for ln in lines)
    )
    scoped_src_hits = sorted(
        path
        for path, lines in workspace_files.items()
        if path.startswith("demo/src/") and any("Person" in ln for ln in lines)
    )

    cases.append(
        check(
            "file_search_all_extensions",
            "Verify Ctrl+H File Search scans all extensions and honors selected folder/project scope",
            {
                "shortcut": "Ctrl+H",
                "all_file_hit_count": 4,
                "scoped_src_hits": [
                    "demo/src/com/example/Person.java",
                    "demo/src/com/example/PersonService.java",
                ],
            },
            {
                "shortcut": "Ctrl+H",
                "all_file_hit_count": len(all_file_hits),
                "scoped_src_hits": scoped_src_hits,
            },
        )
    )

    # Case 2: Java Search (Search For=Type, Limit To=References) finds only Java references to Person
    decl_pat = re.compile(r"\bclass\s+Person\b")
    ref_pat = re.compile(r"\bPerson\b")
    type_declarations = []
    type_references = []
    for path, lines in workspace_files.items():
        if not path.endswith(".java"):
            continue
        for lineno, ln in enumerate(lines, start=1):
            if decl_pat.search(ln):
                type_declarations.append((path, lineno))
            elif ref_pat.search(ln) and "class PersonService" not in ln:
                type_references.append((path, lineno))

    cases.append(
        check(
            "java_search_type_references",
            "Verify Java Search with Search For=Type and Limit To=References finds Java type references only",
            {
                "declarations": [["demo/src/com/example/Person.java", 2]],
                "references": [
                    ["demo/src/com/example/PersonService.java", 3],
                    ["demo/src/com/example/PersonService.java", 4],
                ],
            },
            {
                "declarations": [list(x) for x in type_declarations],
                "references": [list(x) for x in type_references],
            },
        )
    )

    # Case 3: Search view tree grouping by project -> file -> match count
    grouped = {}
    for path, lineno in type_references:
        project = path.split("/")[0]
        grouped.setdefault(project, {}).setdefault(path, []).append(lineno)

    cases.append(
        check(
            "search_view_match_grouping",
            "Verify Search view groups matched lines hierarchically by project and Java compilation unit",
            {
                "project_count": 1,
                "total_reference_matches": 2,
                "matched_lines_in_service": [3, 4],
            },
            {
                "project_count": len(grouped),
                "total_reference_matches": sum(len(v) for files in grouped.values() for v in files.values()),
                "matched_lines_in_service": grouped["demo"]["demo/src/com/example/PersonService.java"],
            },
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/eclipse-search-menu-lab/explore-search-menu.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-search-menu.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2288.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习