首页  编辑  

最小巧的Delphi Pascal/C/C++代码格式化工具及VS Code自动格式化Pascal代码

Tags: /计算机文档/软件应用技巧/   Date Created:
官方出品的代码格式化工具,提取出来使用,下载 Formatter.exe, cf.bat配置文件
可以指定配置文件,附件 Formatter.config 就是默认的配置文件(但缩进为4字节)

Source Code Formatter
    Copyright (c) 2010, Embarcadero Technologies Inc.
Syntax:
    Formatter [<options>] [<filename>]...

Options:
    -cpp Format only C/C++ sources.
    -delphi Format only Delphi sources.
    -config configfile Configuration file name. This file contains Formatter options and can be managed
    using RAD Studio IDE ('Tools > Options > Formatter > Profiles and Status' page).
    -e encoding Encoding for reading/writing.
    -d directory Directory for files to format. If no <filename> is defined, all supported files 
    in the <directory> (or matching the '-cpp' or '-delphi' option) will be formatted.
    -r Format files recursively in the <directory> and all subdirectories.
    -b Create .bak files before formatting.
    -log logfile Log file name. 
    -silent Do not display error messages.
    <filename> Source file name. You can specify several <filename> separated by spaces. 
    Wild characters '*' and '?' can be used. To format a file with an unknown extension
    you should specify the language using the '-cpp' or '-delphi' option.
    Predefined file name extensions:
    C/C++ source files:  *.cpp, *.cxx, *.cc, *.c, *.hpp, *.hxx, *.hh, *.h
    Delphi source files :  *.pas, *.dpr, *.dpk, *.dpkw, *.inc

VS Code中,如何自动格式化Pascal代码:
  1. 首先安装VS Code插件: Pascal和Pascal Formatter
  2. 下载附件 cf.exe,cf.bat,保存到某个目录。为什么要用 cf.bat 呢?因为 Pascal Formatter 默认调用Formatter的时候,字符集未指定 utf-8,导致格式化的时候中文可能出现乱码!
  3. 配置VS Code,找到如下配置并设置如下图:

Formatter.config (28.3KB)
Formatter.exe (362.9KB)
cf.bat (0.1KB)