code-format
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Format Skill
Code Format Skill
Format and validate code in various languages.
格式化并验证多种语言的代码。
When to Use
使用场景
✅ USE this skill when:
- "Format this Python code"
- "Validate this JSON"
- "Pretty print this JavaScript"
- "Format this Markdown"
✅ 在以下场景使用该Skill:
- "Format this Python code"
- "Validate this JSON"
- "Pretty print this JavaScript"
- "Format this Markdown"
When NOT to Use
不适用场景
❌ DON'T use this skill when:
- Linting code → use linters directly
- Compiling code → use compilers
- Minifying code → use minifiers
❌ 以下场景请勿使用该Skill:
- 代码检查 → 直接使用linters
- 编译代码 → 使用编译器
- 压缩代码 → 使用压缩工具
Supported Languages
支持的语言
| Language | Formatter | Notes |
|---|---|---|
| Python | black, autopep8 | Falls back to auto-format |
| JavaScript | prettier | If installed |
| TypeScript | prettier | If installed |
| JSON | python | Built-in |
| YAML | pyyaml | If installed |
| Markdown | prettier | If installed |
| HTML | prettier | If installed |
| CSS | prettier | If installed |
| SQL | sqlparse | If installed |
| Shell | shfmt | If installed |
| Language | Formatter | 说明 |
|---|---|---|
| Python | black, autopep8 | 默认使用自动格式化作为备选 |
| JavaScript | prettier | 需已安装 |
| TypeScript | prettier | 需已安装 |
| JSON | python | 内置支持 |
| YAML | pyyaml | 需已安装 |
| Markdown | prettier | 需已安装 |
| HTML | prettier | 需已安装 |
| CSS | prettier | 需已安装 |
| SQL | sqlparse | 需已安装 |
| Shell | shfmt | 需已安装 |
Commands
命令
Format Code
格式化代码
bash
{baseDir}/format-code.sh <file>
{baseDir}/format-code.sh <file> --language python
{baseDir}/format-code.sh --stdin --language js < code.jsbash
{baseDir}/format-code.sh <file>
{baseDir}/format-code.sh <file> --language python
{baseDir}/format-code.sh --stdin --language js < code.jsValidate Syntax
验证语法
bash
{baseDir}/validate-code.sh <file>
{baseDir}/validate-code.sh <file> --language python
{baseDir}/validate-code.sh --stdin --language json < data.jsonbash
{baseDir}/validate-code.sh <file>
{baseDir}/validate-code.sh <file> --language python
{baseDir}/validate-code.sh --stdin --language json < data.jsonList Languages
列出支持的语言
bash
{baseDir}/format-code.sh --languagesbash
{baseDir}/format-code.sh --languagesOptions
选项
- : Specify language (auto-detect from extension)
--language <lang> - : Read from stdin
--stdin - : Write to file (default: stdout)
--out <file> - : Check if formatting needed (don't modify)
--check - : Show diff instead of formatted output
--diff - : Indentation size (default: language-specific)
--indent <n>
- : 指定语言(默认从文件扩展名自动检测)
--language <lang> - : 从标准输入读取内容
--stdin - : 写入到指定文件(默认输出到标准输出)
--out <file> - : 检查是否需要格式化(不修改文件)
--check - : 显示差异而非格式化后的内容
--diff - : 指定缩进大小(默认根据语言自动设置)
--indent <n>
Examples
示例
Format Python file:
bash
{baseDir}/format-code.py script.pyFormat JSON from stdin:
bash
cat data.json | {baseDir}/format-code.sh --stdin --language jsonCheck if formatting needed:
bash
{baseDir}/format-code.sh script.py --check格式化Python文件:
bash
{baseDir}/format-code.py script.py从标准输入格式化JSON:
bash
cat data.json | {baseDir}/format-code.sh --stdin --language json检查是否需要格式化:
bash
{baseDir}/format-code.sh script.py --checkExit 0 if formatted, 1 if needs formatting
若已格式化则返回0,若需要格式化则返回1
**Format with custom indent:**
```bash
{baseDir}/format-code.sh config.json --indent 4Validate code:
bash
{baseDir}/validate-code.sh script.py
**使用自定义缩进格式化:**
```bash
{baseDir}/format-code.sh config.json --indent 4验证代码:
bash
{baseDir}/validate-code.sh script.pyOutput: ✓ Valid Python syntax
输出:✓ Valid Python syntax
undefinedundefinedNotes
注意事项
- Uses standard formatters when available
- Falls back to built-in formatters
- Does not modify files by default
- Use to save to same or different file
--out
- 在可用情况下使用标准格式化工具
- 默认使用内置格式化工具作为备选
- 默认不会修改文件
- 使用参数保存到原文件或其他文件
--out