Home

Awesome

vscode-proto3

icon

Protobuf 3 support for Visual Studio Code

https://github.com/zxh0/vscode-proto3

VSCode Commands

By default ctrl-shift-p opens the command prompt.

CommandDescription
proto3: Compile All ProtosCompiles all workspace protos using configurations defined with protoc.options.
proto3: Compile This ProtoCompiles the active proto using configurations defined with protoc.options.

Features

gif1

Syntax Highlighting

The grammar is written in tmLanguage JSON format.

Syntax Validation

The validation is triggered when you save the proto file. You need protoc compiler to enable syntax validation. You also need a settings.json file to tell the extension the full path of protoc if it is not in path.

Extension Settings

Below is an example settings.json file which comes from example/.vscode:

{
    "protoc": {
        "path": "/path/to/protoc",
        "compile_on_save": false,
        "options": [
            "--proto_path=protos/v3",
            "--proto_path=protos/v2",
            "--proto_path=${workspaceRoot}/proto",
            "--proto_path=${env.GOPATH}/src",
            "--java_out=gen/java"
        ]
    }
}

Fields

The possible fields under the protoc extension settings which can be defined in a settings.json file.

FieldTypeDefaultDescription
pathstringprotoc in PATHPath to protoc. Defaults to protoc in PATH if omitted.
compile_on_savebooleanfalseOn .proto file save, compiles to --*_out location within options
compile_all_pathstringWorkspace RootSearch Path for Compile All Protos action. Defaults to the Workspace Root
use_absolute_pathbooleanfalseSet true for compile_all_path search files using absolute path
optionsstring[][]protoc compiler arguments/flags, required for proto validation and compilation

In-Line Variables

These variables can be used to inject variables strings within the protoc extension configurations. See above for examples.

VariableDescription
config.*Refer settings items in Preferences.
env.*Refer environment variable.
workspaceRootReturns current workspace root path.

Code Completion

A very simple parser is written to support code completion.

Code Snippets

prefixbody
sp2syntax = "proto2";
sp3syntax = "proto3";
pkgpackage package.name;
impimport "path/to/other/protos.proto";
ojpoption java_package = "java.package.name";
ojocoption java_outer_classname = "ClassName";
o4soption optimize_for = SPEED;
o4csoption optimize_for = CODE_SIZE;
o4lroption optimize_for = LITE_RUNTIME;
odepoption deprecated = true;
oaaoption allow_alias = true;
msgmessage MessageName {}
fbobool field_name = tag;
fi32int32 field_name = tag;
fi64int64 field_name = tag;
fu32uint32 field_name = tag;
fu64uint64 field_name = tag;
fs32sint32 field_name = tag;
fs64sint64 field_name = tag;
ff32fixed32 field_name = tag;
ff64fixed64 field_name = tag;
fsf32sfixed32 field_name = tag;
fsf64sfixed64 field_name = tag;
fflfloat field_name = tag;
fdodouble field_name = tag;
fststring field_name = tag;
fbybytes field_name = tag;
fmmap<key, val> field_name = tag;
foooneof name {}
enenum EnumName {}
svservice ServiceName {}
rpcrpc MethodName (Request) returns (Response);

Google API Design Guide

The following snippets are based on Google API Design Guide.

prefixreference
svgapiStandard Methods

Code Formatting

Support "Format Document" if clang-format is in path, including custom style options.

By default, clang-format's standard coding style will be used for formatting. To define a custom style or use a supported preset add "clang-format.style" in VSCode Settings (settings.json)

Example usage:

"clang-format.style": "google"

This is the equivalent of executing clang-format -style=google from the shell.

With multiple formatting options

"clang-format.style": "{ IndentWidth: 4, BasedOnStyle: google, AlignConsecutiveAssignments: true }"

For further formatting options refer to the official clang-format documentation

Known Issues

Auto-completion not works in some situations.

Some users consistently see an error like spawnsync clang-format enoent when they save. This happens when the "formatOnSave"-setting is enabled in VSCode and "clang-format" cannot be found. To fix this:

On MacOS

  1. Install clang-format on your system: brew install clang-format
  2. Install the clang-format plugin in VSCode

Release Notes

See CHANGELOG.md.