2026-04-19-「插件」OpenCode 实时获取 VSCode/JetBrains 编辑器选区

文章发布时间:

最后更新时间:

页面浏览: 加载中...

OpenCode 实时获取 VSCode/JetBrains 编辑器选区

0x00 现状

OpenCode 官方 VSCode 扩展声称支持 “Context Awareness: Automatically share your current selection or tab with OpenCode”,但实际上这个功能长期未真正实现

  • Issue #3472 (2025 年 10 月) 报告了这个问题:用户在 VSCode 中选中文本后,在 opencode 中引用时,agent 完全不知道选中了什么内容。该 issue 被标记为 bug,最终以 “closed as not planned” 关闭。
  • Reddit 上也有用户反馈 Context Awareness 功能无法正常工作1。

目前官方扩展实际支持的是手动操作:通过快捷键 Cmd+Option+K / Alt+Ctrl+K 插入文件引用(如 @File#L37-42),但这不是实时自动感知选区,而是需要用户主动触发。

目前的一个实现来自社区贡献者 mergeconflict,他在 2026 年 3 月提交了 PR #18649: feat: live IDE context awareness,这是目前唯一真正实现了实时获取 VSCode 选区的方案。

核心架构:

1
2
3
4
5
6
7
8
9
10
11
12
VS Code extension                       CLI (opencode)
┌──────────────────────┐ ┌──────────────────────────┐
│ vscode-editor-state │ editor events │ ide/index.ts │
│ (onDidChange*) │ ──debounce───► │ subscribeToContext() │
│ │ notify │ ├─ readResource() │
│ mcp-server.ts │ ◄─subscribe─── │ ├─ editorContext() │
│ editor://context │ resource │ └─ Bus → ContextUpdated │
│ resource + notify │ read │ │
└──────────────────────┘ │ session/system.ts │
│ TUI sidebar.tsx │
│ TUI index.tsx │
└──────────────────────────┘

实现要点:

通信协议在 VSCode 扩展中嵌入一个 MCP HTTP Server,暴露 editor://context 资源,扩展启动时写入 lock 文件到 {xdgData}/opencode/ide/{port}.lock,包含 auth token 和 PID;CLI 启动时自动发现,后面是做实时更新,监听 onDidChange* 事件,150ms 防抖,通过 notifications/resources/updated 推送,
Bearer token 认证 + PID 存活验证 + 仅绑定 localhost

System prompt 自动注入
TUI 侧边栏实时显示
消息历史 ✂ Selected N lines from filename 标记

当前状态:PR 仍处于 Open 状态,尚未合并到主线。 社区评审通过了 (LGTM),但官方 maintainer 尚未 merge。

三、JetBrains 侧的实现

JetBrains 方面有多个社区项目,但都是手动发送选区,而非实时自动感知

项目 方式 实时性
OpenCode Relay (7 stars) 右键菜单 “Send Selection to OpenCode”,通过 OpenCode Server API 将选区以 fenced code block 形式追加到 TUI prompt 手动触发
OpenCode_UI (34 stars) Opt+Cmd+K 快捷键发送当前文件 / 选区引用到 terminal 手动触发
opencode-jb JetBrains 插件集成 手动触发

另外值得注意的是,JetBrains 官方 MCP Server Plugin 也有人提了 Feature Request: Add get_editor_selection tool,请求暴露当前编辑器选区,该 issue 提到 Claude Code 的 JetBrains 插件已经通过 WebSocket 实现了实时选区共享,但 JetBrains MCP Server Plugin 目前尚不支持。

总之目前没有已发布的、开箱即用的方案可以实时获取 VSCode/JetBrains 的鼠标选区并同步到 OpenCode。 最接近的实现是 PR # 18649,如果你需要这个能力,可以:

  1. Fork 该 PR 的分支自行构建mergeconflict:dev 分支)
  2. 关注 PR #18649 的合并进展
  3. 参考其架构(MCP Server + lock file 发现协议),为 JetBrains 侧做类似实现