Eclipse 首选项(Preferences)
介绍 Eclipse 首选项对话框的插件可扩展架构、Window > Preferences 打开路径、左侧分类导航、左上角关键字快速检索以及 OK/Cancel 保存回滚机制。
学完这一章,你能够
- 介绍 Eclipse 首选项对话框的插件可扩展架构、Window > Preferences 打开路径、左侧分类导航、左上角关键字快速检索以及 OK/Cancel 保存回滚机制。
首选项框架与打开路径
Eclipse 的首选项(Preferences)对话框由核心平台框架统一托管,同时对外开放扩展点——每安装一个第三方插件(如 Java 开发工具 JDT、Maven 或 Python 插件),该插件都可以在对话框中注册专属的配置页面。在 Windows/Linux 下,只需点击顶部菜单 `Window > Preferences` 即可打开首选项窗口(macOS 下对应应用程序菜单中的 `Preferences` / `Settings`)。
分类浏览、快速过滤与保存生效
首选项对话框左侧按功能域组织为多级树形分类(如 `General`、`Java` 等),可以逐层展开节点定位具体设置页。由于配置页数量众多,更高效的方式是使用对话框左上角的过滤搜索框:只需输入目标配置项的关键字母(例如输入 `font`),左侧树便会实时筛出字体(`Colors and Fonts`)等相关页面。
完成参数调整后,点击底部 `OK`(新版常标注为 `Apply and Close`)即可持久化保存配置并关闭对话框;若不希望保留本次更改,点击 `Cancel` 按钮即可放弃修改并恢复原值。本站离线实验脚本已对多插件首选项树、关键字过滤及作用域继承与提交/回滚语义完成校验。
运行 Eclipse 首选项多插件分类树、过滤检索与作用域 OK/Cancel 事务离线核验
"""Offline Eclipse Preferences tree, filter search & scope inheritance verifier for Ch22 (preferences)."""
import json
VERSION = "CPython 3.12.14 offline Eclipse Preferences hierarchy & scope 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 = []
# Case 1: Framework & plugin-contributed preference tree
pref_pages = [
{"path": "General > Appearance > Colors and Fonts", "provider": "platform"},
{"path": "General > Editors > Text Editors", "provider": "platform"},
{"path": "General > Keys", "provider": "platform"},
{"path": "General > Workspace", "provider": "platform"},
{"path": "Java > Compiler", "provider": "jdt-plugin"},
{"path": "Java > Editor > Content Assist", "provider": "jdt-plugin"},
{"path": "Maven > User Settings", "provider": "m2e-plugin"},
]
top_categories = sorted({p["path"].split(" > ")[0] for p in pref_pages})
plugin_pages = [p["path"] for p in pref_pages if p["provider"] != "platform"]
cases.append(
check(
"preference_tree_and_plugin_pages",
"Verify Window > Preferences combines core platform pages and plugin-contributed pages",
{
"top_categories": ["General", "Java", "Maven"],
"plugin_page_count": 3,
},
{
"top_categories": top_categories,
"plugin_page_count": len(plugin_pages),
},
)
)
# Case 2: Top-left quick filter box matches page names case-insensitively (e.g., "font")
query = "font"
matched_font = [p["path"] for p in pref_pages if query.lower() in p["path"].lower()]
query_keys = "keys"
matched_keys = [p["path"] for p in pref_pages if query_keys.lower() in p["path"].lower()]
cases.append(
check(
"preference_filter_search",
"Verify top-left filter input narrows preference tree for queries such as 'font' and 'keys'",
{
"font_matches": ["General > Appearance > Colors and Fonts"],
"keys_matches": ["General > Keys"],
},
{
"font_matches": matched_font,
"keys_matches": matched_keys,
},
)
)
# Case 3: Preference scope inheritance (default -> instance -> project) and OK vs Cancel transaction
default_scope = {"encoding": "ISO-8859-1", "tabWidth": 4, "fontSize": 10}
instance_scope = {"encoding": "UTF-8"}
project_scope = {"tabWidth": 2}
def effective(inst):
merged = dict(default_scope)
merged.update(inst)
merged.update(project_scope)
return merged
# Edit in dialog then Cancel -> discards draft
draft_cancel = dict(instance_scope, fontSize=14)
# Cancel does not write back to instance_scope
after_cancel = effective(instance_scope)
# Edit in dialog then OK -> persists draft
draft_ok = dict(instance_scope, fontSize=12)
instance_scope = draft_ok
after_ok = effective(instance_scope)
cases.append(
check(
"preference_scope_and_ok_cancel",
"Verify preference scope override order and OK commit vs Cancel discard semantics",
{
"after_cancel": {"encoding": "UTF-8", "tabWidth": 2, "fontSize": 10},
"after_ok": {"encoding": "UTF-8", "tabWidth": 2, "fontSize": 12},
},
{
"after_cancel": after_cancel,
"after_ok": after_ok,
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 验证平台与插件配置页聚合、输入 `font`/`keys` 精准筛出目标页面,以及 `default -> instance -> project` 作用域覆盖与 `OK`/`Cancel` 提交回滚。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:2113e22b3889d2ec673571594f3d5df8ba07a99877a04d85dc53e4c168c91cc4
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:cc32327090d4ba5241fc6e2d75dd1966052d28ec5444a3a7270eaf6b60aad059
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
面对 Eclipse 首选项中上百个配置页,如何最快定位字体或快捷键等具体设置?
展开参考解答
打开 `Window > Preferences` 后,直接在对话框左上角的过滤输入框中键入英文关键词(如 `font` 或 `keys`),左侧分类树会即时筛选并高亮匹配的配置页面。