peb

package
v0.1.0-rc.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

peb v20 — Pencode Extension Binary

peb 是 Pencode 和 server 上扩展进程之间的二进制协议。宿主写扩展的 stdin,读取其 stdout;日志只写 stderr。不使用 JSON 消息、换行分隔或 shell 命令。支持 Fonts(字体)、FileIcons(文件图标)、EditorThemes(颜色主题)、CustomEditors(HTML/CSS 文件编辑工具)、LanguageServers(语言服务)、Actions(工作台按钮)、ExtensionUI(弹窗、菜单与 Panel)、DebugAdapters(调试适配器)、ExtensionSettings(扩展配置)和 LanguagePacks(界面语言包)。

server 负责扩展目录、安装、注册探测和进程;GUI 通过 CS 连接代理 peb 流,并在后台解析字体、主题、图标与编辑器图片。已验证资源进入本地内存缓存,渲染时不读取服务端文件。

字体、主题与图标保持静态注册。每个窗口连接为同一扩展按需启动一个常驻进程,工作台 action 和各文件编辑器通过独立逻辑会话复用它。不同窗口保持进程隔离;关闭文件或切换工具只释放该视图,最后一个视图关闭后进程仍保留,禁用、卸载、窗口连接关闭时回收。注册探测仍使用短生命周期进程;LSP 和调试适配器沿用各自的进程管理。注册、进程 I/O 和退出不阻塞 UI。v20 为帧增加会话标识;宿主与 Go、TypeScript 扩展需用当前 SDK 重新构建。

窗口与视图会话

同一物理 stdin/stdout 上,帧的 session ID 标识工作台 action 或文档视图,请求 ID 仅在该会话内关联。宿主分配非零、进程内不复用的 session ID;每个会话独立 Initialize/Register,回复和主动推送必须携带原 session ID。非零会话上的 Shutdown 取消该会话的 context 并释放视图;控制会话 0 上的 Shutdown 退出进程。每进程最多 256 个会话。

宿主为既有 action/editor 调用提供虚拟 stdio 流,在服务端添加和去除 session ID。每个会话输出队列最多约 8 MiB,溢出仅关闭该会话;stderr 共享保留最近 64 KiB,各会话独立读取。扩展崩溃会断开所有视图,包括空闲视图,下一次打开会话可重启扩展。

Handlers.Editor / TS editor 适合无状态处理函数。有缓存、选择或分页状态的编辑器使用 Handlers.NewEditor(ctx) / TS newEditor(signal),每个文档视图创建一次处理函数,同一路径在不同视图中也互相隔离。关闭视图会取消传入的 context/signal,可据此停止后台任务。窗口级 settings、action、UI 和 lifecycle 回调仍串行执行;配置快照应来自该窗口的一致配置。

peb.Handlers{
    NewEditor: func(ctx context.Context) func(peb.EditorMessage) (peb.EditorReply, error) {
        requests := 0
        return func(message peb.EditorMessage) (peb.EditorReply, error) {
            requests++ // 每个视图独立计数。
            return peb.EditorReply{}, ctx.Err()
        }
    },
}

扩展配置

扩展通过 Contributions.Settings 注册配置项。在设置左侧展开“扩展”,仅列出已启用、加载成功且声明了配置项的扩展;点击名称后,右侧显示对应表单。界面沿用 IDE 的输入框、下拉框、复选框和主题颜色。文本在提交更改时保存,布尔和下拉选项立即保存;支持恢复当前扩展的默认设置。

contributions := peb.Contributions{
    Settings: []peb.Setting{
        {ID: "name", Label: "名称", Description: "扩展显示名称",
            Type: peb.SettingString, Default: json.RawMessage(`"Pencode"`)},
        {ID: "enabled", Label: "启用", Type: peb.SettingBoolean,
            Default: json.RawMessage(`true`)},
        {ID: "scale", Label: "比例", Type: peb.SettingNumber,
            Default: json.RawMessage(`1.5`)},
        {ID: "limit", Label: "数量", Type: peb.SettingInteger,
            Default: json.RawMessage(`10`)},
        {ID: "mode", Label: "模式", Type: peb.SettingString,
            Default: json.RawMessage(`"auto"`),
            Options: []peb.SettingOption{{Value: "auto", Label: "自动"}, {Value: "manual", Label: "手动"}}},
    },
}
handlers := peb.Handlers{
    Settings: func(values peb.SettingValues) error {
        name, err := peb.DecodeSetting[string](values, "name")
        if err != nil { return err }
        // 保存 name 供后续 action 或编辑器请求使用。
        _ = name
        return nil
    },
}
err := peb.ServeExtension(os.Stdin, os.Stdout, contributions, handlers)

SettingString=0、SettingBoolean=1、SettingNumber=2、SettingInteger=3;整数为有符号 64 位整数,数字必须可表示为有限的 float64。Default 必填且必须匹配类型。Options 仅用于字符串,提供选项时默认值和用户值必须在选项中。每个扩展最多 256 项,每项最多 256 个选项,ID 在扩展内部唯一,不允许 /、\ 或 NUL;Label 必填,Description 可选。重复 ID、重复选项、未知类型和非法默认值会拒绝注册。

宿主在 Register 成功后、ActionActivate 或首次 EditorRequest 前,向声明配置的扩展发送一次 SettingsRequest;修改或恢复默认时,向正在运行的 action 和文件编辑器进程发送新的完整快照。新启动的进程直接获得最新配置。Handlers.Settings 与其他处理函数串行执行,收到的 map 与 JSON 数据归扩展所有,可以保留;初次回调时 action publisher 尚未激活。回调返回错误时回复 Failure,运行期错误通过现有扩展诊断通路报告。注册探测只读取声明,不调用此回调。

值按扩展 ID 隔离,写入工作区宿主的用户 settings.json:

{"extensions":{"ui-demo":{"name":"Pencode"}}}

远程工作区沿用服务端配置保存通路。未显式设置或不再符合声明的值使用默认值;停用、卸载扩展会移除设置导航,但保留已保存值,以便重新启用或安装。未识别的扩展和配置键也会保留。配置快照仅包含当前声明的键。等待发送的快照合并为最新一次,空闲轮询与已缓存配置查询不分配内存。

配置在二进制帧中编码,JSON 只用于单个值,以保留其类型:

Setting {
  id: string
  label: string
  description: string
  type: u32
  default: string              // JSON scalar
  options: array<SettingOption>
}
SettingOption { value: string, label: string }
SettingsRequest {
  values: array<SettingValue>
}
SettingValue { id: string, value: string } // value 为 JSON scalar
SettingsResponse {}                         // 空 payload

同一快照不能重复 ID,且必须恰好覆盖声明的配置项。所有字符串、数组和 payload 仍受 PEB 的通用大小上限约束。此变更将 PEB 升级为 v10,现有扩展需要用当前 SDK 重新构建。

界面语言包

扩展通过 Contributions.LanguagePacks 注册静态 JSON 语言包,加载后出现在「设置 → 外观 → 界面语言」。声明的 ID、Label 必须与文件中的 id、label 一致,Path 必须位于 resources/ 下。消息缺失时回退到英文,禁用或卸载后保留语言选择以便重新加载。参见语言包格式、Go 与 TypeScript 示例。

文件变化通知

扩展在 peb.Handlers.FileChanges 注册回调,由宿主统一文件服务提供变化,不需要自行递归监听文件系统。只有该回调的扩展也会获得持久 action 会话。已打开的自定义编辑器进程同样接收通知,处理函数与该进程其他请求串行执行。

handlers := peb.Handlers{
    FileChanges: func(changes peb.FileChanges) error {
        // changes.Root 是工作区绝对路径,Files[i].Path 是使用 / 的相对路径。
        // Reset=true 时应重新读取所需状态,不能只应用当前 Files。
        return nil
    },
}

通知按 root:string, reset:u32, files:array<{path:string, op:u32, directory:u32}> 编码。op 位依次为创建、写入、删除、重命名和元数据变化,重命名可能拆成两个路径事件。每个进程按工作区合并最多 128 个路径;队列溢出设置 Reset,消费后释放路径引用。回调收到的批次可保留,返回错误使用 Failure 响应。静态注册探测不调用此回调。

帧

所有整数都是无符号小端整数。每帧由固定 20 字节头部和 payload 组成;流允许任意拆包、粘包。读取方必须精确读取指定长度。

