GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — OpenCode 格式化工具 | 菜鸟教程
OpenCode 教程
OpenCode 教程
OpenCode 简介
OpenCode 安装
OpenCode API 设置
OpenCode DeepSeek
OpenCode 配置文件
OpenCode 第一次使用
OpenCode VSCode
OpenCode TUI
OpenCode CLI 使用
OpenCode Web 使用
OpenCode GitHub
OpenCode GitLab
OpenCode 工具
OpenCode 规则
OpenCode 代理
OpenCode 主题
OpenCode 快捷键
OpenCode 自定义命令
OpenCode 格式化工具
OpenCode 权限配置
OpenCode LSP 服务器配置
OpenCode MCP 服务器配置
OpenCode Skills
OpenCode skills 使用
OpenCode 自定义工具
OpenCode oh-my-openagent
OpenCode 生态系统
OpenCode 配置文件
OpenCode 格式化工具OpenCode 会在每次写入或编辑文件后,自动使用对应语言的格式化工具对其进行格式化,确保生成的代码始终符合你项目的代码风格。整个过程在后台静默完成,无需任何手动操作。 工作原理当 OpenCode 写入或编辑文件时,会按以下步骤自动执行格式化:
例如,如果你的项目 内置格式化工具OpenCode 内置了覆盖主流语言和框架的格式化工具支持,分为两类:
配置文件结构格式化工具的自定义配置写在 实例
{ "$schema": "https://opencode.ai/config.json", "formatter": { // 格式化工具名称: 配置对象 "prettier": { // 在此填写 prettier 的配置项 } } } 每个格式化工具支持以下配置项:
禁用格式化工具1、禁用所有格式化工具如果你不希望 OpenCode 对任何文件进行自动格式化,可以将 实例
{ "$schema": "https://opencode.ai/config.json", "formatter": false // 全局禁用所有格式化工具,文件写入后不会进行任何格式化 } 2、禁用特定格式化工具如果只需要禁用某一个格式化工具,在对应工具的配置对象中将 实例
{ "$schema": "https://opencode.ai/config.json", "formatter": { "prettier": { "disabled": true // 仅禁用 prettier,其他格式化工具(如 ruff、gofmt)仍正常工作 } } } 自定义格式化工具通过 1、覆盖内置格式化工具的命令例如,为 prettier 指定特定的参数,并限制它只处理特定扩展名: 实例
{ "$schema": "https://opencode.ai/config.json", "formatter": { "prettier": { "command": ["npx", "prettier", "--write", "$FILE"], // command 数组中的 $FILE 是占位符,运行时会被替换为待格式化文件的完整路径 // 例如实际执行:npx prettier --write /your/project/src/index.ts "environment": { "NODE_ENV": "development" // 运行 prettier 时注入的环境变量 }, "extensions": [".js", ".ts", ".jsx", ".tsx"] // 覆盖默认扩展名:只对这四种文件运行 prettier,不处理 .html、.css 等 } } } 2、添加自定义格式化工具如果你使用的格式化工具不在内置列表中,可以通过指定任意名称来添加。只需提供启动命令和需要处理的文件扩展名即可: 实例
{ "$schema": "https://opencode.ai/config.json", "formatter": { "custom-markdown-formatter": { // 自定义名称(任意命名,不与内置工具冲突即可) "command": ["deno", "fmt", "$FILE"], // 使用 deno fmt 格式化文件,$FILE 会被替换为实际文件路径 "extensions": [".md"] // 只对 .md 文件运行此格式化工具 } } } 3、同时配置多个格式化工具实例
{ "$schema": "https://opencode.ai/config.json", "formatter": { "prettier": { "command": ["npx", "prettier", "--write", "$FILE"], "environment": { "NODE_ENV": "development" }, "extensions": [".js", ".ts", ".jsx", ".tsx"] }, "ruff": { "disabled": true // 项目中不使用 ruff,将其禁用 }, "custom-markdown-formatter": { "command": ["deno", "fmt", "$FILE"], "extensions": [".md"] } } }
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |