code-format

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code 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

支持的语言

LanguageFormatterNotes
Pythonblack, autopep8Falls back to auto-format
JavaScriptprettierIf installed
TypeScriptprettierIf installed
JSONpythonBuilt-in
YAMLpyyamlIf installed
MarkdownprettierIf installed
HTMLprettierIf installed
CSSprettierIf installed
SQLsqlparseIf installed
ShellshfmtIf installed
LanguageFormatter说明
Pythonblack, autopep8默认使用自动格式化作为备选
JavaScriptprettier需已安装
TypeScriptprettier需已安装
JSONpython内置支持
YAMLpyyaml需已安装
Markdownprettier需已安装
HTMLprettier需已安装
CSSprettier需已安装
SQLsqlparse需已安装
Shellshfmt需已安装

Commands

命令

Format Code

格式化代码

bash
{baseDir}/format-code.sh <file>
{baseDir}/format-code.sh <file> --language python
{baseDir}/format-code.sh --stdin --language js < code.js
bash
{baseDir}/format-code.sh <file>
{baseDir}/format-code.sh <file> --language python
{baseDir}/format-code.sh --stdin --language js < code.js

Validate Syntax

验证语法

bash
{baseDir}/validate-code.sh <file>
{baseDir}/validate-code.sh <file> --language python
{baseDir}/validate-code.sh --stdin --language json < data.json
bash
{baseDir}/validate-code.sh <file>
{baseDir}/validate-code.sh <file> --language python
{baseDir}/validate-code.sh --stdin --language json < data.json

List Languages

列出支持的语言

bash
{baseDir}/format-code.sh --languages
bash
{baseDir}/format-code.sh --languages

Options

选项

  • --language <lang>
    : Specify language (auto-detect from extension)
  • --stdin
    : Read from stdin
  • --out <file>
    : Write to file (default: stdout)
  • --check
    : Check if formatting needed (don't modify)
  • --diff
    : Show diff instead of formatted output
  • --indent <n>
    : Indentation size (default: language-specific)
  • --language <lang>
    : 指定语言(默认从文件扩展名自动检测)
  • --stdin
    : 从标准输入读取内容
  • --out <file>
    : 写入到指定文件(默认输出到标准输出)
  • --check
    : 检查是否需要格式化(不修改文件)
  • --diff
    : 显示差异而非格式化后的内容
  • --indent <n>
    : 指定缩进大小(默认根据语言自动设置)

Examples

示例

Format Python file:
bash
{baseDir}/format-code.py script.py
Format JSON from stdin:
bash
cat data.json | {baseDir}/format-code.sh --stdin --language json
Check 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 --check

Exit 0 if formatted, 1 if needs formatting

若已格式化则返回0,若需要格式化则返回1


**Format with custom indent:**
```bash
{baseDir}/format-code.sh config.json --indent 4
Validate code:
bash
{baseDir}/validate-code.sh script.py

**使用自定义缩进格式化:**
```bash
{baseDir}/format-code.sh config.json --indent 4
验证代码:
bash
{baseDir}/validate-code.sh script.py

Output: ✓ Valid Python syntax

输出:✓ Valid Python syntax

undefined
undefined

Notes

注意事项

  • Uses standard formatters when available
  • Falls back to built-in formatters
  • Does not modify files by default
  • Use
    --out
    to save to same or different file
  • 在可用情况下使用标准格式化工具
  • 默认使用内置格式化工具作为备选
  • 默认不会修改文件
  • 使用
    --out
    参数保存到原文件或其他文件