rc:filetype:java refactored to shell block add-highligher with additional highlighter for module system, added static word list and the keywords var yield.

This commit is contained in:
kjduncan 2021-09-25 01:38:55 +10:00 committed by Maxime Coste
parent 28ef698295
commit d44d07bd80

View File

@ -8,6 +8,8 @@ hook global BufCreate .*\.java %{
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 <a-x>s^\h+$<ret>d } }
hook window InsertChar \n -group java-insert java-insert-on-new-line
@ -33,10 +35,6 @@ add-highlighter shared/java/comment region /\* \*/ fill comment
add-highlighter shared/java/inline_documentation region /// $ fill documentation
add-highlighter shared/java/line_comment region // $ fill comment
add-highlighter shared/java/code/ regex %{\b(this|true|false|null)\b} 0:value
add-highlighter shared/java/code/ regex "\b(void|byte|short|int|long|char|unsigned|float|boolean|double)\b" 0:type
add-highlighter shared/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|throws|instanceof)\b" 0:keyword
add-highlighter shared/java/code/ regex "\b(final|public|protected|private|abstract|synchronized|native|transient|volatile)\b" 0:attribute
add-highlighter shared/java/code/ regex "(?<!\w)@\w+\b" 0:meta
# Commands
@ -76,4 +74,56 @@ define-command -hidden java-indent-on-closing-curly-brace %[
try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
]
# Shell
# ‾‾‾‾‾
# 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>
#
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<space> 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"
# ---------------------------------------------------------------------------------------------- #
}
§