1、文件编码规则并实现自动按照clang-format格式进行自动保存
a、安装LLVM工具: LLVM-18.1.8-win64.exe
下载链接:https://github.com/llvm/llvm-project/releases/tag/llvmorg-18.1.8
需要注意选择 Add LLVM to the system PATH for current user 或 Add LLVM to the system PATH for all users 其中的一项
安装完成后,在命令行中测试:
clang-format -version
2、打开 Options-> Custom Commands

3、点击 Add... 按钮,具体内容为:Command 值为 FormatWithClang
Run 值为 clang-format --style=file -i %f
Dir 留空(即项目目录,这个目录里需要有 .clang-format 文件)
勾选 Save Files First
不要勾选 Pause When Done
勾选 Wait Until Done

4、点击 Menu... 按钮,在合适的菜单中加入刚刚定义的命令,例如在FILE中,insert ,然后就可以在tools菜单中看到clang-format选项,点击后会格式化当前源文件

5、点击 Keys... 按钮,为刚刚定义的命令分配合适的快捷键

6、把.clang-format文件放到根目录下
bash
BasedOnStyle: LLVM
Language: Cpp
IndentWidth: 4
TabWidth: 4
UseTab: Never
PointerAlignment: Right
ColumnLimit: 120
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceInEmptyParentheses: false
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: true
IndentCaseLabels: true
AllowAllArgumentsOnNextLine: false
7、可以使用以下文件进行测试
souce insight 点击 FormatWithClang或着快捷键(刚刚配置的,例如我的ctrl+shift+s)以指定格式保存当前文件

8、提供已下内容可以进行测试,查看是否按照指定地格式进行保存
cpp
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN( a , b ) (( a ) < ( b ) ? ( a ) : ( b ) )
#define VERY_LONG_MACRO_DEFINITION(x, y, z) do { if((x) > (y)) { (z) = (x) + (y); } else { (z) = (x) - (y); } } while(0)
typedef enum
{
VALUE_A=0,
VALUE_B =1 ,
VALUE_C = 2 ,
}ValueType;
typedef struct InnerStruct {
int a;char b;
const char* name;
}InnerStruct;
typedef struct VeryLongStructNameTag
{
int id;
char* description;
InnerStruct inner;
int (*callback)(int arg1,int arg2);
} VeryLongStructName;
static int global_var1=1,global_var2 =2;
static const char * global_str = "hello";
static int sum(int x,int y){return x+y;}
static int very_long_function_name_with_many_parameters(int a, int b, const char* message, VeryLongStructName* s, int (*func)(int, int))
{
int result=0;
if(a> b){result = a+b;}else
{
result=a-b;
}
if (message!=NULL) { printf("msg: %s\n", message ); }
for(int i=0;i<10;i++){ result+=i; if(i%2==0){printf("even %d\n",i);}else{printf("odd %d\n",i);} }
while(result<100){result += 3;}
switch(result%3){
case 0: printf("mod 0\n");break;
case 1:
printf("mod 1\n");
break;
default:
printf("mod other\n");
break;
}
if (s != NULL && s->callback!=NULL)
{
int cb = s->callback(a,b);
printf("callback=%d\n",cb);
}
VERY_LONG_MACRO_DEFINITION(a,b,result);
return result;
}
/* 测试注释对齐:
* 1. 这一行很长很长很长很长很长很长很长很长很长很长很长很长,看 clang-format 会不会换行
* 2. 看看星号对齐方式
*/
void test_pointer_and_array (void)
{
int arr[5]={1,2,3,4,5};
int * p1 = arr; int* p2= &arr[0] ;
int **pp = &p1;
for ( size_t i=0;i<5;i++ )
printf("arr[%zu]=%d\n",i,arr[i]);
*pp = p2;
if( p1 == p2 )
{
printf("same pointer\n");
}
}
int func_add(int x,int y){return x+y;}
int func_sub(int x,int y){ return x-y; }
void test_function_pointer()
{
int (*op)(int,int) = func_add;
int r1 = op(10,3);
op = func_sub;
int r2 = op(10,3);
printf("r1=%d, r2=%d\n",r1,r2);
}
int main( void )
{
VeryLongStructName s = {
.id = 1,
.description = "test",
.inner = { .a = 10, .b ='c', .name = "inner" },
.callback = sum,
};
int x= 10;
int y =20;
int z=0;
int res = very_long_function_name_with_many_parameters(x,y,"hello world",&s,sum);
printf("res=%d\n",res);
test_pointer_and_array();
test_function_pointer();
/* 行内注释测试 */ int inline_var=123; // 尾部注释
return 0;
}