From ea4f2b88372bc7e331c82fe29bc876238910b6f7 Mon Sep 17 00:00:00 2001 From: antab Date: Wed, 16 Sep 2020 00:03:17 +0200 Subject: [PATCH] feat: add basic ninjafile support --- rc/filetype/ninja.kak | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 rc/filetype/ninja.kak diff --git a/rc/filetype/ninja.kak b/rc/filetype/ninja.kak new file mode 100644 index 00000000..c054daa0 --- /dev/null +++ b/rc/filetype/ninja.kak @@ -0,0 +1,111 @@ +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufCreate .+\.ninja %{ + set-option buffer filetype ninja +} + + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=ninja %{ + require-module ninja + + set-option window static_words %opt{ninja_static_words} + + hook window ModeChange insert:.* -group ninja-trim-indent ninja-trim-indent + hook window InsertChar \n -group ninja-indent ninja-indent-on-new-line + # cleanup trailing whitespaces on current line insert end + hook window ModeChange pop:insert:.* -group ninja-trim-indent %{ try %{ execute-keys -draft s ^\h+$ d } } + + hook -once -always window WinSetOption filetype=.* %{ remove-hooks window ninja-.+ } +} + +hook -group ninja-highlight global WinSetOption filetype=ninja %{ + add-highlighter window/ninja ref ninja + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/ninja } +} + +# Lazy loading +provide-module ninja %{ + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +add-highlighter shared/ninja regions + +# `#` +add-highlighter shared/ninja/comment region '#' '\n' fill comment + +# `rule` +add-highlighter shared/ninja/rule region '^rule' '\n' group +add-highlighter shared/ninja/rule/rule regex '^rule' 0:keyword + +# `command` +add-highlighter shared/ninja/command region '^\h+command' '[^$]\n' group +add-highlighter shared/ninja/command/command regex 'command' 0:variable +add-highlighter shared/ninja/command/equal regex '=' 0:operator +add-highlighter shared/ninja/command/linebreak regex '\$$' 0:operator +add-highlighter shared/ninja/command/variables regex '\$\w+|\$\{\w+\}' 0:value + +# `build` +add-highlighter shared/ninja/build region '^build' '\n' group +add-highlighter shared/ninja/build/build regex '^build\h+' 0:keyword +add-highlighter shared/ninja/build/rule regex ':\h+(\w+)' 0:function +add-highlighter shared/ninja/build/colonpipe regex ':|\||\|\|' 0:operator + +# variables +add-highlighter shared/ninja/variable region '^\h*\w+\h+=' '\n' group +add-highlighter shared/ninja/variable/name regex '(\w+)\h+=' 0:variable +# TODO: toplevel builddir is conflicting with build +add-highlighter shared/ninja/variable/equal regex '=' 0:operator + +# `default` +add-highlighter shared/ninja/default region '^default' '\n' group +add-highlighter shared/ninja/default/default regex '^default' 0:keyword + +# `pool` +add-highlighter shared/ninja/pool region '^pool' '\n' group +add-highlighter shared/ninja/pool/pool regex '^pool' 0:keyword + +# keywords/builtin variable names +evaluate-commands %sh{ + keywords="rule build command default" + reserved_names="builddir ninja_required_version pool depfile deps depfile msvc_deps_prefix description dyndep generator restat rspfile rspfile_content" + + printf %s " + declare-option str-list ninja_static_words ${keywords} ${reserved_names} + " + + reserved_names_regex="$(echo ${reserved_names} | tr ' ' '|')" + printf %s " + add-highlighter shared/ninja/variable/reserved_names regex ${reserved_names_regex} 0:meta + " +} + +# Commands +# ‾‾‾‾‾‾‾‾ + +define-command -hidden ninja-trim-indent %{ + # remove trailing white spaces + try %{ execute-keys -draft -itersel s \h+$ d } +} + +define-command -hidden ninja-indent-on-new-line %{ + evaluate-commands -draft -itersel %{ + # copy -- comments prefix and following white spaces + try %{ execute-keys -draft k s ^\h*\K--\h* y gh j P } + # preserve previous line indent + try %{ execute-keys -draft \; K } + # filter previous line + try %{ execute-keys -draft k : ninja-trim-indent } + # indent after lines begining with rule and pool (do people want build too ?) +try %{ execute-keys -draft \; k x ^(\brule|pool) j } + } +} + + +} + +# ref: https://ninja-build.org/manual.html#ref_ninja_file