Skip to content

Commit

Permalink
Protobuf: Added support for RPC (#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jun 7, 2020
1 parent 937e269 commit 939a17c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
23 changes: 15 additions & 8 deletions components/prism-protobuf.js
Expand Up @@ -3,16 +3,23 @@
var builtinTypes = /\b(?:double|float|[su]?int(?:32|64)|s?fixed(?:32|64)|bool|string|bytes)\b/;

Prism.languages.protobuf = Prism.languages.extend('clike', {
'class-name': {
pattern: /(\b(?:enum|extend|message|service)\s+)[A-Za-z_]\w*(?=\s*\{)/,
lookbehind: true
},
'keyword': /\b(?:enum|extend|extensions|import|message|oneof|option|optional|package|public|repeated|required|reserved|service|syntax|to)\b/
'class-name': [
{
pattern: /(\b(?:enum|extend|message|service)\s+)[A-Za-z_]\w*(?=\s*\{)/,
lookbehind: true
},
{
pattern: /(\b(?:rpc\s+\w+|returns)\s*\(\s*(?:stream\s+)?)\.?[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*(?=\s*\))/,
lookbehind: true
}
],
'keyword': /\b(?:enum|extend|extensions|import|message|oneof|option|optional|package|public|repeated|required|reserved|returns|rpc(?=\s+\w)|service|stream|syntax|to)\b(?!\s*=\s*\d)/,
'function': /[a-z_]\w*(?=\s*\()/i
});

Prism.languages.insertBefore('protobuf', 'operator', {
'map': {
pattern: /\bmap<\s*[\w.]+\s*,\s*[\w.]+\s*>(?=\s+[A-Za-z_]\w*\s*[=;])/,
pattern: /\bmap<\s*[\w.]+\s*,\s*[\w.]+\s*>(?=\s+[a-z_]\w*\s*[=;])/i,
alias: 'class-name',
inside: {
'punctuation': /[<>.,]/,
Expand All @@ -21,14 +28,14 @@
},
'builtin': builtinTypes,
'positional-class-name': {
pattern: /(?:\b|\B\.)[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*(?=\s+[A-Za-z_]\w*\s*[=;])/,
pattern: /(?:\b|\B\.)[a-z_]\w*(?:\.[a-z_]\w*)*(?=\s+[a-z_]\w*\s*[=;])/i,
alias: 'class-name',
inside: {
'punctuation': /\./
}
},
'annotation': {
pattern: /(\[\s*)[A-Za-z_]\w*(?=\s*=)/,
pattern: /(\[\s*)[a-z_]\w*(?=\s*=)/i,
lookbehind: true
}
});
Expand Down
2 changes: 1 addition & 1 deletion components/prism-protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/languages/protobuf/keyword_feature.test
Expand Up @@ -11,7 +11,10 @@ public
repeated
required
reserved
returns
rpc LotsOfReplies(
service
stream
syntax
to

Expand All @@ -31,7 +34,12 @@ to
["keyword", "repeated"],
["keyword", "required"],
["keyword", "reserved"],
["keyword", "returns"],
["keyword", "rpc"],
["function", "LotsOfReplies"],
["punctuation", "("],
["keyword", "service"],
["keyword", "stream"],
["keyword", "syntax"],
["keyword", "to"]
]
Expand Down
68 changes: 68 additions & 0 deletions tests/languages/protobuf/rpc_feature.test
@@ -0,0 +1,68 @@
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
rpc CancelOperation(CancelOperationRequest) returns (google.protobuf.Empty) {
option (google.api.http) = { post: "/v1/{name=operations/**}:cancel" body: "*" };
}

----------------------------------------------------

[
["keyword", "rpc"],
["function", "LotsOfReplies"],
["punctuation", "("],
["class-name", "HelloRequest"],
["punctuation", ")"],
["keyword", "returns"],
["punctuation", "("],
["keyword", "stream"],
["class-name", "HelloResponse"],
["punctuation", ")"],
["punctuation", ";"],

["keyword", "rpc"],
["function", "BidiHello"],
["punctuation", "("],
["keyword", "stream"],
["class-name", "HelloRequest"],
["punctuation", ")"],
["keyword", "returns"],
["punctuation", "("],
["keyword", "stream"],
["class-name", "HelloResponse"],
["punctuation", ")"],
["punctuation", ";"],

["keyword", "rpc"],
["function", "CancelOperation"],
["punctuation", "("],
["class-name", "CancelOperationRequest"],
["punctuation", ")"],
["keyword", "returns"],
["punctuation", "("],
["class-name", "google.protobuf.Empty"],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "option"],
["punctuation", "("],
"google",
["punctuation", "."],
"api",
["punctuation", "."],
"http",
["punctuation", ")"],
["operator", "="],
["punctuation", "{"],
" post",
["punctuation", ":"],
["string", "\"/v1/{name=operations/**}:cancel\""],
" body",
["punctuation", ":"],
["string", "\"*\""],
["punctuation", "}"],
["punctuation", ";"],
["punctuation", "}"]
]

----------------------------------------------------

Check for RPC definitions.

0 comments on commit 939a17c

Please sign in to comment.