From 904cec3c4a329cf89fc3219d359239910d61f3f6 Mon Sep 17 00:00:00 2001 From: thing1 Date: Tue, 28 Jan 2025 09:14:32 +0000 Subject: init commit --- autoload/filetype/java.kak | 129 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 autoload/filetype/java.kak (limited to 'autoload/filetype/java.kak') diff --git a/autoload/filetype/java.kak b/autoload/filetype/java.kak new file mode 100644 index 0000000..69b0d74 --- /dev/null +++ b/autoload/filetype/java.kak @@ -0,0 +1,129 @@ +hook global BufCreate .*\.java %{ + set-option buffer filetype java +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=java %{ + require-module java + + set-option window static_words %opt{java_static_words} + + # cleanup trailing whitespaces when exiting insert mode + hook window ModeChange pop:insert:.* -group java-trim-indent %{ try %{ execute-keys -draft xs^\h+$d } } + hook window InsertChar \n -group java-insert java-insert-on-new-line + 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 -once -always window WinSetOption filetype=.* %{ remove-hooks window java-.+ } +} + +hook -group java-highlight global WinSetOption filetype=java %{ + add-highlighter window/java ref java + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/java } +} + +provide-module java %§ + +add-highlighter shared/java regions +add-highlighter shared/java/code default-region group +add-highlighter shared/java/string region %{(?kx s ^\h*\K/{2,}\h* yP } +] + +define-command -hidden java-indent-on-new-line %~ + evaluate-commands -draft -itersel %= + # preserve previous line indent + try %{ execute-keys -draft K } + # indent after lines ending with { or ( + try %[ execute-keys -draft kx [{(]\h*$ j ] + # cleanup trailing white spaces on the previous line + try %{ execute-keys -draft kx s \h+$ d } + # align to opening paren of previous line + try %{ execute-keys -draft [( \A\([^\n]+\n[^\n]*\n?\z s \A\(\h*.|.\z '' & } + # indent after a switch's case/default statements + try %[ execute-keys -draft kx ^\h*(case|default).*:$ j ] + # indent after keywords + try %[ execute-keys -draft )MB \A(if|else|while|for|try|catch)\h*\(.*\)\h*\n\h*\n?\z s \A|.\z 11 ] + # deindent closing brace(s) when after cursor + try %[ execute-keys -draft x ^\h*[})] gh / [})] m 1 ] + = +~ + +define-command -hidden java-indent-on-opening-curly-brace %[ + # align indent with opening paren when { is entered on a new line after the closing paren + try %[ execute-keys -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] +] + +define-command -hidden java-indent-on-closing-curly-brace %[ + # align to opening curly brace when alone on a line + try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] +] + +# Shell +# ‾‾‾‾‾ +# Oracle 2021, 3.9 Keywords, Chapter 3. Lexical Structure, Java Language Specification, Java SE 17, viewed 25 September 2021, +# +evaluate-commands %sh{ + values='false null this true' + + types='boolean byte char double float int long short unsigned void' + + keywords='assert break case catch class continue default do else enum extends + finally for if implements import instanceof interface new package return + static strictfp super switch throw throws try var while yield' + + attributes='abstract final native non-sealed permits private protected public + record sealed synchronized transient volatile' + + modules='exports module open opens provides requires to transitive uses with' + + # ---------------------------------------------------------------------------------------------- # + join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } + # ---------------------------------------------------------------------------------------------- # + add_highlighter() { printf "add-highlighter shared/java/code/ regex %s %s\n" "$1" "$2"; } + # ---------------------------------------------------------------------------------------------- # + add_word_highlighter() { + + while [ $# -gt 0 ]; do + words=$1 face=$2; shift 2 + regex="\\b($(join "${words}" '|'))\\b" + add_highlighter "$regex" "1:$face" + done + + } + + # highlight: open not open() + add_module_highlighter() { + + while [ $# -gt 0 ]; do + words=$1 face=$2; shift 2 + regex="\\b($(join "${words}" '|'))\\b(?=\\s)" + add_highlighter "$regex" "1:$face" + done + + } + # ---------------------------------------------------------------------------------------------- # + printf %s\\n "declare-option str-list java_static_words $(join "${values} ${types} ${keywords} ${attributes} ${modules}" ' ')" + # ---------------------------------------------------------------------------------------------- # + add_word_highlighter "$values" "value" "$types" "type" "$keywords" "keyword" "$attributes" "attribute" + # ---------------------------------------------------------------------------------------------- # + add_module_highlighter "$modules" "module" + # ---------------------------------------------------------------------------------------------- # +} + +§ -- cgit v1.2.3