Rework the handlebars highlighter

- Removes -recurse from handlebars comments. Handlebars parsers (janl/mustache.js, ember-cli/ember-cli-htmlbars), do not treat comments as recursive, so don't highlight them as so.
- Creates shared/hbs-file highligher group. This represents a handlebars file, which is html that happens to contain some handlebars tags.
- Augments the shared/html highlighter when needed. Because handlebars lives inside of html, we need to add the highlighter inside of it. Since there's no way to scope modifications of a shared highlighter to a window, here I'm modifying/unmodifying the shared/html highlighter whenever the user attaches/detaches a filetype of "hbs" to/from the window.
- Matches namespaced helpers as well. In htmlbars, helpers (components) can have '/'s in them, so make sure to continue highlighting through those. Also removes unused capturing groups.
- Allows for de-indenting when closing a block expression
- Brings in html highlighter hooks
- Improves indent matching on close of yielded blocks. Previous version just flat out didn't work.
This commit is contained in:
Matt Schick 2018-09-10 07:22:29 -05:00
parent 93f913705a
commit e7e87db94e

View File

@ -12,23 +12,30 @@ hook global BufCreate .*[.](hbs) %{
# ‾‾‾‾‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾‾‾‾‾
add-highlighter shared/hbs regions add-highlighter shared/hbs regions
add-highlighter shared/hbs/html default-region ref html add-highlighter shared/hbs/comment region \{\{!-- --\}\} fill comment
add-highlighter shared/hbs/comment region -recurse ' ' \{\{!-- --\}\} fill comment
add-highlighter shared/hbs/comment_alt region \{\{! \}\} fill comment add-highlighter shared/hbs/comment_alt region \{\{! \}\} fill comment
add-highlighter shared/hbs/block-expression region \{\{ \}\} group add-highlighter shared/hbs/block-expression region \{\{[#/] \}\} regions
add-highlighter shared/hbs/expression region \{\{ \}\} regions
add-highlighter shared/hbs/block-expression/ regex \{\{((#|/|)(\w|-)+) 1:meta define-command -hidden add-mutual-highlighters -params 1 %~
add-highlighter "shared/hbs/%arg{1}/code" default-region group
add-highlighter "shared/hbs/%arg{1}/single-quote" region '"' (?<!\\)(\\\\)*" fill string
add-highlighter "shared/hbs/%arg{1}/double-quote" region "'" (?<!\\)(\\\\)*' fill string
add-highlighter "shared/hbs/%arg{1}/code/variable" regex \b([\w-]+)\b 1:variable
add-highlighter "shared/hbs/%arg{1}/code/attribute" regex \b([\w-]+)= 1:attribute
add-highlighter "shared/hbs/%arg{1}/code/helper" regex (?:(?:\{\{)|\()([#/]?[\w-]+(?:/[\w-]+)*) 1:keyword
~
# some hbs tags have a special meaning add-mutual-highlighters expression
add-highlighter shared/hbs/block-expression/ regex \{\{((#|/|)(if|else|unless|with|lookup|log)) 1:keyword add-mutual-highlighters block-expression
# 'each' is special as it really is two words 'each' and 'as' add-highlighter shared/hbs/block-expression/code/yield regex \b(as)\s|[\w-]+|\}\} 1:keyword
add-highlighter shared/hbs/block-expression/ regex \{\{((#|/|)((each).*(as))) 2:keyword 4:keyword 5:keyword
add-highlighter shared/hbs/block-expression/ regex ((\w|-)+)= 1:attribute
# highlight the string values of attributes as a bonus # wrapper around shared/html highlighter. The shared/hbs highlighter will be
add-highlighter shared/hbs/block-expression/ regex ((\w|-)+)=(('|").*?('|")) 1:attribute 3:value # added into shared/html when a buffer of filetype 'hbs' is actively displayed in the window.
add-highlighter shared/hbs-file regions
add-highlighter shared/hbs-file/html default-region ref html
# Commands # Commands
# ‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾
@ -38,6 +45,13 @@ define-command -hidden hbs-filter-around-selections %{
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d } try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d }
} }
define-command -hidden hbs-indent-on-char %[
evaluate-commands -draft -itersel %[
# de-indent after closing a yielded block tag
try %[ execute-keys -draft <space> <a-h> s ^\h+\{\{/([\w-.]+(?:/[\w-.]+)*)\}\}$ <ret> {c\{\{#<c-r>1,\{\{/<c-r>1\}\} <ret> s \A|.\z <ret> 1<a-&> ]
]
]
define-command -hidden hbs-indent-on-new-line %{ define-command -hidden hbs-indent-on-new-line %{
evaluate-commands -draft -itersel %{ evaluate-commands -draft -itersel %{
# copy '/' comment prefix and following white spaces # copy '/' comment prefix and following white spaces
@ -54,17 +68,55 @@ define-command -hidden hbs-indent-on-new-line %{
# Initialization # Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
declare-option bool hbs_highlighters_enabled false
define-command -hidden maybe-add-hbs-to-html %{ evaluate-commands %sh{
if [ "$kak_opt_hbs_highlighters_enabled" == "false" ]; then
printf %s "
add-highlighter shared/html/hbs region '\{\{' '\}\}' ref hbs
add-highlighter shared/html/tag/hbs region '\{\{' '\}\}' ref hbs
set-option global hbs_highlighters_enabled true
"
fi
} }
define-command -hidden remove-hbs-from-html %{
remove-highlighter shared/html/hbs
remove-highlighter shared/html/tag/hbs
set-option global hbs_highlighters_enabled false
}
# The two hooks below are wrapped in this "-once" so that they only get enabled
# when atleast one .hbs file is opened. This way people who don't edit .hbs files
# aren't paying a performance penality of these hooks always executing.
hook -once global BufCreate .*\.(hbs) %{
hook -group hbs-highlight global WinDisplay .*\.(hbs) %{ evaluate-commands %sh{
if [ "$kak_opt_filetype" = "hbs" ]; then
printf %s "maybe-add-hbs-to-html"
fi
} }
hook -group hbs-highlight global WinDisplay .*\.(?!hbs).* %{
remove-hbs-from-html
}
}
hook -group hbs-highlight global WinSetOption filetype=hbs %{ hook -group hbs-highlight global WinSetOption filetype=hbs %{
add-highlighter window/hbs ref hbs add-highlighter window/hbs-file ref hbs-file
} }
hook global WinSetOption filetype=hbs %{ hook global WinSetOption filetype=hbs %{
hook window ModeChange insert:.* -group hbs-hooks hbs-filter-around-selections hook window ModeChange insert:.* -group hbs-hooks hbs-filter-around-selections
hook window InsertChar \n -group hbs-indent hbs-indent-on-new-line hook window InsertChar \n -group hbs-indent hbs-indent-on-new-line
hook window InsertChar .* -group hbs-ident hbs-indent-on-char
hook window ModeChange insert:.* -group hbs-hooks html-filter-around-selections
hook window InsertChar '>' -group hbs-indent html-indent-on-greater-than
hook window InsertChar \n -group hbs-indent html-indent-on-new-line
} }
hook -group hbs-highlight global WinSetOption filetype=(?!hbs).* %{ hook -group hbs-highlight global WinSetOption filetype=(?!hbs).* %{
remove-highlighter window/hbs remove-highlighter window/hbs-file
} }
hook global WinSetOption filetype=(?!hbs).* %{ hook global WinSetOption filetype=(?!hbs).* %{