偏移 长度 字段
0 4 magic:70 65 62 00(peb\0)
4 2 version:20
6 2 kind:见下表
8 4 request ID
12 4 payload 字节数,不含头部,最大 4 MiB
16 4 session ID,0 为进程控制会话
kind 名称 方向 request ID
1 Initialize 宿主 → 扩展 非零,当前宿主使用 1
2 Register 扩展 → 宿主 原样返回 Initialize 的 ID
3 Shutdown 宿主 → 扩展 必须为 0,payload 必须为空
4 Failure 扩展 → 宿主 原样返回请求 ID
5 EditorRequest 宿主 → 扩展 非零,当前会话内递增
6 EditorResponse 扩展 → 宿主 原样返回 EditorRequest 的 ID
7 ActionActivate 宿主 → 扩展 0,payload 为空,仅发送一次
8 ActionInvoke 宿主 → 扩展 非零,当前 action 会话内递增
9 ActionResponse 扩展 → 宿主 原样返回 ActionInvoke 的 ID,payload 为空
10 ActionUpdate 扩展 → 宿主 0,无需等待点击即可推送
11 UIUpdate 扩展 → 宿主 0,打开、更新或关闭扩展界面
12 UIRequest 宿主 → 扩展 与 action 共用递增的非零 ID,携带控件或生命周期事件
13 UIResponse 扩展 → 宿主 原样返回 UIRequest 的 ID,payload 为空
14 SettingsRequest 宿主 → 扩展 非零,完整配置快照,与所在会话的请求共用 ID 序列
15 SettingsResponse 扩展 → 宿主 原样返回 SettingsRequest 的 ID,payload 为空
16 FileChangesRequest 宿主 → 扩展 非零,与所在会话共用 ID 序列
17 FileChangesResponse 扩展 → 宿主 原样返回 FileChangesRequest 的 ID,payload 为空
18 LifecycleRequest 宿主 → 扩展 非零,与 action 请求共用 ID 序列
19 LifecycleResponse 扩展 → 宿主 原样返回 LifecycleRequest 的 ID

版本不符、未知 kind、非法 ID、超大帧、截断数据和非法 payload 均终止本次注册。扩展进程启动、握手与退出总计最多 5 秒;失败或关闭会话会关闭管道、终止并回收子进程。GUI 使用最多 4 个资源加载 worker,server 当前串行执行扩展注册与安装控制操作;最多发现 64 个已安装扩展。stderr 持续读取,最多保留 4096 字节诊断。

Payload 编码

string = u32 字节数 + UTF-8 字节,无终止符;最大 64 KiB。array<T> = u32 元素数 + 按序排列的元素,单个数组最多 16384 项。结构体依次编码字段,不写字段名,不填充、不对齐。payload 不允许尾随字节。

Initialize {
  host: string                  // 当前为 "pencode"
  capabilities: u32             // FileIcons=1, EditorThemes=2, CustomEditors=4, Fonts=8, LanguageServers=16, Actions=32, ExtensionUI=64, DebugAdapters=128, ExtensionSettings=256, LanguagePacks=512,当前为 1023
}

Register {
  themes: array<Theme>
  iconThemes: array<IconTheme>
  fonts: array<Font>
  languageServers: array<LanguageServer>
  actions: array<Action>
  actionService: u32             // boolean;允许初始按钮列表为空,之后主动注册
  debugAdapters: array<DebugAdapter>
  settings: array<Setting>
  languagePacks: array<LanguagePack>
}
LanguagePack { id: string, label: string, path: string }

Theme {
  id: string
  label: string                 // 主题选择器显示名称
  path: string                  // 相对扩展根目录,例如 resources/themes/ink.json
}

IconTheme {
  id: string
  label: string
  rules: array<IconRule>
}

IconRule {
  match: u32                    // 见下表
  pattern: string
  path: string                  // 相对扩展根目录,例如 resources/icons/code.svg
}

Font {
  id: string
  label: string
  path: string                  // resources/fonts/regular.ttf
  boldPath: string              // 可选粗体路径,无粗体时为空字符串
}

LanguageServer {
  id: string
  label: string
  languageId: string             // 例如 go / typescript / rust
  command: string               // PATH、绝对路径或相对扩展目录的可执行文件
  args: array<string>
  selectors: array<DocumentSelector>
  initializationOptions: text    // 空字符串或标准 JSON
  configuration: text            // 空字符串或标准 JSON
}

DebugAdapter {
  id: string                    // 例如 Delve,与调试配置的 adapter 对应
  label: string
  command: string               // PATH、绝对路径或相对扩展目录
  args: array<string>           // 启动前缀,位于适配器固定参数和用户参数之前
  templates: text              // 可选的 debug.json 数组;每项 adapter 必须与 id 相同
}

Failure {
  message: string
}

字体、主题和图标主题的 ID 分别唯一,只允许小写字母、数字、.、-,长度为 1–128 字节,不能为 . 或 ..。label 必须非空。宿主将它们注册为 extensions/<扩展 ID>/<贡献 ID>,各类贡献各自使用独立的选择器。扩展应只返回宿主声明支持的能力。

match Go 常量 pattern 匹配对象
0 DefaultFile 空字符串 默认文件
1 FileName 完整文件名,例如 go.mod 文件 basename
2 FileSuffix 带前导点,例如 .go、.d.ts 文件名后缀
3 DefaultFolder 空字符串 默认目录
4 FolderName 完整目录名,例如 src 目录 basename

图标匹配不区分大小写,与 VS Code 一致。完整文件名匹配优先(包括 Dockerfile 等无后缀文件),再匹配最长后缀,再使用默认文件图标;目录名完整匹配优先于默认目录图标。同一类型和 pattern(忽略大小写)重复时,最后一条规则生效。未命中且未声明默认规则时,使用 Pencode 内置图标。pattern 不能包含路径分隔符。

资源必须位于扩展包的 resources/ 中。路径使用 /,禁止绝对路径、..、查询字符串及跳出扩展根目录的符号链接。主题和图标每份资源最多 1 MiB,字体每份最多 32 MiB,一个扩展累计最多读取 64 MiB;相同路径只读取一次。主题使用 Pencode 现有的 JSON/JSONC colors / tokenColors 格式,支持的 include 同样受资源路径与大小限制。

文件图标使用 SVG,原始尺寸为 1–256 像素;SVG 在注册时解析校验,原文缓存于宿主内存。currentColor 随颜色主题和文件选中状态着色,SVG 中显式颜色保留。纹理按资源和前景色缓存。图标规则编译为查找表,匹配时不分配内存,也不发起进程通信。

字体

扩展通过 Contributions.Fonts 提供字体,同一字体可用于代码和界面。Path 必填,BoldPath 可省略;使用引擎支持的 TTF 或 OTF 字体。资源在后台解析,损坏字体会使整次注册失败。同一路径只读取和解析一次,渲染阶段复用字形缓存。字体文件不通过 peb 帧传输,只传资源路径;CS 链路把字节批量传给 GUI,在后台解析并缓存。

peb.Contributions{
    Fonts: []peb.Font{
        {ID: "mono", Label: "My Mono", Path: "resources/fonts/regular.ttf", BoldPath: "resources/fonts/bold.ttf"},
    },
}

字体 ID 在宿主中为 extensions/<扩展 ID>/<字体 ID>,保存在 codeFont / uiFont 设置中。尚未完成注册或加载失败时使用当前平台的系统默认等宽字体;注册完成后恢复选择。禁用、卸载时移除字体选项、回退系统默认字体并释放字形缓存。扩展包应随字体附上相应许可证。

TypeScript 扩展

peb/ts 提供与当前 v20 线格式一致的 TypeScript SDK,包括帧、全部消息 codec 和 serveExtension。TS 插件将 SDK 与业务依赖打包为 .js、.mjs 或 .cjs 入口,工作区主机自动使用 Node.js(优先)或 Bun 启动。无需可执行权限,原生入口保持直接启动。参见 Office Viewer 示例。

Go 扩展

使用 github.com/pencode-labs/pencode/peb:

package main

import (
    "fmt"
    "os"

    "github.com/pencode-labs/pencode/peb"
)

