"""Offline Eclipse Java Editor Code Templates & variable expansion verifier for Ch32 (code-templates)."""
import json
import re
VERSION = "CPython 3.12.14 offline Eclipse Java Code Template & variable verifier (desktop SWT IDE absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def expand_template(pattern, builtins):
def repl(match):
var = match.group(1)
if var in builtins:
return builtins[var]
return var
return re.sub(r"\$\{([A-Za-z0-9_]+)\}", repl, pattern)
def main():
cases = []
# Case 1: Built-in 'main' and 'syso' / 'sysout' template expansion via Alt+/
templates = {
"main": {
"context": "Java type members",
"pattern": "public static void main(String[] args) {\n\t${cursor}\n}",
},
"sysout": {
"context": "Java statements",
"pattern": "System.out.println(${word_selection}${});${cursor}",
},
"syso": {
"context": "Java statements",
"pattern": "System.out.println(${cursor});",
},
"docheader": {
"context": "Java doc",
"pattern": "@date ${date} ${time}",
},
"customlog": {
"context": "Java",
"pattern": "logger.info(\"${date} ${time} - ${myTemplateVariable}\");${cursor}",
},
}
expanded_main = expand_template(templates["main"]["pattern"], {"cursor": ""})
expanded_syso = expand_template(templates["syso"]["pattern"], {"cursor": ""})
cases.append(
check(
"template_main_and_syso_expansion",
"Verify expanding 'main' and 'syso' abbreviations via Alt+/ produces canonical Java boilerplate",
{
"main_signature_present": True,
"syso_output": "System.out.println();",
},
{
"main_signature_present": expanded_main.startswith("public static void main(String[] args) {"),
"syso_output": expanded_syso,
},
)
)
# Case 2: Context scoping across the 4 Java template contexts
all_contexts = ["Java type members", "Java statements", "Java", "Java doc"]
def active_templates(current_context):
return sorted(
name
for name, tpl in templates.items()
if tpl["context"] == current_context or (tpl["context"] == "Java" and current_context != "Java doc")
)
in_members = active_templates("Java type members")
in_statements = active_templates("Java statements")
in_javadoc = active_templates("Java doc")
cases.append(
check(
"template_context_scoping",
"Verify template activation respects Java type members, Java statements, Java, and Java doc contexts",
{
"context_count": 4,
"in_type_members": ["customlog", "main"],
"in_statements": ["customlog", "syso", "sysout"],
"in_javadoc": ["docheader"],
},
{
"context_count": len(all_contexts),
"in_type_members": in_members,
"in_statements": in_statements,
"in_javadoc": in_javadoc,
},
)
)
# Case 3: Built-in variables (${cursor}, ${date}, ${time}, ${line_selection}, ${word_selection}) & custom variable
builtins = {
"cursor": "|",
"date": "2026-11-02",
"time": "09:30:00",
"line_selection": "int count = 0;",
"word_selection": "orderId",
}
expanded_custom = expand_template(templates["customlog"]["pattern"], builtins)
wrap_pattern = "try {\n\t${line_selection}\n} catch (Exception e) {\n\tlog(${word_selection});${cursor}\n}"
expanded_wrap = expand_template(wrap_pattern, builtins)
cases.append(
check(
"template_variables_and_custom_var",
"Verify built-in template variables and custom ${myTemplateVariable} literal rendering",
{
"custom_rendered": 'logger.info("2026-11-02 09:30:00 - myTemplateVariable");|',
"has_line_selection": True,
"has_word_selection": True,
},
{
"custom_rendered": expanded_custom,
"has_line_selection": "int count = 0;" in expanded_wrap,
"has_word_selection": "log(orderId);|" in expanded_wrap,
},
)
)
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-code-templates-lab/explore-code-templates.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-code-templates.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2263.zip