You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code自定义编辑器保存时默认.gpt扩展名问题求助

解决VS Code自定义编辑器保存时默认文件类型问题

核心配置与代码调整方案

你需要同时调整package.json配置和CustomTextEditorProvider的逻辑,才能实现.gpt扩展名的默认关联:

1. 完善语言与文件关联配置

在package.json的contributes字段中补充以下配置,明确.gpt的身份并绑定JSON语法:

"contributes": {
  "languages": [
    {
      "id": "gpt-chat",
      "aliases": ["GPT Chat", "gpt"],
      "extensions": [".gpt"],
      "configuration": "./language-configuration.json"
    }
  ],
  "grammars": [
    {
      "language": "gpt-chat",
      "scopeName": "source.json",
      "path": "./node_modules/jsonc-grammar/extensions/jsonc/syntaxes/jsonc.tmLanguage.json"
    }
  ],
  "files": {
    "associations": {
      "*.gpt": "gpt-chat"
    }
  },
  "customEditors": [
    {
      "viewType": "你的扩展ID.gpt-editor",
      "displayName": "GPT聊天编辑器",
      "selector": [
        {
          "filenamePattern": "*.gpt"
        }
      ],
      "priority": "default"
    }
  ]
}

这里直接复用JSON语法规则,避免重复定义语法逻辑。

2. 调整CustomTextEditorProvider的保存逻辑

在你的CustomTextEditorProvider派生类中,重写saveAs方法,强制默认扩展名:

async saveAs(
  document: vscode.TextDocument,
  destination: vscode.Uri,
  options: vscode.TextDocumentSaveOptions
): Promise<vscode.TextDocument> {
  // 若目标路径无.gpt扩展名,自动添加
  if (!destination.filename.endsWith('.gpt')) {
    destination = destination.with({ path: `${destination.path}.gpt` });
  }
  // 执行原有保存逻辑
  await vscode.workspace.fs.writeFile(destination, Buffer.from(document.getText()));
  return vscode.workspace.openTextDocument(destination);
}

同时确保新建文档时,初始URI默认携带.gpt扩展名,这样第一次Ctrl+S时会直接沿用该类型。

3. 验证生效步骤

  • 重新打包并安装扩展
  • 新建文档后首次保存时,选择.gpt格式,后续Ctrl+S会默认保留该类型
  • 打开“另存为”对话框,下拉列表将显示“GPT Chat (*.gpt)”选项

内容的提问来源于stack exchange,提问作者bueller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:24:54