func main() {
    err := peb.Serve(os.Stdin, os.Stdout, peb.Contributions{
        Themes: []peb.Theme{
            {ID: "ink", Label: "Ink", Path: "resources/themes/ink.json"},
        },
        IconThemes: []peb.IconTheme{
            {ID: "code", Label: "Code", Rules: []peb.IconRule{
                {Match: peb.FileSuffix, Pattern: ".go", Path: "resources/icons/code.svg"},
            }},
        },
    })
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

Reader 复用帧缓冲区,Frame.Payload 只在下次 Read 前有效;各 Decode* 函数返回的字符串拥有独立存储。Encode* 函数接受可复用的目标切片。Reader / Writer 各自应由一个 goroutine 使用。

完整示例及安装步骤见 Pencode README。

自定义文件编辑工具

使用 peb.ServeEditors(stdin, stdout, contributions, handler),在 Contributions.Actions 注册类型为 OpenWith 的 action:

peb.Action{
    ID: "markdown",
    Text: "Markdown 预览",
    Tooltip: "Markdown 预览",
    Icon: "resources/preview.svg",
    Location: peb.OpenWith,
    Default: false,
    Selectors: []peb.DocumentSelector{
        {Match: peb.FileSuffix, Pattern: ".md"},
        {Match: peb.FileName, Pattern: "README"},
    },
}

打开方式使用 action 的 ID、图标与提示校验规则,宿主 ID 为 extensions/<扩展 ID>/<工具 ID>,消息内 Editor 使用扩展本地 ID。selector 只允许 FileName、FileSuffix,必须非空;区分大小写。文本文件默认使用内置编辑器,Default: true 声明自动打开。精确文件名优先于后缀,较长后缀优先,相同优先级按宿主工具 ID 排序。二进制和未知类型文件自动选择匹配工具;没有匹配工具时显示确认页,用户点击“使用代码编辑器打开”后才加载为文本。已知文本后缀仍由文件服务检查内容,包含 NUL 或无效 UTF-8 的文件也会进入确认页。扩展卸载或禁用后重新解析关联。代码区右上角浮动显示匹配的打开方式图标,默认透明度为 30%,悬停或键盘聚焦时为 100%。只有内置代码编辑器时不显示图标组;存在扩展打开方式时显示代码按钮,点击可返回源码。点击扩展图标默认替换整个代码区;路径栏的双栏按钮可同时显示左侧源码与右侧扩展视图,再次点击保留当前扩展为单栏。选择和布局按文件保留,并随项目标签状态恢复。

handler 接收 EditorMessage:Editor、文件绝对 Path、宿主当前 Content、Binary 和 Event。open 请求创建视图,documentchange 表示撤销、重做或从磁盘重新加载后同步文档。HTML 事件提供 Kind、data-ref、Value、Checked、Selected、Anchor、Focus、InputType 和 Data。宿主串行提交事件,每次均附最新文档快照;切换标签后回复仍应用到所属文件。与宿主新内容冲突的过期回复会丢弃并重新同步。

返回 EditorReply{View: ..., Content: ...}:

  • View == nil 保留 DOM、光标与滚动;提供 View 时替换整个 HTML 片段和作用域 CSS。
  • Content == nil 不修改文档;指向空字符串表示清空。修改由宿主统一管理 dirty、保存与撤销/重做。
  • View.Events 显式订阅 {Ref, Kind},支持 click、input、change、focus、blur、submit、reset。Ref 必须对应 HTML 的 data-ref。异步事件不能同步阻止默认行为,因此暂不开放 beforeinput 取消接口。
  • HTML 使用 Onix 现有 HTML/CSS 渲染器。CSS 按组件隔离,元素 id 必须以 pencode-extension- 开头,避免占用工作台控件 ID。可继承工作台 CSS 使用的 --vscode-* 颜色变量,主题切换保留组件样式。
  • 按钮的 title 会由宿主显示为 IDE 风格的悬浮提示;未设置 title 时使用 aria-label。图标按钮应提供简短的功能说明。Action 按钮使用 Tooltip,留空时使用 Text。
  • img src 可使用 resources/ 下的 SVG、PNG、JPEG 或 WebP 相对路径,也可使用本扩展的 peb://<扩展 ID>/resources/...。GUI 后台从 server 批量读取并校验,再改写成缓存的 peb:// URI;每张最大 4 MiB、每边不超过 2048 像素、每视图合计 8 MiB / 64 项 / 4 MP。SVG 按原始尺寸栅格化。资源获取最长 5 秒,文件大小、尺寸和路径限制失败会终止当前视图。CSS 保持内联文本,不增加外部样式表或脚本执行能力。

初次回复必须提供 View。扩展在 server 进程中处理事件,当前没有 JavaScript、VS Code API、任意键盘/指针事件订阅或增量 DOM 消息 API。二进制文件只提供 server 路径 Path、Binary=true、空 Content,扩展可在远程环境中按路径读取;第一版不支持二进制内容写回,宿主文本保存不会触碰这些文件。

每个请求最多等待 5 秒,空闲会话没有超时;错误、超时和进程退出显示在编辑区域。每份文档最多排队 32 个事件;队列溢出会终止该会话并显示错误。保存与关闭会等待尚未处理完的事件,避免丢失最后输入。协议缓冲区与空闲轮询复用内存;进程通信、HTML 替换和修改内容仍有必要分配,不能视为整个编辑链路零分配。

编辑工具新增的 payload 按以下顺序编码。bool 使用 u32,严格只接受 0/1;text 为 u32 字节数 + UTF-8,最大 2 MiB;普通 string 仍为 64 KiB,单帧总大小仍为 4 MiB。

DocumentSelector { match: u32, pattern: string, languageId: string }
EditorRequest {
  editor: string, path: string, content: text, binary: bool,
  kind: string, ref: string, value: text, inputType: string, data: text,
  checked: bool, selected: u32, anchor: u32, focus: u32
}
EditorResponse {
  hasView: bool,
  // hasView=true 时:html: text, css: text, events: array<{ref: string, kind: string}>
  hasContent: bool
  // hasContent=true 时:content: text
}

完整运行示例见 Markdown 预览。插件只提供只读预览,在 open 和 documentchange 时渲染最新宿主快照;源码编辑和双栏布局由宿主管理。

语言服务扩展

Contributions.LanguageServers 注册标准 stdio Language Server。注册扩展进程退出后,宿主在打开匹配文件时,为每个项目根目录和服务 ID 启动一个进程,多个文档共享它;禁用、卸载扩展、关闭项目或退出时执行 shutdown / exit 并回收进程。语言服务 stdout 只允许 LSP 的 Content-Length JSON-RPC 消息,和 peb 扩展进程的二进制 stdout 分开。

err := peb.Serve(os.Stdin, os.Stdout, peb.Contributions{
    LanguageServers: []peb.LanguageServer{{
        ID: "gopls", Label: "Go", LanguageID: "go", Command: "gopls",
        Selectors: []peb.DocumentSelector{{Match: peb.FileSuffix, Pattern: ".go"}},
        Configuration: `{"gopls":{"staticcheck":true}}`,
    }},
})

selector 使用与编辑工具相同的 FileName / FileSuffix 规则,不区分目录,同一服务的文件使用声明的 LanguageID。Command 不通过 shell 执行;带路径分隔符的相对路径基于扩展目录,服务工作目录和 rootUri 为项目根目录。InitializationOptions 原样传给 initialize;Configuration 用于 workspace/configuration 的按 section 查询和初始 workspace/didChangeConfiguration 通知。所有 JSON 文件仍放在扩展的 resources/ 中;也可以像示例一样在程序内生成选项。

完整可构建示例见 Go 扩展,安装和操作见 Pencode LSP 开发文档。

Actions

原生代码和扩展使用相同的 peb.Action 描述按钮。位置为 StatusBar=0(状态栏)、LeftContextMenu=1(左侧文件树右键菜单)、RightContextMenu=2(右侧编辑区右键菜单)、TitleBarRight=3(标题栏右侧)、OpenWith=4(代码区右上角打开方式图标)和 Command=5(无按钮命令)。按钮按注册顺序排列;更新位置后移动到对应区域。禁用或隐藏的按钮不会执行;禁用、卸载扩展及进程异常退出时清理其所有 action。

Action {
  id, text, tooltip, icon, color, iconColor, iconStyle, shortcut: string
  location: u32
  disabled, hidden, default: u32 // boolean,只允许 0 或 1
  selectors: array<DocumentSelector> // OpenWith 必须提供文件名或后缀;其他类型为空
}
ActionInvoke {
  id, projectPath, path: string
  directory: u32                // boolean
  caret, anchor: u32             // Unicode rune offset
}
ActionUpdate {
  action: Action
  remove: u32                   // boolean
}

text 和 tooltip 至少有一个非空;图标按钮用 tooltip 提供可访问名称。扩展 action 的 ID 规则与其他贡献一致,宿主统一添加 extensions/<扩展 ID>/ 前缀;点击回调中的 ID 是扩展本地 ID。icon 是 resources/ 内的 SVG 路径,尺寸 1–256;运行期间可换成其他 SVG,读取和校验在后台完成。使用 currentColor 绘制 SVG 的可着色部分。color 控制文本颜色,iconColor 控制图标颜色,两者接受 #RRGGBB 或 #RRGGBBAA;留空跟随主题。iconStyle 是图标的内联 CSS 声明,例如 opacity:.6;transform:rotate(90deg),最多 1024 字节。

Shortcut 声明单个组合键,例如 Primary+Shift+B、Ctrl+Space、F6。Primary 在 macOS 表示 Command,在其他平台表示 Ctrl;也可显式使用 Ctrl、Super、Shift、Alt。修饰键不能重复,必须在按键之前;名称区分大小写,不支持多段 chord。按键支持 A–Z、0–9、F1–F12、标点(等号、减号、逗号、句点、斜杠、分号、方括号、反斜杠、单引号、反引号),以及 Escape、Tab、Enter、Space、Backspace、Delete、Insert、Home、End、PageUp、PageDown、Left、Right、Up、Down。ParseShortcut 可用于校验和无分配解析。非法组合键会拒绝编码或注册。

Command 只注册可调用命令,不占按钮区域;隐藏、禁用或已移除的 action 不响应快捷键。文件树/编辑区菜单 action 的快捷键只在对应菜单打开时生效;OpenWith 只对匹配当前文件的工具生效,并切换打开方式。工作台内置快捷键优先,扩展之间相同组合键按注册顺序执行第一个可用 action。快捷键沿用 Handlers.Action 回调和当前文件/选区上下文;OpenWith 仍走编辑工具切换流程。

所有已加载、未隐藏的 action 自动显示在“设置 → 快捷键”,用户可在当前方案覆盖扩展默认组合键,即使扩展未声明 Shortcut 也可绑定。清除用户绑定后该方案不再触发该 action,恢复方案默认时重新使用扩展声明。运行期间 Update 可以修改或清空 Shortcut,用户覆盖优先。

快捷键页面提供“默认”及用户创建的方案 selector。通过名称输入框复制当前方案、新建后修改、重命名或删除;每个方案独立保存,切换立即生效。命令覆盖工作台、编辑器剪贴板和缩进、语言服务、调试、搜索、文件树菜单、终端、端口、源代码管理以及菜单/列表导航;录制时 Backspace 清除,Esc 取消。相同上下文内检查冲突,互斥上下文允许复用组合键。原生文本输入和 PTY 内容输入仍由对应输入层处理。

宿主 settings.json 中 keymap 保存当前方案名(默认 default),bindings 保存当前方案映射,keymaps 保存其他方案的映射。每条映射包含 key 和可选的 primary、control、super、shift、alt;零值映射表示禁用。扩展覆盖键为完整 action ID extensions/<扩展 ID>/<action ID>。未加载扩展的覆盖也会保留;本地及远端设置使用相同保存通路。普通按键匹配与未变化的 action 快照复用缓存,热路径不分配内存。

ActionPublisher.Update 对未注册 ID 创建按钮,对已有 ID 完整替换描述;空字段清除旧值,因此更新时应保留仍需要的字段。Remove(id) 移除按钮。每个扩展最多 256 个 action,更新复用已读取的图标资源并遵循资源大小限制。宿主每个会话最多排队 64 次 action/UI 事件,每次只执行一个请求;握手和点击请求超时均为 5 秒,空闲连接没有超时。UI 不等待扩展 I/O。

扩展使用 ServeExtension,提供 Handlers.Action、Handlers.UIEvent 或 ActivateActions 即自动注册 ActionService。ActivateActions 仅在 action 工作台进程启动时执行一次,静态校验和文件编辑进程不会调用它。可以在其中启动后台 goroutine,使用提供的 context 停止任务;publisher 支持并发调用。action 和 editor handler 在其所属进程内顺序执行,多个进程之间不共享内存。

action := peb.Action{
    ID: "build", Text: "构建", Tooltip: "构建当前项目",
    Icon: "resources/icons/build.svg", Location: peb.TitleBarRight,
    Shortcut: "Primary+Shift+B",
}
var buttons *peb.ActionPublisher
err := peb.ServeExtension(os.Stdin, os.Stdout,
    peb.Contributions{Actions: []peb.Action{action}},
    peb.Handlers{
        ActivateActions: func(ctx context.Context, p *peb.ActionPublisher) {
            buttons = p
            // 在这里启动需要主动更新按钮的后台任务,并监听 ctx.Done()。
        },
        Action: func(c peb.ActionContext) error {
            // 根据 c.ProjectPath、c.Path 和 c.Caret 执行扩展逻辑。
            action.Text = "构建完成"
            action.IconColor = "#73c991"
            return buttons.Update(action)
        },
    })

左侧菜单传入右键目标的绝对路径、所属项目和目录标记;右侧菜单传入打开菜单时的编辑器选区;状态栏与标题栏传入当前文件和选区。原生代码在共享 workbench.Session 上注册:

session := workbench.NewSession()
action := peb.Action{ID: "core/build", Text: "构建", Location: peb.StatusBar}
err := session.Actions.Register(actions.Definition{
    Action: action,
    Run: func(c peb.ActionContext) error {
        // 原生回调在 UI 线程执行;耗时工作自行放到后台。
        return nil
    },
})
// 支持从后台 goroutine 更新;自动唤醒界面,保持原点击回调。
action.Text = "构建中"
err = session.Actions.Update(action)
session.Actions.Remove(action.ID)
// 按归属批量清理:session.Actions.RemovePrefix("core/")

actions 包路径为 github.com/pencode-labs/pencode/actions。原生注册 ID 在整个 registry 中唯一,重复注册返回错误;Update 更新已有按钮,Remove 删除按钮。未变化的注册表快照、UI 描述同步以及复用缓冲区后的编码路径保持零分配。

弹窗、上下文菜单、Panel 与通知

ActionPublisher.Show(peb.UI{...}) 打开或更新扩展界面,CloseUI(id) 关闭;Handlers.UIEvent 接收控件和生命周期事件。必须注册 UIEvent,否则 Show 返回 ErrProtocol。这些界面复用 action 的常驻进程,I/O、图片读取和资源校验在后台执行。

Kind 行为
peb.Dialog 居中的模态弹窗;包含标题、关闭按钮、焦点循环及 Esc 关闭
peb.Notification 右下角堆叠通知;包含关闭按钮,不抢焦点,可设置自动关闭及自定义内容与操作
peb.Popup 类似扩展市场的非模态浮窗;支持坐标、锚点、宽高与关闭按钮;可操作背后的工作区,焦点在浮窗内时 Esc 关闭
peb.ContextMenu 锚定 action 的菜单;自动限制在工作区内,支持方向键、Tab、Esc 和点击外部关闭
peb.Panel 使用可调整高度的底部工作台面板;支持工具栏关闭、Esc 关闭及切换可见性

Panel 和 EditorTab 内容可通过 data-pencode-layout="fill" 填满可用空间,移除宿主内边距和内容滚动。填满的 Panel 由扩展提供标题行和内部滚动区域,宿主不再显示重复标题;标题右侧需预留 40px 给工作台关闭按钮。

UI.ID 属于扩展自身命名空间,遵循 action 本地 ID 规则。Title 必须非空,用作界面的可访问名称。Anchor 是本扩展的 action ID,打开时留空采用触发 action;文件树和编辑器菜单中的 action 也可作为锚点。也可用 ui:<surface ID>#<data-ref> 锚定本扩展另一界面的可聚焦控件(该界面须已打开)。首次打开必须提供 View,以后 View:nil 保留现有视图和输入状态;非空 View 替换 HTML/CSS 和事件绑定。其余描述字段完整替换,同一 ID 的 Kind 在关闭前不能改变。

浮层使用 UI.Placement 设置位置和大小,单位为工作区 CSS 像素(随工作区缩放):

字段 含义
Mode: peb.PositionDefault Dialog/Popup 居中,ContextMenu 锚定 action;X/Y 是偏移
Mode: peb.PositionAbsolute X/Y 是相对工作区左上角的坐标
Mode: peb.PositionAnchor X/Y 是相对 Anchor action 左下角的偏移;锚点不可用时采用触发位置
Mode: peb.PositionAnchorAbove 在锚点左上方显示,Y 相对菜单底边;上方空间不足时改在下方显示
Width, Height 浮层宽高;0 使用默认大小;负数及非有限数值无效

打开后可发送相同 ID、View:nil 更新位置和大小,保留输入、选区和滚动。坐标可为负数,最终位置和大小受工作区边界约束;窗口变化后会重新按请求的位置定位,锚定浮层跟随 action。Panel 使用底部布局,Notification 使用右下角通知布局,两者 Placement 必须为零值。Popup 的 Tab 使用原生焦点遍历;焦点移到工作区后不会截获工作区快捷键。

UI.CloseOnOutsideClick 控制 Popup 点击外部的行为:false(默认)保持打开,true 关闭并发送 Event.Kind="close"、Reason="outside" 的 UIEvent。鼠标按下或触摸按下时检测;即使焦点已经移到工作区,配置仍然生效。关闭保留底层点击和焦点。可用 View:nil 实时修改该配置;其余描述字段完整替换,后续更新需继续传入希望保留的值。此字段只影响 Popup;ContextMenu 始终点击外部关闭,Dialog/Panel 使用各自的关闭行为,关闭按钮、Esc 和 CloseUI 也保留原有行为。

err := publisher.Show(peb.UI{
    ID: "market", Title: "扩展市场", Kind: peb.Popup,
    CloseOnOutsideClick: true,
    Placement: peb.UIPlacement{
        Mode: peb.PositionAbsolute, X: 80, Y: 100, Width: 640, Height: 480,
    },
    View: &peb.EditorView{HTML: `<p>扩展内容</p>`},
})
// 改为显示在 action 下方;保留现有视图。
err = publisher.Show(peb.UI{
    ID: "market", Title: "扩展市场", Kind: peb.Popup, Anchor: "tools",
    CloseOnOutsideClick: true,
    Placement: peb.UIPlacement{
        Mode: peb.PositionAnchor, X: -480, Y: 8, Width: 640, Height: 480,
    },
})

Severity 选择通知风格:peb.NotificationInfo(默认普通通知)、peb.NotificationWarning(警告)、peb.NotificationError(错误)。工作台使用当前主题的通知配色、左侧色条及对应图标区分;错误使用 alert 语义。该字段仅适用于 Notification,每次更新需传入当前级别,省略恢复普通通知。

通知使用相同的 View 和 UIEvent,支持自定义 HTML/CSS、表单与多个操作按钮。AutoCloseMS 仅适用于 Notification:0(默认)持续显示,正数指定自动关闭的毫秒数,关闭时发送 Reason="timeout"。更新同一 ID 会重置计时;传 0 取消自动关闭。通知始终可以点击关闭按钮或调用 CloseUI 关闭;焦点在通知控件内时支持 Esc,编辑器中的 Esc 不会关闭通知。自动关闭和关闭按钮不会移走其他控件的焦点。

在扩展 UI 中添加 <button data-ref="logs" data-pencode-action="show-output" type="button">查看日志</button>,工作台会直接打开 Output 并选中当前扩展的 Extension: <id> 通道,清空日志过滤条件。此按钮无需声明 Events,点击由工作台处理,不发送 UIEvent;禁用和隐藏按钮不触发操作。

err := publisher.Show(peb.UI{
    ID: "build-result", Title: "构建完成", Kind: peb.Notification,
    Severity: peb.NotificationInfo, // 警告和错误分别使用 NotificationWarning / NotificationError。
    AutoCloseMS: 5000, // 省略则一直显示,等待手动关闭或扩展调用 CloseUI。
    View: &peb.EditorView{
        HTML: `<p>构建成功,可以查看结果。</p><button data-ref="open" type="button">查看结果</button>`,
        Events: []peb.EditorBinding{{Ref: "open", Kind: "click"}},
    },
})
// 在 Handlers.UIEvent 中处理自定义操作;是否关闭由扩展决定。
// if e.ID == "build-result" && e.Event.Kind == "click" && e.Event.Ref == "open" {
//     return publisher.CloseUI(e.ID)
// }

工作台最多保留 32 个扩展界面(包含通知),同时展示一个弹窗、浮窗或菜单、一个底部 Panel,以及多条堆叠通知。通知独立显示,打开通知不会替换浮层、关闭其他界面或移动焦点。打开新的浮层会关闭原来的浮层,关闭原因为 replace。底部面板切换保留扩展视图,返回后继续接收事件。禁用、卸载、重新加载扩展或进程异常退出会移除所属界面,退出后的进程不再接收生命周期事件。

var ui *peb.ActionPublisher
err := peb.ServeExtension(os.Stdin, os.Stdout,
    peb.Contributions{Actions: []peb.Action{{
        ID: "tools", Text: "工具", Location: peb.TitleBarRight,
    }}},
    peb.Handlers{
        ActivateActions: func(ctx context.Context, p *peb.ActionPublisher) { ui = p },
        Action: func(c peb.ActionContext) error {
            return ui.Show(peb.UI{
                ID: "tools-menu", Title: "工具菜单", Kind: peb.ContextMenu, Anchor: c.ID,
                View: &peb.EditorView{
                    HTML: `<button data-ref="open" type="button" role="menuitem">打开 Panel</button>`,
                    Events: []peb.EditorBinding{{Ref: "open", Kind: "click"}},
                },
            })
        },
        UIEvent: func(e peb.UIEvent) error {
            if e.ID == "tools-menu" && e.Event.Kind == "click" && e.Event.Ref == "open" {
                if err := ui.CloseUI(e.ID); err != nil { return err }
                return ui.Show(peb.UI{
                    ID: "result", Title: "工具结果", Kind: peb.Panel,
                    View: &peb.EditorView{HTML: `<p>操作完成</p>`},
                })
            }
            return nil
        },
    })

View 使用与自定义编辑器相同的 EditorView、作用域 CSS、主题变量和图片资源规则。通过 Events 订阅 click、input、change、focus、blur、submit、reset,Ref 对应 data-ref。HTML 控件和表单缺少 ID 时由宿主生成独立 ID;未命名控件也能参与键盘焦点导航。@peb-focus- 为宿主生成的 ref 前缀,请勿用于自定义 ref。自定义 ID 仍需以 pencode-extension- 开头,且应在所有视图间保持唯一。菜单项由扩展使用标准 role="menuitem" 等语义描述,表单可直接使用 form 和 button type="submit"。

输入控件的 input 事件携带以 Unicode 字符计数的 Anchor 和 Focus。设置 data-pencode-completion="true" 后,宿主优先将无修饰键的 ↑/↓、Enter/Tab 和 Esc 转为 completion 事件,Ref 为输入控件的 ref,Data 分别为 previous、next、accept 和 dismiss,并携带最新文本和选区;无需额外订阅此事件。Shift+Enter 和输入法确认保持原生行为。扩展负责候选的筛选、绘制和应用;插入或替换文本后,可用 data-pencode-caret="字符位置:修订号" 恢复光标,每次编辑使用新的修订号。动态增加候选或附件时,应为输入控件及表单指定固定 ID,以保留焦点。

按钮设置 data-ref 和 data-pencode-copy="文本" 后,点击由宿主直接将属性值写入本机剪贴板,保留原始换行、空格和 Markdown,无需声明 Events。属性值须进行 HTML 转义;禁用和隐藏按钮不执行复制。显式订阅该按钮的 click 时,由扩展接管操作。

文本控件设置 data-pencode-paste-images 后,粘贴快捷键可接收剪贴板图片,无需额外订阅事件。宿主发送 Kind:"paste"、InputType:"image/png"、Data:PNG data URL,并携带当前 Value、Anchor 和 Focus。普通文本继续原生粘贴。图片编码后超过 MaxDocument(2 MiB,原图约 1.5 MiB)时发送 InputType:"error",Data 为可展示的错误消息。扩展 UI 的 img 支持 PNG/JPEG base64 data URL;宿主校验后转为缓存的内存图片资源,沿用现有图片尺寸及总量限制。

生命周期事件无需显式订阅:

e.Event.Kind 内容
open 新界面已被宿主接受;同一 ID 的更新不会重复触发
close e.Reason 为 extension、button、escape、outside、replace 或 timeout
visibilitychange e.Event.Value 为 visible 或 hidden,用于 Panel 切换
projectchange 活动项目变化时通知 EditorTab,e.Context.ProjectPath 为新项目目录;关闭最后一个项目时为空

每个事件附带 e.ID 和打开界面时的 e.Context(项目、文件路径、目录标记及选区)。EditorTab 收到 projectchange 后更新上下文的项目目录,并清空文件路径、目录标记及选区。SDK 在 Action 和 UIEvent handler 中自动继承当前上下文;后台任务应保存并显式传入 UI.Context,避免使用后来触发的 action 上下文。异步控件事件无法同步取消浏览器默认行为,当前不支持 JavaScript 或任意键盘、指针事件订阅。

UI 和 action 请求共享串行队列及 5 秒请求超时,handler 内可以继续推送界面更新;不要在 handler 中等待后续用户事件。空闲轮询和复用缓冲区的编码保持零分配;创建或替换视图、解码字符串与加载资源有必要的分配。

ActionPublisher.Show 自动比较带 data-pencode-region 的视图。外层结构、CSS 和事件绑定相同时,只传内容变化的区域;新增/删除区域或结构变化发送完整视图。ViewVersion 标识结果版本,补丁的 Base 必须等于接收端版本,且结果版本等于 Base+1。区域替换的是内部 HTML;空字符串清空区域,未列出的区域保持不变,嵌套区域由最外层区域一起更新。View 与 Patch 互斥,补丁不能与 Close 同时使用。

宿主使用每个 action 会话独立的 peb.ViewReceiver 在 I/O worker 重建原始视图,然后沿用资源处理和 UI 校验。版本不匹配时请求内部 resync 事件;SDK 自动重发最新完整视图,不交给扩展业务回调。关闭界面释放差分基线,进程重连后首帧为完整视图。发送失败不会推进基线,重复或乱序补丁不应用。每个会话最多保留 32 个界面基线,重建结果仍受完整帧大小限制。

调用方提交的视图字符串必须保持不可变。此优化减少传输量;扩展仍生成完整视图,宿主仍重建并校验完整视图。新增 HTML tokenizer 依赖后,仓库内独立扩展模块的 go.mod/go.sum 已同步;所有扩展需使用当前 SDK 重建。

UI payload 的字段顺序如下。ActionContext 与 ActionInvoke 编码一致,EditorEvent 与 EditorRequest 的事件部分一致,bool 使用严格的 u32 0/1。

UIUpdate {
  id, title, anchor: string
  kind: u32                     // Dialog=0, ContextMenu=1, Panel=2, Popup=3, Notification=4, EditorTab=5
  close: bool
  placementMode: u32            // Default=0, Absolute=1, Anchor=2, AnchorAbove=3
  x, y, width, height: f32      // IEEE 754,小端
  closeOnOutsideClick: bool    // Popup 点击外部是否关闭,默认 false
  autoCloseMS: u32             // Notification 自动关闭毫秒数,0 持续显示
  severity: u32               // Notification: Info=0, Warning=1, Error=2
  busy: bool                  // EditorTab 关闭图标切换为处理动画
  unread: bool                // EditorTab 显示绿色未读完成圆点,busy 优先
  interceptClose: bool        // EditorTab 用户关闭时先发送 closerequest,保留标签页
  context: ActionContext
  hasView: bool
  // hasView=true: html: text, css: text, events: array<{ref: string, kind: string}>
  viewVersion: u32
  hasPatch: bool
  // hasPatch=true: base: u32, regions: array<{ref: string, html: text}>
}
UIRequest {
  id, reason: string
  context: ActionContext
  event: EditorEvent
}
UIResponse {}                   // 成功应答;错误使用 Failure

可构建的完整示例见 UI Demo,包含 action 菜单、表单弹窗、自由定位浮窗、点击外部关闭选项、输入与提交事件、Panel 更新、通知自定义操作、自动关闭和关闭事件。

协议版本 16 为 UIUpdate 增加 interceptClose:bool;宿主与扩展需要使用相同版本的 PEB 重新构建。运行 make install-extensions 会通过临时 Go workspace 使用当前仓库的 SDK 构建所有扩展,扩展源目录的模块文件保持不变。

调试工具

Contributions.DebugAdapters 为 workspace server 提供调试启动命令。Go 扩展注册 Delve,以自身的 --tool dlv 启动器按需安装工具。显式配置的 command 优先;同一 adapter ID 有多个提供者时返回冲突。禁用、卸载和断开连接会取消启动并回收对应适配器进程。

DebugAdapter.Templates 是可选的 .pencode/debug.json 数组文本,每项须为有效配置,且 adapter 与该适配器 ID 一致。工作台的“添加启动器”从已加载扩展展示模板,选中后将单项追加写入项目文件并打开编辑;注册和添加模板不会启动调试。模板中的 ${workspaceFolder} 原样保留到启动时解析。Go 扩展提供项目运行和包测试模板,PHP 扩展提供调试当前文件和监听 Xdebug 模板。

DocumentSelector 在 match、pattern 后增加 languageId:string,为空则继承 LanguageServer.LanguageID,允许同一 gopls 会话分别用 go、go.mod、go.work 打开文档。

独立编辑器标签页

peb.EditorTab 为会话、工具结果等不对应文件的视图创建编辑器标签页,Title 同时作为标签标题。同一扩展内的 ID 标识独立标签,Placement 必须为零值。事件继续通过 UIEvent 发送。

设置 Busy:true 时,关闭图标替换为主题色的点阵动画,后台标签同样显示动画,点击仍发送关闭事件。任务结束后更新为 Busy:false 恢复关闭图标。每次更新需传入当前忙碌状态;扩展可在最终关闭事件中中断任务并退出会话。

需要确认关闭的扩展应设置 InterceptClose:true(仅适用于 EditorTab)。用户点击关闭、使用关闭快捷键或右键菜单批量关闭时,宿主保留标签页并发送 Event.Kind="closerequest"、Reason="button"。扩展可在任务进行中显示 Dialog,取消时保留标签页和任务,确认后调用 CloseUI(id);空闲时可直接调用 CloseUI(id)。只有实际移除后才发送 Event.Kind="close"、Reason="extension"。每次更新均需传入当前 InterceptClose 值。

publisher.Show(peb.UI{
    ID: "conversation-123", Title: "实现搜索功能", Kind: peb.EditorTab,
    View: &peb.EditorView{HTML: `<article><p>聊天记录</p></article>`},
})

首次创建自动选中。带 View 的更新替换记录并更新标题,保留当前标签选择;更新后台会话不会抢走正在编辑的文件。发送相同 ID、View:nil 则主动选中已有标签。切换时发送 visibilitychange,关闭标签时发送 close;扩展卸载会清理其标签。标签页关闭不要求扩展终止后台任务,扩展可自行管理会话持久化和任务生命周期。

扩展 UI 的 CSS 支持与自定义编辑器相同的 var(--vscode-...) 主题变量,随 IDE 主题变更重新解析。

窗口与项目关闭事件

PEB v18 增加 Handlers.Lifecycle,在 action 会话中接收 WindowClosing、ProjectClosing、ProjectClosed;所有扩展界面关闭后仍会收到。事件按 kind:u32, projectPath:string 编码,kind 分别为 0、1、2;窗口事件的项目路径为空,项目事件使用绝对路径。返回 LifecycleReply{Message: "…"},编码为 message:string。关闭前事件返回非空消息时,宿主汇总所有扩展的消息并要求用户确认;空消息允许继续。用户取消后保留窗口、项目和扩展任务,下次关闭重新检查。

宿主在确认扩展任务之后继续处理未保存文件。ProjectClosed 只在项目实际关闭后发送,其回复不能阻止关闭;扩展可在此释放对应项目的状态。窗口关闭前使用 WindowClosing 检查,进程退出由激活上下文取消负责清理。回调与其他 action 请求串行执行,必须及时返回,不应等待用户交互。

Lifecycle: func(event peb.LifecycleEvent) (peb.LifecycleReply, error) {
    switch event.Kind {
    case peb.WindowClosing, peb.ProjectClosing:
        if hasRunningWork(event.ProjectPath) {
            return peb.LifecycleReply{Message: "仍有任务正在运行。"}, nil
        }
    case peb.ProjectClosed:
        releaseProject(event.ProjectPath)
    }
    return peb.LifecycleReply{}, nil
},

UIUpdate 增加 unread:bool,放在 busy 与 interceptClose 之间。宿主将其显示为主题绿色圆点;扩展在收到会话选择或界面可见事件后清除。宿主、server 和扩展需使用当前 SDK 一起重新构建,不提供旧版本兼容路径。

Documentation

Overview

Package peb implements Pencode Extension Binary protocol version 20. Frames and payloads use little endian integers; stdout is reserved for frames.

Index

Constants

View Source
const (
	Version    = 20
	HeaderSize = 20
	MaxPayload = 4 << 20
	MaxString  = 64 << 10
	MaxItems   = 16384
)
View Source
const MaxDocument = 2 << 20
View Source
const MaxSessions = 256

Variables

View Source
var ErrProtocol = errors.New("peb: invalid protocol message")
View Source
var ErrViewBase = errors.New("peb: missing view patch base")

ErrViewBase asks the session to request a full view through resync.

Functions

func DecodeFailure

func DecodeFailure(data []byte) (string, error)

func DecodeSetting

func DecodeSetting[T any](values SettingValues, id string) (value T, err error)

DecodeSetting reads a typed value from a complete settings snapshot.

func EncodeActionChange

func EncodeActionChange(dst []byte, v ActionChange) ([]byte, error)

func EncodeActionContext

func EncodeActionContext(dst []byte, v ActionContext) ([]byte, error)

func EncodeContributions

func EncodeContributions(dst []byte, v Contributions) ([]byte, error)

func EncodeEditorMessage

func EncodeEditorMessage(dst []byte, v EditorMessage) ([]byte, error)

func EncodeEditorReply

func EncodeEditorReply(dst []byte, v EditorReply) ([]byte, error)

func EncodeFailure

func EncodeFailure(dst []byte, message string) ([]byte, error)

func EncodeFileChanges

func EncodeFileChanges(dst []byte, v FileChanges) ([]byte, error)

func EncodeHello

func EncodeHello(dst []byte, v Hello) ([]byte, error)

Encode functions reset and reuse dst. Decoders own their returned strings.

func EncodeLifecycleEvent

func EncodeLifecycleEvent(dst []byte, v LifecycleEvent) ([]byte, error)

func EncodeLifecycleReply

func EncodeLifecycleReply(dst []byte, v LifecycleReply) ([]byte, error)

func EncodeSettingValues

func EncodeSettingValues(dst []byte, values SettingValues) ([]byte, error)

EncodeSettingValues reuses dst. Values must be validated against their schema before publication; the peer validates JSON when decoding the snapshot.

func EncodeUI

func EncodeUI(dst []byte, v UI) ([]byte, error)

func EncodeUIEvent

func EncodeUIEvent(dst []byte, v UIEvent) ([]byte, error)

func Serve

func Serve(input io.Reader, output io.Writer, contributions Contributions) error

Serve registers static resources, then waits for Shutdown. Logs belong on stderr. The host closes the process after registration; no per-file RPC runs.

func ServeEditors

func ServeEditors(input io.Reader, output io.Writer, contributions Contributions, handle func(EditorMessage) (EditorReply, error)) error

ServeEditors shares a stateless handler across document views until Shutdown. Stateful editors use Handlers.NewEditor with ServeExtension.

func ServeExtension

func ServeExtension(input io.Reader, output io.Writer, contributions Contributions, handlers Handlers) error

ServeExtension supports static registration, editors and live actions. Logs belong on stderr. Action updates can be published independently of clicks.

func ValidateSettings

func ValidateSettings(settings []Setting) error

Types

type Action

type Action struct {
	ID, Text, Tooltip, Icon, Color, IconColor, IconStyle string
	// Shortcut is a single chord, e.g. "Primary+Shift+B"; empty disables it.
	Shortcut         string
	Selectors        []DocumentSelector
	Location         ActionLocation
	Disabled, Hidden bool
	Default          bool
}

Action is a complete button description. Updates replace the description; empty fields clear their previous values. Icon is an SVG resource path. OpenWith actions use Selectors to match documents and Default to opt into automatic activation. Their view is served by Handlers.Editor, not Action.

func (Action) Equal

func (a Action) Equal(b Action) bool

Equal compares full descriptions, including document associations.

type ActionChange

type ActionChange struct {
	Action Action
	Remove bool
}

func DecodeActionChange

func DecodeActionChange(data []byte) (ActionChange, error)

type ActionContext

type ActionContext struct {
	ID, ProjectPath, Path string
	Directory             bool
	Caret, Anchor         uint32
}

ActionContext uses absolute paths; Caret and Anchor are Unicode rune offsets.

func DecodeActionContext

func DecodeActionContext(data []byte) (ActionContext, error)

type ActionLocation

type ActionLocation uint32

ActionLocation identifies workbench button slots, including document views.

const (
	StatusBar ActionLocation = iota
	LeftContextMenu
	RightContextMenu
	TitleBarRight
	OpenWith
	// Command registers an action without a workbench button.
	Command
)

type ActionPublisher

type ActionPublisher struct {
	// contains filtered or unexported fields
}

ActionPublisher serializes unsolicited updates with request replies. It may be used from background goroutines until the activation context is canceled.

func (*ActionPublisher) CloseUI

func (p *ActionPublisher) CloseUI(id string) error

func (*ActionPublisher) Remove

func (p *ActionPublisher) Remove(id string) error

func (*ActionPublisher) Show

func (p *ActionPublisher) Show(ui UI) error

Show opens or updates a surface from an action handler or background worker.

func (*ActionPublisher) Update

func (p *ActionPublisher) Update(action Action) error

type Capability

type Capability uint32
const (
	FileIcons Capability = 1 << iota
	EditorThemes
	CustomEditors
	Fonts
	LanguageServers
	Actions
	ExtensionUI
	DebugAdapters
	ExtensionSettings
	LanguagePacks
	Capabilities = FileIcons | EditorThemes | CustomEditors | Fonts | LanguageServers | Actions | ExtensionUI | DebugAdapters | ExtensionSettings | LanguagePacks
)

type Contributions

type Contributions struct {
	LanguagePacks   []LanguagePack
	Actions         []Action
	Themes          []Theme
	IconThemes      []IconTheme
	Fonts           []Font
	LanguageServers []LanguageServer
	DebugAdapters   []DebugAdapter
	Settings        []Setting
	// ActionService keeps a live process even when no buttons are registered yet.
	ActionService bool
}

func DecodeContributions

func DecodeContributions(data []byte) (Contributions, error)

type DebugAdapter

type DebugAdapter struct {
	ID, Label, Command string
	// Templates is a debug.json array copied into project configuration on demand.
	Templates string
	Args      []string
}

DebugAdapter supplies an executable and argument prefix for an adapter ID (for example Delve). The workspace server owns its process and transport. Command follows the same path rules as LanguageServer.Command.

type DocumentSelector

type DocumentSelector struct {
	Pattern string
	// LanguageID overrides a language server's default for this selector.
	LanguageID string
	Match      Match
}

type EditorBinding

type EditorBinding struct{ Ref, Kind string }

type EditorEvent

type EditorEvent struct {
	Kind, Ref, Value, InputType, Data string
	Checked                           bool
	Selected, Anchor, Focus           uint32
}

type EditorMessage

type EditorMessage struct {
	Editor, Path, Content string
	Event                 EditorEvent
	Binary                bool
}

EditorMessage contains a host-owned document snapshot. Binary files supply only Path and Binary=true; this first API permits text edits only.

func DecodeEditorMessage

func DecodeEditorMessage(data []byte) (EditorMessage, error)

type EditorReply

type EditorReply struct {
	View    *EditorView
	Content *string
}

A nil View preserves the retained DOM, caret and scroll. A non-nil View replaces the fragment. Content=nil preserves the host document; &"" clears it.

func DecodeEditorReply

func DecodeEditorReply(data []byte) (EditorReply, error)

type EditorView

type EditorView struct {
	HTML, CSS string
	Events    []EditorBinding
}

Extension UI fragments can use img with data-ref and data-pencode-icon="name" to reuse a built-in IDE icon with its theme color, raster scale and texture cache; omit src. Icons inside anchors or role="link" use textLink.foreground. UI surface anchors with data-ref and href open workspace files in permanent editor tabs or HTTP(S)/mailto links in the system browser. Relative paths in UI surfaces resolve against ActionContext.ProjectPath. File links accept :line[:column] and #Lline locations. An explicit click binding takes ownership of the link. Native buttons can use role="link" and data-pencode-href for the same navigation. EditorTab and Panel fragments can use data-pencode-layout="fill" without host padding or scrolling. A fill Panel supplies its own header; reserve 40px on the header's right for the workbench close button. An element with data-ref and data-pencode-scroll="end" follows new output until the user scrolls up; changing its data-pencode-scroll-key reveals the end of a different transcript. data-pencode-scroll="preserve" retains another scroll area's position while its data-pencode-scroll-key stays the same. A scroll container with data-ref and data-pencode-selectable supports native text selection, copy/select-all shortcuts, and the text context menu. Copy retains paragraph breaks, explicit br elements and preformatted text. Other containers with CSS white-space:pre-wrap can opt into preserving source whitespace when copied with data-pencode-preserve-whitespace. A custom editor container with data-ref and data-pencode-searchable enables the host find bar for its rendered text, including highlight and navigation. Find in a rendered view is read-only; replacement applies to source editors. An empty div with data-ref and data-pencode-grid carries an HTML-escaped JSON sparse grid: rows/columns (counts), rowHeight/columnWidth (pixels), optional rowSizes/columnSizes ([oneBasedIndex,pixels] overrides), styles (CSS strings), and cells ([row,column,text,styleIndex] tuples sorted by row then column). Within a searchable/selectable custom editor, the host mounts only the visible two-axis window plus overscan. Find indexes every value and reveals unmounted matches; selection/copy retains data offsets when cells are unmounted. Copy emits TSV data without row/column headings. Use .pencode-grid-cell and .pencode-grid-header CSS to style cells/headings. The outer editor scrolls. An optional grid source string defers cell transfer. The host sends internal EditorEvent{Kind:"grid",Ref:source,Value:JSON} requests with integer request, offset (UTF-8 bytes), firstRow/endRow and firstColumn/endColumn (half-open, one-based), plus all and metadata booleans. all loads a complete sparse index only for find/copy/select-all; metadata requests styles and size overrides. Reply with a standalone div whose data-pencode-grid-chunk is escaped JSON: {source,request,offset,done,data}, where data is base64 for up to 64 KiB of a UTF-8 JSON stream containing cells and optionally styles/rowSizes/columnSizes. The host requests consecutive offsets until done, then applies atomically; stale source/request IDs are discarded. Supply a new source on reload/switch. The 2 MiB per-view and 4 MiB message limits still apply to every reply. A button with data-ref and data-pencode-copy="text" copies that exact text to the local clipboard on click. An explicit click binding overrides this action. A container with data-ref and data-pencode-region updates its children independently. Changes confined to regions preserve the surrounding DOM, including native input focus and selection. Nested regions update together. A container with data-ref and data-pencode-focus="input-ref" focuses the named control when its blank area is clicked, preserving child control focus. A textarea with data-ref and data-pencode-autosize grows with its native text layout. Use border-box sizing and CSS min/max-height to bound its height. data-pencode-submit-on-enter="form" submits the named form binding on Enter; Shift+Enter inserts a newline, and IME confirmation keeps its native behavior. data-pencode-completion="true" routes unmodified Up/Down, Enter/Tab and Escape to EditorEvent{Kind:"completion", Ref:inputRef, Data:action}, with action "previous", "next", "accept" or "dismiss", and the current text/selection. data-pencode-caret="runeOffset:revision" restores focus and a collapsed caret when this attribute changes. Use a new revision for each programmatic edit. data-pencode-markdown-links displays inline [label](path) references using the theme link color and hides their syntax with a one-pixel inset. References behave as atomic tokens for selection, arrow navigation and deletion. Values, selection offsets, clipboard contents and undo retain Markdown source. A text control with data-pencode-paste-images receives images on the paste shortcut as Kind:"paste", InputType:"image/png", Data:a PNG data URL, with the current Value/Anchor/Focus. Text pastes retain native editing behavior. InputType:"error" reports an image paste failure in Data.

type FileChanges

type FileChanges struct {
	Root  string
	Files []FileEvent
	Reset bool
}

FileChanges comes from the host's unified file service, never a private extension watcher. Paths are relative to Root. Reset requests reconciliation after native, history or subscriber overflow; a rename may appear as remove and create. The batch owns its paths and may be retained by the handler.

func DecodeFileChanges

func DecodeFileChanges(data []byte) (FileChanges, error)

type FileEvent

type FileEvent struct {
	Path      string
	Op        FileOperation
	Directory bool
}

type FileOperation

type FileOperation uint8
const (
	FileCreated FileOperation = 1 << iota
	FileWritten
	FileRemoved
	FileRenamed
	FileMetadataChanged
)

type Font

type Font struct{ ID, Label, Path, BoldPath string }

Font contributes a regular face and an optional bold face from resources/.

type Frame

type Frame struct {
	Payload []byte
	ID      uint32
	Session uint32
	Kind    Kind
}

Frame.Payload is borrowed until the next Read. Session identifies a window action session or a document view; zero addresses the process control session. Request IDs correlate replies; Initialize/Register/Failure use the same nonzero ID. Shutdown, ActionActivate and unsolicited ActionUpdate/UIUpdate notifications use zero.

type Handlers

type Handlers struct {
	// NewEditor creates isolated document-view state. Its context ends when the view closes.
	// Editor may instead be used for stateless handlers shared by all views.
	NewEditor func(context.Context) func(EditorMessage) (EditorReply, error)
	// Settings runs before activation/first document request and on each change.
	// The snapshot owns its data and may be retained by the extension.
	Settings        func(SettingValues) error
	Lifecycle       func(LifecycleEvent) (LifecycleReply, error)
	FileChanges     func(FileChanges) error
	Editor          func(EditorMessage) (EditorReply, error)
	Action          func(ActionContext) error
	UIEvent         func(UIEvent) error
	ActivateActions func(context.Context, *ActionPublisher)
}

Handlers shares one SDK for editor requests and persistent workbench actions. ActivateActions runs once in the window's workbench action session. Start any background work there; return promptly and stop when ctx is canceled.

type Hello

type Hello struct {
	Host         string
	Capabilities Capability
}

func DecodeHello

func DecodeHello(data []byte) (Hello, error)

type IconRule

type IconRule struct {
	Pattern, Path string
	Match         Match
}

IconRule matches file and folder names case-insensitively.

type IconTheme

type IconTheme struct {
	ID, Label string
	Rules     []IconRule
}

type Kind

type Kind uint16
const (
	Initialize Kind = 1 + iota
	Register
	Shutdown
	Failure
	EditorRequest
	EditorResponse
	ActionActivate
	ActionInvoke
	ActionResponse
	ActionUpdate
	UIUpdate
	UIRequest
	UIResponse
	SettingsRequest
	SettingsResponse
	FileChangesRequest
	FileChangesResponse
	LifecycleRequest
	LifecycleResponse
)

type LanguagePack

type LanguagePack struct{ ID, Label, Path string }

LanguagePack points to a JSON language pack under resources/.

type LanguageServer

type LanguageServer struct {
	ID, Label, LanguageID, Command       string
	InitializationOptions, Configuration string
	Args                                 []string
	Selectors                            []DocumentSelector
}

LanguageServer contributes a standard stdio LSP server. Command is an executable from PATH, an absolute path, or an extension-relative path. JSON options are carried verbatim; the host validates them before activation.

type LifecycleEvent

type LifecycleEvent struct {
	ProjectPath string
	Kind        LifecycleKind
}

LifecycleEvent reaches the action process even when all its views are closed. ProjectPath is absolute for project events and empty for WindowClosing.

func DecodeLifecycleEvent

func DecodeLifecycleEvent(data []byte) (LifecycleEvent, error)

type LifecycleKind

type LifecycleKind uint32

LifecycleKind identifies a host close request or a completed project closure.

const (
	WindowClosing LifecycleKind = iota
	ProjectClosing
	ProjectClosed
)

type LifecycleReply

type LifecycleReply struct{ Message string }

LifecycleReply asks the host to confirm closure before proceeding. Empty Message allows closure. ProjectClosed replies cannot prevent closure.

func DecodeLifecycleReply

func DecodeLifecycleReply(data []byte) (LifecycleReply, error)

type Match

type Match uint32

Match selects basenames or suffixes. Suffix includes its leading dot; the longest suffix wins, after an exact basename match. Directory names use FolderName.

const (
	DefaultFile Match = iota
	FileName
	FileSuffix
	DefaultFolder
	FolderName
)

type NotificationSeverity

type NotificationSeverity uint32

NotificationSeverity selects the notification's theme colors and status icon.

const (
	NotificationInfo NotificationSeverity = iota
	NotificationWarning
	NotificationError
)

type Reader

type Reader struct {
	Source io.Reader
	// contains filtered or unexported fields
}

func (*Reader) Read

func (r *Reader) Read() (Frame, error)

type Setting

type Setting struct {
	ID, Label, Description string
	Default                json.RawMessage
	Options                []SettingOption
	Type                   SettingType
}

Setting IDs are local to an extension. Defaults are JSON scalar values; Options, when present, constrain a string setting to the listed values.

func (Setting) Validate

func (s Setting) Validate(value json.RawMessage) error

type SettingOption

type SettingOption struct{ Value, Label string }

type SettingType

type SettingType uint32
const (
	SettingString SettingType = iota
	SettingBoolean
	SettingNumber
	SettingInteger
)

type SettingValues

type SettingValues map[string]json.RawMessage

func DecodeSettingValues

func DecodeSettingValues(data []byte) (SettingValues, error)

func ResolveSettings

func ResolveSettings(settings []Setting, saved SettingValues) SettingValues

ResolveSettings creates an immutable snapshot with defaults for missing or invalid saved values. Unknown keys remain in storage, outside this snapshot.

type Shortcut

type Shortcut struct {
	Key                                 string
	Primary, Control, Super, Shift, Alt bool
}

Shortcut is the parsed, platform-independent form of an Action.Shortcut. Primary means Command on macOS and Control elsewhere.

func ParseShortcut

func ParseShortcut(chord string) (Shortcut, error)

ParseShortcut accepts a single chord with case-sensitive key names. Successful parsing does not allocate.

type Theme

type Theme struct{ ID, Label, Path string }

type UI

type UI struct {
	Patch             *ViewPatch
	View              *EditorView
	ID, Title, Anchor string
	Context           ActionContext
	Placement         UIPlacement
	// ViewVersion identifies a complete view or the result of Patch. Patches
	// replace only existing region contents; structure, CSS or event changes
	// require a full View. ActionPublisher manages these fields automatically.
	ViewVersion uint32
	Kind        UIKind
	// AutoCloseMS dismisses a Notification after this many milliseconds. Zero
	// keeps it open until closed by the user or extension. Updates reset the timer.
	AutoCloseMS uint32
	// Severity applies only to Notification. Zero uses the information style.
	Severity NotificationSeverity
	Close    bool
	// CloseOnOutsideClick dismisses a Popup on an outside pointer press.
	// False keeps it open. ContextMenu always dismisses on outside presses.
	CloseOnOutsideClick bool
	// Busy replaces an EditorTab's close icon with a processing animation.
	Busy bool
	// Unread marks an EditorTab with completed work that has not been viewed.
	Unread bool
	// InterceptClose keeps an EditorTab open on user close and emits
	// "closerequest" instead. Call CloseUI after any confirmation succeeds.
	InterceptClose bool
}

UI replaces a surface's description. A nil View retains its controls, focus and scroll. Anchor is a local action ID, or "ui:<surface ID>#<data-ref>" for a focusable control in another surface owned by this extension. Close removes the surface. Empty Context.ID inherits the current Action/UIEvent context; background workers should pass their saved Context explicitly. Title names the surface. EditorTab opens selected. Updates with a View retain selection; a nil View explicitly reveals an existing tab. Closing a tab emits the usual close event.

func DecodeUI

func DecodeUI(data []byte) (UI, error)

type UIEvent

type UIEvent struct {
	ID, Reason string
	Context    ActionContext
	Event      EditorEvent
}

UIEvent carries DOM events and "open", "closerequest", "close", "visibilitychange", "projectchange". visibilitychange uses Event.Value="visible" or "hidden". projectchange notifies EditorTab surfaces when the active project changes. Its Context contains the new ProjectPath and clears file-specific fields. Close reasons are "extension", "button", "escape", "outside", "replace", "timeout". Context is the action invocation that opened this surface, updated by projectchange.

func DecodeUIEvent

func DecodeUIEvent(data []byte) (UIEvent, error)

type UIKind

type UIKind uint32

UIKind selects a host-owned surface. Menus are anchored to an action; dialogs are modal; panels live in the resizable workbench bottom area.

const (
	Dialog UIKind = iota
	ContextMenu
	Panel
	Popup        // Nonmodal floating window; workspace interaction remains available.
	Notification // Bottom-right message stack; never takes focus when opened.
	EditorTab    // Document-independent tab in the editor area.
)

type UIPlacement

type UIPlacement struct {
	Mode                UIPosition
	X, Y, Width, Height float32
}

UIPlacement controls an overlay's position and size. X/Y offset the selected origin. Zero Width/Height use the surface's default size. The host constrains overlays to the viewport. Panels use their workbench layout instead.

type UIPosition

type UIPosition uint32

UIPosition selects the origin for Placement.X/Y, in workspace CSS pixels.

const (
	PositionDefault     UIPosition = iota // Center dialogs/popups; anchor context menus.
	PositionAbsolute                      // Workspace top-left.
	PositionAnchor                        // Action's bottom-left, falling back to its captured location.
	PositionAnchorAbove                   // Above the anchor's top-left; falls below when space is insufficient.
)

type ViewPatch

type ViewPatch struct {
	Regions []ViewRegion
	Base    uint32
}

ViewPatch is relative to exactly one previously received view version. Nested regions belong to their outer region and cannot be patched separately.

type ViewReceiver

type ViewReceiver struct {
	// contains filtered or unexported fields
}

ViewReceiver reconstructs raw views on the session I/O worker, before asset preparation and UI validation. It owns at most 32 surface bases. A new session starts empty and its publisher sends a full view first. Apply is transactional. Do not share a receiver across processes or access it concurrently.

func (*ViewReceiver) Apply

func (r *ViewReceiver) Apply(ui UI) (UI, error)

type ViewRegion

type ViewRegion struct {
	Ref, HTML string
	// contains filtered or unexported fields
}

ViewRegion replaces the children of an existing data-pencode-region element.

func SplitViewRegions

func SplitViewRegions(source string) (string, []ViewRegion, error)

SplitViewRegions separates outer region contents from their surrounding template. Nested regions remain part of their outer region's HTML.

type ViewRegionSplitter

type ViewRegionSplitter struct {
	// contains filtered or unexported fields
}

ViewRegionSplitter reuses region boundaries when an update changes only one region. The returned region slice is borrowed until the next successful Split. Strings remain immutable. A splitter must not be used concurrently.

func (*ViewRegionSplitter) Split

func (s *ViewRegionSplitter) Split(source string) (string, []ViewRegion, error)

type Writer

type Writer struct {
	Target io.Writer
	// contains filtered or unexported fields
}

func (*Writer) Write

func (w *Writer) Write(kind Kind, id uint32, payload []byte) error

Write handles short writes on the process control session.

func (*Writer) WriteSession

func (w *Writer) WriteSession(session uint32, kind Kind, id uint32, payload []byte) error

WriteSession serializes complete frames from independent session publishers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL