diff --git a/doc/pages/changelog.asciidoc b/doc/pages/changelog.asciidoc index 264019e5..8c9e7c9c 100644 --- a/doc/pages/changelog.asciidoc +++ b/doc/pages/changelog.asciidoc @@ -43,9 +43,11 @@ change to make Kakoune command model cleaner and more robust. ------------------------------------------------------------------ add-highlighter / regions add-highlighter // region \ - + ------------------------------------------------------------------ + The recursion regex is opt-in through a `-recurse ` flag. + They also are not necessarily groups anymore, a region can directly apply any other highlighter diff --git a/doc/pages/highlighters.asciidoc b/doc/pages/highlighters.asciidoc index 34bab82e..d9c1a70b 100644 --- a/doc/pages/highlighters.asciidoc +++ b/doc/pages/highlighters.asciidoc @@ -188,7 +188,8 @@ Individual region definitions can then be added to that highlighter ---------------------------------------------------- add-highlighter // region \ - ... + [-match-capture] [-recurse ] \ + ... ---------------------------------------------------- *opening*:: @@ -199,7 +200,8 @@ add-highlighter // region \ *recurse*:: regex that defines the text that matches recursively an end token - into the region + into the region, every match of *recurse* will consume a following + match of *end*, preventing it from closing the region. *type* and *params*:: An highlighter type, and associated params, as they would be passed @@ -224,9 +226,9 @@ accepts nested braces scopes ('{ ... }') so the following string is valid: This region can be defined with: ------------------------- -shell_expand %sh\{ \} \{ ------------------------- +--------------------------------- +shell_expand -recurse \{ %sh\{ \} +--------------------------------- Regions are matched using the left-most rule: the left-most region opening starts a new region. When a region closes, the closest next opening start @@ -247,8 +249,8 @@ highlighter as root: ----------------------------------------------------------------- add-highlighter / regions -add-highlighter //string '"' '"' '' fill string -add-highlighter //comment '//' '$' '' fill comment +add-highlighter //string '"' '"' fill string +add-highlighter //comment '//' '$' fill comment add-highlighter //code default-region group add-highlighter //code/variable regex ... add-highlighter //code/function regex ... diff --git a/rc/base/css.kak b/rc/base/css.kak index c7669da4..79bfc69e 100644 --- a/rc/base/css.kak +++ b/rc/base/css.kak @@ -13,12 +13,12 @@ hook global BufCreate .*[.](css) %{ add-highlighter shared/css regions add-highlighter shared/css/selector default-region group -add-highlighter shared/css/declaration region [{] [}] '' regions -add-highlighter shared/css/comment region /[*] [*]/ '' fill comment +add-highlighter shared/css/declaration region [{] [}] regions +add-highlighter shared/css/comment region /[*] [*]/ fill comment add-highlighter shared/css/declaration/base default-region group -add-highlighter shared/css/declaration/double_string region '"' (??@\\\^|~=]|$) $ '' fill comment +add-highlighter shared/haskell/string region (??@\\\^|~=]|$) $ fill comment add-highlighter shared/haskell/code/ regex (? '' fill comment -add-highlighter shared/html/tag region < > '' regions -add-highlighter shared/html/style region \K (?=) '' ref css -add-highlighter shared/html/script region \K (?=) '' ref javascript +add-highlighter shared/html/comment region fill comment +add-highlighter shared/html/tag region < > regions +add-highlighter shared/html/style region \K (?=) ref css +add-highlighter shared/html/script region \K (?=) ref javascript add-highlighter shared/html/tag/base default-region group -add-highlighter shared/html/tag/ region '"' (?])(?!>\()) (|/>) (?])(?!>\()) (|/>) regions + add-highlighter "shared/%arg{1}/division" region '[\w\)\]]\K(/|(\h+/\h+))' '(?=\w)' group # Help Kakoune to better detect /…/ literals # Regular expression flags are: g → global match, i → ignore case, m → multi-lines, u → unicode, y → sticky # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp @@ -70,14 +70,14 @@ define-command -hidden init-javascript-filetype -params 1 %~ # We inline a small XML highlighter here since it anyway need to recurse back up to the starting highlighter. # To make things simple we assume that jsx is always enabled. - add-highlighter "shared/%arg{1}/jsx/tag" region <(?=[/a-zA-Z]) (? < regions - add-highlighter "shared/%arg{1}/jsx/expr" region \{ \} \{ ref %arg{1} + add-highlighter "shared/%arg{1}/jsx/tag" region -recurse < <(?=[/a-zA-Z]) (? regions + add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{ \} ref %arg{1} add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group - add-highlighter "shared/%arg{1}/jsx/tag/double_string" region =\K" (?|<=|=|>=) 0:operator diff --git a/rc/base/lua.kak b/rc/base/lua.kak index 6a20fc56..13e0459f 100644 --- a/rc/base/lua.kak +++ b/rc/base/lua.kak @@ -13,11 +13,11 @@ hook global BufCreate .*[.](lua) %{ add-highlighter shared/lua regions add-highlighter shared/lua/code default-region group -add-highlighter shared/lua/double_string region '"' (? < fill meta -add-highlighter shared/ruby/heredoc region '<<[-~]?(\w+)' '^\h*(\w+)$' '' fill string -add-highlighter shared/ruby/division region '[\w\)\]](/|(\h+/\h+))' '\w' '' group # Help Kakoune to better detect /…/ literals +add-highlighter shared/ruby/double_string region '"' (? fill meta +add-highlighter shared/ruby/heredoc region '<<[-~]?(\w+)' '^\h*(\w+)$' fill string +add-highlighter shared/ruby/division region '[\w\)\]](/|(\h+/\h+))' '\w' group # Help Kakoune to better detect /…/ literals # Regular expression flags are: i → ignore case, m → multi-lines, o → only interpolate #{} blocks once, x → extended mode (ignore white spaces) # Literals are: i → array of symbols, q → string, r → regular expression, s → symbol, w → array of words, x → capture shell result add-highlighter shared/ruby/double_string/ default-region fill string -add-highlighter shared/ruby/double_string/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/double_string/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/backtick/ default-region fill meta -add-highlighter shared/ruby/backtick/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/backtick/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/regex/ default-region fill meta -add-highlighter shared/ruby/regex/interpolation region \Q#{ \} \{ fill meta +add-highlighter shared/ruby/regex/interpolation region -recurse \{ \Q#{ \} fill meta add-highlighter shared/ruby/code/ regex \b([A-Za-z]\w*:(?!:))|([$@][A-Za-z]\w*)|((?' '<' ref sh -add-highlighter shared/kakrc/shell5 region '(^|\h)\K-shell-(completion|candidates)\h+%\{' '\}' '\{' ref sh -add-highlighter shared/kakrc/shell6 region '(^|\h)\K-shell-(completion|candidates)\h+%\(' '\)' '\(' ref sh -add-highlighter shared/kakrc/shell7 region '(^|\h)\K-shell-(completion|candidates)\h+%\[' '\]' '\[' ref sh -add-highlighter shared/kakrc/shell8 region '(^|\h)\K-shell-(completion|candidates)\h+%<' '>' '<' ref sh +add-highlighter shared/kakrc/comment region (^|\h)\K# $ fill comment +add-highlighter shared/kakrc/double_string region -recurse %{(?' ref sh +add-highlighter shared/kakrc/shell5 region -recurse '\{' '(^|\h)\K-shell-(completion|candidates)\h+%\{' '\}' ref sh +add-highlighter shared/kakrc/shell6 region -recurse '\(' '(^|\h)\K-shell-(completion|candidates)\h+%\(' '\)' ref sh +add-highlighter shared/kakrc/shell7 region -recurse '\[' '(^|\h)\K-shell-(completion|candidates)\h+%\[' '\]' ref sh +add-highlighter shared/kakrc/shell8 region -recurse '<' '(^|\h)\K-shell-(completion|candidates)\h+%<' '>' ref sh evaluate-commands %sh{ # Grammar diff --git a/rc/core/makefile.kak b/rc/core/makefile.kak index bf1d79de..ad509296 100644 --- a/rc/core/makefile.kak +++ b/rc/core/makefile.kak @@ -11,8 +11,8 @@ hook global BufCreate .*/?[mM]akefile %{ add-highlighter shared/makefile regions add-highlighter shared/makefile/content default-region group -add-highlighter shared/makefile/comment region '#' '$' '' fill comment -add-highlighter shared/makefile/evaluate-commands region '\$\(' '\)' '\(' fill value +add-highlighter shared/makefile/comment region '#' '$' fill comment +add-highlighter shared/makefile/evaluate-commands region -recurse '\(' '\$\(' '\)' fill value add-highlighter shared/makefile/content/ regex ^[\w.%-]+\h*:\s 0:variable add-highlighter shared/makefile/content/ regex [+?:]= 0:operator diff --git a/rc/core/python.kak b/rc/core/python.kak index 1dbf7c69..f844ba4e 100644 --- a/rc/core/python.kak +++ b/rc/core/python.kak @@ -13,10 +13,10 @@ hook global BufCreate .*[.](py) %{ add-highlighter shared/python regions add-highlighter shared/python/code default-region group -add-highlighter shared/python/docstring region -match-capture ("""|''') ("""|''') '' regions -add-highlighter shared/python/double_string region '"' (?>> \K' '\z' '' ref python -add-highlighter shared/python/docstring/ region '\.\.\. \K' '\z' '' ref python +add-highlighter shared/python/docstring/ region '>>> \K' '\z' ref python +add-highlighter shared/python/docstring/ region '\.\.\. \K' '\z' ref python evaluate-commands %sh{ # Grammar diff --git a/rc/core/sh.kak b/rc/core/sh.kak index 8bba36eb..caa68720 100644 --- a/rc/core/sh.kak +++ b/rc/core/sh.kak @@ -4,10 +4,10 @@ hook global BufCreate .*\.(z|ba|c|k|mk)?sh(rc|_profile)? %{ add-highlighter shared/sh regions add-highlighter shared/sh/code default-region group -add-highlighter shared/sh/double_string region %{(?]?=?)?\d+(\.\d+)+) 0:value add-highlighter shared/cabal/code/ regex \b(if|else)\b 0:keyword diff --git a/rc/extra/cmake.kak b/rc/extra/cmake.kak index 58f60f29..d2348bec 100644 --- a/rc/extra/cmake.kak +++ b/rc/extra/cmake.kak @@ -4,14 +4,14 @@ hook global BufCreate .+\.cmake|.*/CMakeLists.txt %{ add-highlighter shared/cmake regions add-highlighter shared/cmake/code default-region group -add-highlighter shared/cmake/comment region '#' '$' '' fill comment -add-highlighter shared/cmake/argument region '\w+\h*\(\K' '(?=\))' '\(' regions +add-highlighter shared/cmake/comment region '#' '$' fill comment +add-highlighter shared/cmake/argument region -recurse '\(' '\w+\h*\(\K' '(?=\))' regions add-highlighter shared/cmake/code/ regex '\w+\h*(?=\()' 0:meta add-highlighter shared/cmake/argument/args default-region regex '\$\{\w+\}' 0:variable -add-highlighter shared/cmake/argument/quoted region '"' '(?' '' ref php +add-highlighter shared/php-file/php region '<\?(php)?' '\?>' ref php # Commands # ‾‾‾‾‾‾‾‾ diff --git a/rc/extra/pony.kak b/rc/extra/pony.kak index 853cdecb..3c8d105d 100644 --- a/rc/extra/pony.kak +++ b/rc/extra/pony.kak @@ -13,10 +13,10 @@ hook global BufCreate .*[.](pony) %{ add-highlighter shared/pony regions add-highlighter shared/pony/code default-region group -add-highlighter shared/pony/triple_string region '"""' '"""' '' fill string -add-highlighter shared/pony/double_string region '"' (?' 0:variable diff --git a/rc/extra/sass.kak b/rc/extra/sass.kak index fe203b5c..6367628b 100644 --- a/rc/extra/sass.kak +++ b/rc/extra/sass.kak @@ -13,9 +13,9 @@ hook global BufCreate .*[.](sass) %{ add-highlighter shared/sass regions add-highlighter shared/sass/code default-region group -add-highlighter shared/sass/single_string region '"' (?value.factory(parser.positionals_from(4), nullptr); - return std::make_unique(std::move(delegate), Regex{parser[0], flags}, Regex{parser[1], flags}, parser[2].empty() ? Regex{} : Regex{parser[2], flags}, match_capture); + Regex recurse; + if (auto recurse_switch = parser.get_switch("recurse")) + recurse = Regex{*recurse_switch, flags}; + + auto delegate = it->value.factory(parser.positionals_from(3), nullptr); + return std::make_unique(std::move(delegate), Regex{parser[0], flags}, Regex{parser[1], flags}, recurse, match_capture); } static std::unique_ptr create_default_region(HighlighterParameters params, Highlighter* parent) diff --git a/test/highlight/regions-recurse/rc b/test/highlight/regions-recurse/rc index 94244158..416c9669 100644 --- a/test/highlight/regions-recurse/rc +++ b/test/highlight/regions-recurse/rc @@ -1,3 +1,3 @@ add-highlighter window/regions_test regions add-highlighter window/regions_test/code default-region fill yellow -add-highlighter window/regions_test/argument region '\w+\h*\(\K' '(?=\))' '\(' fill red +add-highlighter window/regions_test/argument region -recurse '\(' '\w+\h*\(\K' '(?=\))' fill red diff --git a/test/highlight/regions/rc b/test/highlight/regions/rc index 21d886cb..dccc2a89 100644 --- a/test/highlight/regions/rc +++ b/test/highlight/regions/rc @@ -1,4 +1,4 @@ add-highlighter window/regions_test regions add-highlighter window/regions_test/code default-region fill yellow -add-highlighter window/regions_test/string region %{"} %{(?