Eclipse 添加书签
讲解如何在 Eclipse 编辑器任意代码行添加书签标记、打开并使用 Bookmarks 视图快速跳转,以及通过垂直标尺或视图菜单删除不再需要的书签。
学完这一章,你能够
- 讲解如何在 Eclipse 编辑器任意代码行添加书签标记、打开并使用 Bookmarks 视图快速跳转,以及通过垂直标尺或视图菜单删除不再需要的书签。
书签的作用与添加步骤
Eclipse 允许开发者在编辑器的任意代码行放置书签(Bookmark)。书签既可以作为阅读代码时的备忘提示,也便于在复杂工程的多个关键位置之间一键跳转。
添加书签的方法非常直观:在目标代码行左侧的垂直标尺(Vertical Ruler)上单击鼠标右键,选择 `Add Bookmark...`,然后在弹出的对话框中输入便于识别的书签名称并确认。
添加成功后,该行垂直标尺上会立即出现书签图标标记,同时该条目会自动登记进工作空间的 `Bookmarks`(书签)视图列表中。
打开并使用 Bookmarks 视图跳转
若当前界面尚未显示书签视图,可按以下路径开启:点击顶部菜单 `Window > Show View > Other...`,在弹出的视图搜索框中输入 `Bookmark`,展开 `General` 分类选中 `Bookmarks`,最后点击 `OK` 按钮即可打开。
在 `Bookmarks` 视图中会汇总展示所有书签的描述、所在资源路径及具体行号。只需双击任意书签条目,或右键点击条目选择 `Go to`,Eclipse 便会自动打开对应文件并将光标精准定位到书签所在行。
删除不再需要的书签
书签使用完毕后有两种移除方式:其一是在代码编辑器左侧垂直标尺的书签图标上右键,选择 `Remove Bookmark` 直接清除当前行书签;
其二是在 `Bookmarks` 视图中右键点击目标条目(或选中多条)并选择 `Delete` 菜单项,即可集中删除。本站离线实验脚本已对书签创建、视图过滤定位、`Go to` 跳转及双端同步删除完成校验。
运行 Eclipse 书签标尺登记、Bookmarks 视图发现/跳转与双端删除离线核验
"""Offline Eclipse Bookmarks ruler marker & Bookmarks view verifier for Ch29 (add-bookmarks)."""
import json
VERSION = "CPython 3.12.14 offline Eclipse Bookmarks marker & view 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: Add Bookmark on vertical ruler stores file, line, description, and ruler icon
bookmarks = []
def add_bookmark(file_path, line, description):
bm = {"id": len(bookmarks) + 1, "file": file_path, "line": line, "description": description}
bookmarks.append(bm)
return bm
add_bookmark("src/com/example/OrderService.java", 42, "Check discount rounding rule")
add_bookmark("src/com/example/PaymentClient.java", 88, "Retry timeout configuration")
cases.append(
check(
"add_bookmark_ruler_and_store",
"Verify Add Bookmark on vertical ruler records file, line, and custom bookmark name",
{
"count": 2,
"first_entry": {
"file": "src/com/example/OrderService.java",
"line": 42,
"description": "Check discount rounding rule",
},
},
{
"count": len(bookmarks),
"first_entry": {
"file": bookmarks[0]["file"],
"line": bookmarks[0]["line"],
"description": bookmarks[0]["description"],
},
},
)
)
# Case 2: Open Bookmarks view (Window > Show View > Other -> General > Bookmarks) and Go to navigation
views = [
{"category": "General", "name": "Bookmarks"},
{"category": "General", "name": "Outline"},
{"category": "General", "name": "Tasks"},
{"category": "Java", "name": "Package Explorer"},
]
matched_views = [f"{v['category']} > {v['name']}" for v in views if "bookmark" in v["name"].lower()]
target = bookmarks[1]
goto_cursor = (target["file"], target["line"])
cases.append(
check(
"bookmarks_view_discovery_and_goto",
"Verify filtering 'Bookmark' in Show View locates General > Bookmarks and Go to jumps to line",
{
"matched_views": ["General > Bookmarks"],
"goto_target": ["src/com/example/PaymentClient.java", 88],
},
{
"matched_views": matched_views,
"goto_target": list(goto_cursor),
},
)
)
# Case 3: Remove Bookmark via ruler and Delete via Bookmarks view
# Remove via vertical ruler on line 42
bookmarks = [b for b in bookmarks if not (b["file"] == "src/com/example/OrderService.java" and b["line"] == 42)]
after_ruler_remove = len(bookmarks)
# Delete remaining entry from Bookmarks view
bookmarks = [b for b in bookmarks if b["id"] != 2]
after_view_delete = len(bookmarks)
cases.append(
check(
"remove_bookmark_ruler_and_view",
"Verify removing bookmarks via vertical ruler Remove Bookmark and Bookmarks view Delete stays synchronized",
{
"after_ruler_remove": 1,
"after_view_delete": 0,
},
{
"after_ruler_remove": after_ruler_remove,
"after_view_delete": after_view_delete,
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 验证 `Add Bookmark` 记录文件/行号/名称、`Show View > Other` 过滤 `Bookmark` 定位 `General > Bookmarks`、`Go to` 跳转以及标尺/视图同步移除。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:682e3d3a22787a83c32eca5a391c812e2f97d7385390c692d7c5ba70860c0627
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:c6446d6824cd9b75e091800d926d1d38b80afca5bbc9beaa543819919e0be5fa
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Eclipse 中添加书签后,若关闭了对应源码标签页,如何快速重新回到该书签所在的代码行?
展开参考解答
通过 `Window > Show View > Other > General > Bookmarks` 打开书签视图,在列表中双击该书签或右键选择 `Go to`,即可自动打开文件并跳转到标记行。