安装vscode-proto3插件
在应用商店中安装vscode-proto3插件
安装clang-format 可执行程序
sudo apt install clang-format
执行命令查看是否安装成功clang-format -help
OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
If no arguments are specified, it formats the code from standard input
and writes the result to the standard output.
If <file>s are given, it reformats the files. If -i is specified
together with <file>s, the files are edited in-place. Otherwise, the
result is written to the standard output.
USAGE: clang-format [options] [<file> ...]
OPTIONS:
格式化代码
此时在proto文件上按 ctrl+shift+i (linux下) 尝试格式化代码,为了方便可以在 setting.json中配置"editor.formatOnSave": true,
保存时自动格式化代码
另一种格式化方式
以上格式化程序使用的是proto3的方式进行的格式化,也可以尝试使用clang-format的方式。
在应用商店安装 Clang-Format 插件,
此时 保存文件时不会自动格式化代码(因为两种格式化程序冲突),需要先配置默认的格式化程序 ctrl+shift+i vscode会弹框提示选择一个默认的格式化程序 ,这之后 ctrl+s 就可以自动格式化文件了
配置的默认格式化程序在setting.json中的就是如下两个配置,可以手动在两个之间自己切换:
// "[proto3]": {
// "editor.defaultFormatter": "xaver.clang-format"
// },
"[proto3]": {
"editor.defaultFormatter": "zxh404.vscode-proto3"
},
clang-format可以使用自定义的配置文件来格式化代码:
在项目的根目录下(或者项目的任意上级目录下)新建一个.clang-format或_clang-format文件 配置内容可以参考官网:https://clang.llvm.org/docs/ClangFormatStyleOptions.html
除此之外无需其它配置,格式化代码即会根据该clang-format文件内容来格式化代码。
.clang-format文件示例:
Language: Proto
# # 每行字符的限制,0表示没有限制
ColumnLimit: 1000
# 对齐连续的尾随的注释
AlignTrailingComments: true
# 在一个注释中引入换行的penalty
PenaltyBreakComment: 1000
# 允许重新排版注释
ReflowComments: true