diff --git a/rc/base/java.kak b/rc/base/java.kak index 7cca4130..8e835fff 100644 --- a/rc/base/java.kak +++ b/rc/base/java.kak @@ -12,8 +12,54 @@ add-highlighter -group /java/comment fill comment add-highlighter -group /java/code regex %{\b(this|true|false|null)\b} 0:value add-highlighter -group /java/code regex "\b(void|int|char|unsigned|float|boolean|double)\b" 0:type -add-highlighter -group /java/code regex "\b(while|for|if|else|do|static|switch|case|default|class|interface|goto|break|continue|return|import|try|catch|throw|new|package|extends|implements)\b" 0:keyword -add-highlighter -group /java/code regex "\b(final|public|protected|private|abstract)\b" 0:attribute +add-highlighter -group /java/code regex "\b(while|for|if|else|do|static|switch|case|default|class|interface|enum|goto|break|continue|return|import|try|catch|throw|new|package|extends|implements|instanceof)\b" 0:keyword +add-highlighter -group /java/code regex "\b(final|public|protected|private|abstract|synchronized|native|transient|volatile)\b" 0:attribute +# Commands +# ‾‾‾‾‾‾‾‾ + +def -hidden java-indent-on-new-line %~ + eval -draft -itersel %= + # preserve previous line indent + try %{ exec -draft \;K } + # indent after lines ending with { or ( + try %[ exec -draft k [{(]\h*$ j ] + # cleanup trailing white spaces on the previous line + try %{ exec -draft k s \h+$ d } + # align to opening paren of previous line + try %{ exec -draft [( \`\([^\n]+\n[^\n]*\n?\' s \`\(\h*.|.\' '' & } + # copy // comments prefix + try %{ exec -draft \;k s ^\h*\K/{2,} yP } + # indent after a switch's case/default statements + try %[ exec -draft k ^\h*(case|default).*:$ j ] + # indent after keywords + try %[ exec -draft \;)MB \`(if|else|while|for|try|catch)\h*\(.*\)\h*\n\h*\n?\' s \`|.\' 11 ] + = +~ + +def -hidden java-indent-on-opening-curly-brace %[ + # align indent with opening paren when { is entered on a new line after the closing paren + try %[ exec -draft -itersel h)M \`\(.*\)\h*\n\h*\{\' s \`|.\' 1 ] +] + +def -hidden java-indent-on-closing-curly-brace %[ + # align to opening curly brace when alone on a line + try %[ exec -itersel -draft ^\h+\}$hms\`|.\'1 ] +] + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +hook global WinSetOption filetype=java %{ + # cleanup trailing whitespaces when exiting insert mode + hook window InsertEnd .* -group java-hooks %{ try %{ exec -draft s^\h+$d } } + hook window InsertChar \n -group java-indent java-indent-on-new-line + hook window InsertChar \{ -group java-indent java-indent-on-opening-curly-brace + hook window InsertChar \} -group java-indent java-indent-on-closing-curly-brace +} + +hook global WinSetOption filetype=(?!java).* %{ + remove-hooks window java-hooks + remove-hooks window java-indent +} hook -group java-highlight global WinSetOption filetype=java %{ add-highlighter ref java } hook -group java-highlight global WinSetOption filetype=(?!java).* %{ remove-highlighter java }