twig.kak (3688B)
1 # https://twig.symfony.com/doc/3.x/templates.html 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*[.](twig) %{ 8 set-option buffer filetype twig 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=twig %[ 15 require-module twig 16 17 hook window ModeChange pop:insert:.* -group twig-trim-indent twig-trim-indent 18 hook window InsertChar \n -group twig-insert twig-insert-on-new-line 19 hook window InsertChar \n -group twig-indent twig-indent-on-new-line 20 hook window InsertChar '>' -group twig-indent twig-indent-on-greater-than 21 hook window InsertChar '#' -group twig-auto-close twig-auto-close-delim 22 hook window InsertChar '%' -group twig-auto-close twig-auto-close-delim 23 set-option buffer extra_word_chars '_' '-' 24 25 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window twig-.+ } 26 ] 27 28 hook -group twig-highlight global WinSetOption filetype=twig %{ 29 add-highlighter window/twig ref twig 30 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/twig } 31 } 32 33 34 provide-module twig %[ 35 36 require-module html 37 38 # Highlighters 39 # ‾‾‾‾‾‾‾‾‾‾‾‾ 40 41 add-highlighter shared/twig regions 42 add-highlighter shared/twig/core default-region group 43 add-highlighter shared/twig/comment region \{# [#]\} fill comment 44 add-highlighter shared/twig/delim region \{([%]-?|\{) (-?[%]|\})\} regions 45 46 add-highlighter shared/twig/core/ ref html 47 48 add-highlighter shared/twig/delim/base default-region group 49 add-highlighter shared/twig/delim/double_string region '"' (?<!\\)(\\\\)*" fill string 50 add-highlighter shared/twig/delim/single_string region "'" (?<!\\)(\\\\)*' fill string 51 add-highlighter shared/twig/delim/base/ regex (\w+)\h= 1:variable 52 53 add-highlighter shared/twig/delim/base/functions regex \b(\w+)\( 1:function 54 55 add-highlighter shared/twig/delim/base/filters regex \b(abs|batch|capitalize|column|convert_encoding|country_name|currency_name|currency_symbol|data_uri|date|date_modify|default|e|escape|filter|first|format|format_currency|format_date|format_datetime|format_number|format_time|html_to_markdown|inline_css|inky_to_html|join|json_encode|keys|language_name|last|length|locale_name|lower|map|markdown_to_html|merge|nl2br|number_format|raw|reduce|replace|reverse|round|slice|slug|sort|spaceless|split|striptags|timezone_name|title|trim|u|upper|url_encode)(\()?\b 1:operator 56 57 add-highlighter shared/twig/delim/base/tags regex \b((extends|deprecated|do|flush|import|from|elseif|else|include|set|use)|(end)?(apply|autoescape|block|cache|embed|for|if|macro|sandbox|set|verbatim|with))\b 0:keyword 0:+i 58 59 60 add-highlighter shared/twig/delim/base/delimiter_code regex (\{[%]|[%]\}) 0:function 61 add-highlighter shared/twig/delim/base/delimiter_output regex (\{\{|\}\}) 0:operator 62 63 # Commands 64 # ‾‾‾‾‾‾‾‾ 65 66 define-command -hidden twig-trim-indent html-trim-indent 67 define-command -hidden twig-indent-on-new-line html-indent-on-new-line 68 define-command -hidden twig-indent-on-greater-than html-indent-on-greater-than 69 70 define-command -hidden twig-auto-close-delim %[ 71 evaluate-commands -itersel %[ 72 try %[ 73 execute-keys -draft <semicolon>hH<a-k>\h*\{<ret>lyp 74 execute-keys <esc>hi<space><esc>hi<space> 75 ] 76 ] 77 ] 78 79 define-command -hidden twig-insert-on-new-line %[ 80 evaluate-commands -draft -itersel %/ 81 execute-keys <semicolon> 82 try %[ 83 execute-keys -draft kx<a-k>^\h*\{\[%#\{\]\h+$<ret> 84 execute-keys -draft jghd 85 ] 86 / 87 ] 88 89 ]