在不知道代码格式化之前,很多小伙伴编程时花费大量的时间去调整代码的格式,也许为了美观和强迫症。那么,为了更高效地编程,下面介绍标题中几种工具的代码格式化方法。
Codeblocks 17.12
首先在代码框右键,选择 Format use AStyle,代码将会按照默认的格式重新布置。可以自定义快捷键,依次进入 Setting->Editor->Keyboard Shortcut->Plugins,选中Source code formatter(AStyle)。那么,如何打造自己的Style?依次进入 Setting->Editor->Source formatter,在Style处选中Custom(自定义,否则就是默认格式),然后自定义,
Brackets(大括号):
attach classes表示类的格式为
class name {
};
而不是
class name
{
};
其它的类似。
Indentation(缩进):
比如Indentation size(in spaces):4 说明默认缩进4个空格。
Formatting(格式):
比如 Break 'else if()'header combinations into seperate lines ,表示将
else if()
格式化为
else
if()
Padding(填充):
比如
Insert space padding around operators 表示在操作符前后各填充1个空格;
Insert space padding around parentheses on the inside 表示在插入语的两边各填充1个空格;
Remove extra space padding around parentheses 表示清除语句两边多余的空格;
Delete empty lines within a function or method 表示清除空行;
Pointer alignment 选Name 则定义指针时的格式为 type *name;
Eclipse
默认格式化快捷键是Ctrl+Shift+F,可以在Preference修改格式化规则,搜索formatter,Java分类里的Code Style,Debug,Editor等修改。
VS Code
默认格式化快捷键是Shift+Alt+F,若要修改格式化规则,可以下载插件,如Prettier-Code formatter,再进行配置。