代码格式化工具

ClangFormat

  • C/C++
  • Java
  • JavaScript
  • JSON
  • Objective-C
  • Protobuf
  • C#

安装:

  • macOS:

    brew install clang-format
    
  • Ubuntu:

    sudo apt install clang-format
    

使用:

clang-format --style=<style> -i <file>

style 选项:LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit, file

我个人最喜欢 Google 风格。

可以在项目根目录下创建 .clang-format 文件来自定义格式化风格:

BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100

参考:

Black

项目地址:

支持语言:

  • Python

安装:

pipx install black

使用:

black <file>         # 格式化单个文件
black .              # 格式化整个项目
black --check <file> # 检查文件格式(不修改)
black --diff <file>  # 显示差异而不修改文件

配置:

可以在 pyproject.toml 文件中配置 black:

[tool.black]
line-length = 80
target-version = ['py36', 'py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

Prettier

项目地址:

安装:

npx install prettier

使用:

npx prettier <file> # Prettier 将根据文件后缀名确认文件类型

支持语言:

  • Markdown
  • HTML
  • CSS
  • SCSS
  • JavaScript
  • TypeScript
  • JSON
  • GraphQL
  • YAML
  • Vue
  • Angular
  • Less
  • Flow
  • JSX

cmake-format

  • CMake

安装:

pipx install cmake-format --include-deps

使用:

cmake-format <file>

参见:

posted @ 2024-07-09 17:47  Undefined443  阅读(5)  评论(0编辑  收藏  举报