【代码格式化】linux代码格式化(Astyle)+wind格式转unix格式(Doc2Unix)----git提交代码前的处理

 

乐鑫的git代码提交前的建议:值得学习

格式化代码

您在提交代码之前需要对代码进行格式化,ESP-MDF 使用的格式化式工具为:astyle 和 dos2unix。您需要先安装他们,在 Linux 安装命令如下:

sudo apt-get install astyle
sudo apt-get install doc2unix

运行如下代码格式化命令:

tools/format.sh new_file.c

API 文档生成

在编写代码注释时,请遵循 Doxygen 格式。您可以通过 Doxygen 按如下步骤自动生成 API 文档:

sudo apt-get install doxygen
export MDF_PATH=~/esp/esp-mdf
cd ~/esp/esp-mdf/docs
pip install -r requirements.txt
cd ~/esp/esp-mdf/docs/en
make html

 

 

dos2unix的使用

 

由于在DOS(windows系统)下,文本文件的换行符为CRLF,而在Linux下换行符为LF,使用git进行代码管理时,git会自动进行CRLF和LF之间的转换,这个我们不用操心。而有时候,我们需要将windows下的文件上传到linux上,例如shell脚本,执行的时候有时会出现奇怪的问题,这时候,就需要安装dos2unix软件。

在centos下安装dos2unix:

yum install -y dos2unix

 

安装完成后,对文件进行转换

dos2unix abc.sh

现在执行就不会出问题了

 

转自:https://www.cnblogs.com/lucky-heng/p/10679900.html

 

Astyle格式化代码

 

基本命令

astyle --style=ansi main.cs

 

astyle 使用说明和集成到 各种IDE平台:https://www.cnblogs.com/jiangxinnju/p/4908575.html

 

Astyle格式化代码-选项说明


官方文档 http://astyle.sourceforge.net/astyle.html
配置了最佳的教科书格式


astyle *.c *.cpp *.h --recursive  --style=bsd  --convert-tabs --indent=spaces=8 --attach-closing-while  --indent-switches  --indent-namespaces --indent-continuation=4 --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --pad-oper   --pad-paren-in   --unpad-paren  --delete-empty-lines --align-pointer=name    --align-reference=name --break-elseifs  --add-braces 

--indent-switches 缩进case标签


switch (foo)
{
case 1:
    a += 1;
    break;

case 2:
{
    a += 2;
    break;
}
}
becomes:

switch (foo)
{
    case 1:
        a += 1;
        break;

    case 2:
    {
        a += 2;
        break;
    }
}


--indent=spaces=8  缩进8个空格

void Foo() 
{
........bar();
}

--indent-namespaces 缩进命名空间块

namespace foospace
{
class Foo
{
    public:
        Foo();
        virtual ~Foo();
};
}
becomes:

namespace foospace
{
    class Foo
    {
        public:
            Foo();
            virtual ~Foo();
    };
}


--indent-continuation=4 等号=或(结尾后续本语句符号插入空格,默认为1,可取1~4

isLongVariable =
    foo1 ||
    foo2;

isLongFunction(
    bar1,
    bar2);
becomes (with indent-continuation=3):

isLongVariable =
            foo1 ||
            foo2;

isLongFunction(
            bar1,
            bar2);

--style=bsd 大括号独占一行,上下对齐

int Foo(bool isBar)
{
    if (isBar)
    {
        bar();
        return 1;
    }
    else
        return 0;
}


--attach-closing-while (while紧贴)

do
{
    bar();
    ++x;
}
while x == 1;
becomes:

do
{
    bar();
    ++x;
} while x == 1;
 

--indent-preproc-block 缩进#开头的处理语句

#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
becomes:

#ifdef _WIN32
    #include <windows.h>
    #ifndef NO_EXPORT
        #define EXPORT
    #endif
#endif


--indent-preproc-cond  预处理语句也缩进


        isFoo = true;
#ifdef UNICODE
        text = wideBuff;
#else
        text = buff;
#endif
becomes:

        isFoo = true;
        #ifdef UNICODE
        text = wideBuff;
        #else
        text = buff;
        #endif


--indent-col1-comments 注释也缩进

void Foo()\n"
{
// comment
    if (isFoo)
        bar();
}
becomes:

void Foo()\n"
{
    // comment
    if (isFoo)
        bar();
}
 

--pad-oper 操作符间插入空格


if (foo==2)
    a=bar((b-c)*a,d--);
becomes:

if (foo == 2)
    a = bar((b - c) * a, d--);
 

--pad-comma 逗号间插入空格(--pad-oper中已有此效果)


if (isFoo(a,b))
    bar(a,b);
becomes:

if (isFoo(a, b))
    bar(a, b);
 

--pad-paren-in 括号里内插入空格


if (isFoo((a+2), b))
    bar(a, b);
becomes:

if ( isFoo( ( a+2 ), b ) )
    bar( a, b );


--unpad-paren 紧凑括号内外

if ( isFoo( ( a+2 ), b ) )
    bar ( a, b );
becomes (with no padding option requested):

if(isFoo((a+2), b))
    bar(a, b);
 

--delete-empty-lines 清除函数间的空行


void Foo()
{

    foo1 = 1;

    foo2 = 2;

}
becomes:

void Foo()
{
    foo1 = 1;
    foo2 = 2;
}


  指针符号紧贴哪

char* foo1;
char & foo2;
string ^s1;


becomes (with --align-pointer=type):

char* foo1;
char& foo2;
string^ s1;
char* foo1;
char & foo2;
string ^s1;


becomes (with --align-pointer=middle):

 

char * foo1;
char & foo2;
string ^ s1;
char* foo1;
char & foo2;
string ^s1;


becomes (with --align-pointer=name):

 

char *foo1;
char &foo2;
string ^s1;

//引用符号紧贴哪
char &foo1;


becomes (with --align-reference=type):

char& foo1;
char& foo2;
becomes (with --align-reference=middle):

char & foo2;
char& foo3;
becomes (with --align-reference=name):

char &foo3;
 

char &foo1;
becomes (with --align-reference=type):

char& foo1;
char& foo2;
becomes (with --align-reference=middle):

char & foo2;
char& foo3;
becomes (with --align-reference=name):

char &foo3;
 

--attach-return-type-decl 返回类型紧贴符号名


void
Foo(bool isFoo);
becomes:

void Foo(bool isFoo);


--add-braces 在'if', 'for', 'while'等句块中只有一行也加入大括号

if (isFoo)
    isFoo = false;
becomes:

if (isFoo) {
    isFoo = false;
}
 

--convert-tabs 将TAB符转化成空格,由转化参数指定,引号内的不转化
 

--recursive 遍历目录,文件名要指定为带通配符(*)的名字,含有空格的文件名要加引号



原文链接:https://blog.csdn.net/wisepragma/article/details/80993437

posted on 2022-10-04 01:27  bdy  阅读(42)  评论(0编辑  收藏  举报

导航