groovy.kak (4797B)
1 # Adapted from the file created by Daniel Lewan TeddyDD 2 3 hook global BufCreate "(.+\.(groovy|gvy|gy|gsh|gradle))|.+[Jj]enkinsfile.*" %{ 4 set-option buffer filetype groovy 5 } 6 7 hook global WinSetOption filetype=groovy %{ 8 require-module groovy 9 10 set-option window static_words %opt{groovy_static_words} 11 12 hook window ModeChange pop:insert:.* -group groovy-trim-indent %{ try %{ execute-keys -draft xs^\h+$<ret>d } } 13 hook window InsertChar \n -group groovy-insert groovy-insert-on-new-line 14 hook window InsertChar \n -group groovy-indent groovy-indent-on-new-line 15 hook window InsertChar \{ -group groovy-indent groovy-indent-on-opening-curly-brace 16 hook window InsertChar \} -group groovy-indent groovy-indent-on-closing-curly-brace 17 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window groovy-.+ } 18 } 19 20 hook -group groovy-highlight global WinSetOption filetype=groovy %{ 21 add-highlighter window/groovy ref groovy 22 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/groovy } 23 } 24 25 provide-module groovy %§ 26 27 add-highlighter shared/groovy regions 28 29 add-highlighter shared/groovy/code default-region group 30 add-highlighter shared/groovy/triple_quote region '"{3}' (?<!\\)(\\\\)*"{3} fill string 31 add-highlighter shared/groovy/double_string region '"' (?<!\\)(\\\\)*" fill string 32 add-highlighter shared/groovy/single_string region "'" (?<!\\)(\\\\)*' fill string 33 add-highlighter shared/groovy/comment1 region '/\*[^*]?' '\*/' fill comment 34 add-highlighter shared/groovy/comment2 region '//[^/]?' $ fill comment 35 add-highlighter shared/groovy/shellbang region '#!.+' $ fill comment 36 add-highlighter shared/groovy/dollar_string region "\$/" "(?<!\$)/\$" fill string 37 # add-highlighter shared/groovy/code/identifiers regex '\b[$_]?[a-zA-Z0-9_]+\b' 0:variable 38 add-highlighter shared/groovy/code/declaration regex "(?<typ>\w+)(?:\[.*?\])?\s+(\$?\w+)\s=" typ:type 39 add-highlighter shared/groovy/code/numbers regex '\b[-+]?0x[A-Fa-f0-9_]+[.A-Fa-f0-9_p]*[lLiDgGIF]?|\b[-+]?[\d]+b?[.p_\dEe]*[lLiDgGIF]?' 0:value 40 add-highlighter shared/groovy/slashy_string region "\b/\w" "(?<!\\)\w/\b" fill string 41 42 evaluate-commands %sh{ 43 keywords=' 44 as assert break case catch class const continue def default 45 do else enum extends finally for goto if implements import in 46 instanceof interface new package return super switch this throw 47 throws trait try while 48 ' 49 builtins='true false null println' 50 types='byte char short int long BigInteger float double BigDecimal boolean' 51 52 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 53 54 printf %s\\n "add-highlighter shared/groovy/code/keyword regex \\b($(join "$keywords" '|'))\\b 0:keyword" 55 printf %s\\n "add-highlighter shared/groovy/code/builtin regex \\b($(join "$builtins" '|'))\\b 0:builtin" 56 printf %s\\n "add-highlighter shared/groovy/code/types regex \\b($(join "$types" '|'))\\b 0:type" 57 58 printf %s\\n "declare-option str-list groovy_static_words $(join "$keywords $builtins $types" ' ')" 59 } 60 61 define-command -hidden groovy-insert-on-new-line %[ 62 # copy // comments prefix and following white spaces 63 try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> } 64 ] 65 66 define-command -hidden groovy-indent-on-new-line %~ 67 evaluate-commands -draft -itersel %= 68 # preserve previous line indent 69 try %{ execute-keys -draft <semicolon>K<a-&> } 70 # indent after lines ending with { or ( 71 try %[ execute-keys -draft kx <a-k> [{(]\h*$ <ret> j<a-gt> ] 72 # cleanup trailing white spaces on the previous line 73 try %{ execute-keys -draft kx s \h+$ <ret>d } 74 # align to opening paren of previous line 75 try %{ execute-keys -draft [( <a-k> \A\(\N+\n\N*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & } 76 # indent after a switch's case/default statements 77 try %[ execute-keys -draft kx <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ] 78 # indent after keywords 79 try %[ execute-keys -draft <semicolon><a-F>)MB <a-k> \A(if|else|while|for|try|catch)\h*\(.*\)\h*\n\h*\n?\z <ret> s \A|.\z <ret> 1<a-&>1<a-space><a-gt> ] 80 # deindent closing brace(s) when after cursor 81 try %[ execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ] 82 = 83 ~ 84 85 define-command -hidden groovy-indent-on-opening-curly-brace %[ 86 # align indent with opening paren when { is entered on a new line after the closing paren 87 try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ] 88 ] 89 90 define-command -hidden groovy-indent-on-closing-curly-brace %[ 91 # align to opening curly brace when alone on a line 92 try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ] 93 ] 94 §