2018-08-16 06:39:29 +02:00
|
|
|
# https://developers.google.com/protocol-buffers/
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*\.proto$ %{
|
|
|
|
set-option buffer filetype protobuf
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global WinSetOption filetype=protobuf %[
|
2019-03-13 06:24:33 +01:00
|
|
|
require-module protobuf
|
2019-04-10 05:54:19 +02:00
|
|
|
|
|
|
|
set-option window static_words %opt{protobuf_static_words}
|
|
|
|
|
2022-05-08 15:01:25 +02:00
|
|
|
hook window ModeChange pop:insert:.* -group protobuf-trim-indent protobuf-trim-indent
|
2023-02-24 08:41:16 +01:00
|
|
|
hook -group protobuf-insert window InsertChar \n protobuf-insert-on-newline
|
2019-04-10 05:54:19 +02:00
|
|
|
hook -group protobuf-indent window InsertChar \n protobuf-indent-on-newline
|
|
|
|
hook -group protobuf-indent window InsertChar \{ protobuf-indent-on-opening-curly-brace
|
|
|
|
hook -group protobuf-indent window InsertChar \} protobuf-indent-on-closing-curly-brace
|
|
|
|
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window protobuf-.+ }
|
|
|
|
]
|
|
|
|
|
|
|
|
hook -group protobuf-highlight global WinSetOption filetype=protobuf %{
|
|
|
|
add-highlighter window/protobuf ref protobuf
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/protobuf }
|
2019-03-13 06:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
provide-module protobuf %[
|
|
|
|
|
2018-08-16 06:39:29 +02:00
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
add-highlighter shared/protobuf regions
|
|
|
|
add-highlighter shared/protobuf/code default-region group
|
|
|
|
add-highlighter shared/protobuf/double_string region '"' (?<!\\)(\\\\)*" fill string
|
|
|
|
add-highlighter shared/protobuf/comment region /\* \*/ fill comment
|
|
|
|
add-highlighter shared/protobuf/comment_line region '//' $ fill comment
|
|
|
|
|
|
|
|
add-highlighter shared/protobuf/code/ regex %{(0x)?[0-9]+\b} 0:value
|
|
|
|
|
|
|
|
evaluate-commands %sh{
|
|
|
|
# Grammer
|
2019-04-10 05:54:19 +02:00
|
|
|
keywords='default deprecated enum extend import message oneof option
|
|
|
|
package service syntax'
|
|
|
|
attributes='optional repeated required'
|
|
|
|
types='double float int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64
|
|
|
|
sfixed32 sfixed64 bool string bytes rpc'
|
|
|
|
values='false true'
|
|
|
|
|
|
|
|
join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
|
2018-08-16 06:39:29 +02:00
|
|
|
|
|
|
|
# Add the language's grammer to the static completion list
|
2019-04-10 05:54:19 +02:00
|
|
|
printf %s\\n "declare-option str-list protobuf_static_words $(join "${keywords} ${attributes} ${types} ${values}" ' ')"
|
2018-08-16 06:39:29 +02:00
|
|
|
|
|
|
|
# Highlight keywords
|
|
|
|
printf %s "
|
2019-04-10 05:54:19 +02:00
|
|
|
add-highlighter shared/protobuf/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
|
|
|
|
add-highlighter shared/protobuf/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
|
|
|
|
add-highlighter shared/protobuf/code/types regex \b($(join "${types}" '|'))\b 0:type
|
|
|
|
add-highlighter shared/protobuf/code/values regex \b($(join "${values}" '|'))\b 0:value
|
2018-08-16 06:39:29 +02:00
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2022-05-08 15:01:25 +02:00
|
|
|
define-command -hidden protobuf-trim-indent %{
|
|
|
|
evaluate-commands -no-hooks -draft -itersel %{
|
2022-03-16 23:20:07 +01:00
|
|
|
execute-keys x
|
2022-05-08 15:01:25 +02:00
|
|
|
# remove trailing white spaces
|
|
|
|
try %{ execute-keys -draft s \h + $ <ret> d }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:58:31 +01:00
|
|
|
define-command -hidden protobuf-insert-on-newline %{
|
2023-02-24 08:41:16 +01:00
|
|
|
evaluate-commands -draft -itersel %[
|
|
|
|
# copy // comments prefix
|
|
|
|
try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}(\h*(?=\S))? <ret> y<c-o>P<esc> }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-08-16 06:39:29 +02:00
|
|
|
define-command -hidden protobuf-indent-on-newline %~
|
|
|
|
evaluate-commands -draft -itersel %[
|
|
|
|
# preserve previous line indent
|
2019-10-22 11:02:06 +02:00
|
|
|
try %{ execute-keys -draft <semicolon>K<a-&> }
|
2018-08-16 06:39:29 +02:00
|
|
|
# indent after lines ending with {
|
2022-03-16 23:20:07 +01:00
|
|
|
try %[ execute-keys -draft kx <a-k> \{\h*$ <ret> j<a-gt> ]
|
2018-08-16 06:39:29 +02:00
|
|
|
# cleanup trailing white spaces on the previous line
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft kx s \h+$ <ret>d }
|
2020-07-30 04:31:43 +02:00
|
|
|
# deindent closing brace(s) when after cursor
|
2022-03-16 23:20:07 +01:00
|
|
|
try %[ execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ]
|
2018-08-16 06:39:29 +02:00
|
|
|
]
|
|
|
|
~
|
|
|
|
|
|
|
|
define-command -hidden protobuf-indent-on-opening-curly-brace %[
|
|
|
|
# align indent with opening paren when { is entered on a new line after the closing paren
|
|
|
|
try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
|
|
|
|
]
|
|
|
|
|
|
|
|
define-command -hidden protobuf-indent-on-closing-curly-brace %[
|
|
|
|
# align to opening curly brace when alone on a line
|
|
|
|
try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
|
|
|
|
]
|
|
|
|
|
2019-03-13 06:24:33 +01:00
|
|
|
]
|