vscode格式化proto文件

安装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
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值