java.kak (5801B)
1 hook global BufCreate .*\.java %{ 2 set-option buffer filetype java 3 } 4 5 # Initialization 6 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 7 8 hook global WinSetOption filetype=java %{ 9 require-module java 10 11 set-option window static_words %opt{java_static_words} 12 13 # cleanup trailing whitespaces when exiting insert mode 14 hook window ModeChange pop:insert:.* -group java-trim-indent %{ try %{ execute-keys -draft xs^\h+$<ret>d } } 15 hook window InsertChar \n -group java-insert java-insert-on-new-line 16 hook window InsertChar \n -group java-indent java-indent-on-new-line 17 hook window InsertChar \{ -group java-indent java-indent-on-opening-curly-brace 18 hook window InsertChar \} -group java-indent java-indent-on-closing-curly-brace 19 20 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window java-.+ } 21 } 22 23 hook -group java-highlight global WinSetOption filetype=java %{ 24 add-highlighter window/java ref java 25 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/java } 26 } 27 28 provide-module java %§ 29 30 add-highlighter shared/java regions 31 add-highlighter shared/java/code default-region group 32 add-highlighter shared/java/string region %{(?<!')"} %{(?<!\\)(\\\\)*"} fill string 33 add-highlighter shared/java/character region %{'} %{(?<!\\)'} fill value 34 add-highlighter shared/java/comment region /\* \*/ fill comment 35 add-highlighter shared/java/inline_documentation region /// $ fill documentation 36 add-highlighter shared/java/line_comment region // $ fill comment 37 38 add-highlighter shared/java/code/ regex "(?<!\w)@\w+\b" 0:meta 39 40 # Commands 41 # ‾‾‾‾‾‾‾‾ 42 43 define-command -hidden java-insert-on-new-line %[ 44 # copy // comments prefix and following white spaces 45 try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> } 46 ] 47 48 define-command -hidden java-indent-on-new-line %~ 49 evaluate-commands -draft -itersel %= 50 # preserve previous line indent 51 try %{ execute-keys -draft <semicolon>K<a-&> } 52 # indent after lines ending with { or ( 53 try %[ execute-keys -draft kx <a-k> [{(]\h*$ <ret> j<a-gt> ] 54 # cleanup trailing white spaces on the previous line 55 try %{ execute-keys -draft kx s \h+$ <ret>d } 56 # align to opening paren of previous line 57 try %{ execute-keys -draft [( <a-k> \A\(\N+\n\N*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & } 58 # indent after a switch's case/default statements 59 try %[ execute-keys -draft kx <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ] 60 # indent after keywords 61 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-,><a-gt> ] 62 # deindent closing brace(s) when after cursor 63 try %[ execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ] 64 = 65 ~ 66 67 define-command -hidden java-indent-on-opening-curly-brace %[ 68 # align indent with opening paren when { is entered on a new line after the closing paren 69 try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ] 70 ] 71 72 define-command -hidden java-indent-on-closing-curly-brace %[ 73 # align to opening curly brace when alone on a line 74 try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ] 75 ] 76 77 # Shell 78 # ‾‾‾‾‾ 79 # Oracle 2021, 3.9 Keywords, Chapter 3. Lexical Structure, Java Language Specification, Java SE 17, viewed 25 September 2021, <https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.9> 80 # 81 evaluate-commands %sh{ 82 values='false null this true' 83 84 types='boolean byte char double float int long short unsigned void' 85 86 keywords='assert break case catch class continue default do else enum extends 87 finally for if implements import instanceof interface new package return 88 static strictfp super switch throw throws try var while yield' 89 90 attributes='abstract final native non-sealed permits private protected public 91 record sealed synchronized transient volatile' 92 93 modules='exports module open opens provides requires to transitive uses with' 94 95 # ---------------------------------------------------------------------------------------------- # 96 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 97 # ---------------------------------------------------------------------------------------------- # 98 add_highlighter() { printf "add-highlighter shared/java/code/ regex %s %s\n" "$1" "$2"; } 99 # ---------------------------------------------------------------------------------------------- # 100 add_word_highlighter() { 101 102 while [ $# -gt 0 ]; do 103 words=$1 face=$2; shift 2 104 regex="\\b($(join "${words}" '|'))\\b" 105 add_highlighter "$regex" "1:$face" 106 done 107 108 } 109 110 # highlight: open<space> not open() 111 add_module_highlighter() { 112 113 while [ $# -gt 0 ]; do 114 words=$1 face=$2; shift 2 115 regex="\\b($(join "${words}" '|'))\\b(?=\\s)" 116 add_highlighter "$regex" "1:$face" 117 done 118 119 } 120 # ---------------------------------------------------------------------------------------------- # 121 printf %s\\n "declare-option str-list java_static_words $(join "${values} ${types} ${keywords} ${attributes} ${modules}" ' ')" 122 # ---------------------------------------------------------------------------------------------- # 123 add_word_highlighter "$values" "value" "$types" "type" "$keywords" "keyword" "$attributes" "attribute" 124 # ---------------------------------------------------------------------------------------------- # 125 add_module_highlighter "$modules" "module" 126 # ---------------------------------------------------------------------------------------------- # 127 } 128 129 §